diff options
Diffstat (limited to 'Master/tlpkg/tlperl/lib/Pod/Text/Color.pm')
-rw-r--r-- | Master/tlpkg/tlperl/lib/Pod/Text/Color.pm | 99 |
1 files changed, 74 insertions, 25 deletions
diff --git a/Master/tlpkg/tlperl/lib/Pod/Text/Color.pm b/Master/tlpkg/tlperl/lib/Pod/Text/Color.pm index fa1aadb4633..8d956f2a5dd 100644 --- a/Master/tlpkg/tlperl/lib/Pod/Text/Color.pm +++ b/Master/tlpkg/tlperl/lib/Pod/Text/Color.pm @@ -1,14 +1,10 @@ -# Pod::Text::Color -- Convert POD data to formatted color ASCII text +# Convert POD data to formatted color ASCII text # # This is just a basic proof of concept. It should later be modified to make # better use of color, take options changing what colors are used for what # text, and the like. # -# Copyright 1999, 2001, 2004, 2006, 2008, 2009, 2014 -# Russ Allbery <rra@cpan.org> -# -# This program is free software; you may redistribute it and/or modify it -# under the same terms as Perl itself. +# SPDX-License-Identifier: GPL-1.0-or-later OR Artistic-1.0-Perl ############################################################################## # Modules and declarations @@ -21,13 +17,13 @@ use strict; use warnings; use Pod::Text (); -use Term::ANSIColor qw(colored); +use Term::ANSIColor qw(color colored); use vars qw(@ISA $VERSION); @ISA = qw(Pod::Text); -$VERSION = '4.10'; +$VERSION = '4.11'; ############################################################################## # Overrides @@ -37,6 +33,7 @@ $VERSION = '4.10'; sub cmd_head1 { my ($self, $attrs, $text) = @_; $text =~ s/\s+$//; + local $Term::ANSIColor::EACHLINE = "\n"; $self->SUPER::cmd_head1 ($attrs, colored ($text, 'bold')); } @@ -52,9 +49,27 @@ sub cmd_b { return colored ($_[2], 'bold') } sub cmd_f { return colored ($_[2], 'cyan') } sub cmd_i { return colored ($_[2], 'yellow') } +# Analyze a single line and return any formatting codes in effect at the end +# of that line. +sub end_format { + my ($self, $line) = @_; + my $reset = color ('reset'); + my $current; + while ($line =~ /(\e\[[\d;]+m)/g) { + my $code = $1; + if ($code eq $reset) { + undef $current; + } else { + $current .= $code; + } + } + return $current; +} + # Output any included code in green. sub output_code { my ($self, $code) = @_; + local $Term::ANSIColor::EACHLINE = "\n"; $code = colored ($code, 'green'); $self->output ($code); } @@ -77,19 +92,48 @@ sub wrap { my $spaces = ' ' x $$self{MARGIN}; my $width = $$self{opt_width} - $$self{MARGIN}; - # We have to do $shortchar and $longchar in variables because the + # $codes matches a single special sequence. $char matches any number of + # special sequences preceding a single character other than a newline. + # $shortchar matches some sequence of $char ending in codes followed by + # whitespace or the end of the string. $longchar matches exactly $width + # $chars, used when we have to truncate and hard wrap. + # + # $shortchar and $longchar are created in a slightly odd way because the # construct ${char}{0,$width} didn't do the right thing until Perl 5.8.x. - my $char = '(?:(?:\e\[[\d;]+m)*[^\n])'; - my $shortchar = $char . "{0,$width}"; - my $longchar = $char . "{$width}"; + my $code = '(?:\e\[[\d;]+m)'; + my $char = "(?>$code*[^\\n])"; + my $shortchar = '^(' . $char . "{0,$width}(?>$code*)" . ')(?:\s+|\z)'; + my $longchar = '^(' . $char . "{$width})"; while (length > $width) { - if (s/^($shortchar)\s+// || s/^($longchar)//) { + if (s/$shortchar// || s/$longchar//) { $output .= $spaces . $1 . "\n"; } else { last; } } $output .= $spaces . $_; + + # less -R always resets terminal attributes at the end of each line, so we + # need to clear attributes at the end of lines and then set them again at + # the start of the next line. This requires a second pass through the + # wrapped string, accumulating any attributes we see, remembering them, + # and then inserting the appropriate sequences at the newline. + if ($output =~ /\n/) { + my @lines = split (/\n/, $output); + my $start_format; + for my $line (@lines) { + if ($start_format && $line =~ /\S/) { + $line =~ s/^(\s*)(\S)/$1$start_format$2/; + } + $start_format = $self->end_format ($line); + if ($start_format) { + $line .= color ('reset'); + } + } + $output = join ("\n", @lines); + } + + # Fix up trailing whitespace and return the results. $output =~ s/\s+$/\n\n/; $output; } @@ -101,13 +145,13 @@ sub wrap { 1; __END__ +=for stopwords +Allbery + =head1 NAME Pod::Text::Color - Convert POD data to formatted color ASCII text -=for stopwords -Allbery - =head1 SYNOPSIS use Pod::Text::Color; @@ -135,23 +179,28 @@ This is just a basic proof of concept. It should be seriously expanded to support configurable coloration via options passed to the constructor, and B<pod2text> should be taught about those. -=head1 SEE ALSO - -L<Pod::Text>, L<Pod::Simple> - -The current version of this module is always available from its web site at -L<http://www.eyrie.org/~eagle/software/podlators/>. It is also part of the -Perl core distribution as of 5.6.0. - =head1 AUTHOR Russ Allbery <rra@cpan.org>. =head1 COPYRIGHT AND LICENSE -Copyright 1999, 2001, 2004, 2006, 2008, 2009 Russ Allbery <rra@cpan.org>. +Copyright 1999, 2001, 2004, 2006, 2008, 2009, 2018 Russ Allbery +<rra@cpan.org> This program is free software; you may redistribute it and/or modify it under the same terms as Perl itself. +=head1 SEE ALSO + +L<Pod::Text>, L<Pod::Simple> + +The current version of this module is always available from its web site at +L<https://www.eyrie.org/~eagle/software/podlators/>. It is also part of the +Perl core distribution as of 5.6.0. + =cut + +# Local Variables: +# copyright-at-end-flag: t +# End: |