line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MojoMojo::Formatter::DocBook::Colorize; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
#--------------------------------------------------------------------# |
4
|
|
|
|
|
|
|
# Transform XML Docbook in XHTML (colorized programlisting|screen ) |
5
|
|
|
|
|
|
|
# 'lang' is lost in 'transformation xslt' step then: |
6
|
|
|
|
|
|
|
# mark lang -> transformation xslt -> colorize && unmark lang |
7
|
|
|
|
|
|
|
#--------------------------------------------------------------------# |
8
|
|
|
|
|
|
|
|
9
|
27
|
|
|
27
|
|
165
|
use strict; |
|
27
|
|
|
|
|
66
|
|
|
27
|
|
|
|
|
26683
|
|
10
|
27
|
|
|
27
|
|
681
|
eval "use Syntax::Highlight::Engine::Kate;"; |
|
27
|
|
|
|
|
13702
|
|
|
27
|
|
|
|
|
416
|
|
11
|
|
|
|
|
|
|
my $eval_res = $@; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head2 module_loaded |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
Return true if the module is loaded. |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=cut |
18
|
|
|
|
|
|
|
|
19
|
0
|
0
|
|
0
|
1
|
|
sub module_loaded { $eval_res ? 0 : 1 } |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
my $hl_node="programlisting|screen"; |
22
|
|
|
|
|
|
|
my $hl_attrib="lang"; |
23
|
|
|
|
|
|
|
my $marklang=0; |
24
|
|
|
|
|
|
|
my $colorize=0; |
25
|
|
|
|
|
|
|
my $tomark; |
26
|
|
|
|
|
|
|
my $tocolorize; |
27
|
|
|
|
|
|
|
my $lang; |
28
|
|
|
|
|
|
|
my $doc; |
29
|
|
|
|
|
|
|
my $step; |
30
|
|
|
|
|
|
|
my $debug; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub new { |
34
|
0
|
|
|
0
|
1
|
|
my $type = shift; |
35
|
0
|
|
|
|
|
|
$debug = shift; |
36
|
|
|
|
|
|
|
|
37
|
0
|
0
|
|
|
|
|
my $self = ( $#_ == 0 ) ? shift : { @_ }; |
38
|
|
|
|
|
|
|
|
39
|
0
|
|
|
|
|
|
return bless $self, $type; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub step{ |
44
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
45
|
0
|
|
|
|
|
|
$step = shift; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub start_document{ |
50
|
0
|
0
|
|
0
|
1
|
|
print STDERR "start_document\n" if $debug; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub end_document{ |
55
|
0
|
|
|
0
|
1
|
|
my $result=$doc; |
56
|
0
|
|
|
|
|
|
$doc=""; |
57
|
|
|
|
|
|
|
|
58
|
0
|
0
|
|
|
|
|
print STDERR "end_document\n" if $debug; |
59
|
0
|
|
|
|
|
|
return $result; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub start_element{ |
64
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
65
|
0
|
|
|
|
|
|
my $el = shift; |
66
|
|
|
|
|
|
|
|
67
|
0
|
|
|
|
|
|
my @Attributes = keys %{$el->{Attributes}}; |
|
0
|
|
|
|
|
|
|
68
|
0
|
|
|
|
|
|
my $name = $el->{Name}; |
69
|
|
|
|
|
|
|
|
70
|
0
|
0
|
|
|
|
|
print STDERR "[$step]start_element: $name\n" if $debug; |
71
|
|
|
|
|
|
|
|
72
|
0
|
|
|
|
|
|
$doc .= "<$name"; |
73
|
0
|
|
|
|
|
|
foreach my $att (@Attributes) { |
74
|
0
|
|
|
|
|
|
my $val = $el->{Attributes}->{$att}->{Value}; |
75
|
|
|
|
|
|
|
|
76
|
0
|
|
|
|
|
|
$att =~ s/^\{\}//; |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
# Uppercase fisrt letter of lang |
79
|
0
|
0
|
0
|
|
|
|
$val =~ s/\b(\w)/\U$1/g if (( $att eq "lang" )&&($el->{Name} =~ /$hl_node/)); |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
# Bug XML::SAX::ParserFactory (???) |
82
|
|
|
|
|
|
|
# It add {http://www.w3.org/XML/1998/namespace} before lang="fr" |
83
|
|
|
|
|
|
|
# if attrib class=article|section |
84
|
0
|
0
|
|
|
|
|
if ( $att eq "{http://www.w3.org/XML/1998/namespace}lang") { |
85
|
0
|
|
|
|
|
|
next; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# to be conform to xhtml 1.1 |
89
|
0
|
0
|
0
|
|
|
|
if (( $name eq "div" ) && ( $att eq "lang" )) { |
90
|
0
|
|
|
|
|
|
$att = "xml:lang"; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
0
|
|
|
|
|
|
$doc .= " $att=\"$val\""; |
94
|
|
|
|
|
|
|
|
95
|
0
|
0
|
|
|
|
|
print STDERR " $att=\"$val\"\n" if $debug; |
96
|
|
|
|
|
|
|
|
97
|
0
|
0
|
0
|
|
|
|
if (( $step eq 'marklang') && ( $att =~ /$hl_attrib/i )&&($el->{Name} =~ /$hl_node/ )) { |
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
98
|
0
|
|
|
|
|
|
$lang = $val; |
99
|
0
|
|
|
|
|
|
$marklang=1; |
100
|
|
|
|
|
|
|
} elsif (( $step eq 'colorize' ) && ($el->{Name} eq 'pre' )&&($val =~ /$hl_node/i)) { |
101
|
0
|
|
|
|
|
|
$colorize=1; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
0
|
|
|
|
|
|
$doc .= ">"; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
sub end_element{ |
110
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
111
|
0
|
|
|
|
|
|
my $el = shift; |
112
|
|
|
|
|
|
|
|
113
|
0
|
|
|
|
|
|
my $name = $el->{Name}; |
114
|
|
|
|
|
|
|
|
115
|
0
|
0
|
|
|
|
|
print STDERR "[$step]end_element: $name\n" if $debug; |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
# Mark language |
118
|
0
|
0
|
0
|
|
|
|
if (( $el->{Name} =~ /$hl_node/ ) && ($marklang eq 1 )) { |
|
|
0
|
0
|
|
|
|
|
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
#$tomark =~ s/</</g; |
121
|
|
|
|
|
|
|
#$tomark =~ s/>/>/g; |
122
|
|
|
|
|
|
|
|
123
|
0
|
|
|
|
|
|
$doc .= "[lang=$lang\]\n${tomark}\n\[\/lang\]"; |
124
|
|
|
|
|
|
|
|
125
|
0
|
0
|
|
|
|
|
print STDERR " => MARK LANG\n" if $debug; |
126
|
|
|
|
|
|
|
|
127
|
0
|
|
|
|
|
|
$marklang=0; |
128
|
0
|
|
|
|
|
|
$lang=""; |
129
|
0
|
|
|
|
|
|
$tomark=""; |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
# Colorize |
132
|
|
|
|
|
|
|
elsif (( $el->{Name} =~ /pre/ ) && ($colorize eq 1 )) { |
133
|
|
|
|
|
|
|
|
134
|
0
|
0
|
|
|
|
|
print STDERR " => COLORIZE\n" if $debug; |
135
|
0
|
|
|
|
|
|
$doc .= ColorizeCode($tocolorize); |
136
|
0
|
|
|
|
|
|
$colorize=0; |
137
|
0
|
|
|
|
|
|
$tocolorize=""; |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
0
|
0
|
|
|
|
|
if ( ! $lang ){ $doc =~ s/\n$// } |
|
0
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
|
142
|
0
|
|
|
|
|
|
$doc .= "</$name>"; |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
sub characters{ |
147
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
148
|
0
|
|
|
|
|
|
my $el = shift; |
149
|
|
|
|
|
|
|
|
150
|
0
|
0
|
|
|
|
|
print STDERR "[$step]characters: " . $el->{Data} . "\n" if $debug; |
151
|
|
|
|
|
|
|
|
152
|
0
|
0
|
|
|
|
|
if ( $marklang ) { |
|
|
0
|
|
|
|
|
|
153
|
0
|
|
|
|
|
|
$tomark .= $el->{Data}; |
154
|
|
|
|
|
|
|
} elsif ( $colorize ) { |
155
|
0
|
|
|
|
|
|
$tocolorize .= $el->{Data}; |
156
|
|
|
|
|
|
|
} else { |
157
|
0
|
0
|
|
|
|
|
$doc .= $el->{Data} if ( defined $el->{Data} ); |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
sub ColorizeCode{ |
163
|
0
|
|
|
0
|
1
|
|
my $code = shift; |
164
|
|
|
|
|
|
|
|
165
|
0
|
|
|
|
|
|
$code =~ m/\[lang=(.*)\]/; |
166
|
0
|
|
|
|
|
|
my $lang=$1; |
167
|
|
|
|
|
|
|
|
168
|
0
|
|
|
|
|
|
$code =~ s/^\n//; |
169
|
0
|
|
|
|
|
|
$code =~ s/\[lang=\w*\]\n//g; |
170
|
0
|
|
|
|
|
|
$code =~ s/\[\/lang\]\s*//; |
171
|
0
|
|
|
|
|
|
$code =~ s/\n\s*$//; |
172
|
|
|
|
|
|
|
|
173
|
0
|
0
|
|
|
|
|
if ( $debug ) { |
174
|
0
|
|
|
|
|
|
print STDERR "lang=$lang\ncode=$code\n" . "-"x60 . "\n"; |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
|
177
|
0
|
0
|
|
|
|
|
return $code if ( ! $lang ); |
178
|
0
|
0
|
|
|
|
|
return $code unless __PACKAGE__->module_loaded; |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
|
181
|
0
|
|
|
|
|
|
my $hl = Syntax::Highlight::Engine::Kate->new( |
182
|
|
|
|
|
|
|
language => 'Perl', |
183
|
|
|
|
|
|
|
substitutions => { |
184
|
|
|
|
|
|
|
"&" => "&", |
185
|
|
|
|
|
|
|
" " => " ", |
186
|
|
|
|
|
|
|
"\t" => " ", |
187
|
|
|
|
|
|
|
"\n" => "\n", |
188
|
|
|
|
|
|
|
}, |
189
|
|
|
|
|
|
|
format_table => { |
190
|
|
|
|
|
|
|
Alert => [ q{<span class="kateAlert">}, "</span>" ], |
191
|
|
|
|
|
|
|
BaseN => [ q{<span class="kateBaseN">}, "</span>" ], |
192
|
|
|
|
|
|
|
BString => [ q{<span class="kateBString">}, "</span>" ], |
193
|
|
|
|
|
|
|
Char => [ q{<span class="kateChar">}, "</span>" ], |
194
|
|
|
|
|
|
|
Comment => [ q{<span class="kateComment"><i>}, "</i></span>" ], |
195
|
|
|
|
|
|
|
DataType => [ q{<span class="kateDataType">}, "</span>" ], |
196
|
|
|
|
|
|
|
DecVal => [ q{<span class="kateDecVal">}, "</span>" ], |
197
|
|
|
|
|
|
|
Error => [ q{<span class="kateError"><b><i>}, "</i></b></span>" ], |
198
|
|
|
|
|
|
|
Float => [ q{<span class="kateFloat">}, "</span>" ], |
199
|
|
|
|
|
|
|
Function => [ q{<span class="kateFunction">}, "</span>" ], |
200
|
|
|
|
|
|
|
IString => [ q{<span class="kateIString">}, "" ], |
201
|
|
|
|
|
|
|
Keyword => [ q{<b>}, "</b>" ], |
202
|
|
|
|
|
|
|
Normal => [ q{}, "" ], |
203
|
|
|
|
|
|
|
Operator => [ q{<span class="kateOperator">}, "</span>" ], |
204
|
|
|
|
|
|
|
Others => [ q{<span class="kateOthers">}, "</span>" ], |
205
|
|
|
|
|
|
|
RegionMarker => [ q{<span class="kateRegionMarker"><i>}, "</i></span>" ], |
206
|
|
|
|
|
|
|
Reserved => [ q{<span class="kateReserved"><b>}, "</b></span>" ], |
207
|
|
|
|
|
|
|
String => [ q{<span class="kateString">}, "</span>" ], |
208
|
|
|
|
|
|
|
Variable => [ q{<span class="kateVariable"><b>}, "</b></span>" ], |
209
|
|
|
|
|
|
|
Warning => [ q{<span class="kateWarning"><b><i>}, "</b></i></span>" ], |
210
|
|
|
|
|
|
|
}, |
211
|
|
|
|
|
|
|
); |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
|
214
|
0
|
|
|
|
|
|
my @LANGS=$hl->languageList; |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
# check lang |
217
|
0
|
|
|
|
|
|
my @goodlang = grep(/$lang/i, @LANGS ); |
218
|
0
|
0
|
|
|
|
|
if ( ! $goodlang[0] ) { |
219
|
0
|
|
|
|
|
|
return "{<span class=\"kateError\">Language '$lang' unknown !!! in :\n". "-"x80 . "\n${code}\n" ."-"x80 . "\n" . "Authorized languages : @LANGS</span>"; |
220
|
|
|
|
|
|
|
} |
221
|
|
|
|
|
|
|
|
222
|
0
|
|
|
|
|
|
$hl->language($goodlang[0]); |
223
|
0
|
|
|
|
|
|
my $result = $hl->highlightText($code); |
224
|
|
|
|
|
|
|
|
225
|
0
|
|
|
|
|
|
return $result; |
226
|
|
|
|
|
|
|
} |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
1; |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
__END__ |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
=head1 NAME |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
ColorizeDbk - syntax-highlight docbook |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
=head1 FUNCTIONS |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
I think these are all internal. |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
=head2 new |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
=head2 start_tag |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
=head2 end_tag |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
=head2 ColorizeCode |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
=head2 characters |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
=head2 end_document |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
=head2 end_element |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
=head2 start_document |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
=head2 start_element |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
=head2 step |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
=head1 AUTHORS |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
Daniel Brosseau <dab@catapulse.org> |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
=head1 LICENSE |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
This module is licensed under the same terms as Perl itself. |
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
=cut |