Changeset 22877 for src

Show
Ignore:
Timestamp:
11/04/08 19:05:26 (2 months ago)
Author:
azawawi
Message:

[STD_syntax_highlight] implemented --snippet-html. Enjoy ;-)

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • src/perl6/STD_syntax_highlight

    r22876 r22877  
    3434    STD_syntax_highlight --simple-html=foo.pl.html foo.pl 
    3535 
     36    # write simple snippet html output to foo.pl.html 
     37    STD_syntax_highlight --snippet-html=foo.pl.html foo.pl 
     38 
    3639    # write simple ansi-colored output to STDOUT 
    3740    STD_syntax_highlight --ansi-text=- foo.pl 
     
    4447 
    4548my ($clean_html,$help) = (0,0); 
    46 my ($full_html,$simple_html,$ansi_text) = (0,0,0); 
     49my ($full_html,$simple_html,$snippet_html,$ansi_text) = (0,0,0,0); 
    4750my ($file, $parser, $src_text);  
    4851 
     
    6164        "full-html=s"=>\$full_html, 
    6265        "simple-html=s"=>\$simple_html, 
     66        "snippet-html=s"=>\$snippet_html, 
    6367        "ansi-text=s"=>\$ansi_text, 
    6468        "help"=>\$help 
     
    8286        option is selected, - for STDOUT) 
    8387 
     88    --snippet-html=filename 
     89        same as --simple-html but with only the body section.  
     90        This is typically ideal for inline html code. 
     91 
    8492    --ansi-text=filename    
    8593        write simple-mode ansi color text to filename (- for STDOUT) 
     
    8997 
    9098    #default is --simple-html=- if no option is selected 
    91     if(!($ansi_text || $full_html) && !$simple_html) { 
     99    if(!($ansi_text || $full_html || $snippet_html) && !$simple_html) { 
    92100        $simple_html = '-';     
    93101    } 
     
    142150        my $html = highlight_perl6_simple(); 
    143151        write_output $simple_html, $html; 
     152    } 
     153    if($snippet_html) { 
     154        my $html = highlight_perl6_snippet_html(); 
     155        write_output $snippet_html, $html; 
    144156    } 
    145157    if($ansi_text) { 
     
    296308} 
    297309 
     310=item highlight_perl6_snippet_html 
     311 
     312This is same as C<highlight_perl6_full> when --snippet-html is used. 
     313No more javascript tree viewer or anything fancy.  
     314Only nodes that have a color are printed. Not optimal but works ;-) 
     315=cut 
     316sub highlight_perl6_snippet_html { 
     317    my $str = ""; 
     318    my %colors = (); 
     319 
     320    my $CSS = "STD_syntax_highlight.css"; 
     321    open CSS_FILE, $CSS 
     322        or die "Could not open $CSS: $OS_ERROR\n"; 
     323    my $line; 
     324    while($line = <CSS_FILE>) { 
     325        if($line =~ /^\s*\.(\w+)\s*{\s*(.+?)\s*}/) { 
     326            $colors{$1} = $2; 
     327        } 
     328    } 
     329    close CSS_FILE; 
     330 
     331    $str .= "<pre>"; 
     332 
     333    local *spit_snippet_html = sub { 
     334        my ($i, $buffer, $rule, $tree) = @ARG; 
     335        $buffer = escape_html($buffer); 
     336        my $style = $colors{$rule}; 
     337        if($rule) { 
     338            $str .= qq{<span style="$style">$buffer</span>}; 
     339        } else { 
     340            $str .= $buffer; 
     341        } 
     342    }; 
     343 
     344    redspans_traverse(\&spit_snippet_html,%colors);  
     345 
     346    $str .= "</pre>"; 
     347 
     348    $str; 
     349} 
     350 
    298351 
    299352=item highlight_perl6_ansi 
     
    329382 
    330383    redspans_traverse(\&spit_ansi_text,%colors);  
    331     
     384 
    332385    $str; 
    333386}