mirror of
https://github.com/pragma-/pbot.git
synced 2025-02-16 21:40:46 +01:00
Use Text::Balanced to extract code blocks from functions
This commit is contained in:
parent
3acdd74920
commit
af0e1a7969
@ -9,6 +9,7 @@ use HTML::Entities;
|
|||||||
use HTML::Parse;
|
use HTML::Parse;
|
||||||
use HTML::FormatText;
|
use HTML::FormatText;
|
||||||
use IPC::Open2;
|
use IPC::Open2;
|
||||||
|
use Text::Balanced qw(extract_codeblock);
|
||||||
|
|
||||||
my @languages = qw/C C++ D Haskell Lua OCaml PHP Perl Python Ruby Scheme Tcl/;
|
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 $nick = shift @ARGV;
|
||||||
my $code = join ' ', @ARGV;
|
my $code = join ' ', @ARGV;
|
||||||
|
my $output;
|
||||||
|
|
||||||
open FILE, ">> codepad_log.txt";
|
open FILE, ">> codepad_log.txt";
|
||||||
print FILE "$nick: $code\n";
|
print FILE "$nick: $code\n";
|
||||||
@ -64,10 +66,22 @@ if($lang eq "C" or $lang eq "C++") {
|
|||||||
|
|
||||||
my $prelude = '';
|
my $prelude = '';
|
||||||
$prelude = "$1$2" if $precode =~ s/^\s*(#.*)(#.*?[>\n])//s;
|
$prelude = "$1$2" if $precode =~ s/^\s*(#.*)(#.*?[>\n])//s;
|
||||||
|
|
||||||
while($precode =~ s/([a-zA-Z0-9_*\[\]]+)\s+([a-zA-Z0-9_*]+)\s*\((.*?)\)\s*{(.*?)}//) {
|
while($precode =~ s/([ a-zA-Z0-9_*\[\]]+)\s+([a-zA-Z0-9_*]+)\s*\((.*?)\)\s*{//) {
|
||||||
my ($ret, $ident, $params, $body) = ($1, $2, $3, $4);
|
my ($ret, $ident, $params) = ($1, $2, $3);
|
||||||
$code .= "$ret $ident($params) { $body }\n\n";
|
|
||||||
|
$precode = "{$precode";
|
||||||
|
my @extract = extract_codeblock($precode, '{}');
|
||||||
|
my $body;
|
||||||
|
if(not defined $extract[0]) {
|
||||||
|
$output .= "<pre>error: unmatched brackets for function '$ident'; </pre>";
|
||||||
|
$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';
|
$has_main = 1 if $ident eq 'main';
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,15 +111,14 @@ if(not $response->is_success) {
|
|||||||
|
|
||||||
my $text = $response->decoded_content;
|
my $text = $response->decoded_content;
|
||||||
my $url = $response->request->uri;
|
my $url = $response->request->uri;
|
||||||
my $output;
|
|
||||||
|
|
||||||
# remove line numbers
|
# remove line numbers
|
||||||
$text =~ s/<a style="" name="output-line-\d+">\d+<\/a>//g;
|
$text =~ s/<a style="" name="output-line-\d+">\d+<\/a>//g;
|
||||||
|
|
||||||
if($text =~ /<span class="heading">Output:<\/span>.+?<div class="code">(.*)<\/div>.+?<\/table>/si) {
|
if($text =~ /<span class="heading">Output:<\/span>.+?<div class="code">(.*)<\/div>.+?<\/table>/si) {
|
||||||
$output = "$1";
|
$output .= "$1";
|
||||||
} else {
|
} else {
|
||||||
$output = "<pre>No output.</pre>";
|
$output .= "<pre>No output.</pre>";
|
||||||
}
|
}
|
||||||
|
|
||||||
$output = decode_entities($output);
|
$output = decode_entities($output);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user