Skip Menu |

This queue is for tickets about the Text-VimColor CPAN distribution.

Report information
The Basics
Id: 69338
Status: resolved
Priority: 0/
Queue: Text-VimColor

People
Owner: Nobody in particular
Requestors: RWSTAUNER [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: 0.11
Fixed in: 0.12



Subject: [PATCH] add support for ANSI (terminal) output
I found this handy module and love it, but was surprised that there was no Term::ANSIColor support (especially in the text-vimcolor script). The attached patch adds the format option to the module, makes it the default output format for the script (instead of having to specify a format), documents and tests it. What do you think?
Subject: text-vimcolor-ansi.patch
diff --git a/Makefile.PL b/Makefile.PL index b3af18f..f75ac68 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -13,6 +13,7 @@ WriteMakefile( PREREQ_PM => { 'Path::Class' => 0.02, + 'Term::ANSIColor' => 0, }, ); diff --git a/lib/Text/VimColor.pm b/lib/Text/VimColor.pm index dfca060..7c80a90 100644 --- a/lib/Text/VimColor.pm +++ b/lib/Text/VimColor.pm @@ -35,6 +35,19 @@ our %SYNTAX_TYPE = ( Todo => 1, ); +our %ANSI_COLORS = ( + Comment => 'blue', + Constant => 'red', + Identifier => 'cyan', + Statement => 'yellow', + PreProc => 'magenta', + Type => 'green', + Special => 'bright_magenta', + Underlined => 'underline', + Error => 'on_red', + Todo => 'on_cyan', +); + # Set to true to print the command line used to run Vim. our $DEBUG = 0; @@ -104,6 +117,28 @@ sub syntax_mark_string return $self; } +sub ansi +{ + my ($self) = @_; + my $syntax = $self->marked; + + require Term::ANSIColor; + # allow the environment to overwrite; + my %colors = (%ANSI_COLORS, + split(/\s*[=;]\s*/, $ENV{TEXT_VIMCOLOR_ANSI} || '') + ); + + my $ansi = ''; + foreach (@$syntax) { + $ansi .= $_->[1], next + if $_->[0] eq ''; + + $ansi .= Term::ANSIColor::colored([ $colors{ $_->[0] } ], $_->[1]); + } + + return $ansi; +} + sub html { my ($self) = @_; @@ -624,6 +659,20 @@ I<string> can also be a reference to a string. Returns the object it was called on. Supports the C<filetype> option just as C<syntax_mark_file> does. +=item ansi() + +Return the string marked with ANSI escape sequences (using L<Term::ANSIColor>) +based on the Vim syntax colouring of the input file. + +This is the default format for the included L<text-vimcolor> script +which makes it like a colored version of C<cat>. + +You can alter the color scheme using the C<TEXT_VIMCOLOR_ANSI> +environment variable in the format of C<< "SynGroup=color;" >>. +For example: + + TEXT_VIMCOLOR_ANSI='Comment=green;Statement = magenta; ' + =item html() Return XHTML markup based on the Vim syntax colouring of the input file. diff --git a/t/06ansi.t b/t/06ansi.t new file mode 100644 index 0000000..027b144 --- /dev/null +++ b/t/06ansi.t @@ -0,0 +1,39 @@ +use strict; +use warnings; +use Test::More; +use Text::VimColor; +use Term::ANSIColor; +use Path::Class qw( file ); + +no warnings 'redefine'; +local *Term::ANSIColor::colored = sub { + return sprintf '[%s]%s[]', $_[0]->[0], $_[1]; +}; + +# clear out possible user customizations that could upset the tests +$ENV{TEXT_VIMCOLOR_ANSI} = ''; +$ENV{HOME} = 't'; + +plan tests => 2; + +my $filetype = 'perl'; +my $syntax = Text::VimColor->new( + filetype => $filetype, +); + + my $input = "# comment\nuse Mod::Name;\nour \$VERSION = 1.0;\n"; + + $syntax->syntax_mark_string($input); + is($syntax->ansi, tag_input(qw(Comment blue Statement yellow Identifier cyan Constant red)), 'default ansi colors'); + + $ENV{TEXT_VIMCOLOR_ANSI} = 'Comment=green;Statement = magenta; '; + + $syntax->syntax_mark_string($input); + is($syntax->ansi, tag_input(qw(Comment green Statement magenta Identifier cyan Constant red)), 'custom ansi colors'); + +sub tag_input { + my %c= @_; + return "[$c{Comment}]# comment[]\n[$c{Statement}]use []Mod::Name;\n[$c{Statement}]our[] [$c{Identifier}]\$VERSION[] = [$c{Constant}]1.0[];\n"; +} + +# vim:ft=perl ts=3 sw=3 expandtab: diff --git a/text-vimcolor b/text-vimcolor index 12c1ee1..aab06f0 100755 --- a/text-vimcolor +++ b/text-vimcolor @@ -11,7 +11,7 @@ my $XSL_STYLESHEET = file($Text::VimColor::SHARED, 'light.xsl'); # Default values for options. my $filetype; -my $format; +my $format = 'ansi'; my $usage; my $output_filename; my $html_full_page; @@ -34,7 +34,7 @@ my $option = GetOptions( if ($usage) { print STDERR - "Usage: $0 --format html|xml [options] filename\n", + "Usage: $0 --format ansi|html|xml [options] filename\n", " $0 --format pdf --output foo.pdf [options] filename\n", "(the output is written to standard output, except in PDF\n", "mode, where you have to supply a filename for the output.)\n", @@ -43,8 +43,8 @@ if ($usage) { " --debug turn on Text::VimColor debugging mode\n", " --filetype set Vim filetype name, if it can't be guessed from\n", " the file's name or contents\n", - " --format set format to use for output, can be xml,\n", - " html, or pdf\n", + " --format set format to use for output, can be ansi,\n", + " html, xml, or pdf\n", " --help show this helpful message\n", " --output set filename to write output to (required with\n", " PDF format, otherwise defaults to standard output)\n", @@ -56,11 +56,11 @@ if ($usage) { } defined $format - or die "$0: an output format must be specified (html, pdf or xml).\n"; + or die "$0: an output format must be specified (ansi, html, pdf or xml).\n"; $format = lc $format; -$format eq 'html' || $format eq 'pdf' || $format eq 'xml' - or die "$0: invalid output format '$format' (must be html, pdf or xml).\n"; +$format eq 'ansi' || $format eq 'html' || $format eq 'pdf' || $format eq 'xml' + or die "$0: invalid output format '$format' (must be ansi, html, pdf or xml).\n"; my $output; if (defined $output_filename) { @@ -107,6 +107,10 @@ elsif ($format eq 'html') { print $output $syntax->html or die "$0: error writing to output file '$output_filename': $!\n"; } +elsif ($format eq 'ansi') { + print $output $syntax->ansi + or die "$0: error writing to output file '$output_filename': $!\n"; +} else { # ($format eq 'pdf') my ($fh, $tmp_filename) = tempfile(); print $fh $syntax->xml @@ -127,10 +131,12 @@ __END__ =head1 NAME -text-vimcolor - command-line program to syntax color a file in HTML, XML or PDF +text-vimcolor - command-line program to syntax color a file in ANSI, HTML, XML or PDF =head1 SYNOPSIS + $ text-vimcolor FILENAME + # (like a colored "cat" (same as "text-vimcolor --format ansi FILENAME")) $ text-vimcolor --format html --full-page FILENAME > OUTPUT.html $ text-vimcolor --format xml FILENAME > OUTPUT.xml $ text-vimcolor --format pdf FILENAME --output OUTPUT.pdf @@ -138,7 +144,7 @@ text-vimcolor - command-line program to syntax color a file in HTML, XML or PDF =head1 DESCRIPTION This program uses the Vim text editor to highlight text according to its -syntax, and turn the highlighting into HTML, XML or PDF output. It works +syntax, and turn the highlighting into ANSI, HTML, XML or PDF output. It works with any file type which Vim itself can highlight. Usually Vim will be able to autodetect the file format based on the filename (and sometimes the contents of the file). @@ -182,6 +188,17 @@ The output format to generate. Must be one of the following: =over 4 +=item ansi + +Output text marked up with ANSI escape sequences (using L<Term::ANSIColor>). +This is like a colorized version of C<cat>. + +You can alter the color scheme using the C<TEXT_VIMCOLOR_ANSI> +environment variable in the format of C<< "SynGroup=color;" >>. +For example: + + TEXT_VIMCOLOR_ANSI='Comment=green;Statement = magenta; ' + =item html Generate XHTML output, with text marked with C<E<lt>spanE<gt>> elements @@ -208,8 +225,8 @@ documentation for Text::VimColor. =item --output I<output-filename> -Specifies the name of the output file (which will end up containing either -HTML, XML or PDF). If this option is omitted, the output will be sent +Specifies the name of the output file. +If this option is omitted, the output will be sent to stdout (the standard output). This option is required when the output format is PDF (because of limitations in FOP).
merged.