| 1 | package Term::ANSIColor::Markup::Parser; | ||||||||||
| 2 | 4 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 4 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 4 | ▶ | use Carp qw(croak); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 4 | ▶ | use base qw( | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | HTML::Parser | ||||||||||
| 7 | Class::Accessor::Lvalue::Fast | ||||||||||
| 8 | 4 | ); | |||||||||
| 9 | |||||||||||
| 10 | # copied from Term::ANSIColor | ||||||||||
| 11 | 4 | our %TAGS = ( | |||||||||
| 12 | clear => 0, | ||||||||||
| 13 | reset => 0, | ||||||||||
| 14 | bold => 1, | ||||||||||
| 15 | dark => 2, | ||||||||||
| 16 | faint => 2, | ||||||||||
| 17 | underline => 4, | ||||||||||
| 18 | underscore => 4, | ||||||||||
| 19 | blink => 5, | ||||||||||
| 20 | reverse => 7, | ||||||||||
| 21 | concealed => 8, | ||||||||||
| 22 | |||||||||||
| 23 | black => 30, on_black => 40, | ||||||||||
| 24 | red => 31, on_red => 41, | ||||||||||
| 25 | green => 32, on_green => 42, | ||||||||||
| 26 | yellow => 33, on_yellow => 43, | ||||||||||
| 27 | blue => 34, on_blue => 44, | ||||||||||
| 28 | magenta => 35, on_magenta => 45, | ||||||||||
| 29 | cyan => 36, on_cyan => 46, | ||||||||||
| 30 | white => 37, on_white => 47, | ||||||||||
| 31 | ); | ||||||||||
| 32 | |||||||||||
| 33 | 4 | __PACKAGE__->mk_accessors(qw(result stack)); | |||||||||
| 34 | |||||||||||
| 35 | 9 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 36 | 9 | my $class = shift; | |||||||||
| 37 | 9 | my $self = $class->SUPER::new(@_); | |||||||||
| 38 | 9 | $self->result = ''; | |||||||||
| 39 | 9 | $self->stack = []; | |||||||||
| 40 | 9 | $self; | |||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | 23 | ▶ | sub start { | ||||||||
|
Subroutine
start: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 44 | 23 | my ($self, $tagname, $attr, $attrseq, $text) = @_; | |||||||||
| 45 | 23 | ▶ | if (my $escape_sequence = $self->get_escape_sequence($tagname)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | 21 | push @{$self->stack}, $tagname; | |||||||||
| 47 | 21 | $self->result .= $escape_sequence; | |||||||||
| 48 | } | ||||||||||
| 49 | else { | ||||||||||
| 50 | 2 | $self->result .= $text; | |||||||||
| 51 | } | ||||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | 59 | ▶ | sub text { | ||||||||
|
Subroutine
text: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 55 | 59 | my ($self, $text) = @_; | |||||||||
| 56 | 59 | $self->result .= $self->unescape($text); | |||||||||
| 57 | } | ||||||||||
| 58 | |||||||||||
| 59 | 25 | ▶ | sub end { | ||||||||
|
Subroutine
end: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 60 | 25 | my ($self, $tagname, $text) = @_; | |||||||||
| 61 | 25 | ▶ | if (my $color = $self->get_escape_sequence($tagname)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | 21 | my $top = pop @{$self->stack}; | |||||||||
| 63 | 21 | ▶ | croak "Invalid end tag was found: $text" if $top ne $tagname; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 20 | $self->result .= $self->get_escape_sequence('reset'); | |||||||||
| 65 | 20 | ▶ | if (scalar @{$self->stack}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 10 | $self->result .= $self->get_escape_sequence($self->stack->[-1]); | |||||||||
| 67 | } | ||||||||||
| 68 | } | ||||||||||
| 69 | else { | ||||||||||
| 70 | 4 | $self->result .= $text; | |||||||||
| 71 | } | ||||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | 78 | ▶ | sub get_escape_sequence { | ||||||||
|
Subroutine
get_escape_sequence: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 75 | 78 | my ($self, $name) = @_; | |||||||||
| 76 | 78 | my $escape_sequence = ''; | |||||||||
| 77 | 78 | for my $key (keys %TAGS) { | |||||||||
| 78 | 1319 | ▶ | if (lc $name eq lc $key) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | 72 | $escape_sequence = sprintf "\e[%dm", $TAGS{$key}; | |||||||||
| 80 | 72 | last; | |||||||||
| 81 | } | ||||||||||
| 82 | } | ||||||||||
| 83 | 78 | $escape_sequence; | |||||||||
| 84 | } | ||||||||||
| 85 | |||||||||||
| 86 | 59 | ▶ | sub unescape { | ||||||||
|
Subroutine
unescape: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 87 | 59 | my ($self, $text) = @_; | |||||||||
| 88 | 59 | ▶ | return '' if !defined $text; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | 51 | $text =~ s/</</ig; | |||||||||
| 90 | 51 | $text =~ s/>/>/ig; | |||||||||
| 91 | 51 | $text; | |||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | 4 | 1; | |||||||||