Quiq/LaTeX/Code.pm

filter: Statementstmt 92.1% 59 / 64 Branchbran 71.4% 10 / 14 Conditioncond 83.3% 5 / 6 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 100% 9 / 9 Podpod 100% 4 / 4 totaltotal 89.1% 90 / 101 CC 10 SCAR 15.2
CC
10
Cov
88%
env
8.3
section
3.1
BEGIN
1.0
CRAP 4.6
« Quiq/Gimp.pm ↑ Summary Quiq/Xml/LibXml.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::LaTeX::Code - Generator für LaTeX Code
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::TeX::Code>
12  
13 =head1 DESCRIPTION
14  
15 Ein Objekt der Klasse repräsentiert einen LaTeX
16 Code-Generator. Mit den Methoden der Klasse kann aus einem
17 Perl-Programm heraus LaTeX-Code erzeugt werden. Die Klasse stützt
18 sich ab auf ihre Basisklasse Quiq::TeX::Code. Weitere Methoden
19 siehe dort.
20  
21 =cut
22  
23 # -----------------------------------------------------------------------------
24  
25 package Quiq::LaTeX::Code;
26 4 ▶use base qw/Quiq::TeX::Code/;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 4 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 4 our $VERSION = '1.240';
33  
34 4 ▶use Quiq::Option;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 # -----------------------------------------------------------------------------
37  
38 =head1 METHODS
39  
40 =head2 Konstruktor
41  
42 =head3 new() - Instantiiere LaTeX Code-Generator
43  
44 =head4 Synopsis
45  
46 $l = $class->new;
47  
48 =head4 Description
49  
50 Instantiiere einen LaTeX Code-Generator und liefere eine Referenz
51 auf dieses Objekt zurück.
52  
53 =cut
54  
55 # -----------------------------------------------------------------------------
56  
57 8 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
58 8 return shift->SUPER::new;
59 }
60  
61 # -----------------------------------------------------------------------------
62  
63 =head2 Objektmethoden
64  
65 =head3 protect() - Schütze LaTeX Metazeichen
66  
67 =head4 Synopsis
68  
69 $code = $l->protect($text);
70  
71 =head4 Description
72  
73 Schütze LaTeX-Metazeichen in $text und liefere den resultierenden
74 Code zurück.
75  
76 Liste/Erläuterung der LaTeX-Metazeichen:
77 L<https://www.namsu.de/Extra/strukturen/Sonderzeichen.html>
78  
79 =head4 Examples
80  
81 B<Dollarzeichen>
82  
83 $l->protect('Der Text $text wird geschützt.');
84  
85 produziert
86  
87 Der Text \$text wird geschützt.
88  
89 =cut
90  
91 # -----------------------------------------------------------------------------
92  
93 16 ▶sub protect {
Subroutine protect: called CC=1 SCAR=0.0
Pod documented
94 16 my ($self,$text) = @_;
95  
96 # Vorhandene Backslashes kennzeichnen und zum Schluss ersetzen.
97 # Dies ist wg. der eventuellen Ersetzung in \textbackslash{}
98 # nötig, wobei dann geschweifte Klammern entstehen würden.
99 16 $text =~ s/\\/\\\x1d/g;
100  
101 # Reservierte und Sonderzeichen wandeln
102 16 $text =~ s/([\$_%{}#&])/\\$1/g; # $ _ % { } # &
103 16 $text =~ s/>/\\textgreater{}/g; # >
104 16 $text =~ s/</\\textless{}/g; # <
105 16 $text =~ s/~/\\textasciitilde{}/g; # ~
106 16 $text =~ s/\^/\\textasciicircum{}/g; # ^
107 16 $text =~ s/\|/\\textbar{}/g; # |
108 16 $text =~ s/LaTeX/\\LaTeX{}/g; # LaTeX
109 16 $text =~ s/(?<!La)TeX/\\TeX{}/g; # TeX
110  
111 # Gekennzeichnete Backslashes zum Schluss wandeln
112 16 $text =~ s/\\\x1d/\\textbackslash{}/g; # \
113  
114 16 return $text;
115 }
116  
117 # -----------------------------------------------------------------------------
118  
119 =head3 env() - Erzeuge LaTeX-Umgebung
120  
121 =head4 Synopsis
122  
123 $code = $l->env($name,$body,@args);
124  
125 =head4 Options
126  
127 Siehe Methode $t->macro(). Weitere Optionen:
128  
129 =over 4
130  
131 =item -indent => $n (Default: 2)
132  
133 Rücke den Inhalt der Umgebung für eine bessere
134 Quelltext-Lesbarkeit um $n Leerzeichen ein. Achtung: In einer
135 Verbatim-Umgebung hat dies Auswirkungen auf die Darstellung und
136 sollte in dem Fall mit C<< -indent => 0 >> abgeschaltet werden.
137  
138 =back
139  
140 =head4 Description
141  
142 Erzeuge eine LaTeX-Umgebung und liefere den resultierenden Code
143 zurück. Body $body und @args können in beliebiger Reihenfolge
144 auftreten.
145  
146 =head4 Examples
147  
148 B<Document-Umgebung mit Text>
149  
150 $l->env('document','Dies ist ein Text.');
151  
152 produziert
153  
154 \begin{document}
155 Dies ist ein Text.
156 \end{document}
157  
158 =cut
159  
160 # -----------------------------------------------------------------------------
161  
162 4 ▶sub env {
Subroutine env: called CC=8 SCAR=21.1
Pod documented
163 4 my $self = shift;
164 4 my $name = shift;
165 # @_: $body,@args
166  
167 # Optionen, die hier sonderbehandelt werden
168  
169 4 my $indent = 0;
170 4 my $nl = 1;
171  
172 4 Quiq::Option->extract(-mode=>'sloppy',\@_,
173 -nl => \$nl,
174 -indent => \$indent,
175 );
176  
177 # Body ermitteln. Wir fügen alle Argumente ohne Option
178 # zu dem Body zusammen.
179  
180 4 my $body;
181 4 ▶ for (my $i = 0; $i < @_; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @_) 6 4 10
182 6 ▶ if (defined($_[$i]) && $_[$i] =~ /^-(o|p|pnl)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[$i] and $_[$i] =~ /^-(o|p|pnl)$/) 2 4 6
Condition66%2/3
defined $_[$i] and $_[$i] =~ /^-(o|p|pnl)$/
!ll&&!rl&&r
042
MC/DC50%1/2
defined $_[$i] and $_[$i] =~ /^-(o|p|pnl)$/
defined $_[$i] $_[$i] =~ /^-(o|p|pnl)$/
Truth table66%2/3
A and B
A= defined $_[$i]B= $_[$i] =~ /^-(o|p|pnl)$/
ABresult
0X0
100
111
183 # Optionen, die wir weiterleiten, übergehen
184 2 $i++;
185 2 next;
186 }
187 4 $body .= splice @_,$i--,1;
188 }
189  
190 # Umgebung erzeugen
191  
192 4 ▶ if (!defined $body) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $body) 0 4 4
193 0 $body = '';
194 }
195 4 ▶ if ($body ne '' && substr($body,-1) ne "\n") {
Branch: 1 branch
BranchTrueFalseTotal
if ($body ne "" and substr($body, -1) ne "\n") 1 3 4
Condition100%3/3
$body ne "" and substr($body, -1) ne "\n"
!ll&&!rl&&r
121
MC/DC100%2/2
$body ne "" and substr($body, -1) ne "\n"
$body ne "" substr($body, -1) ne "\n"
Truth table100%3/3
A and B
A= $body ne ""B= substr($body, -1) ne "\n"
ABresult
0X0
100
111
196 1 $body .= "\n";
197 }
198 4 ▶ if ($indent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($indent) 0 4 4
199 0 $indent = ' ' x $indent;
200 0 $body =~ s/^/$indent/gm;
201 }
202
203 4 my $code = $self->macro('\begin',-p=>$name,@_);
204 4 $code .= $body;
205 4 $code .= $self->macro('\end',-p=>$name,-nl=>$nl);
206  
207 4 return $code;
208 }
209  
210 # -----------------------------------------------------------------------------
211  
212 =head3 section() - Erzeuge LaTeX Section
213  
214 =head4 Synopsis
215  
216 $code = $l->section($sectionName,$title);
217  
218 =head4 Arguments
219  
220 =over 4
221  
222 =item $sectionName
223  
224 Name des LaTeX-Abschnitts. Mögliche Werte: 'part', 'chapter', 'section',
225 'subsection', 'susubsection', 'paragraph', 'subparagraph'.
226  
227 =back
228  
229 =head4 Options
230  
231 =over 4
232  
233 =item -label => $label
234  
235 Kennzeichne Abschnitt mit Label $label.
236  
237 =item -notToc => $bool (Default: 0)
238  
239 Nimm die Überschrift nicht ins Inhaltsverzeichnis auf.
240  
241 =back
242  
243 =head4 Description
244  
245 Erzeuge ein LaTeX Section und liefere den resultierenden Code
246 zurück.
247  
248 =head4 Examples
249  
250 B<Ein Abschnitt der Ebene 1>
251  
252 $l->section('subsection','Ein Abschnitt');
253  
254 produziert
255  
256 \subsection{Ein Abschnitt}
257  
258 =cut
259  
260 # -----------------------------------------------------------------------------
261  
262 1 ▶sub section {
Subroutine section: called CC=3 SCAR=11.4
Pod documented
263 1 my $self = shift;
264 1 my $sectionName = shift;
265 1 my $title = shift;
266  
267 # Optionen
268  
269 1 my $notToc = 0;
270 1 my $label = undef;
271  
272 1 Quiq::Option->extract(\@_,
273 -label => \$label,
274 -notToc => \$notToc,
275 );
276  
277 1 ▶ if ($notToc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($notToc) 0 1 1
278 0 $sectionName .= '*';
279 }
280  
281 1 my $code = $self->c('\%s{%s}',$sectionName,$title);
282 1 ▶ if ($label) {
Branch: 1 branch
BranchTrueFalseTotal
if ($label) 0 1 1
283 0 $code .= $self->c('\label{%s}',$label);
284 }
285 1 $code .= "\n";
286  
287 1 return $code;
288 }
289  
290 # -----------------------------------------------------------------------------
291  
292 =head1 VERSION
293  
294 1.240
295  
296 =head1 AUTHOR
297  
298 Frank Seitz, L<http://fseitz.de/>
299  
300 =head1 COPYRIGHT
301  
302 Copyright (C) 2026 Frank Seitz
303  
304 =head1 LICENSE
305  
306 This code is free software; you can redistribute it and/or modify
307 it under the same terms as Perl itself.
308  
309 =cut
310  
311 # -----------------------------------------------------------------------------
312  
313 4 1;
314  
315 # eof
« Quiq/Gimp.pm ↑ Summary Quiq/Xml/LibXml.pm »