- Timestamp:
- 10/31/08 19:41:57 (2 months ago)
- Location:
- src/perl6
- Files:
-
- 1 added
- 1 modified
-
STD_syntax_highlight (modified) (9 diffs)
-
STD_syntax_highlight.ansi (added)
Legend:
- Unmodified
- Added
- Removed
-
src/perl6/STD_syntax_highlight
r22840 r22841 11 11 # CPAN modules 12 12 use File::Slurp; 13 use Term::ANSIColor; 13 14 14 15 # And finally our modules … … 30 31 STD_syntax_highlight foo.pl statementlist 31 32 32 # write simple output to foo.pl.html33 # write simple html output to foo.pl.html 33 34 STD_syntax_highlight --simple-html=foo.pl.html foo.pl 34 35 36 # write simple ansi-colored output to STDOUT 37 STD_syntax_highlight --ansi-text=- foo.pl 38 35 39 =head1 SUBROUTINES 36 40 … … 40 44 41 45 my ($clean_html,$help) = (0,0); 42 my ($full_html,$simple_html ) = (0,'-');46 my ($full_html,$simple_html,$ansi_text) = (0,'-',0); 43 47 my ($file, $parser, $src_text); 44 48 … … 57 61 "full-html=s"=>\$full_html, 58 62 "simple-html=s"=>\$simple_html, 63 "ansi-text=s"=>\$ansi_text, 59 64 "help"=>\$help 60 65 ); … … 76 81 write simple-mode html to filename (enabled by default, - for STDOUT) 77 82 83 --ansi-text=filename 84 write simple-mode ansi color text to filename (enabled by default, - for STDOUT) 85 78 86 HELP 79 87 } … … 99 107 =item write_html_file 100 108 101 Writes the html file to filename or toSTDOUT102 =cut 103 sub write_ html_file{104 my ($ html_file, $html) = @ARG;105 if($ html_file eq '-') {106 say $ html;109 Writes the output to a file or STDOUT 110 =cut 111 sub write_output { 112 my ($file, $output) = @ARG; 113 if($file eq '-') { 114 say $output; 107 115 } else { 108 open FILE, ">$ html_file" or109 die "Cannot open $ html_file for writing: $OS_ERROR\n";110 say FILE $ html;116 open FILE, ">$file" or 117 die "Cannot open $file for writing: $OS_ERROR\n"; 118 say FILE $output; 111 119 close FILE; 112 120 } … … 115 123 =item highlight_match 116 124 117 Returns the generated Perl6 highlighted HTML from C<highlight_perl6 >125 Returns the generated Perl6 highlighted HTML from C<highlight_perl6_*> 118 126 subroutine after traversing the STD parse tree using 119 127 DumpMatch.pm C<traverse_match>. … … 122 130 if($full_html) { 123 131 my $html = highlight_perl6_full(); 124 write_ html_file$full_html, $html;132 write_output $full_html, $html; 125 133 } 126 134 if($simple_html) { 127 135 my $html = highlight_perl6_simple(); 128 write_html_file $simple_html, $html; 136 write_output $simple_html, $html; 137 } 138 if($ansi_text) { 139 my $text = highlight_perl6_ansi(); 140 write_output $ansi_text, $text; 129 141 } 130 142 } … … 336 348 } 337 349 350 =item highlight_perl6_ansi 351 352 This is same as C<highlight_perl6_full> when --ansi-text is used. 353 No more javascript tree viewer or anything fancy. 354 Only nodes that have a color are printed. Not optimal but works ;-) 355 =cut 356 sub highlight_perl6_ansi { 357 my $str = ""; 358 my %colors = (); 359 360 my $ANSI = "STD_syntax_highlight.ansi"; 361 open ANSI_FILE, $ANSI 362 or die "Could not open $ANSI: $OS_ERROR\n"; 363 my $line; 364 while($line = <ANSI_FILE>) { 365 if($line =~ /^(\w+)=(.+)$/) { 366 $colors{$1} = $2; 367 } 368 } 369 close ANSI_FILE; 370 371 my ($last_tree,$buffer) = ("",""); 372 for my $i (0 .. @loc-1) { 373 next unless defined $loc[$i]; 374 my $c = substr($src_text,$i,1); 375 my $tree = "@{$loc[$i]}"; 376 if($tree ne $last_tree) { 377 my $rule; 378 my $rule_to_color = 0; 379 my @rules = (); 380 @rules = reverse(split / /,$last_tree) if $last_tree ne ''; 381 for $rule (@rules) { 382 if($rule eq 'unv') { 383 $rule_to_color = '_comment'; 384 last; 385 } elsif($colors{$rule} && $buffer ne '') { 386 $rule_to_color = $rule; 387 last; 388 } 389 } 390 if($rule_to_color) { 391 my $color = $colors{$rule_to_color}; 392 if($last_tree =~ /identifier/) { 393 if($parser->is_type($buffer)) { 394 $str .= (color $color) . $buffer. (color 'reset'); 395 } elsif($parser->is_routine($buffer)) { 396 $str .= (color $color) . $buffer . (color 'reset'); 397 } else { 398 $str .= (color $color) . $buffer . (color 'reset'); 399 } 400 } else { 401 $str .= (color $color) . $buffer . (color 'reset'); 402 } 403 } else { 404 $str .= qq{$buffer}; 405 } 406 $buffer = $c; 407 } else { 408 $buffer .= $c; 409 } 410 $last_tree = $tree; 411 } 412 413 $str; 414 } 415 338 416 ################################################################### 339 417 # R E D S P A N S
