From af0e1a79691291d79819525e2fda4f232ac88855 Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Thu, 13 May 2010 20:22:17 +0000 Subject: [PATCH] Use Text::Balanced to extract code blocks from functions --- modules/codepad.pl | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/modules/codepad.pl b/modules/codepad.pl index 5068672c..4a2dacf4 100755 --- a/modules/codepad.pl +++ b/modules/codepad.pl @@ -9,6 +9,7 @@ use HTML::Entities; use HTML::Parse; use HTML::FormatText; use IPC::Open2; +use Text::Balanced qw(extract_codeblock); my @languages = qw/C C++ D Haskell Lua OCaml PHP Perl Python Ruby Scheme Tcl/; @@ -23,6 +24,7 @@ if($#ARGV <= 0) { my $nick = shift @ARGV; my $code = join ' ', @ARGV; +my $output; open FILE, ">> codepad_log.txt"; print FILE "$nick: $code\n"; @@ -64,10 +66,22 @@ if($lang eq "C" or $lang eq "C++") { my $prelude = ''; $prelude = "$1$2" if $precode =~ s/^\s*(#.*)(#.*?[>\n])//s; - - while($precode =~ s/([a-zA-Z0-9_*\[\]]+)\s+([a-zA-Z0-9_*]+)\s*\((.*?)\)\s*{(.*?)}//) { - my ($ret, $ident, $params, $body) = ($1, $2, $3, $4); - $code .= "$ret $ident($params) { $body }\n\n"; + + while($precode =~ s/([ a-zA-Z0-9_*\[\]]+)\s+([a-zA-Z0-9_*]+)\s*\((.*?)\)\s*{//) { + my ($ret, $ident, $params) = ($1, $2, $3); + + $precode = "{$precode"; + my @extract = extract_codeblock($precode, '{}'); + my $body; + if(not defined $extract[0]) { + $output .= "
error: unmatched brackets for function '$ident'; 
"; + $body = $extract[1]; + $precode = ''; + } else { + $body = $extract[0]; + $precode = $extract[1]; + } + $code .= "$ret $ident($params) $body\n\n"; $has_main = 1 if $ident eq 'main'; } @@ -97,15 +111,14 @@ if(not $response->is_success) { my $text = $response->decoded_content; my $url = $response->request->uri; -my $output; # remove line numbers $text =~ s/\d+<\/a>//g; if($text =~ /Output:<\/span>.+?
(.*)<\/div>.+?<\/table>/si) { - $output = "$1"; + $output .= "$1"; } else { - $output = "
No output.
"; + $output .= "
No output.
"; } $output = decode_entities($output);