in reply to perldoc terminal syntax color highlighting
And the concept is proven
#!/usr/bin/perl -- use strict; use warnings; use PPI; use PPI::HTML; use Term::ANSIColor::Markup; use Win32::Console::ANSI; exit print colorme( " use PPI;\n use PPI::HTML;\n \n # Loa +d your Perl file\n my \$Document = PPI::Document->new( __FILE__ ); +\n \n # Create a reusable syntax highlighter\n my \$Highligh +t = PPI::HTML->new( line_numbers => 1 );\n \n # Spit out the HT +ML\n print \$Highlight->html( \$Document );\n ", ); #~ exit Pod::Perldoc::ToTermPPIHTML->new->parse_from_file ( __FILE__ ) +; exit print self_to_html(); sub self_to_html { use PPI; use PPI::HTML; # Load your Perl file my $Document = PPI::Document->new( __FILE__ ); # Create a reusable syntax highlighter my $Highlight = PPI::HTML->new( line_numbers => 1 ); # Spit out the HTML return join '', $Highlight->html( $Document ); } sub colorme { use PPI; use PPI::HTML; use Term::ANSIColor::Markup; # Load your Perl file my $Document = PPI::Document->new( \$_[0] ); # Create a reusable syntax highlighter my $Highlight = PPI::HTML->new( line_numbers => 0 ); # Spit out the HTML #~ return join '', $Highlight->html( $Document ); my $html = join '', $Highlight->html( $Document ); $html =~ s/<br>//g; #~ $html =~ s{<span class="([^"]+)">(.*?)</span>}{<$1>$2</$1>}gs; our %color = ( #### # Standard token classes pod => '#008080', comment => '#008080', operator => '#DD7700', single => '#999999', double => '#999999', literal => '#999999', interpolate => '#999999', words => '#999999', regex => '#9900FF', match => '#9900FF', substitute => '#9900FF', transliterate => '#9900FF', number => '#990000', magic => '#0099FF', cast => '#339999', # Special classes pragma => '#990000', keyword => '#0000FF', core => '#FF0000', line_number => '#666666', comment => 'green', keyword => 'blue', symbol => 'pink', word => 'black', structure => 'black', operator => 'black', ); sub yoCo { my( $colo, $text ) = @_; #~ $colo = $color{$colo}||$colo; #~ $colo = $color{$colo} || do{ warn $colo; $colo; }; #~ $colo = $color{$colo} || 'black'; $colo = $color{$colo} ; $colo or return $text; return "<$colo>$text</$colo>"; } #~ $html =~ s{<span class="([^"]+)">(.*?)</span>}{<$color{$1}>$2</ +$color{$1}>}gs; $html =~ s{<span class="([^"]+)">(.*?)</span>}{yoCo($1,$2);}gse; #~ return $html; return Term::ANSIColor::Markup->colorize($html); } package Pod::Perldoc::ToTermPPIHTML; use parent qw/ Pod::Perldoc::ToTerm /; sub parse_from_file { my $self = shift; local *Pod::Text::cmd_verbatim = *my_cmd_verbatim; ## necessity ## + typing $self->SUPER::parse_from_file( @_ ); } # Handle a verbatim paragraph. Just print it out, but indent it accor +ding to # our margin. sub my_cmd_verbatim { my ($self, $attrs, $text) = @_; $self->item if defined $$self{ITEM}; return if $text =~ /^\s*$/; #~ ::dd( -verbatim , $text ); ## YAY $text =~ s/^(\n*)([ \t]*\S+)/$1 . (' ' x $$self{MARGIN}) . $2/gme; $text =~ s/\s*$/\n\n/; $self->output ($text); return ''; } __END__ =pod =head1 USE IT OR LOSE IT I<I> B<boldyly> C<csay> F<fhello.pl> to you! use PPI; use PPI::HTML; # Load your Perl file my $Document = PPI::Document->new( __FILE__ ); # Create a reusable syntax highlighter my $Highlight = PPI::HTML->new( line_numbers => 1 ); # Spit out the HTML print $Highlight->html( $Document ); =cut
All thats left to do, is settle on a name (maybe call it perldocolor), maybe package it up as a module or a exe, improve color mapping naturally (allow for background and foreground... just finish it up, fill in the blanks, provide for stylesheet-alike specification, default stylesheets , work out the quirks with colorize)...
You're welcome
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: perldoc terminal syntax color highlighting ( perldocolor )
by LanX (Saint) on Aug 04, 2014 at 16:24 UTC | |
|
Re^2: perldoc terminal syntax color highlighting ( perldocolor )
by Anonymous Monk on Aug 13, 2014 at 07:12 UTC |