Changeset 22841 for src

Show
Ignore:
Timestamp:
10/31/08 19:41:57 (2 months ago)
Author:
azawawi
Message:

[STD_syntax_highlight] implemented --ansi-text to print out ANSI color escape sequences

Location:
src/perl6
Files:
1 added
1 modified

Legend:

Unmodified
Added
Removed
  • src/perl6/STD_syntax_highlight

    r22840 r22841  
    1111# CPAN modules 
    1212use File::Slurp; 
     13use Term::ANSIColor; 
    1314 
    1415# And finally our modules 
     
    3031    STD_syntax_highlight foo.pl statementlist 
    3132 
    32     # write simple output to foo.pl.html 
     33    # write simple html output to foo.pl.html 
    3334    STD_syntax_highlight --simple-html=foo.pl.html foo.pl 
    3435 
     36    # write simple ansi-colored output to STDOUT 
     37    STD_syntax_highlight --ansi-text=- foo.pl 
     38 
    3539=head1 SUBROUTINES 
    3640 
     
    4044 
    4145my ($clean_html,$help) = (0,0); 
    42 my ($full_html,$simple_html) = (0,'-'); 
     46my ($full_html,$simple_html,$ansi_text) = (0,'-',0); 
    4347my ($file, $parser, $src_text);  
    4448 
     
    5761        "full-html=s"=>\$full_html, 
    5862        "simple-html=s"=>\$simple_html, 
     63        "ansi-text=s"=>\$ansi_text, 
    5964        "help"=>\$help 
    6065    ); 
     
    7681        write simple-mode html to filename (enabled by default, - for STDOUT) 
    7782 
     83    --ansi-text=filename    
     84        write simple-mode ansi color text to filename (enabled by default, - for STDOUT) 
     85 
    7886HELP 
    7987    } 
     
    99107=item write_html_file 
    100108 
    101 Writes the html file to filename or to STDOUT 
    102 =cut 
    103 sub write_html_file { 
    104     my ($html_file, $html) = @ARG; 
    105     if($html_file eq '-') { 
    106         say $html; 
     109Writes the output to a file or STDOUT 
     110=cut 
     111sub write_output { 
     112    my ($file, $output) = @ARG; 
     113    if($file eq '-') { 
     114        say $output; 
    107115    } else { 
    108         open FILE, ">$html_file" or 
    109             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; 
    111119        close FILE; 
    112120    } 
     
    115123=item highlight_match 
    116124 
    117 Returns the generated Perl6 highlighted HTML from C<highlight_perl6> 
     125Returns the generated Perl6 highlighted HTML from C<highlight_perl6_*> 
    118126subroutine after traversing the STD parse tree using  
    119127DumpMatch.pm C<traverse_match>. 
     
    122130    if($full_html) { 
    123131        my $html = highlight_perl6_full(); 
    124         write_html_file $full_html, $html; 
     132        write_output $full_html, $html; 
    125133    } 
    126134    if($simple_html) { 
    127135        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; 
    129141    } 
    130142} 
     
    336348} 
    337349 
     350=item highlight_perl6_ansi 
     351 
     352This is same as C<highlight_perl6_full> when --ansi-text is used. 
     353No more javascript tree viewer or anything fancy.  
     354Only nodes that have a color are printed. Not optimal but works ;-) 
     355=cut 
     356sub 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 
    338416################################################################### 
    339417# R E D S P A N S