Quiq/Html/Pygments.pm

filter: Statementstmt 48.0% 37 / 77 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 75.0% 12 / 16 Podpod 100% 4 / 4 totaltotal 47.7% 53 / 111 CC 8 SCAR 25.3
CC
8
Cov
42%
css
42.0
styles
6.0
stylesPage
6.0
CRAP 12.6
« Quiq/SQLite.pm ↑ Summary Quiq/FileHandle.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Html::Pygments - Syntax Highlighting in HTML
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Hash>
12  
13 =head1 SYNOPSIS
14  
15 Modul laden:
16  
17 use Quiq::Html::Pygments;
18  
19 Liefere die CSS-Regeln für Pygments-Style 'emacs', eingeschränkt
20 auf einen Container 'highlight', der den gehighlighteten Code
21 aufnimmt:
22  
23 ($rules,$bgColor) = Quiq::Html::Pygments->css('emacs','highlight');
24 # .highlight .hll { background-color: #ffffcc }
25 # ...
26 # #f8f8f8
27  
28 Erzeuge Syntax-Highlighting für Perl-Code $code. Der gelieferte
29 HTML-Code $html muss in einen Container 'highlight' (s.o.)
30 eingebettet werden, damit die oben erzeugten CSS-Regeln greifen:
31  
32 $html = Quiq::Html::Pygments->html('perl',$code);
33  
34 Liefere die Namen aller Pygments-Styles:
35  
36 @styles = Quiq::Html::Pygments->styles;
37  
38 Liefere eine HTML-Seite mit einem Darstellungsbeispiel
39 für jeden Pygments-Style. Gehighlightet wird der Code $code
40 der Programmiersprache $lang:
41  
42 $html = Quiq::Html::Pygments->stylesPage($h,$lang,$code);
43  
44 =head1 DESCRIPTION
45  
46 Diese Klasse stellt eine Schnittstelle zum Pygments Syntax
47 Highlighting Paket dar, speziell zum Syntax Highlighting in HTML.
48 Die Methoden der Klassen liefern die CSS-Regeln und den HTML-Code,
49 um gehighlighteten Quelltext in HTML-Seiten integrieren zu können.
50  
51 =cut
52  
53 # -----------------------------------------------------------------------------
54  
55 package Quiq::Html::Pygments;
56 1 ▶use base qw/Quiq::Hash/;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62 1 our $VERSION = '1.240';
63  
64 1 ▶use Quiq::CommandLine;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 1 ▶use Quiq::Shell;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 1 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
67 1 ▶use Quiq::Ipc;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 1 ▶use Quiq::Unindent;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 1 ▶use Quiq::Css;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 1 ▶use Quiq::Html::Table::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 1 ▶use Quiq::Html::Page;
Subroutine BEGIN: called CC=1 SCAR=0.0
72  
73 # -----------------------------------------------------------------------------
74  
75 =head1 METHODS
76  
77 =head2 Klassenmethoden
78  
79 =head3 css() - CSS-Information für Highlighting in HTML
80  
81 =head4 Synopsis
82  
83 ($rules,$bgColor) | $rules = $class->css;
84 ($rules,$bgColor) | $rules = $class->css($style);
85 ($rules,$bgColor) | $rules = $class->css($style,$selector);
86  
87 =head4 Arguments
88  
89 =over 4
90  
91 =item $style (Default: 'default')
92  
93 Name des Pygments-Style, für den die CSS-Information geliefert wird.
94  
95 Mögliche Werte: abap, algol, algol_nu, arduino, autumn, borland,
96 bw, colorful, default, emacs, friendly, fruity, igor, lovelace,
97 manni, monokai, murphy, native, paraiso-dark, paraiso-light,
98 pastie, perldoc, rainbow_dash, rrt, tango, trac, vim, vs, xcode.
99  
100 Die definitiv gültige Liste der Stylenamen liefert die Methode
101 styles().
102  
103 =item $selector (Default: I<kein zusätzlicher Selektor>)
104  
105 Zusätzlicher CSS-Selektor, der den CSS-Regeln vorangestellt
106 wird. Der Selektor schränkt den Gültigkeitsbereich der CSS-Regeln
107 auf ein Parent-Element ein. Ist kein Selektor angegeben, gelten
108 die CSS-Regeln global.
109  
110 =back
111  
112 =head4 Returns
113  
114 CSS-Regeln und Hintergrundfarbe (String, String). Im Skalarkontext
115 werden nur die CSS-Regeln geliefert.
116  
117 =head4 Description
118  
119 Liefere die CSS-Regeln für die Vordergrund-Darstellung von
120 Syntax-Elementen und die zugehörige Hintergrundfarbe für
121 Pygments-Style $style.
122  
123 =head4 Example
124  
125 Gib die CSS-Regeln für den Pyments-Style 'emacs' aus:
126  
127 print scalar Quiq::Html::Pygments->css('emacs');
128 __END__
129 .hll { background-color: #ffffcc }
130 .c { color: #008800; font-style: italic } /* Comment */
131 .err { border: 1px solid #FF0000 } /* Error */
132 .k { color: #AA22FF; font-weight: bold } /* Keyword */
133 .o { color: #666666 } /* Operator */
134  
135 Gib die CSS-Regeln für den Pyments-Style 'emacs' und
136 Parent-Elemente der Klasse 'highlight' aus:
137  
138 print scalar Quiq::Html::Pygments->css('emacs','.syntax');
139 __END__
140 .syntax .hll { background-color: #ffffcc }
141 .syntax .c { color: #008800; font-style: italic } /* Comment */
142 .syntax .err { border: 1px solid #FF0000 } /* Error */
143 .syntax .k { color: #AA22FF; font-weight: bold } /* Keyword */
144 .syntax .o { color: #666666 } /* Operator */
145  
146 =cut
147  
148 # -----------------------------------------------------------------------------
149  
150 0 ▶sub css {
Subroutine css: not called CC=6 SCAR=37.4
Pod documented
151 0 my $class = shift;
152 0 ▶ my $style = shift // 'default';
Condition0%0/2
shift() // "default"
l!l
00
MC/DC0%0/1
shift() // "default"
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
153 0 ▶ my $selector = shift // 'D-U-M-M-Y';
Condition0%0/2
shift() // "D-U-M-M-Y"
l!l
00
MC/DC0%0/1
shift() // "D-U-M-M-Y"
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
154  
155 0 my $c = Quiq::CommandLine->new('pygmentize');
156 0 $c->addOption(
157 -f => 'html',
158 -S => $style,
159 -a => $selector,
160 );
161  
162 0 my $rules = Quiq::Shell->exec($c->command,-capture=>'stdout');
163 0 $rules = Encode::decode('utf-8',$rules); # Wir erwarten UTF-8
164  
165 # Bestimme Hintergrundfarbe, diese muss existieren, da die
166 # Forderungrundfarben darauf abgestimmt sind.
167  
168 0 $rules =~ s/^$selector\s*\{.*background:\s*(\S+);.*\n//m;
169 0 my $bgColor = $1;
170 0 ▶ if (!$bgColor) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bgColor) 0 0 0
171 0 $class->throw(
172 'PYG-00001: Can\'t determine main background-color',
173 Style => $style,
174 CssRules => $rules,
175 );
176 }
177  
178 0 ▶ if ($selector eq 'D-U-M-M-Y') {
Branch: 1 branch
BranchTrueFalseTotal
if ($selector eq "D-U-M-M-Y") 0 0 0
179 # Entferne Dummy-Selektor aus den CSS-Regeln
180 0 $rules =~ s/^$selector\s+//mg;
181 }
182  
183 0 ▶ return wantarray? ($rules,$bgColor): $rules;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
184 }
185  
186 # -----------------------------------------------------------------------------
187  
188 =head3 html() - Quellcode in HTML highlighten
189  
190 =head4 Synopsis
191  
192 $html = $class->html($lang,$code);
193  
194 =head4 Arguments
195  
196 =over 4
197  
198 =item $lang
199  
200 Die Sprache des Quelltexts $code. In Pygments-Terminiologie
201 handelt es sich um den Namen eines "Lexers". Die Liste aller
202 Lexer liefert das Kommando:
203  
204 $ pygmentize -L lexers
205  
206 =item $code
207  
208 Der Quelltext, der gehighlightet wird.
209  
210 =back
211  
212 =head4 Returns
213  
214 HTML-Code mit gehighlightetem Quelltext (String)
215  
216 =head4 Description
217  
218 Liefere den HTML-Code mit dem Syntax-Highlighting für Quelltext $code
219 der Sprache $lang.
220  
221 =cut
222  
223 # -----------------------------------------------------------------------------
224  
225 0 ▶sub html {
Subroutine html: not called CC=1 SCAR=6.9
Pod documented
226 0 my ($class,$lang,$code) = @_;
227  
228 # Quelltext highlighten
229  
230 0 my $c = Quiq::CommandLine->new('pygmentize');
231 0 $c->addOption(
232 -f => 'html',
233 -l => $lang, # = lexer
234 );
235 0 my $html = Quiq::Ipc->filter($c->command,$code);
236 0 $html = Encode::decode('utf-8',$html); # Wir erwarten UTF-8
237  
238 # Nicht benötigte "Umrahmung" des gehighlighteten Code entfernen
239  
240 0 $html =~ s|^<div.*?><pre>(<span></span>)?||;
241 0 $html =~ s|</pre></div>\s*$||;
242  
243 0 return $html;
244 }
245  
246 # -----------------------------------------------------------------------------
247  
248 =head3 styles() - Liste der Pygments-Styles
249  
250 =head4 Synopsis
251  
252 @styles | $styleA = $class->styles;
253  
254 =head4 Returns
255  
256 Liste von Pygments Stylenamen (Array of Strings).
257  
258 =head4 Description
259  
260 Ermittele die Liste der Namen aller Pygments-Styles und liefere diese
261 zurück. Im Skalarkontext liefere ein Referenz auf die Liste.
262  
263 Interaktiv lässt sich die (kommentierte) Liste aller Styles
264 ermitteln mit:
265  
266 $ pygmentize -L styles
267  
268 =cut
269  
270 # -----------------------------------------------------------------------------
271  
272 0 ▶sub styles {
Subroutine styles: not called CC=2 SCAR=17.9
Pod documented
273 0 my $class = shift;
274  
275 # Styles ermitteln
276  
277 0 my $c = Quiq::CommandLine->new('pygmentize');
278 0 $c->addOption(
279 -L => 'styles',
280 );
281 0 my $text = Quiq::Shell->exec($c->command,-capture=>'stdout');
282 0 my @styles = sort $text =~ /^\* (\S+?):/mg;
283  
284 0 ▶ return wantarray? @styles: \@styles;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
285 }
286  
287 # -----------------------------------------------------------------------------
288  
289 =head3 stylesPage() - HTML-Seite mit allen Styles
290  
291 =head4 Synopsis
292  
293 $html = $class->stylesPage($h,$lang,$code);
294  
295 =head4 Arguments
296  
297 =over 4
298  
299 =item $h
300  
301 HTML-Generator.
302  
303 =item $lang
304  
305 Die Sprache des Quelltexts $code (siehe auch Methode html()).
306  
307 =item $code
308  
309 Beispiel-Quelltext der Sprache $lang.
310  
311 =back
312  
313 =head4 Returns
314  
315 HTML-Seite (String)
316  
317 =head4 Description
318  
319 Erzeuge für Codebeispiel $code der Sprache (des "Lexers") $lang
320 eine HTML-Seite mit allen Pygments-Styles und liefere diese
321 zurück.
322  
323 Diese Seite bietet Hilfestellung für die Entscheidung, welcher
324 Style am besten passt.
325  
326 =head4 Example
327  
328 Generiere eine Seite mit allen Styles und schreibe sie auf Datei $file:
329  
330 my $h = Quiq::Html::Tag->new;
331 my $html = Quiq::Html::Pygments->stylesPage($h,'perl',q~
332 PERL-CODE
333 ~));
334 Quiq::Path->write($file,$html);
335  
336 =cut
337  
338 # -----------------------------------------------------------------------------
339  
340 0 ▶sub stylesPage {
Subroutine stylesPage: not called CC=2 SCAR=17.9
Pod documented
341 0 my ($class,$h,$lang,$code) = @_;
342  
343 0 $code = Quiq::Unindent->trimNl($code);
344  
345 0 my $css = Quiq::Css->new('flat');
346  
347 0 my ($rules,$html);
348 0 $rules .= $css->rule('td pre',
349 margin => 0,
350 );
351 0 for my $style ($class->styles) {
352 0 my ($styleRules,$bgColor) = $class->css($style,".$style");
353 0 $rules .= $css->rule(".$style",
354 backgroundColor => $bgColor,
355 border => '1px solid #e0e0e0',
356 marginLeft => '1em',
357 padding => '4px',
358 );
359 0 $rules .= $styleRules;
360  
361 0 $html .= $h->tag('h3',$style);
362 0 $html .= Quiq::Html::Table::Simple->html($h,
363 class => $style,
364 rows => [[[$h->tag('pre',$class->html($lang,$code))]]],
365 );
366 }
367  
368 0 return Quiq::Html::Page->html($h,
369 title => 'Pygments Styles',
370 styleSheet => $rules,
371 body => $html,
372 );
373 }
374  
375 # -----------------------------------------------------------------------------
376  
377 =head1 VERSION
378  
379 1.240
380  
381 =head1 AUTHOR
382  
383 Frank Seitz, L<http://fseitz.de/>
384  
385 =head1 COPYRIGHT
386  
387 Copyright (C) 2026 Frank Seitz
388  
389 =head1 LICENSE
390  
391 This code is free software; you can redistribute it and/or modify
392 it under the same terms as Perl itself.
393  
394 =cut
395  
396 # -----------------------------------------------------------------------------
397  
398 1 1;
399  
400 # eof
« Quiq/SQLite.pm ↑ Summary Quiq/FileHandle.pm »