App/Chart/Pango/Ex/ANSItoMarkup.pm untested

filter: Statementstmt 0.0% 0 / 61 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 133 CC 25 SCAR 63.0
CC
25
Cov
0%
convert
650.0
new
2.0
BEGIN
2.0
CRAP 542.0
1 # sit on unmatched partial esc[30m etc
2  
3  
4  
5  
6 # Copyright 2007, 2008, 2009, 2010 Kevin Ryde
7  
8 # This file is part of Chart.
9 #
10 # Chart is free software; you can redistribute it and/or modify it under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 3, or (at your option) any later version.
13 #
14 # Chart is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 # details.
18 #
19 # You should have received a copy of the GNU General Public License along
20 # with Chart. If not, see <http://www.gnu.org/licenses/>.
21  
22  
23 package App::Chart::Pango::Ex::ANSItoMarkup;
24 use 5.008;
25 use strict;
26 use warnings;
27  
28 my %shorthand = ('<span weight=bold' => 'b',
29 '<span style=italic' => 'i',
30 '<span strikethrough=true' => 's',
31 '<span underline=single' => 'u');
32  
33 my @colour = ('black',
34 'red',
35 'green',
36 'yellow',
37 'blue',
38 'magenta',
39 'cyan',
40 'white');
41  
42 use constant { _A_FOREGROUND => 0,
43 _A_BACKGROUND => 1,
44 _A_STRIKETHROUGH => 2,
45 _A_STYLE => 3,
46 _A_UNDERLINE => 4,
47 _A_WEIGHT => 5,
48 _A_RISE => 6,
49 };
50  
51 sub new {
52 my ($class) = @_;
53 return bless { attr => [],
54 }, $class;
55 }
56  
57 sub convert {
58 my ($class_or_self, $str) = @_;
59 my $self = (ref $class_or_self ? $class_or_self : $class_or_self->new);
60  
61 my $attr = $self->{'attr'};
62 if (defined (my $prev = delete $self->{'previous'})) {
63 $str = $prev . $str;
64 }
65  
66 defined (my $pos = index ($str, "\e"))
67 or return $str;
68  
69 pos($str) = $pos;
70 my $ret = substr ($str, 0, $pos);
71  
72 while ($str =~ /\G
73 ( # $1 whole esc+text
74 (?:\e( # $2 after esc
75 \[([0-9;]*) # $3 SGR numbers
76 m
77 |
78 [KL])?)?
79 ([^\e]*)) # $4 plain text after
80 /gx) {
81 my $part = $4;
82 if (defined $3) {
83 foreach my $num (split /;/, $3) {
84 if ($num == 0) {
85 @$attr = ();
86  
87 } elsif ($num == 1) {
88 # bold
89 $attr->[_A_WEIGHT] = 'weight=bold';
90  
91 } elsif ($num == 2) {
92 # faint, or dark, or something
93 $attr->[_A_WEIGHT] = 'weight=light';
94  
95 } elsif ($num == 3) {
96 # italic
97 $attr->[_A_STYLE] = 'style=italic';
98  
99 } elsif ($num == 4) {
100 # single underline
101 $attr->[_A_UNDERLINE] = 'underline=single';
102  
103 # 5 slow blink
104 # 6 fast blink
105 # nothing for these in pango
106  
107 # 7 "negative image", reverse video
108 # would kinda need to know what the normal colours are
109  
110 # 8 concealed
111 # does this mean invisible?
112  
113 } elsif ($num == 9) {
114 $attr->[_A_STRIKETHROUGH] = 'strikethrough=true';
115  
116 # 10 primary font
117 # 11 first alternative font
118 # ...
119 # 19 ninth alternative font
120 # 20 fraktur gothic
121  
122 } elsif ($num == 21) {
123 # double underline
124 $attr->[_A_UNDERLINE] = 'underline=double';
125  
126 } elsif ($num == 22) {
127 # normal colour/intensity, ie. neither bold nor faint
128 $attr->[_A_WEIGHT] = undef;
129  
130 } elsif ($num == 23) {
131 # not italic
132 $attr->[_A_STYLE] = undef;
133  
134 } elsif ($num == 24) {
135 # not underlined
136 $attr->[_A_UNDERLINE] = undef;
137  
138 # 25 not blinking
139 # nothing for blinking in pango
140 # 26 reserved (for proportional spacing)
141 # 27 positive image, ie. not reverse video
142 # 28 revealled chars, ie. not concealed
143  
144 } elsif ($num == 29) {
145 # not strikethrough
146 $attr->[_A_STRIKETHROUGH] = undef;
147  
148 } elsif ($num >= 30 && $num <= 37) {
149 $attr->[_A_FOREGROUND] = 'foreground=' . $colour[$num-30];
150  
151 # 38 reserved (for foreground colour)
152  
153 } elsif ($num == 39) {
154 # default foreground colour
155 $attr->[_A_FOREGROUND] = undef;
156  
157 } elsif ($num >= 40 && $num <= 47) {
158 $attr->[_A_BACKGROUND] = 'background=' . $colour[$num-40];
159  
160 # 48 reserved (for background colour)
161  
162 } elsif ($num == 49) {
163 # default background colour
164 $attr->[_A_BACKGROUND] = undef;
165  
166 # 50 reserved (for cancelling 26 proportional spacing)
167 # 51 framed
168 # 52 encircled
169 # 53 overlined
170 # no overline in pango markup
171 # 54 not framed or encircled
172 # 55 not overlined
173 # 56-59 reserved
174 # 60 ideogram underline right side line
175 # 61 ideogram double underline right side line
176 # 62 ideogram overline left side line
177 # 63 ideogram double overline left side
178 # 64 ideogram stress
179 # 65 ideogram normal, ie. undo 60-64
180 }
181 }
182 } elsif (defined $2) {
183 # sub/sup rise 5000, meaning 0.5 em, same as pango-markup.c does for
184 # <sub>, <sup> shorthands
185 if ($2 eq 'K') {
186 # PLD partial line down subscript or cancel superscript
187 $self->{'rise'} -= 5000;
188  
189 } elsif ($2 eq 'L') {
190 # PLU partial line up superscript or cancel subscript
191 $self->{'rise'} += 5000;
192 }
193 $attr->[_A_RISE] = (($self->{'rise'}||0) == 0 ? undef
194 : "rise=".$self->{'rise'});
195  
196 } else {
197 $part = $1;
198 }
199  
200 my $span = join (' ', '<span', grep {defined} @$attr);
201 if ($span eq '<span') {
202 # no markup at all
203 $ret .= $part;
204 } elsif (my $short = $shorthand{$span}) {
205 $ret .= "<$short>$part</$short>";
206 } else {
207 $ret .= "$span>$part</span>";
208 }
209 }
210 return $ret;
211 }
212  
213 # print __PACKAGE__->convert("\eKsub\eLn\eZorm\e[1mxyz\e[0mabc\e[30mxyz\e[0m");
214  
215 1;
216 __END__