blib/lib/Text/BibTeX/BibStyle.pm

filter: Statementstmt 76.1% 601 / 789 Branchbran 59.5% 225 / 378 Conditioncond 44.9% 58 / 129 MC/DCmcdc 26.0% 19 / 73 Subroutinesub 76.1% 64 / 84 Podpod 100% 8 / 8 totaltotal 66.7% 975 / 1461 CC 253 SCAR 33.1
CC
253
Cov
68%
_function_width
132.0
_function_text_prefix
110.0
_function_format_name
91.4
CRAP 27.3
↑ Summary
1 package Text::BibTeX::BibStyle;
2  
3 =head1 NAME
4  
5 Text::BibTeX::BibStyle - Format Text::BibTeX::Entry items using .bst
6  
7 =cut
8  
9 4 our $VERSION = '0.03';
10  
11 =head1 DESCRIPTION
12  
13 C<Text::BibTeX::BibStyle> is a module that can format
14 C<Text::BibTeX::Entry> objects by interpreting a bibstyle (C<.bst>) file
15 such as C<ieeetr.bst>. In this way, Perl can use the same
16 bibliographic style files that bibtex does.
17  
18 For a large collection of C<.bst> files, see
19 http://www.math.utah.edu/pub/tex/bibtex/index.html.
20  
21 =head1 SYNOPSIS
22  
23 $bibstyle = Text::BibTeX::BibStyle->new(%options);
24  
25 $ENV{BSTINPUTS} = "my/bstfiles/";
26 $bibstyle->read_bibstyle("bibstyle");
27 OR
28 $bibstyle->replace_bibstyle($bibstyle_def);
29  
30 $ENV{BIBINPUTS} = "my/bibfiles/";
31 $bibstyle->execute([qw(bibfile1 bibfile2)]);
32 OR
33 $bibstyle->execute([qw(bibfile1 bibfile2)], \@ref_list);
34  
35 @warnings = $bibstyle->warnings;
36  
37 $output = $bibstyle->get_output();
38 $output = $bibstyle->convert_format(Text::BibTex::BibStyle::html);
39 OR
40 $output = $bibstyle->get_output(\%options);
41  
42 =cut
43  
44 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 4 ▶use vars qw(@ISA @EXPORT_OK $LATEX $HTML $RST);
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 4 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 4 @ISA = qw( Exporter );
51 4 @EXPORT_OK = qw($HTML $LATEX $RST);
52  
53 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 4 ▶use Text::BibTeX qw(:metatypes :subs :joinmethods :macrosubs);
Subroutine BEGIN: called CC=1 SCAR=0.0
55 4 ▶use Text::BibTeX::Name;
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 =head1 METHODS
58  
59 =over
60  
61 =item C<new [(%options])>
62  
63 Class method. Creates a new C<Text::BibTeX::BibStyle> object with the
64 options specified in the optional C<option=>'value'> arguments.The
65 following options are understood:
66  
67 =over
68  
69 =item C<debug>
70  
71 Turns on debugging messages during execute.
72  
73 =item C<nowarn>
74  
75 Turns off warnings from certain sanity checks, such as the existence
76 of a unique C<ENTRIES> and C<READ> statement within the bibstyle.
77  
78 =back
79  
80 =cut
81  
82 # Exportable output options
83 4 $LATEX = { wrap => 1 };
84 { # Closure
85 4 my $ASCIIMathML_parser;
86 4 my %Styles = (em => 'em',
87 bf => 'b',
88 it => 'i',
89 sl => 'i',
90 tt => 'tt');
91  
92 $HTML = {
93 delete_braces => 1,
94 substitute_newcommand => 1,
95 402 ▶ character => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
96 402 my ($bst, $latex, $unicode, $chars, $accent) = @_;
97 4 ▶ use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
98 402 ▶ return $unicode ? encode_entities($unicode) : $chars;
Branch: 1 branch
BranchTrueFalseTotal
$unicode ? : 375 27 402
99 },
100 588 ▶ command => sub {
Subroutine __ANON__: called CC=10 SCAR=23.0
101 588 my ($bst, $cmd, @args) = @_;
102 588 ▶ if ($cmd =~ /^(begin|end)$/) {
Branch: 4 branches
BranchTrueFalseTotal
if ($cmd =~ /^(begin|end)$/) { } 10 578 588
elsif ($cmd eq 'bibitem') { } 180 398 578
elsif ($cmd eq 'mbox') { } 20 378 398
elsif ($cmd eq 'cite') { } 30 348 378
103 10 ▶ if ($args[0] eq 'thebibliography') {
Branch: 1 branch
BranchTrueFalseTotal
if ($args[0] eq "thebibliography") 10 0 10
104 10 ▶ my $slash = $cmd eq 'end' ? '/' : '';
Branch: 1 branch
BranchTrueFalseTotal
$cmd eq 'end' ? : 5 5 10
105 10 my $pre;
106 10 ▶ if ($cmd eq 'begin') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq 'begin') { } 5 5 10
107 5 $pre = qq(<h2>References</h2>\n\n);
108 }
109 else {
110 5 $pre = "</td>";
111 }
112 10 return "$pre<${slash}table>";
113 }
114 }
115 elsif ($cmd eq 'bibitem') {
116 180 my $key = pop @args;
117 180 $bst->{html}{Bib_count}++;
118 180 ▶ my $label = $args[0] || $bst->{html}{Bib_count};
Condition66%2/3
$args[0] || $bst->{'html'}{'Bib_count'}
l!l&&r!l&&!r
361440
MC/DC0%0/2
$args[0] || $bst->{'html'}{'Bib_count'}
$args[0] $bst->{'html'}{'Bib_count'}
Truth table66%2/3
A || B
A= $args[0]B= $bst->{'html'}{'Bib_count'}
ABresult
1X1
011
000
119 180 ($bst->{html}{Cites}{$key} = $label) =~ s/[{}]//g; # Remove braces
120 180 ▶ my $pre = $bst->{html}{Bib_count} > 1 ? "</td>\n" : '';
Branch: 1 branch
BranchTrueFalseTotal
$bst->{'html'}{'Bib_count'} > 1 ? : 175 5 180
121 180 return qq($pre<tr valign="top"><td><a name="$key">[$label]</a></td><td>);
122 }
123 elsif ($cmd eq 'mbox') {
124 20 return $args[0];
125 }
126 elsif ($cmd eq 'cite') {
127 30 return qq(<a href="#$args[0]"><cite>$args[0]</cite></a>);
128 }
129 },
130 5 ▶ init => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
131 5 my ($bst) = @_;
132 # Initialize instance variables
133 5 $bst->{html} = { Bib_count => 0, Cites => {} };
134 },
135 10 ▶ math => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
136 10 my ($bst, $latex, $math) = @_;
137 4 ▶ use Text::ASCIIMathML;
Subroutine BEGIN: called CC=1 SCAR=0.0
138 10 ▶ $ASCIIMathML_parser = Text::ASCIIMathML->new()
Branch: 1 branch
BranchTrueFalseTotal
unless $ASCIIMathML_parser 1 9 10
139 unless $ASCIIMathML_parser;
140 10 return $ASCIIMathML_parser->TextToMathML($math);
141 },
142 5 ▶ postprocess => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
143 5 my ($bst, $text) = @_;
144  
145 # Substitute back any cite tags
146 5 $text =~ s!<cite>(.*?)</cite>![$bst->{html}{Cites}{$1}]!g;
147 5 return $text;
148 },
149 136 ▶ style => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
150 136 my ($bst, $latex, $style, $text) = @_;
151 136 my $html_style = $Styles{$style};
152 136 ▶ return defined $html_style ? "<$html_style>$text</$html_style>" :
Branch: 1 branch
BranchTrueFalseTotal
defined $html_style ? : 136 0 136
153 $text;
154 },
155 4 };
156 }
157 { # Closure
158 4 my %Styles = (em => '*',
159 bf => '**',
160 it => '*',
161 sl => '*',
162 tt => '``');
163  
164 $RST = {
165 delete_braces => 1,
166 substitute_newcommand => 1,
167 # prologue => "",
168 402 ▶ character => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
169 402 my ($bst, $latex, $unicode, $chars, $accent) = @_;
170 402 ▶ return $chars if ! $unicode;
Branch: 1 branch
BranchTrueFalseTotal
unless $unicode 27 375 402
171 375 my $code = ord $unicode;
172 375 $bst->{rst}{unicode}{$code} = 1;
173 375 return sprintf '\\ |unicode(%x)|\\ ', $code;
174 },
175 588 ▶ command => sub {
Subroutine __ANON__: called CC=9 SCAR=22.0
176 588 my ($bst, $cmd, @args) = @_;
177 588 ▶ if ($cmd =~ /^(begin|end)$/) {
Branch: 4 branches
BranchTrueFalseTotal
if ($cmd =~ /^(begin|end)$/) { } 10 578 588
elsif ($cmd eq 'bibitem') { } 180 398 578
elsif ($cmd eq 'mbox') { } 20 378 398
elsif ($cmd eq 'cite') { } 30 348 378
178 10 ▶ if ($args[0] eq 'thebibliography') {
Branch: 1 branch
BranchTrueFalseTotal
if ($args[0] eq "thebibliography") 10 0 10
179 10 ▶ if ($cmd eq 'begin') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "begin") 5 5 10
180 5 return qq(**References**\n\n\n);
181 }
182 }
183 }
184 elsif ($cmd eq 'bibitem') {
185 180 my $key = pop @args;
186 180 $bst->{rst}{Bib_count}++;
187 180 ▶ my $label = $args[0] || $bst->{rst}{Bib_count};
Condition66%2/3
$args[0] || $bst->{'rst'}{'Bib_count'}
l!l&&r!l&&!r
361440
MC/DC0%0/2
$args[0] || $bst->{'rst'}{'Bib_count'}
$args[0] $bst->{'rst'}{'Bib_count'}
Truth table66%2/3
A || B
A= $args[0]B= $bst->{'rst'}{'Bib_count'}
ABresult
1X1
011
000
188 180 ▶ if ($args[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args[0]) 36 144 180
189 36 my $bst2 = Text::BibTeX::BibStyle->new;
190 36 $label = $bst2->convert_format($label, $RST);
191 }
192 180 $bst->{rst}{Cites}{$key} = $label;
193 180 return ".. [$key] ";
194 }
195 elsif ($cmd eq 'mbox') {
196 20 return $args[0];
197 }
198 elsif ($cmd eq 'cite') {
199 30 return "\\ [$args[0]]_";
200 }
201 },
202 41 ▶ init => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
203 41 my ($bst) = @_;
204 # Initialize instance variables
205 41 $bst->{rst} = { Bib_count => 0, Cites => {}, unicode => {} };
206 },
207 10 ▶ math => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
208 10 my ($bst, $latex, $math) = @_;
209 # Latex sometimes starts with ^ or _ for super/subscript
210 10 $math =~ s/^([_^])/{::}$1/;
211 10 $math =~ s/([{}])/\\$1/g;
212 10 $math =~ s/\\/\\\\/g;
213 10 return "\\ :mathml:`$math`\\ ";
214 },
215 41 ▶ postprocess => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
216 41 my ($bst, $text) = @_;
217  
218 # Fix the indentations
219 41 $text =~ s/^[ ]*$//mg;
220 41 $text =~ s/^(?!\A|\.\.|\*\*)[ ]*(.+)/ $1/mg;
221 41 $text =~ s/\\([\\{}])/$1/g;
222 41 foreach my $code (sort keys %{$bst->{rst}{unicode}}) {
223 32 $text .= sprintf ".. |unicode(%x)| unicode:: U+%x\n", $code, $code;
224 }
225 41 return $text;
226 },
227 136 ▶ style => sub {
Subroutine __ANON__: called CC=3 SCAR=11.6
228 136 my ($bst, $latex, $style, $text) = @_;
229 136 my $rst_style = $Styles{$style};
230 136 ▶ if ($rst_style) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rst_style) 136 0 136
231 136 my ($pre, undef, $post) = $text =~ s/^(\s.)(.*?)(\s.)$/$2/;
232 136 ▶ $post ||= '';
Condition50%1/2
$post ||= ""
l!l
0136
MC/DC0%0/1
$post ||= ""
$post
Truth table50%1/2
A
A= $post
Aresult
11
00
233 136 return "$pre$rst_style$text$rst_style$post" ;
234 }
235 0 return $text;
236 },
237 4 };
238 }
239 40 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
240 40 my ($class, %options) = @_;
241  
242 40 my $self = bless {}, $class;
243  
244 40 $self->{options} = \%options;
245  
246 40 return $self;
247 }
248  
249 # A Text::BibTeX::BibStyle hash has the following keys:
250 # bibtex A reference to a "bibtex" hash
251 # interp Array reference containing the interpreter
252 # stack Reference to array of evaluation stack
253 # symbols Reference to "symbols" hash
254 # warnings Array of warnings produced during execution
255 #
256 # "Symbols" hash has the following keys, each of which is a hash reference
257 # whose key is the symbol name and whose value is its definition:
258 # const Built-in constants
259 # field Reference to hash of field/value pairs for current entry
260 # entry_int Reference to hash of integer/value pairs for current entry
261 # entry_str Reference to hash of string/value pairs for current entry
262 # function Function defined in FUNCTION command or built-in function
263 # integer Integer defined in INTEGERS command
264 # string String defined in STRINGS command
265 #
266 # "Bibtex" hash has the following key/value pairs
267 # bibfiles Reference to array of bib file names
268 # bt_entry Reference to the current Text::BibTeX::Entry object
269 # bt_entries Reference to hash whose keys are bibtex keys and whose value
270 # is the corresponding Text::BibTeX::Entry for that key
271 # cite Cite key for the current entry
272 # cites Optional reference to array of keys of citations to format
273 # entries Reference to hash whose keys are bibtex keys and whose value
274 # is a reference to its entry hash
275 # format Reference to a format hash defined by an ENTRY command
276 # preamble Reference to array of @PREAMBLE items
277 #
278 # "Entry" hash has the following keys, each of which is a hash reference
279 # whose key is the symbol name and whose value is its definition:
280 # field Bibliography field from ENTRY command
281 # integer Entry integer from ENTRY command
282 # string Entry string from ENTRY command
283 #
284 # "Format" hash has the following keys, each of which is a reference to
285 # an array of names that can appear in a corresponding entry hash
286 # field Bibliography fields from ENTRY command
287 # integer Entry integer variables from ENTRY command
288 # string Entry string variables from ENTRY command
289  
290 =item C<convert_format ($text, \%options)>
291  
292 Method. Converts a LaTeX bibliography in $text into some other format
293 using the options specified by C<%options> and returns the result.
294 This method can also be used to convert a standard BibTeX output to
295 a different format.
296  
297 Assuming that C<$text> contains a LaTeX bibliography (e.g., the
298 contents of a C<.bbl> file), the following option packages may be
299 useful for the options hash reference:
300  
301 =over
302  
303 =item C<$Text::BibTeX::BibStyle::HTML>
304  
305 Produces HTML code to render the formatted bibliography. Exportable.
306  
307 =item C<$Text::BibTeX::BibStyle::LATEX>
308  
309 Outputs LaTeX code identical to bibtex (specifies (wrap => 1)). Exportable.
310  
311 =item C<$Text::BibTeX::BibStyle::RST>
312  
313 Produces reStructuredText code. Exportable.
314  
315 =back
316  
317 The following options are supported, if you want to write your own
318 translation package:
319  
320 =over
321  
322 =item C<character>
323  
324 Reference to a subroutine to call for special characters. The
325 subroutine is called with the arguments C<($bst, $latex, [$unicode],
326 $char, [$accent])>, where C<$bst> is the Text::BibTeX::BibStyle
327 object, C<$latex> is the original latex for the special character,
328 C<$unicode> is the equivalent unicode character (if it exists), $char
329 is the special character(s), and C<$accent> is the latex accent code
330 to be applied (if specified). It should return the string to be
331 substituted.
332  
333 =item C<command>
334  
335 Reference to a subroutine to call for LaTeX commands. The subroutine
336 is called with the arguments C<($bst, $cmd, @args)>, where C<$bst> is
337 the Text::BibTeX::BibStyle object, C<$cmd> is the name of the LaTeX
338 command and C<@args> is the array of arguments (including optional
339 arguments) to the command. At a minimum, the subroutine should handle
340 the following commands: C<\begin{thebibliography}>,
341 C<\bibitem[label]{key}>, C<\cite{ref}>, C<\end{thebibliography}>,
342 C<\mbox{text}>, C<\newblock>. It should return the string to be
343 substituted.
344  
345 =item C<delete_braces>
346  
347 Boolean to delete from the output any braces that are not
348 backslash-quoted.
349  
350 =item C<init>
351  
352 Reference to a subroutine to call before processing the output. The
353 subroutine is called with the argument C<($bst)>, which is the
354 Text::BibTeX::BibStyle object.
355  
356 =item C<math>
357  
358 Reference to a subroutine to call for latex math. The subroutine is
359 called with the arguments C<($bst, $latex, $math)>, where C<$bst> is
360 the Text::BibTeX::BibStyle object, C<$latex> is the original latex and
361 C<$math> is the part that actually translates to math. It should
362 return the string to be substituted.
363  
364 =item C<postprocess>
365  
366 Reference to a subroutine to call to post-process the output. The
367 subroutine is called with the arguments C<($bst, $text)>, where
368 C<$bst> is the Text::BibTeX::BibStyle object and C<$text> contains the
369 text of the entire formatted bibliography. It should return the final
370 formatted bibliography.
371  
372 =item C<prologue>
373  
374 A string or reference to a subroutine to call to produce any
375 pre-bibliography definitions needed by the format.
376  
377 =item C<style>
378  
379 Reference to a subroutine to call for different font styles. The
380 subroutine is called with the arguments C<($bst, $latex, $style,
381 $text)>, where C<$bst> is the Text::BibTeX::BibStyle object, C<$latex>
382 is the original latex, C<$style> is one of C<rm>, C<em>, C<bf>, C<it>,
383 C<sl>, C<sf>, C<sc>, or C<tt> indicating the font style, and C<$text>
384 is the text to be output in that style. It should return the string
385 to be substituted.
386  
387 =item C<substitute_newcommand>
388  
389 Boolean to process and do substitutions for any C<\newcommand>
390 definitions in the output.
391  
392 =item C<wrap>
393  
394 Boolean to force the standard bibtex wrapping on the output.
395  
396 =back
397  
398 =cut
399  
400 { # Closure
401 4 my $Acc_char = qq([\'\`^\"~=.]);
402 4 my $Acc_let = "[uvHtcdb]";
403 4 my $Acc_sym = q(\\\\(?:(?:a[ae]|A[AE]|copyright|dd?ag|[lL]|oe?|OE?|P|pounds|S|ss)\\b)|``|''|\?`|!`|~|---?);
404 4 my $Style = '\\\\(?:[er]m|bf|[it]t|s[lfc])\\b';
405  
406 46 ▶sub convert_format : method {
Subroutine convert_format: called CC=21 SCAR=33.9
Pod documented
407 46 my ($self, $text, $opts) = @_;
408  
409 46 ▶ $opts->{init}->($self) if $opts->{init};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"init"} 46 0 46
410 46 ▶ $text = _substitute_newcommand($text) if $opts->{substitute_newcommand};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"substitute_newcommand"} 46 0 46
411 46 ▶ if ($opts->{wrap}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"wrap"}) 0 46 46
412 0 ▶ 1 while ($text =~ s/^(?=.{80})(.{1,79})(\s.*)/$1\n $2/m ||
Branch: 1 branch
BranchTrueFalseTotal
$text =~ s/^(?=.{80})(.{1,79})(\s.*)/$1\n $2/m || $text =~ s/^(?=.{80})(.{80,}?)(\s.*)/$1\n $2/m and do { '???' } 0 0 0
Condition0%0/3
$text =~ s/^(?=.{80})(.{1,79})(\s.*)/$1\n $2/m or $text =~ s/^(?=.{80})(.{80,}?)(\s.*)/$1\n $2/m
l!l&&r!l&&!r
000
MC/DC0%0/2
$text =~ s/^(?=.{80})(.{1,79})(\s.*)/$1\n $2/m or $text =~ s/^(?=.{80})(.{80,}?)(\s.*)/$1\n $2/m
$text =~ s/^(?=.{80})(.{1,79})(\s.*)/$1\n $2/m $text =~ s/^(?=.{80})(.{80,}?)(\s.*)/$1\n $2/m
Truth table0%0/3
A or B
A= $text =~ s/^(?=.{80})(.{1,79})(\s.*)/$1\n $2/mB= $text =~ s/^(?=.{80})(.{80,}?)(\s.*)/$1\n $2/m
ABresult
1X1
011
000
413 $text =~ s/^(?=.{80})(.{80,}?)(\s.*)/$1\n $2/m);
414 }
415 46 ▶ if ($opts->{prologue}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"prologue"}) 0 46 46
416 my $prologue = ref $opts->{prologue} ? $opts->{prologue}->($self) :
417 0 ▶ $opts->{prologue};
Branch: 1 branch
BranchTrueFalseTotal
ref $opts->{'prologue'} ? : 0 0 0
418 0 $text = "$prologue$text";
419 }
420 46 ▶ if ($opts->{character}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"character"}) 46 0 46
421 # Handle accents
422 46 $text =~ s/\\i\b/i/g;
423 46 $text =~ s/(\{ \\ ($Acc_char|$Acc_let\b) [ ]* ([a-zA-Z]+) [ ]*
424 24 (?:\}|\Z))/_character($self, $opts, $1, $3, $2)/exog;
425 46 $text =~ s/(\\ ($Acc_char|$Acc_let) [ ]* \{ [ ]* ([a-zA-Z]+) [ ]*
426 80 (?:\}|\Z))/_character($self, $opts, $1, $3, $2)/exog;
427 46 $text =~ s/($Acc_sym)/_character($self, $opts, $1, $1)/exog;
428 0 $text =~ s/(\\ ([\#\$\%&_]))/_character($self, $opts, $1, $2)/xge;
429 46 $text =~ s!(\\/)!_character($self, $opts, $1, '')!ge;
430 }
431 46 ▶ if ($opts->{math}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"math"}) 46 0 46
432 # Handle math mode
433 46 $text =~ s/((\$\$?)(.*?)\2)/$opts->{math}->($self, $1, $3)/sexg;
434 0 $text =~ s/(\\ \( (.*?) \\ \))/$opts->{math}->($self, $1, $2)/sexg;
435 0 $text =~ s/(\\ \[ (.*?) \\ \])/$opts->{math}->($self, $1, $2)/sexg;
436 }
437 46 ▶ if ($opts->{style}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"style"}) 46 0 46
438 # Handle text styles
439 46 ▶ 1 while $text =~ s/(\{ $Style .*)/do {
Branch: 1 branch
BranchTrueFalseTotal
$text =~ s[(\{ $Style .*)][do { () = _remove_matched_brace($1); (my $text = $latex) =~ s/\{ ($Style) [ ]*(.*) \}$/$2/sx; my $style = $1; $style =~ /([a-z]+)/; $opts->{"style"}->($self, $latex, $1, $text) . $next };]egsx and do { '???' } 272 46 318
440 272 my ($latex, $next) = _remove_matched_brace($1);
441 272 (my $text = $latex) =~ s!\{ ($Style) [ ]*(.*) \}$!$2!sx;
442 272 my $style = $1;
443 272 $style =~ m!([a-z]+)!;
444 272 $opts->{style}->($self, $latex, $1, $text) . $next;
445 }/sexg;
446 }
447 46 ▶ if ($opts->{command}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"command"}) 46 0 46
448 46 ▶ 1 while $text =~ s/(\A|[^\\])\\([a-z]+)(.*)/do {
Branch: 1 branch
BranchTrueFalseTotal
$text =~ s<(\A|[^\\])\\([a-z]+)(.*)>(do { () = ($1, $2, $3); my @args; while ($next =~ /^[\{\[]/) { if ($next =~ /^\{/) { my $arg; ($arg, $next) = _remove_matched_brace($next); $arg =~ s/^\{ (.*) \}$/$1/sx; push @args, $arg; } else { $next =~ s/^\[ (.*?) \]//sx; push @args, $1; } ; } ; $pre . $opts->{"command"}->($self, $cmd, @args) . $next };)eisx and do { '???' } 1176 46 1222
449 1176 my ($pre, $cmd, $next) = ($1, $2, $3);
450 1176 my @args;
451 1176 ▶ while ($next =~ m!^[\{\[]!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next =~ /^[\{\[]/) 562 1176 1738
452 562 ▶ if ($next =~ m!^\{!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next =~ /^\{/) { } 490 72 562
453 490 my $arg;
454 490 ($arg, $next) = _remove_matched_brace($next);
455 490 $arg =~ s!^\{ (.*) \}$!$1!sx;
456 490 push @args, $arg;
457 }
458 else {
459 72 $next =~ s!^\[ (.*?) \]!!sx;
460 72 push @args, $1;
461 }
462 }
463 1176 $pre . $opts->{command}->($self, $cmd, @args) . $next;
464 }/exis;
465 }
466 46 ▶ if ($opts->{delete_braces}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"delete_braces"}) 46 0 46
467 # Note: need to do twice to handle {}
468 46 ▶ $text =~ s/(\A|.) ([{}])/$1 eq "\\" ? "$1$2" : $1/sexg;
Branch: 1 branch
BranchTrueFalseTotal
$1 eq '\\' ? : 0 442 442
469 46 ▶ $text =~ s/(\A|.) ([{}])/$1 eq "\\" ? "$1$2" : $1/sexg;
Branch: 1 branch
BranchTrueFalseTotal
$1 eq '\\' ? : 0 70 70
470 }
471 46 ▶ if ($opts->{character}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"character"}) 46 0 46
472 # Handle backslash-quoted braces
473 0 $text =~ s/(\\ ([{}]))/_character($self, $opts, $1, $2)/xge;
474 }
475 46 ▶ $text = $opts->{postprocess}->($self, $text) if $opts->{postprocess};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"postprocess"} 46 0 46
476  
477 46 return $text;
478 }
479  
480 =item C<execute [(\@bibfiles[, \@cites])]>
481  
482 Method. Executes the current bibstyle interpreter on a set of cited
483 references passed in C<@cites> looking in a set of C<.bib> files
484 passed in C<@bibfiles>. If the C<@cites> argument is undefined, uses
485 all the references in all the bibfiles. The files in C<@bibfiles>
486 should be without the ".bib" extension. The search path for bibfiles
487 is taken from the C<BIBINPUTS> environment variables if it is defined.
488 The C<@bibfiles> argument is not needed if the bibstyle interpreter
489 does not contain a C<READ> command. Croaks if a bibstyle interpreter
490 has not been defined using either the C<read_bibstyle> or
491 C<replace_bibstyle> method.
492  
493 =cut
494  
495 {
496 # Closure for local variables
497 4 my %Commands = ( # Info is no. of arguments and subroutine reference
498 entry => [3, \&_command_entry],
499 execute => [1, \&_command_execute],
500 function => [2, \&_command_function],
501 integers => [1, \&_command_variables],
502 iterate => [1, \&_command_iterate],
503 macro => [2, \&_command_macro],
504 read => [0, \&_command_read],
505 reverse => [1, \&_command_iterate],
506 sort => [0, \&_command_sort],
507 strings => [1, \&_command_variables],
508 );
509  
510 20 ▶sub execute : method {
Subroutine execute: called CC=14 SCAR=27.4
Pod documented
511 20 my ($self, $bibfiles_ar, $cites_ar) = @_;
512  
513 croak "No bibstyle interpreter has been defined: call read_bibstyle or replace_bibstyle first"
514 20 ▶ unless $self->{interp};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"interp"} 0 20 20
515  
516 $self->{bibtex} = {
517 20 bibfiles => $bibfiles_ar,
518 bt_entry => undef,
519 bt_entries => { },
520 cite => undef,
521 cites => $cites_ar,
522 entries => { },
523 format => undef,
524 preamble => [ ],
525 };
526
527 20 Text::BibTeX::delete_all_macros();
528 20 my %cmd_count;
529 20 my ($filename, $lineno);
530 20 my @interp = @{$self->{interp}};
531 20 ▶ while (@interp) {
Branch: 1 branch
BranchTrueFalseTotal
if (@interp) 351 18 369
532 351 my $token = shift @interp;
533 351 ▶ if ($token =~ /^\#line (\d+) (\S+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token =~ /^\#line (\d+) (\S+)/) 211 140 351
534 211 ($lineno, $filename) = ($1, $2);
535 211 next;
536 }
537 140 ▶ croak "$filename, $lineno: Invalid argument" if ref($token);
Branch: 1 branch
BranchTrueFalseTotal
if ref $token 0 140 140
538 140 my $lc_token = lc $token;
539 croak "$filename, $lineno: Unknown command '$token'"
540 140 ▶ unless my $command_ar = $Commands{$lc_token};
Branch: 1 branch
BranchTrueFalseTotal
unless my $command_ar = $Commands{$lc_token} 0 140 140
541  
542 140 my ($cmd, $cmd_f, $cmd_l) = ($token, $filename, $lineno);
543 140 $self->{lineno} = "$filename, $lineno";
544 # Get the arguments
545 140 my @args;
546 140 ▶ while (@args < $command_ar->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args < $command_ar->[0]) 328 140 468
547 328 my $token = shift @interp;
548 328 ▶ if ($token =~ /^\#line (\d+) (\S+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token =~ /^\#line (\d+) (\S+)/) 77 251 328
549 77 ($lineno, $filename) = ($1, $2);
550 77 next;
551 }
552 251 ▶ last unless ref($token);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $token 0 251 251
553 251 push @args, $token;
554 }
555 140 ▶ croak "$filename, $lineno: Insufficient arguments for command '$cmd' at line $cmd_l"
Branch: 1 branch
BranchTrueFalseTotal
if @args < $command_ar->[0] 0 140 140
556 if @args < $command_ar->[0];
557 140 ▶ if (++$cmd_count{$lc_token} > 1 && $lc_token =~ /^(entry|read)$/)
Branch: 1 branch
BranchTrueFalseTotal
if (++$cmd_count{$lc_token} > 1 and $lc_token =~ /^(entry|read)$/) 0 140 140
Condition66%2/3
++$cmd_count{$lc_token} > 1 and $lc_token =~ /^(entry|read)$/
!ll&&!rl&&r
51890
MC/DC0%0/2
++$cmd_count{$lc_token} > 1 and $lc_token =~ /^(entry|read)$/
++$cmd_count{$lc_token} > 1 $lc_token =~ /^(entry|read)$/
Truth table66%2/3
A and B
A= ++$cmd_count{$lc_token} > 1B= $lc_token =~ /^(entry|read)$/
ABresult
0X0
100
111
558 {
559 0 $self->_warning("Duplicate '$token' command ignored");
560 0 next;
561 }
562 140 $command_ar->[1]->($self, $cmd, $cmd_f, $cmd_l, \@args);
563 }
564 18 my @missing_cmds = grep !$cmd_count{$_}, qw(entry read);
565 $self->_warning(sprintf("Need to have one %s command",
566 join ' and one ', map(uc $_, @missing_cmds)))
567 18 ▶ if @missing_cmds && ! $self->{options}{nowarn};
Branch: 1 branch
BranchTrueFalseTotal
if @missing_cmds and not $self->{"options"}{"nowarn"} 0 18 18
Condition33%1/3
@missing_cmds and not $self->{"options"}{"nowarn"}
!ll&&!rl&&r
0180
MC/DC0%0/2
@missing_cmds and not $self->{"options"}{"nowarn"}
@missing_cmds not $self->{"options"}{"nowarn"}
Truth table33%1/3
A and not(B)
A= @missing_cmdsB= not $self->{"options"}{"nowarn"}
ABresult
0X0
100
111
568 }
569 }
570  
571 =item C<get_output [(\%options)]>
572  
573 Method. Returns the output produced by C<write$> commands in the C<.bst>
574 file. The options are listed under the C<convert_format> method,
575 which it calls.
576  
577 =cut
578  
579 4 ▶sub get_output : method {
Subroutine get_output: called CC=2 SCAR=7.0
Pod documented
580 18 my ($self, $opts) = @_;
581  
582 18 my $out = join '', @{$self->{output}};
583 18 ▶ $out = $self->convert_format($out, $opts) if $opts;
Branch: 1 branch
BranchTrueFalseTotal
if $opts 0 18 18
584  
585 18 return $out;
586 }
587  
588 =item C<num_warnings>
589  
590 Method. Returns the number of warning messages generated during execution.
591  
592 =cut
593  
594 46 ▶sub num_warnings : method {
Subroutine num_warnings: called CC=1 SCAR=0.0
Pod documented
595 46 my ($self) = @_;
596  
597 46 return 0+@{$self->{warnings}};
598 }
599  
600  
601  
602 =item C<read_bibstyle ($bibstyle)>
603  
604 Method. Replaces the bibstyle interpreter with a new one obtained by
605 reading the file C<$bibstyle.bst>. The search path for the bibstyle
606 file is taken from the C<BSTINPUTS> environment variable if it is
607 defined.
608  
609 =cut
610  
611 1 ▶sub read_bibstyle : method {
Subroutine read_bibstyle: called CC=4 SCAR=14.0
Pod documented
612 1 my ($self, $bibstyle) = @_;
613  
614 1 my $f = "$bibstyle.bst";
615 1 ▶ my $path = $ENV{BSTINPUTS} || '.';
Condition50%1/2
$ENV{'BSTINPUTS'} || '.'
l!l
10
MC/DC0%0/1
$ENV{'BSTINPUTS'} || '.'
$ENV{'BSTINPUTS'}
Truth table50%1/2
A
A= $ENV{'BSTINPUTS'}
Aresult
11
00
616 1 my @path = split /:/, $path;
617 1 my ($dir) = grep -f "$_/$f", @path;
618 1 ▶ croak("Cannot find $f on path: $path") unless $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless $dir 0 1 1
619 1 my $fullfile = "$dir/$f";
620  
621 # Read the file
622 1 ▶ open BSTINPUTS, "$fullfile" or croak("$fullfile: $!");
Branch: 1 branch
BranchTrueFalseTotal
open BSTINPUTS, "$fullfile" or croak("${fullfile}: $!") 1 0 1
623 1 my @bibstyle = <BSTINPUTS>;
624 1 close BSTINPUTS;
625  
626 1 my $interp = join '', @bibstyle;
627 1 $self->replace_bibstyle($interp, $fullfile);
628 }
629  
630 =item C<replace_bibstyle ($string[, $filename])>
631  
632 Method. Replaces the bibstyle interpreter by parsing C<$string>. The
633 optional C<$filename> argument is used for warning messages. Written
634 primarily for testing purposes; most users will call it only
635 indirectly through the C<read_bibstyle> method.
636  
637 =cut
638  
639 { # Closure for private variables
640 4 my %BuiltIn =
641 (
642 '>' => \&_function_arith,
643 '<' => \&_function_arith,
644 '=' => \&_function_eq,
645 '+' => \&_function_arith,
646 '-' => \&_function_arith,
647 '*' => \&_function_concat,
648 ':=' => \&_function_assign,
649 'add.period$' => \&_function_add_period,
650 'call.type$' => \&_function_call_type,
651 'change.case$' => \&_function_change_case,
652 'chr.to.int$' => \&_function_chr_to_int,
653 'cite$' => \&_function_cite,
654 'duplicate$' => \&_function_duplicate,
655 'empty$' => \&_function_empty,
656 'format.name$' => \&_function_format_name,
657 'if$' => \&_function_if,
658 'int.to.chr$' => \&_function_int_to_chr,
659 'int.to.str$' => \&_function_int_to_str,
660 'missing$' => \&_function_missing,
661 'newline$' => \&_function_newline,
662 'num.names$' => \&_function_num_names,
663 0 ▶ 'pop$' => sub { my ($self) = @_; $self->_pop; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
664 'preamble$' => \&_function_preamble,
665 'purify$' => \&_function_purify,
666 'quote$' => 1, # Handled as a constant symbol
667 0 ▶ 'skip$' => sub {},
Subroutine __ANON__: not called CC=1 SCAR=0.0
668 4 'stack$' => \&_function_stack,
669 'substring$' => \&_function_substring,
670 'swap$' => \&_function_swap,
671 'text.length$' => \&_function_text_length,
672 'text.prefix$' => \&_function_text_prefix,
673 'top$' => \&_function_top,
674 'type$' => \&_function_type,
675 'warning$' => \&_function_warning,
676 'while$' => \&_function_while,
677 'width$' => \&_function_width,
678 'write$' => \&_function_write,
679 );
680  
681 # Here's where the actual parsing takes place
682 20 ▶sub replace_bibstyle : method {
Subroutine replace_bibstyle: called CC=12 SCAR=25.6
Pod documented
683 20 my ($self, $interp, $filename) = @_;
684  
685 20 ▶ $filename ||= '<string>';
Condition100%2/2
$filename ||= "<string>"
l!l
119
MC/DC100%1/1
$filename ||= "<string>"
$filename
Truth table100%2/2
A
A= $filename
Aresult
11
00
686 # Remove comments
687 20 $interp =~ s/(^|[^\\]) \% .*/$1/mgx ;
688 20 my @interp = grep $_, split(/(\s+)|([{}]|\".*?\"|\#line \d+ .*\n)/, $interp);
689 # Put '{' .. '}' pairs into array refs
690 20 my @stack;
691 20 my $nest = 0;
692 20 push @stack, [];
693 20 my $lineno = 1;
694 20 push @{$stack[-1]}, "#line $lineno $filename";
695 20 foreach (@interp) {
696 5670 ▶ if ($_ =~ /^\s+$/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($_ =~ /^\s+$/) { } 2624 3046 5670
elsif ($_ eq '{') { } 464 2582 3046
elsif ($_ eq '}') { } 464 2118 2582
697 2624 my $nl = y/\n//;
698 2624 ▶ if ($nl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nl) 1125 1499 2624
699 1125 $lineno += $nl;
700 1125 push @{$stack[-1]}, "#line $lineno $filename";
701 }
702 }
703 elsif ($_ eq '{') {
704 464 $nest++;
705 464 push @stack, [];
706 464 push @{$stack[-1]}, "#line $lineno $filename";
707 }
708 elsif ($_ eq '}') {
709 464 $nest--;
710 464 ▶ croak("$filename, $lineno: Unmatched '}'") if $nest < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $nest < 0 0 464 464
711 464 push @{$stack[-2]}, pop(@stack);
712 }
713 else {
714 2118 ▶ ($lineno, $filename) = ($1, $2) if /^\#line (\d+) (.*)/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\#line (\d+) (.*)/ 76 2042 2118
715 2118 push @{$stack[-1]}, $_;
716 }
717 }
718 20 ▶ if ($nest > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nest > 0) 0 20 20
719 # Find the error line number
720 0 my $errline = 1;
721 0 foreach (reverse @{$stack[$nest-1]}) {
722 0 ▶ if (/^\#line (\d+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\#line (\d+)/) 0 0 0
723 0 $errline = $1;
724 0 last;
725 }
726 }
727 0 croak("$filename, $errline: Unmatched '{'");
728 }
729  
730 20 $self->{interp} = $stack[0];
731 20 $self->{output} = [];
732 20 $self->{stack} = [];
733 $self->{symbols} = {
734 20 const => { 'quote$' => '"""' },
735 entry_str => { 'sort.key$' => undef },
736 field => { crossref => undef },
737 function => {
738 map(($_ => "'$_"), keys %BuiltIn)
739 },
740 integer => {
741 'entry.max$' => 100,
742 'global.max$' => 1000,
743 },
744 };
745 20 $self->{warnings} = [];
746 }
747  
748 =item C<warnings>
749  
750 Method. Returns an array of the warning messages generated during execution.
751  
752 =cut
753  
754 18 ▶sub warnings : method {
Subroutine warnings: called CC=1 SCAR=0.0
Pod documented
755 18 my ($self) = @_;
756  
757 18 return @{$self->{warnings}};
758 }
759  
760 =back
761  
762 =head1 ENVIRONMENT
763  
764 The following environment variables are used:
765  
766 =over
767  
768 =item C<BIBINPUTS>
769  
770 The search path for bibliography (.bib) files.
771  
772 =item C<BSTINPUTS>
773  
774 The search path for bibstyle (.bst) files.
775  
776 =back
777  
778 =head1 LIMITATIONS
779  
780 The $Text::BibTeX::BibStyle::HTML output filter has the following
781 limitations:
782  
783 =over
784  
785 =item Math mode
786  
787 The math mode interpretation depends upon using Text::ASCIIMathML to
788 convert to MathML. ASCIIMathML accepts most, but not all, LaTeX
789 constructs. In order to render correctly in some browsers, it will
790 need to use xhtml and put the appropriate MathML entity definitions in
791 the header.
792  
793 =item latex2e symbols
794  
795 Extended symbols defined by latex2e are not supported.
796  
797 =back
798  
799 =head1 COPYRIGHT & LICENSE
800  
801 Copyright 2007 Mark Nodine, all rights reserved.
802  
803 This program is free software; you can redistribute it and/or modify it
804 under the same terms as Perl itself.
805  
806 =cut
807  
808 ################ Internal routines
809  
810 # Checks a name to be define to be sure it is valid
811 # Arguments: name
812 # Returns: name
813 123 ▶sub _check_name : method {
Subroutine _check_name: called CC=4 SCAR=14.2
814 123 my ($self, $name) = @_;
815  
816 123 my $where = $self->{lineno};
817 123 ▶ croak "$where: Illegal name '$name'"
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /^[^\"\#\%\'(),={}\s\d][^\"\#\%\'(),={}\s]*$/ 0 123 123
818 unless $name =~ /^[^\"\#\%\'(),={}\s\d][^\"\#\%\'(),={}\s]*$/;
819
820 123 foreach my $sym (qw(const field entry_int entry_str function
821 integer string)) {
822 croak "$where: Cannot redefine $sym '$name'"
823 861 ▶ if exists $self->{symbols}{$sym}{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"symbols"}{$sym}{$name} 0 861 861
824 }
825  
826 123 return $name;
827 }
828  
829 { # Closure for static variables
830 4 my $Arg_num;
831 4 my $Check_type_warnings;
832  
833 # Checks the type of an argument
834 # Arguments: argument, type(s), token, true if first arg of function
835 # The types are a concatenation of
836 # i: integer
837 # q: quoted value (e.g., 'a)
838 # s: string
839 # x: expression (array ref)
840 119 ▶sub _check_type : method {
Subroutine _check_type: called CC=9 SCAR=22.0
841 119 my ($self, $arg, $types, $token, $first_arg) = @_;
842  
843 119 ▶ $Arg_num = $first_arg ? 1 : $Arg_num + 1;
Branch: 1 branch
BranchTrueFalseTotal
$first_arg ? : 67 52 119
844 119 ▶ $Check_type_warnings = 0 if $first_arg;
Branch: 1 branch
BranchTrueFalseTotal
if $first_arg 67 52 119
845 119 $_ = $arg;
846 119 ▶ my $type = (! defined $_ || /^\"/? 's' :
Branch: 4 branches
BranchTrueFalseTotal
!defined($_) || /^\"/ ? : 55 64 119
ref $_ ? : 2 62 64
/^\'/ ? : 12 50 62
/^-?\d+$/ ? : 50 0 50
Condition100%3/3
not defined $_ or /^\"/
l!l&&r!l&&!r
35264
MC/DC100%2/2
not defined $_ or /^\"/
not defined $_ /^\"/
Truth table100%3/3
not(A) or B
A= not defined $_B= /^\"/
ABresult
1X1
011
000
847 ref($_) ? 'x' :
848 /^\'/ ? 'q' :
849 /^-?\d+$/ ? 'i' :
850 croak("$self->{lineno}: value '$_' has unknown type"));
851 119 ▶ $Check_type_warnings += $self->_warning
Branch: 1 branch
BranchTrueFalseTotal
unless $types =~ /$type/i 15 104 119
852 (sprintf "Argument $Arg_num of '$token' has wrong type (%s)",
853 $self->_format_token($_))
854 unless $types =~ /$type/i;
855 }
856  
857 # Returns the number of check_type warnings for this function
858 57 ▶sub _check_type_warnings {
Subroutine _check_type_warnings: called CC=1 SCAR=0.0
859 57 return $Check_type_warnings;
860 }
861 }
862  
863 # All the _command routines are used to execute a command.
864 # Inputs: $self, command name, file name, line num, ref to array of arguments
865 4 ▶sub _command_entry {
Subroutine _command_entry: called CC=1 SCAR=0.0
866 2 my ($self, $cmd, $filename, $lineno, $args_ar) = @_;
867  
868 $self->{bibtex}{format} = {
869 field => ['crossref',
870 map($self->_check_name($_),
871 2 grep ! /^\#line/, @{$args_ar->[0]})],
872 integer => [map($self->_check_name($_),
873 2 grep ! /^\#line/, @{$args_ar->[1]})],
874 string => ['sort.key$',
875 map($self->_check_name($_),
876 2 grep ! /^\#line/, @{$args_ar->[2]})]
877 };
878  
879 $self->{symbols}{field} =
880 2 { map(($_=>undef), @{$self->{bibtex}{format}{field}}) };
881 $self->{symbols}{entry_int} =
882 2 { map(($_=>"-0"), @{$self->{bibtex}{format}{integer}}) };
883 $self->{symbols}{entry_str} =
884 2 { map(($_=>undef), @{$self->{bibtex}{format}{string}}) };
885
886 2 1;
887 }
888  
889 19 ▶sub _command_execute {
Subroutine _command_execute: called CC=3 SCAR=11.1
890 19 my ($self, $cmd, $filename, $lineno, $args_ar) = @_;
891  
892 19 my @symbols = grep ! /^\#line/, @{$args_ar->[0]};
893 19 ▶ croak "$filename, $lineno: first argument to '$cmd' must contain exactly one name"
Branch: 1 branch
BranchTrueFalseTotal
unless @symbols == 1 0 19 19
894 unless @symbols == 1;
895 19 my $func_name = $symbols[0];
896 19 my $function = $self->{symbols}{function}{$func_name};
897 19 ▶ croak "$filename, $lineno: Function '$func_name' has not been defined"
Branch: 1 branch
BranchTrueFalseTotal
unless $function 0 19 19
898 unless $function;
899 19 $self->{stack} = []; # Start with a new stack
900 19 _evaluate ($self, $function);
901 }
902  
903 76 ▶sub _command_function {
Subroutine _command_function: called CC=2 SCAR=7.0
904 76 my ($self, $cmd, $filename, $lineno, $args_ar) = @_;
905  
906 76 my @symbols = grep ! /^\#line/, @{$args_ar->[0]};
907 76 ▶ croak "$filename, $lineno: first argument to '$cmd' must contain exactly one name"
Branch: 1 branch
BranchTrueFalseTotal
unless @symbols == 1 0 76 76
908 unless @symbols == 1;
909 76 my $func_name = $symbols[0];
910 76 $self->_check_name($func_name);
911 76 $self->{symbols}{function}{$func_name} = $args_ar->[1];
912 }
913  
914 0 ▶sub _command_iterate {
Subroutine _command_iterate: not called CC=5 SCAR=34.0
915 0 my ($self, $cmd, $filename, $lineno, $args_ar) = @_;
916  
917 0 my @entries = @{$self->{bibtex}{cites}};
918 0 ▶ @entries = reverse @entries if $cmd =~ /reverse/i;
Branch: 1 branch
BranchTrueFalseTotal
if $cmd =~ /reverse/i 0 0 0
919
920 0 my @symbols = grep ! /^\#line/, @{$args_ar->[0]};
921 0 ▶ croak "$filename, $lineno: first argument to '$cmd' must contain exactly one name"
Branch: 1 branch
BranchTrueFalseTotal
unless @symbols == 1 0 0 0
922 unless @symbols == 1;
923 0 my $func_name = $symbols[0];
924 0 my $function = $self->{symbols}{function}{$func_name};
925 0 ▶ croak "$filename, $lineno: Function '$func_name' has not been defined"
Branch: 1 branch
BranchTrueFalseTotal
unless $function 0 0 0
926 unless $function;
927 0 foreach my $cite (@entries) {
928 0 $self->{stack} = []; # Start with a new stack
929  
930 # Initialize the cite, bt_entry, and entry references
931 0 $self->{bibtex}{cite} = $cite;
932 0 $self->{bibtex}{bt_entry} = $self->{bibtex}{bt_entries}{$cite};
933 0 my $entry = $self->{bibtex}{entries}{$cite};
934 0 $self->{symbols}{field} = $entry->{field};
935 0 $self->{symbols}{entry_int} = $entry->{integer};
936 0 $self->{symbols}{entry_str} = $entry->{string};
937 0 _evaluate ($self, $function);
938 }
939 }
940  
941 32 ▶sub _command_macro {
Subroutine _command_macro: called CC=4 SCAR=14.5
942 32 my ($self, $cmd, $filename, $lineno, $args_ar) = @_;
943  
944 32 my @symbols = grep ! /^\#line/, @{$args_ar->[0]};
945 32 ▶ croak "$filename, $lineno: first argument to '$cmd' must contain exactly one name"
Branch: 1 branch
BranchTrueFalseTotal
unless @symbols == 1 0 32 32
946 unless @symbols == 1;
947 32 my @defs = grep ! /^\#line/, @{$args_ar->[1]};
948 32 ▶ croak "$filename, $lineno: second argument to '$cmd' must contain exactly one string"
Branch: 1 branch
BranchTrueFalseTotal
unless @defs == 1 and $defs[0] =~ /^\"(.*)\"$/s 0 32 32
Condition33%1/3
@defs == 1 and $defs[0] =~ /^\"(.*)\"$/s
!ll&&!rl&&r
0032
MC/DC0%0/2
@defs == 1 and $defs[0] =~ /^\"(.*)\"$/s
@defs == 1 $defs[0] =~ /^\"(.*)\"$/s
Truth table33%1/3
A and B
A= @defs == 1B= $defs[0] =~ /^\"(.*)\"$/s
ABresult
0X0
100
111
949 unless @defs == 1 && $defs[0] =~ /^\"(.*)\"$/s;
950 32 my $macro = $symbols[0];
951 32 Text::BibTeX::add_macro_text($macro, _trim_string($defs[0]),
952 $filename, $lineno);
953
954 # $self->_check_name($macro);
955 # $self->{symbols}{macro}{$macro} = $1;
956 }
957  
958 1 ▶sub _command_read {
Subroutine _command_read: called CC=25 SCAR=38.2
959 1 my ($self, $cmd, $filename, $lineno, $args_ar) = @_;
960  
961 croak "$self->{lineno}: I found no bib files"
962 1 ▶ unless $self->{bibtex}{bibfiles} && @{$self->{bibtex}{bibfiles}};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"bibtex"}{"bibfiles"} and @{$self->{"bibtex"}{"bibfiles"};} 0 1 1
Condition33%1/3
$self->{"bibtex"}{"bibfiles"} and @{$self->{"bibtex"}{"bibfiles"};}
!ll&&!rl&&r
001
MC/DC0%0/2
$self->{"bibtex"}{"bibfiles"} and @{$self->{"bibtex"}{"bibfiles"};}
$self->{"bibtex"}{"bibfiles"} @{$self->{"bibtex"}{"bibfiles"};}
Truth table33%1/3
A and B
A= $self->{"bibtex"}{"bibfiles"}B= @{$self->{"bibtex"}{"bibfiles"};}
ABresult
0X0
100
111
963  
964 1 my %cited;
965 my @cites;
966  
967 1 my $cite_all = !$self->{bibtex}{cites};
968 1 ▶ $self->{bibtex}{cites} ||= [];
Condition50%1/2
$self->{"bibtex"}{"cites"} ||= []
l!l
01
MC/DC0%0/1
$self->{"bibtex"}{"cites"} ||= []
$self->{"bibtex"}{"cites"}
Truth table50%1/2
A
A= $self->{"bibtex"}{"cites"}
Aresult
11
00
969  
970 1 my %cites; # The refs we want if not $cite_all
971 1 $cites{$_} = 1 foreach @{$self->{bibtex}{cites}};
972  
973 # First read the bibtex files and get entries for each reference
974 1 foreach my $filename (@{$self->{bibtex}{bibfiles}}) {
975 1 my $f = "$filename.bib";
976 1 ▶ my $path = $ENV{BIBINPUTS} || '.';
Condition50%1/2
$ENV{'BIBINPUTS'} || '.'
l!l
10
MC/DC0%0/1
$ENV{'BIBINPUTS'} || '.'
$ENV{'BIBINPUTS'}
Truth table50%1/2
A
A= $ENV{'BIBINPUTS'}
Aresult
11
00
977 1 my @path = split /:/, $path;
978 1 my ($dir) = grep -f "$_/$f", @path;
979 1 ▶ die "Cannot find $f on path: $path" unless $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless $dir 0 1 1
980 1 my $dirfile = "$dir/$f";
981 1 ▶ my $bibfile = Text::BibTeX::File->new($dirfile)
Branch: 1 branch
BranchTrueFalseTotal
my $bibfile = "Text::BibTeX::File"->new($dirfile) or die "${dirfile}: $!\n" 1 0 1
982 or die "$dirfile: $!\n";
983 1 ▶ while (my $bt_entry = new Text::BibTeX::Entry $bibfile)
Branch: 1 branch
BranchTrueFalseTotal
if (my $bt_entry = "Text::BibTeX::Entry"->new($bibfile)) 3 0 3
984 {
985 3 ▶ next unless $bt_entry->parse_ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $bt_entry->parse_ok 0 3 3
986 3 my $metatype = $bt_entry->metatype;
987 3 ▶ if ($metatype == BTE_REGULAR) {
Branch: 3 branches
BranchTrueFalseTotal
if ($metatype == BTE_REGULAR()) { } 2 1 3
elsif ($metatype == BTE_PREAMBLE()) { } 1 0 1
elsif ($metatype == BTE_MACRODEF()) { } 0 0 0
988 # Skip entries that we don't want
989 2 my $key = $bt_entry->key;
990 2 ▶ next unless $cites{$key} || $cite_all;
Branch: 1 branch
BranchTrueFalseTotal
unless $cites{$key} or $cite_all 0 2 2
Condition33%1/3
$cites{$key} or $cite_all
l!l&&r!l&&!r
020
MC/DC0%0/2
$cites{$key} or $cite_all
$cites{$key} $cite_all
Truth table33%1/3
A or B
A= $cites{$key}B= $cite_all
ABresult
1X1
011
000
991 2 ▶ push @cites, $key if $cite_all && ! $cited{$key}++;
Branch: 1 branch
BranchTrueFalseTotal
if $cite_all and not $cited{$key}++ 2 0 2
Condition33%1/3
$cite_all and not $cited{$key}++
!ll&&!rl&&r
002
MC/DC0%0/2
$cite_all and not $cited{$key}++
$cite_all not $cited{$key}++
Truth table33%1/3
A and not(B)
A= $cite_allB= not $cited{$key}++
ABresult
0X0
100
111
992 2 $self->{bibtex}{bt_entries}{$key} = $bt_entry;
993 # Create an entry hash for this entry
994 2 my $entry = { };
995 $entry->{field}{$_} =
996 $bt_entry->exists($_) ? ('"' . $bt_entry->get($_) . '"') :
997 undef
998 2 ▶ foreach @{$self->{bibtex}{format}{field}};
Branch: 1 branch
BranchTrueFalseTotal
$bt_entry->exists($_) ? : 13 33 46
999 $entry->{field}{crossref} =
1000 lc $entry->{field}{crossref}
1001 2 ▶ if defined $entry->{field}{crossref};
Branch: 1 branch
BranchTrueFalseTotal
if defined $entry->{"field"}{"crossref"} 0 2 2
1002 $entry->{integer}{$_} = "-0" # 0?
1003 2 foreach @{$self->{bibtex}{format}{integer}};
1004 $entry->{string}{$_} = undef
1005 2 foreach @{$self->{bibtex}{format}{string}};
1006 2 $self->{bibtex}{entries}{$key} = $entry;
1007 }
1008 elsif ($metatype == BTE_PREAMBLE) {
1009 1 push @{$self->{bibtex}{preamble}}, $bt_entry->value;
1010 }
1011 elsif ($metatype == BTE_MACRODEF) {
1012 # These are handled internally by Text::BibTeX
1013 }
1014 }
1015 }
1016  
1017 0 ▶ $self->{bibtex}{cites} = \@cites if $cite_all;
Branch: 1 branch
BranchTrueFalseTotal
if $cite_all 0 0 0
1018 croak "$self->{lineno} I found no citations"
1019 0 ▶ unless @{$self->{bibtex}{cites}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"bibtex"}{"cites"};} 0 0 0
1020 }
1021  
1022 0 ▶sub _command_sort {
Subroutine _command_sort: not called CC=1 SCAR=6.9
1023 0 my ($self, $cmd, $filename, $lineno, $args_ar) = @_;
1024  
1025 0 @{$self->{bibtex}{cites}} =
1026 sort { $self->{bibtex}{entries}{$a}{string}{'sort.key$'} cmp
1027 0 $self->{bibtex}{entries}{$b}{string}{'sort.key$'} }
1028 0 @{$self->{bibtex}{cites}};
1029 # croak "$filename, $lineno: Command '$cmd' not implemented yet";
1030 }
1031  
1032 10 ▶sub _command_variables {
Subroutine _command_variables: called CC=3 SCAR=11.0
1033 10 my ($self, $cmd, $filename, $lineno, $args_ar) = @_;
1034  
1035 10 ▶ my $init = $cmd =~ /integers/i ? "-0" : undef;
Branch: 1 branch
BranchTrueFalseTotal
$cmd =~ /integers/i ? : 5 5 10
1036 10 $cmd =~ s/(.*)s/\L$1/i;
1037 $self->{symbols}{$cmd}{$_} = $init
1038 10 foreach map($self->_check_name($_),
1039 10 grep ! /^\#line/, @{$args_ar->[0]});
1040 }
1041  
1042 { # Static variables
1043 4 my %In_function;
1044  
1045 # Evaluates a function
1046 # Arguments: $self, function definition
1047 22 ▶sub _evaluate {
Subroutine _evaluate: called CC=20 SCAR=33.2
1048 22 my ($self, $function_ar) = @_;
1049  
1050 22 $function_ar =~ s/^\'//;
1051 22 ▶ $function_ar = [ $function_ar ] unless ref $function_ar;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $function_ar 0 22 22
1052 22 local ($self->{lineno}) = $self->{lineno};
1053 22 my $old_warnings = $self->num_warnings;
1054 token:
1055 22 foreach my $token (@{$function_ar}) {
1056 329 ▶ if ($token =~ /^\#line (\d+) (.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token =~ /^\#line (\d+) (.*)/) 127 202 329
1057 127 my ($lineno, $filename) = ($1, $2);
1058 127 $self->{lineno} = "$filename, $lineno";
1059 127 next token;
1060 }
1061 printf STDERR "$self->{lineno}: {%s} %s\n",
1062 0 join(' ', map($self->_format_token($_), @{$self->{stack}})),
1063 $self->_format_token($token)
1064 202 ▶ if $self->{options}{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"options"}{"debug"} 0 202 202
1065 202 ▶ if (ref $token) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $token) 2 200 202
1066 2 $self->_push($token);
1067 2 next token;
1068 }
1069 200 ▶ if ($token =~ /^\#(-?\d+)/ || $token =~ /^(\'.*)/ ||
Branch: 1 branch
BranchTrueFalseTotal
if ($token =~ /^\#(-?\d+)/ or $token =~ /^(\'.*)/ or $token =~ /^(\".*\")/) 98 102 200
Condition100%6/6
$token =~ /^\#(-?\d+)/ or $token =~ /^(\'.*)/ or $token =~ /^(\".*\")/
l!l&&r!l&&!r
6137102
$token =~ /^\#(-?\d+)/ or $token =~ /^(\'.*)/
l!l&&r!l&&!r
4615139
MC/DC100%3/3
$token =~ /^\#(-?\d+)/ or $token =~ /^(\'.*)/ or $token =~ /^(\".*\")/
$token =~ /^\#(-?\d+)/ $token =~ /^(\'.*)/ $token =~ /^(\".*\")/
Truth table100%4/4
A or B or C
A= $token =~ /^\#(-?\d+)/B= $token =~ /^(\'.*)/C= $token =~ /^(\".*\")/
ABCresult
1XX1
01X1
0011
0000
1070 $token =~ /^(\".*\")/) {
1071 98 $self->_push($1);
1072 98 next token;
1073 }
1074 # Check for constants and variables
1075 102 foreach my $sym (qw(const entry_int entry_str integer string)) {
1076 483 ▶ if (exists $self->{symbols}{$sym}{$token}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"symbols"}{$sym}{$token}) 26 457 483
1077 26 $self->_push($self->{symbols}{$sym}{$token});
1078 26 next token;
1079 }
1080 }
1081 # Check for fields
1082 76 ▶ if (exists $self->{symbols}{field}{$token}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"symbols"}{"field"}{$token}) 2 74 76
1083 2 my $val = $self->{symbols}{field}{$token};
1084 # Supply crossreferenced fields if applicable
1085 2 ▶ if (! defined $val && $self->{bibtex}{bt_entry} &&
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $val and $self->{"bibtex"}{"bt_entry"} and my $xref = $self->{"bibtex"}{"bt_entry"}->get("crossref")) 0 2 2
Condition33%2/6
not defined $val and $self->{"bibtex"}{"bt_entry"} and my $xref = $self->{"bibtex"}{"bt_entry"}->get("crossref")
!ll&&!rl&&r
200
not defined $val and $self->{"bibtex"}{"bt_entry"}
!ll&&!rl&&r
020
MC/DC0%0/3
not defined $val and $self->{"bibtex"}{"bt_entry"} and my $xref = $self->{"bibtex"}{"bt_entry"}->get("crossref")
not defined $val $self->{"bibtex"}{"bt_entry"} my $xref = $self->{"bibtex"}{"bt_entry"}->get("crossref")
Truth table25%1/4
not(A) and B and C
A= not defined $valB= $self->{"bibtex"}{"bt_entry"}C= my $xref = $self->{"bibtex"}{"bt_entry"}->get("crossref")
ABCresult
0XX0
10X0
1100
1111
1086 (my $xref = $self->{bibtex}{bt_entry}->get('crossref'))) {
1087 0 my $bt_xref = $self->{bibtex}{bt_entries}{lc $xref};
1088 0 ▶ if ($bt_xref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bt_xref) { } 0 0 0
1089 0 ▶ $val = $bt_xref->exists($token) ?
Branch: 1 branch
BranchTrueFalseTotal
$bt_xref->exists($token) ? : 0 0 0
1090 ('"' . $bt_xref->get($token) . '"') : undef;
1091 }
1092 else {
1093 0 $self->_warning("Unknown cross reference: $xref");
1094 }
1095 }
1096 2 $self->_push($val);
1097 2 next token;
1098 }
1099 74 ▶ if ($BuiltIn{$token}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($BuiltIn{$token}) 73 1 74
1100 73 $BuiltIn{$token}->($self, $token);
1101 71 next token;
1102 }
1103 1 ▶ if (exists $self->{symbols}{function}{$token}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"symbols"}{"function"}{$token}) 1 0 1
1104 1 ▶ if ($In_function{$token}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($In_function{$token}) { } 0 1 1
1105 0 $self->_warning("Recursive call to '$token' ignored");
1106 }
1107 else {
1108 1 $In_function{$token}++;
1109 1 _evaluate ($self, $self->{symbols}{function}{$token});
1110 0 $In_function{$token}--;
1111 }
1112 0 next token;
1113 }
1114 0 $self->_warning("Undefined function '$token'");
1115 }
1116 19 return $old_warnings == $self->num_warnings;
1117 }
1118 }
1119 4 }
1120  
1121 # A subroutine to format a token for printing
1122 # Arguments: token (popped from stack)
1123 80 ▶sub _format_token : method {
Subroutine _format_token: called CC=5 SCAR=16.3
1124 80 my ($self, $token) = @_;
1125  
1126 80 $_ = $token;
1127 80 ▶ return (! defined $_ || $_ eq "-0"
Branch: 3 branches
BranchTrueFalseTotal
!defined($_) || $_ eq '-0' ? : 2 78 80
/^-?\d+$/ ? : 47 31 78
ref $_ ? : 0 31 31
Condition66%2/3
not defined $_ or $_ eq "-0"
l!l&&r!l&&!r
2078
MC/DC50%1/2
not defined $_ or $_ eq "-0"
not defined $_ $_ eq "-0"
Truth table66%2/3
not(A) or B
A= not defined $_B= $_ eq "-0"
ABresult
1X1
011
000
1128 ? 'missing' :
1129 /^-?\d+$/ ? "#$_" :
1130 ref($_) ? "{" . join(' ', map $self->_format_token($_),
1131 grep(! /\#line/, @$_)) . "}" :
1132 $_);
1133 }
1134  
1135 # Subroutines for built-in functions
1136 # All have the same arguments: $self, function token
1137  
1138 5 ▶sub _function_add_period {
Subroutine _function_add_period: called CC=3 SCAR=11.0
1139 5 my ($self, $token) = @_;
1140  
1141 5 my $arg = $self->_pop(s => $token, 1);
1142 5 ▶ $arg =~ s/\"$/.\"/ unless $arg =~ /[.?!]\}*\"$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $arg =~ /[.?!]\}*\"$/ 2 3 5
1143 5 ▶ $self->_push(_check_type_warnings() ? '""' : $arg);
Branch: 1 branch
BranchTrueFalseTotal
_check_type_warnings() ? : 1 4 5
1144 }
1145  
1146 19 ▶sub _function_arith {
Subroutine _function_arith: called CC=2 SCAR=6.9
1147 19 my ($self, $token) = @_;
1148  
1149 # We can use builtin eval for these functions
1150 19 my $arg1 = $self->_pop(i => $token, 1);
1151 19 my $arg2 = $self->_pop(i => $token);
1152 19 ▶ $self->_push(_check_type_warnings() ? 0 : eval "0+($arg2 $token $arg1)");
Branch: 1 branch
BranchTrueFalseTotal
_check_type_warnings() ? : 4 15 19
1153 }
1154  
1155 13 ▶sub _function_assign {
Subroutine _function_assign: called CC=6 SCAR=18.1
1156 13 my ($self, $token) = @_;
1157  
1158 13 my $sym = $self->_pop(q => $token, 1);
1159 13 ▶ my $have_sym if $sym =~ s/^\'//;
Branch: 1 branch
BranchTrueFalseTotal
if $sym =~ s/^\'// 12 1 13
1160 13 my ($type) = grep(exists $self->{symbols}{$_}{$sym},
1161 qw(entry_int entry_str integer string));
1162 13 ▶ my $val_type = !$have_sym ? 'si' : $type =~ /str/ ? 's' : 'i';
Branch: 2 branches
BranchTrueFalseTotal
!$have_sym ? : 13 0 13
$type =~ /str/ ? : 0 0 0
1163 13 my $val = $self->_pop($val_type => $token);
1164 13 my $bad_arg = _check_type_warnings();
1165 13 ▶ return if $bad_arg;
Branch: 1 branch
BranchTrueFalseTotal
if $bad_arg 1 12 13
1166 12 ▶ if (! $type) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $type) { } 1 11 12
1167 1 $self->_warning("Undefined variable '$sym'");
1168 }
1169 else {
1170 11 $self->{symbols}{$type}{$sym} = $val;
1171 }
1172 }
1173  
1174 1 ▶sub _function_call_type {
Subroutine _function_call_type: called CC=2 SCAR=8.1
1175 1 my ($self, $token) = @_;
1176  
1177 return $self->_warning("No current entry in function '$token'")
1178 1 ▶ unless $self->{bibtex}{bt_entry};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"bibtex"}{"bt_entry"} 1 0 1
1179 0 $self->_evaluate($self->{bibtex}{bt_entry}->type);
1180 }
1181  
1182 5 ▶sub _function_change_case {
Subroutine _function_change_case: called CC=5 SCAR=16.1
1183 5 my ($self, $token) = @_;
1184  
1185 5 my $old_num_warnings = $self->num_warnings;
1186 5 my $spec = _trim_string($self->_pop(s => $token, 1));
1187 5 my $str = _trim_string($self->_pop(s => $token));
1188 5 ▶ return $self->_push('""') if _check_type_warnings();
Branch: 1 branch
BranchTrueFalseTotal
if _check_type_warnings() 1 4 5
1189 4 ▶ if ($spec !~ /^[tul]$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($spec =~ /^[tul]$/) 1 3 4
1190 1 $self->_warning("Argument 1 of '$token' has illegal specification ($spec)");
1191 1 return $self->_push(qq("$str"));
1192 }
1193 3 my $changed;
1194 3 $str = _protect($str);
1195 3 ▶ if ($spec eq 't') {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec eq 't') { } 1 2 3
1196 1 my @unchanged = $str =~ /(?:\A|:\s+)([a-z])/ig;
1197 1 $changed = lc $str;
1198 1 my $cnt = 0;
1199 1 $changed =~ s/(\A|:\s+)([a-z])/$1$unchanged[$cnt++]/g;
1200 }
1201 else {
1202 2 ▶ $changed = $spec eq 'u' ? uc $str : lc $str;
Branch: 1 branch
BranchTrueFalseTotal
$spec eq 'u' ? : 1 1 2
1203 }
1204 # my $changed = Text::BibTeX::change_case($spec, $str);
1205 3 $changed = _unprotect($changed);
1206 3 $self->_push(qq("$changed"));
1207 }
1208  
1209 4 ▶sub _function_chr_to_int {
Subroutine _function_chr_to_int: called CC=5 SCAR=16.1
1210 4 my ($self, $token) = @_;
1211  
1212 4 my $arg1 = $self->_pop(s => $token, 1);
1213 4 my $bad_arg = _check_type_warnings();
1214 4 ▶ $self->_warning
Branch: 1 branch
BranchTrueFalseTotal
unless $bad_arg or $arg1 =~ /^\"(.)\"$/ 1 3 4
Condition100%3/3
$bad_arg or $arg1 =~ /^\"(.)\"$/
l!l&&r!l&&!r
121
MC/DC100%2/2
$bad_arg or $arg1 =~ /^\"(.)\"$/
$bad_arg $arg1 =~ /^\"(.)\"$/
Truth table100%3/3
A or B
A= $bad_argB= $arg1 =~ /^\"(.)\"$/
ABresult
1X1
011
000
1215 ("Argument 1 to '$token' must be a single character")
1216 if ! $bad_arg && $arg1 !~ /^\"(.)\"$/;
1217 4 ▶ $self->_push($bad_arg ? 0 : ord($1 || ''));
Branch: 1 branch
BranchTrueFalseTotal
$bad_arg ? : 1 3 4
Condition100%2/2
$1 || ''
l!l
21
MC/DC100%1/1
$1 || ''
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
1218 }
1219  
1220 1 ▶sub _function_cite {
Subroutine _function_cite: called CC=2 SCAR=7.4
1221 1 my ($self, $token) = @_;
1222  
1223 1 ▶ if (! $self->{bibtex}{bt_entry}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"bibtex"}{"bt_entry"}) 1 0 1
1224 1 $self->_warning("No current entry in function '$token'");
1225 1 return $self->_push('""');
1226 }
1227 0 $self->_push(lc qq("$self->{bibtex}{cite}"));
1228 }
1229  
1230 5 ▶sub _function_concat {
Subroutine _function_concat: called CC=1 SCAR=0.0
1231 5 my ($self, $token) = @_;
1232  
1233 5 my $arg1 = $self->_pop(s => $token, 1);
1234 5 my $arg2 = $self->_pop(s => $token);
1235 5 $arg1 = _trim_string($arg1);
1236 5 $arg2 = _trim_string($arg2);
1237 5 $self->_push(qq("$arg2$arg1"));
1238 }
1239  
1240 4 ▶sub _function_duplicate {
Subroutine _function_duplicate: called CC=1 SCAR=0.0
1241 4 my ($self, $token) = @_;
1242  
1243 4 my $arg1 = $self->_pop;
1244 4 $self->_push($arg1);
1245 4 $self->_push($arg1);
1246 }
1247  
1248 6 ▶sub _function_empty {
Subroutine _function_empty: called CC=2 SCAR=6.9
1249 6 my ($self, $token) = @_;
1250  
1251 6 my $arg1 = $self->_pop(s => $token, 1);
1252 6 ▶ $self->_push(0+(! defined $arg1 || $arg1 =~ /^\"\s*\"$/));
Condition100%3/3
!defined($arg1) || $arg1 =~ /^\"\s*\"$/
l!l&&r!l&&!r
312
MC/DC100%2/2
!defined($arg1) || $arg1 =~ /^\"\s*\"$/
!defined($arg1) $arg1 =~ /^\"\s*\"$/
Truth table100%3/3
not(A) || B
A= !defined($arg1)B= $arg1 =~ /^\"\s*\"$/
ABresult
1X1
011
000
1253 }
1254  
1255 6 ▶sub _function_eq {
Subroutine _function_eq: called CC=4 SCAR=13.9
1256 6 my ($self, $token) = @_;
1257  
1258 6 my $arg1 = $self->_pop(is => $token, 1);
1259 6 ▶ my $type = $arg1 =~ /^\"/ ? 's' : 'i';
Branch: 1 branch
BranchTrueFalseTotal
$arg1 =~ /^\"/ ? : 3 3 6
1260 6 my $arg2 = $self->_pop($type => $token);
1261 6 ▶ my $op = $type eq 's' ? 'eq' : '==';
Branch: 1 branch
BranchTrueFalseTotal
$type eq 's' ? : 3 3 6
1262 6 ▶ $self->_push(_check_type_warnings() ? 0 : eval "0+(q($arg2) $op q($arg1))");
Branch: 1 branch
BranchTrueFalseTotal
_check_type_warnings() ? : 2 4 6
1263 }
1264  
1265 1 ▶sub _function_format_name {
Subroutine _function_format_name: called CC=30 SCAR=45.2
1266 1 my ($self, $token) = @_;
1267  
1268 1 my $format = $self->_pop(s => $token, 1);
1269 1 my $index = $self->_pop(i => $token);
1270 1 my $list = $self->_pop(s => $token);
1271 1 ▶ return $self->_push('""') if _check_type_warnings();
Branch: 1 branch
BranchTrueFalseTotal
if _check_type_warnings() 0 1 1
1272 1 $format = _trim_string($format);
1273 1 $list = _trim_string($list);
1274 1 my @list = Text::BibTeX::split_list($list, 'and');
1275 1 ▶ if ($index > @list || $index < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index > @list or $index < 1) 0 1 1
Condition33%1/3
$index > @list or $index < 1
l!l&&r!l&&!r
001
MC/DC0%0/2
$index > @list or $index < 1
$index > @list $index < 1
Truth table33%1/3
A or B
A= $index > @listB= $index < 1
ABresult
1X1
011
000
1276 0 $self->_warning("Index $index is out of range for '$token'");
1277 0 return $self->_push('""');
1278 }
1279 1 my $name = $list[$index-1];
1280 1 my $bt_name = Text::BibTeX::Name->new($name);
1281 1 my %name;
1282 1 @name{qw(f v l j)} = map [$bt_name->part($_)], qw(first von last jr);
1283  
1284 1 my %parts = (f => 'first', v => 'von', l => 'last', j => 'jr');
1285  
1286 # Parse the format string
1287 1 $format = _protect($format);
1288 1 my @format = split /(PROTECT\(0D\d+\))/, $format;
1289 1 my $output = '';
1290 1 foreach (@format) {
1291 8 ▶ if (/^PROTECT/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^PROTECT/) { } 4 4 8
1292 4 my $spec = _unprotect($_);
1293 4 $spec =~ s/^\{(.*)\}/$1/s;
1294 4 my ($pre, $form, $long, $inter, $post, $may_tie, $must_tie) =
1295 $spec =~ /(.*?)([fjlv])(\2)?(?:\{(.*)\})?(.*?)?(~)?(~)?$/;
1296 4 ▶ if (! defined $form) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $form) 0 4 4
1297 0 $self->_warning("Invalid format specifier '$spec' for '$token'");
1298 0 return $self->_push('""');
1299 }
1300 4 my $name = $name{$form};
1301 4 ▶ next unless @$name;
Branch: 1 branch
BranchTrueFalseTotal
unless @$name 0 4 4
1302 4 ▶ $pre = ' ' if $pre eq '' && $output ne '' &&
Branch: 1 branch
BranchTrueFalseTotal
if $pre eq "" and $output ne "" and not $output =~ /[ ~]$/ 2 2 4
Condition83%5/6
$pre eq "" and $output ne "" and not $output =~ /[ ~]$/
!ll&&!rl&&r
202
$pre eq "" and $output ne ""
!ll&&!rl&&r
112
MC/DC66%2/3
$pre eq "" and $output ne "" and not $output =~ /[ ~]$/
$pre eq "" $output ne "" not $output =~ /[ ~]$/
Truth table75%3/4
A and B and not(C)
A= $pre eq ""B= $output ne ""C= not $output =~ /[ ~]$/
ABCresult
0XX0
10X0
1100
1111
1303 $output !~ /[ ~]$/;
1304 4 my $need_tie = 0;
1305 4 my @out_names;
1306 4 foreach (@$name) {
1307 9 my $name = _unprotect($_);
1308 8 ▶ my $tie = @out_names && $need_tie && $may_tie ? '~' : ' ';
Branch: 1 branch
BranchTrueFalseTotal
@out_names && $need_tie && $may_tie ? : 3 5 8
Condition100%6/6
@out_names and $need_tie and $may_tie
!ll&&!rl&&r
413
@out_names and $need_tie
!ll&&!rl&&r
314
MC/DC100%3/3
@out_names and $need_tie and $may_tie
@out_names $need_tie $may_tie
Truth table100%4/4
A and B and C
A= @out_namesB= $need_tieC= $may_tie
ABCresult
0XX0
10X0
1100
1111
1309 8 ▶ if ($long) {
Branch: 1 branch
BranchTrueFalseTotal
if ($long) { } 8 0 8
1310 8 ▶ push @out_names, $tie if @out_names;
Branch: 1 branch
BranchTrueFalseTotal
if @out_names 5 3 8
1311 8 push @out_names, $name;
1312 }
1313 else {
1314 # Handle hyphenated names
1315 0 my @parts = split /-/, $name;
1316 0 my @part_letters;
1317 0 foreach my $part (@parts) {
1318 0 my ($letter) = $part =~ /^(\{\\[a-z]+|.*?[a-z])/i;
1319 0 ▶ $letter .= '}' while
Branch: 1 branch
BranchTrueFalseTotal
$letter =~ tr/{// > $letter =~ tr/}// and do { $letter .= '}' } 0 0 0
1320 $letter =~ tr/{// > $letter =~ tr/}//;
1321 0 push @part_letters, $letter;
1322 }
1323 0 ▶ my $join = defined $inter ? "$inter" : '.-';
Branch: 1 branch
BranchTrueFalseTotal
defined $inter ? : 0 0 0
1324 0 ▶ $tie = defined $inter ? $inter : ".$tie";
Branch: 1 branch
BranchTrueFalseTotal
defined $inter ? : 0 0 0
1325 0 ▶ push @out_names, $tie if @out_names;
Branch: 1 branch
BranchTrueFalseTotal
if @out_names 0 0 0
1326 0 push @out_names, join $join, @part_letters;
1327 # Hyphen counts as a tie
1328 0 ▶ $tie = '~' if @part_letters > 1 && $tie;
Branch: 1 branch
BranchTrueFalseTotal
if @part_letters > 1 and $tie 0 0 0
Condition0%0/3
@part_letters > 1 and $tie
!ll&&!rl&&r
000
MC/DC0%0/2
@part_letters > 1 and $tie
@part_letters > 1 $tie
Truth table0%0/3
A and B
A= @part_letters > 1B= $tie
ABresult
0X0
100
111
1329 }
1330 8 $need_tie = $tie !~ /~/;
1331 }
1332 3 ▶ my $out_tie =
Branch: 1 branch
BranchTrueFalseTotal
!$long && ($must_tie || $may_tie && $need_tie) ? : 0 3 3
Condition11%1/9
not $long and $must_tie || $may_tie && $need_tie
!ll&&!rl&&r
300
$must_tie or $may_tie and $need_tie
l!l&&r!l&&!r
000
$may_tie and $need_tie
!ll&&!rl&&r
000
MC/DC0%0/4
not $long and $must_tie || $may_tie && $need_tie
not $long $must_tie $may_tie $need_tie
Truth table20%1/5
not(A) and B or C and D
A= not $longB= $must_tieC= $may_tieD= $need_tie
ABCDresult
0XXX0
100X0
10100
11XX1
10111
1333 ! $long && ($must_tie || $may_tie && $need_tie) ? '~' : '';
1334 3 ▶ $output .= ($pre || '') . join('', @out_names) . ($post || '') .
Condition75%3/4
$pre || ''
l!l
21
$post || ''
l!l
03
MC/DC100%1/1
$pre || ''
$pre
MC/DC0%0/1
$post || ''
$post
Truth table100%2/2
A
A= $pre
Aresult
11
00
Truth table50%1/2
A
A= $post
Aresult
11
00
1335 # '';
1336 $out_tie;
1337 }
1338 else {
1339 4 $output .= $_;
1340 }
1341 }
1342  
1343 0 $self->_push(qq("$output"));
1344 # my $format = $self->_pop(s => $token, 1);
1345 # my $index = $self->_pop(i => $token);
1346 # my $list = $self->_pop(s => $token);
1347 # return $self->_push('""') if _check_type_warnings();
1348 # $format = _trim_string($format);
1349 # $list = _trim_string($list);
1350 # my @list = split /\s+and\s+/, _protect($list);
1351 # if ($index > @list || $index < 1) {
1352 # $self->_warning("Index $index is out of range for '$token'");
1353 # return $self->_push('""');
1354 # }
1355 # # N.B. $name is still protected
1356 # my $name = $list[$index-1];
1357 # # Decompose the string into first, von, last, and junior
1358 # my @names = split /,\s*/, $name;
1359 # my $von_re = '[a-z](?:PROTECT\(0D\d+\)|\\.|[a-z])*';
1360 # my %name; # Four keys: f, j, l, v
1361 # if (@names > 1) { # "von Last, Junior, First" style
1362 # $name{f} = $names[-1];
1363 # $name{j} = $names[1] if @names > 2;
1364 # my @last = split /\s+/, $names[0];
1365 # my @von;
1366 # while ($_ = shift @last) {
1367 # # Get the first letter
1368 # my $name = _unprotect($_);
1369 # my ($letter) = $name =~ /([a-z])/i;
1370 # if ($letter =~ /[a-z]/) {
1371 # push @von, $_;
1372 # }
1373 # else {
1374 # unshift @last, $_;
1375 # last;
1376 # }
1377 # }
1378 # $name{v} = join ' ', @von;
1379 # $name{l} = join ' ', @last;
1380 # }
1381 # else {
1382 # my (@first, @von, @last);
1383 # @names = split /\s+/, $name;
1384 # @last = pop @names;
1385 # while ($_ = shift @names) {
1386 # # Get the first letter
1387 # my $name = _unprotect($_);
1388 # my ($letter) = $name =~ /([a-z])/i;
1389 # if ($letter =~ /[a-z]/) {
1390 # push @von, $_;
1391 # }
1392 # elsif (@von) {
1393 # unshift @last, $_;
1394 # last;
1395 # }
1396 # else {
1397 # push @first, $_;
1398 # }
1399 # }
1400 # $name{f} = join ' ', @first;
1401 # $name{v} = join ' ', @von;
1402 # $name{l} = join ' ', @last;
1403 # }
1404  
1405 # # Parse the format string
1406 # $format = _protect($format);
1407 # my @format = split /(PROTECT\(0D\d+\))/, $format;
1408 # my $output = '';
1409 # foreach (@format) {
1410 # if (/^PROTECT/) {
1411 # my $spec = _unprotect($_);
1412 # $spec =~ s/^\{(.*)\}/$1/s;
1413 # my ($pre, $form, $long, $inter, $post, $tie, $must_tie) =
1414 # $spec =~ /(.*?)([fjlv])(\2)?(\{.*\})?(.*?)?(~)?(~)?$/;
1415 # if (! defined $form) {
1416 # $self->_warning("Invalid format specifier '$spec' for '$token'");
1417 # return $self->_push('""');
1418 # }
1419 # my $name = $name{$form};
1420 # next unless defined $name && $name ne '';
1421 # $inter = $long ? '~' : '.~' unless ($inter || '') ne '';
1422 # $inter =~ s/^\{(.*)\}$/$1/s;
1423 # $pre = ' ' if $pre eq '' && $output ne '' &&
1424 # $output !~ /[ ~]$/;
1425 # if (! $long) {
1426 # my @names = split /\s+/, $name;
1427 # my @inits;
1428 # foreach (@names) {
1429 # my $name = _unprotect($_);
1430 # my ($letter) = $name =~ /^(\{\\[a-z]+|.*?[a-z])/i;
1431 # $letter .= '}' while $letter =~ tr/{// > $letter =~ tr/}//;
1432 # push @inits, $letter;
1433 # }
1434 # $name = join ' ', @inits;
1435 # }
1436 # my @names = split /\s+/, $name;
1437 # $output .= ($pre || '') . join($inter, @names) . ($post || '') .
1438 # ($must_tie || '');
1439 # }
1440 # else {
1441 # $output .= $_;
1442 # }
1443 # }
1444 # $output = _unprotect($output);
1445 # $self->_push(qq("$output"));
1446 }
1447  
1448 0 ▶sub _function_if {
Subroutine _function_if: not called CC=3 SCAR=24.8
1449 0 my ($self, $token) = @_;
1450  
1451 0 my $else = $self->_pop(qx => $token, 1);
1452 0 my $if = $self->_pop(qx => $token);
1453 0 my $cond = $self->_pop(i => $token);
1454 0 ▶ return if _check_type_warnings();
Branch: 1 branch
BranchTrueFalseTotal
if _check_type_warnings() 0 0 0
1455 0 ▶ my $branch = $cond > 0 ? $if : $else;
Branch: 1 branch
BranchTrueFalseTotal
$cond > 0 ? : 0 0 0
1456 0 $self->_evaluate($branch);
1457 }
1458  
1459 1 ▶sub _function_int_to_chr {
Subroutine _function_int_to_chr: called CC=2 SCAR=7.1
1460 1 my ($self, $token) = @_;
1461  
1462 1 my $arg1 = $self->_pop(i => $token, 1);
1463 1 ▶ $self->_push(_check_type_warnings() ? '""' : '"' . chr($arg1) . '"');
Branch: 1 branch
BranchTrueFalseTotal
_check_type_warnings() ? : 0 1 1
1464 }
1465  
1466 0 ▶sub _function_int_to_str {
Subroutine _function_int_to_str: not called CC=2 SCAR=17.9
1467 0 my ($self, $token) = @_;
1468  
1469 0 my $arg1 = $self->_pop(i => $token, 1);
1470 0 ▶ $self->_push(_check_type_warnings() ? '""' : qq("$arg1"));
Branch: 1 branch
BranchTrueFalseTotal
_check_type_warnings() ? : 0 0 0
1471 }
1472  
1473 0 ▶sub _function_missing {
Subroutine _function_missing: not called CC=1 SCAR=6.9
1474 0 my ($self, $token) = @_;
1475  
1476 0 my $arg1 = $self->_pop(s => $token, 1);
1477 0 $self->_push(0+(! defined $arg1));
1478 }
1479  
1480 0 ▶sub _function_newline {
Subroutine _function_newline: not called CC=1 SCAR=6.9
1481 0 my ($self, $token) = @_;
1482  
1483 0 push @{$self->{output}}, "\n";
1484 }
1485  
1486 1 ▶sub _function_num_names {
Subroutine _function_num_names: called CC=2 SCAR=7.0
1487 1 my ($self, $token) = @_;
1488  
1489 1 my $arg1 = _trim_string($self->_pop(s => $token, 1));
1490 1 ▶ return $self->_push(0) if _check_type_warnings();
Branch: 1 branch
BranchTrueFalseTotal
if _check_type_warnings() 0 1 1
1491 1 my @split = Text::BibTeX::split_list($arg1, 'and');
1492 1 $self->_push(0+@split);
1493 }
1494  
1495 0 ▶sub _function_preamble {
Subroutine _function_preamble: not called CC=1 SCAR=6.9
1496 0 my ($self, $token) = @_;
1497  
1498 0 $self->_push(sprintf '"%s"', join('', @{$self->{bibtex}{preamble}}));
1499 }
1500  
1501 0 ▶sub _function_purify {
Subroutine _function_purify: not called CC=4 SCAR=30.0
1502 0 my ($self, $token) = @_;
1503  
1504 0 my $arg = _trim_string($self->_pop(s => $token, 1));
1505 0 ▶ return $self->_push('""') if _check_type_warnings();
Branch: 1 branch
BranchTrueFalseTotal
if _check_type_warnings() 0 0 0
1506 # my $pure = Text::BibTeX::purify_string($arg);
1507 # $self->_push(qq("$pure"));
1508  
1509 0 $arg =~ s/[\s~]+/ /g;
1510 # Handle accents
1511 0 $arg =~ s/\\i\b/i/g;
1512 0 $arg =~ s/\{ \\ (?:$Acc_char|$Acc_let[ ]) [ ]* ([a-z]+) [ ]*
1513 (?:\}|\Z)/$1/ogix;
1514 0 $arg =~ s/\\ (?:$Acc_char|$Acc_let) [ ]* \{ [ ]* ([a-z]+) [ ]*
1515 (?:\}|\Z)/$1/ogix;
1516 0 $arg =~ s/($Acc_sym)/(my $v = $1) =~ tr!a-zA-Z!!cd; $v/goxe;
1517 # Remove \latex[options]{commands}
1518 0 ▶ 1 while $arg =~ s/\\([^\{\[]+)//g;
Branch: 1 branch
BranchTrueFalseTotal
$arg =~ s/\\([^\{\[]+)//g and do { '???' } 0 0 0
1519 # Remove braces around only alphanum
1520 0 ▶ 1 while $arg =~ s/\{([a-z0-9 ]+)\}/$1/gi;
Branch: 1 branch
BranchTrueFalseTotal
$arg =~ s/\{([a-z0-9 ]+)\}/$1/gi and do { '???' } 0 0 0
1521 0 $arg =~ s/\{[^{}]*\}//g; # Remove all other braces
1522 0 $arg =~ tr/a-zA-Z0-9 ~//cd;
1523 0 $arg =~ tr/~/ /;
1524 0 $arg =~ s/\s+/ /g;
1525 0 $self->_push(qq("$arg"));
1526 }
1527  
1528 0 ▶sub _function_stack {
Subroutine _function_stack: not called CC=2 SCAR=17.9
1529 0 my ($self, $token) = @_;
1530  
1531 0 ▶ while (@{$self->{stack}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"stack"};}) 0 0 0
1532 0 my $val = $self->_pop;
1533 0 $self->_warning($self->_format_token($val), 1);
1534 }
1535 }
1536  
1537 0 ▶sub _function_substring {
Subroutine _function_substring: not called CC=5 SCAR=34.0
1538 0 my ($self, $token) = @_;
1539  
1540 0 my $len = $self->_pop(i => $token, 1);
1541 0 my $start = $self->_pop(i => $token);
1542 0 my $string = $self->_pop(s => $token);
1543 0 ▶ return $self->_push('""') if _check_type_warnings();
Branch: 1 branch
BranchTrueFalseTotal
if _check_type_warnings() 0 0 0
1544 0 ▶ if ($start == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start == 0) 0 0 0
1545 0 $self->_warning("Argument 2 to '$token' cannot be 0");
1546 0 return $self->_push('""');
1547 }
1548 0 $string = _trim_string($string);
1549 0 my $str_len = length $string;
1550 0 ▶ $start = $start > 0 ? $start - 1 : $str_len + $start - $len + 1;
Branch: 1 branch
BranchTrueFalseTotal
$start > 0 ? : 0 0 0
1551 0 ▶ if ($start < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start < 0) 0 0 0
1552 0 $len += $start;
1553 0 $start = 0;
1554 }
1555 0 $self->_push(sprintf '"%s"', substr($string, $start, $len));
1556 }
1557  
1558 0 ▶sub _function_swap {
Subroutine _function_swap: not called CC=1 SCAR=6.9
1559 0 my ($self, $token) = @_;
1560  
1561 0 my ($arg1, $arg2) = ($self->_pop, $self->_pop);
1562 0 $self->_push($arg1, $arg2);
1563 }
1564  
1565 0 ▶sub _function_text_length {
Subroutine _function_text_length: not called CC=2 SCAR=17.9
1566 0 my ($self, $token) = @_;
1567  
1568 0 my $arg = $self->_pop(s => $token, 1);
1569 0 ▶ return $self->_push(0) if _check_type_warnings();
Branch: 1 branch
BranchTrueFalseTotal
if _check_type_warnings() 0 0 0
1570 0 $arg = _trim_string($arg);
1571 # Count the accents
1572 0 my $length =
1573 $arg =~ s/\{ \\ (?:$Acc_char|$Acc_let[ ]) [ ]* [a-z]+ [ ]*
1574 (?:\}|\Z)//ogix;
1575 0 $length +=
1576 $arg =~ s/\\ (?:$Acc_char|$Acc_let) [ ]* \{ [ ]* [a-z]+ [ ]*
1577 (?:\}|\Z)//ogix;
1578 0 $length +=
1579 $arg =~ s/$Acc_sym//gox;
1580 # Remove any remaining braces
1581 0 $arg =~ s/[{}]//g;
1582 # Count whatever's left
1583 0 $length += length $arg;
1584 0 $self->_push($length);
1585 }
1586  
1587 0 ▶sub _function_text_prefix {
Subroutine _function_text_prefix: not called CC=10 SCAR=47.0
1588 0 my ($self, $token) = @_;
1589  
1590 0 my $len = $self->_pop(i => $token, 1);
1591 0 my $str = $self->_pop(s => $token);
1592 0 ▶ return $self->_push('""') if _check_type_warnings();
Branch: 1 branch
BranchTrueFalseTotal
if _check_type_warnings() 0 0 0
1593 0 my $answer = '';
1594 0 $_ = _trim_string($str);
1595 0 ▶ while ($_ && $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ and $len) 0 0 0
Condition0%0/3
$_ and $len
!ll&&!rl&&r
000
MC/DC0%0/2
$_ and $len
$_ $len
Truth table0%0/3
A and B
A= $_B= $len
ABresult
0X0
100
111
1596 0 ▶ if (s/^(\{ \\ (?:$Acc_char|$Acc_let[ ]) [ ]* [a-z]+ [ ]*
Branch: 2 branches
BranchTrueFalseTotal
if (s/^(\{ \\ (?:$Acc_char|${Acc_let}[ ]) [ ]* [a-z]+ [ ]* (?:\}|\Z))//iox or s/^(\\ (?:$Acc_char|$Acc_let) [ ]* \{ [ ]* [a-z]+ [ ]* (?:\}|\Z))//iox or s/^($Acc_sym)//o or s/^([^{}])//) { } 0 0 0
elsif (/^(\{)/) { } 0 0 0
Condition0%0/9
s/^(\{ \\ (?:$Acc_char|${Acc_let}[ ]) [ ]* [a-z]+ [ ]* (?:\}|\Z))//iox or s/^(\\ (?:$Acc_char|$Acc_let) [ ]* \{ [ ]* [a-z]+ [ ]* (?:\}|\Z))//iox or s/^($Acc_sym)//o or s/^([^{}])//
l!l&&r!l&&!r
000
s/^(\{ \\ (?:$Acc_char|${Acc_let}[ ]) [ ]* [a-z]+ [ ]* (?:\}|\Z))//iox or s/^(\\ (?:$Acc_char|$Acc_let) [ ]* \{ [ ]* [a-z]+ [ ]* (?:\}|\Z))//iox or s/^($Acc_sym)//o
l!l&&r!l&&!r
000
s/^(\{ \\ (?:$Acc_char|${Acc_let}[ ]) [ ]* [a-z]+ [ ]* (?:\}|\Z))//iox or s/^(\\ (?:$Acc_char|$Acc_let) [ ]* \{ [ ]* [a-z]+ [ ]* (?:\}|\Z))//iox
l!l&&r!l&&!r
000
MC/DC0%0/4
s/^(\{ \\ (?:$Acc_char|${Acc_let}[ ]) [ ]* [a-z]+ [ ]* (?:\}|\Z))//iox or s/^(\\ (?:$Acc_char|$Acc_let) [ ]* \{ [ ]* [a-z]+ [ ]* (?:\}|\Z))//iox or s/^($Acc_sym)//o or s/^([^{}])//
s/^(\{ \\ (?:$Acc_char|${Acc_let}[ ]) [ ]* [a-z]+ [ ]* (?:\}|\Z))//iox s/^(\\ (?:$Acc_char|$Acc_let) [ ]* \{ [ ]* [a-z]+ [ ]* (?:\}|\Z))//iox s/^($Acc_sym)//o s/^([^{}])//
Truth table0%0/5
A or B or C or D
A= s/^(\{ \\ (?:$Acc_char|${Acc_let}[ ]) [ ]* [a-z]+ [ ]* (?:\}|\Z))//ioxB= s/^(\\ (?:$Acc_char|$Acc_let) [ ]* \{ [ ]* [a-z]+ [ ]* (?:\}|\Z))//ioxC= s/^($Acc_sym)//oD= s/^([^{}])//
ABCDresult
1XXX1
01XX1
001X1
00011
00000
1597 (?:\}|\Z))//oix ||
1598 s/^(\\ (?:$Acc_char|$Acc_let) [ ]* \{ [ ]* [a-z]+ [ ]*
1599 (?:\}|\Z))//oix ||
1600 s/^($Acc_sym)//o ||
1601 s/^([^{}])//) {
1602 0 $answer .= $1;
1603 0 $len--;
1604 }
1605 elsif (/^(\{)/) {
1606 0 my $brace;
1607 0 ($brace, $_) = _remove_matched_brace($_);
1608 0 $answer .= $brace;
1609 0 $len--;
1610 }
1611 else {
1612 0 s/^[{}]//;
1613 }
1614 }
1615 0 ▶ $answer .= '}' while $answer =~ tr/{// > $answer =~ tr/}//;
Branch: 1 branch
BranchTrueFalseTotal
$answer =~ tr/{// > $answer =~ tr/}// and do { $answer .= '}' } 0 0 0
1616 0 $self->_push(qq("$answer"));
1617 }
1618  
1619 { # Closure
1620 4 my %Char_widths =
1621 ( 0040 => 278, 0041 => 278, 0042 => 500, 0043 => 833, 0044 => 500,
1622 0045 => 833, 0046 => 778, 0047 => 278, 0050 => 389, 0051 => 389,
1623 0052 => 500, 0053 => 778, 0054 => 278, 0055 => 333, 0056 => 278,
1624 0057 => 500, 0060 => 500, 0061 => 500, 0062 => 500, 0063 => 500,
1625 0064 => 500, 0065 => 500, 0066 => 500, 0067 => 500, 0070 => 500,
1626 0071 => 500, 0072 => 278, 0073 => 278, 0074 => 278, 0075 => 778,
1627 0076 => 472, 0077 => 472, 0100 => 778,
1628  
1629 # A-Z
1630 0101 => 750, 0102 => 708, 0103 => 722, 0104 => 764, 0105 => 681,
1631 0106 => 653, 0107 => 785, 0110 => 750, 0111 => 361, 0112 => 514,
1632 0113 => 778, 0114 => 625, 0115 => 917, 0116 => 750, 0117 => 778,
1633 0120 => 681, 0121 => 778, 0122 => 736, 0123 => 556, 0124 => 722,
1634 0125 => 750, 0126 => 750, 0127 => 1028, 0130 => 750, 0131 => 750,
1635 0132 => 611,
1636  
1637 0133 => 278, 0134 => 500, 0135 => 278, 0136 => 500, 0137 => 278,
1638 0140 => 278,
1639  
1640 # a-z
1641 0141 => 500, 0142 => 556, 0143 => 444, 0144 => 556, 0145 => 444,
1642 0146 => 306, 0147 => 500, 0150 => 556, 0151 => 278, 0152 => 306,
1643 0153 => 528, 0154 => 278, 0155 => 833, 0156 => 556, 0157 => 500,
1644 0160 => 556, 0161 => 528, 0162 => 392, 0163 => 394, 0164 => 389,
1645 0165 => 556, 0166 => 528, 0167 => 722, 0170 => 528, 0171 => 528,
1646 0172 => 444, 0173 => 500, 0174 => 1000, 0175 => 500, 0176 => 500,
1647  
1648 aa => 500, AA => 750, o => 500, O => 778, l => 278, L => 625,
1649 ss => 500, ae => 722, oe => 778, AE => 903, OE => 1014, '?`' => 472,
1650 '!`' => 278,
1651  
1652 );
1653  
1654 0 ▶sub _function_width {
Subroutine _function_width: not called CC=11 SCAR=48.8
1655 0 my ($self, $token) = @_;
1656  
1657 0 my $arg = _trim_string($self->_pop(s => $token, 1));
1658 0 ▶ return $self->_push(0) if _check_type_warnings();
Branch: 1 branch
BranchTrueFalseTotal
if _check_type_warnings() 0 0 0
1659  
1660 # Approximate most special characters with their base character
1661 0 my $width = 0;
1662 0 ▶ while ($arg =~
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ s/^(?:(?:\{ \\ (?:$Acc_char|${Acc_let}[ ]) [ ]* ([a-zA-Z]+) [ ]* | \\ (?:$Acc_char|$Acc_let) [ ]* \{ [ ]* ([a-zA-Z]+) [ ]* | \{ ($Acc_sym) ) (?:\}|\Z) | ($Acc_sym) | (.))/($1 || "") . ($2 || "");/eox) 0 0 0
1663 s/^(?:(?:\{ \\ (?:$Acc_char|$Acc_let[ ]) [ ]* ([a-zA-Z]+) [ ]* |
1664 \\ (?:$Acc_char|$Acc_let) [ ]* \{ [ ]* ([a-zA-Z]+) [ ]* |
1665 \{ ($Acc_sym) ) (?:\}|\Z) |
1666 ($Acc_sym) |
1667 0 ▶ (.))/($1 || '').($2 || '')/exo) {
Condition0%0/4
$1 || ''
l!l
00
$2 || ''
l!l
00
MC/DC0%0/1
$1 || ''
$1
MC/DC0%0/1
$2 || ''
$2
Truth table0%0/2
A
A= $1
Aresult
11
00
Truth table0%0/2
A
A= $2
Aresult
11
00
1668 0 ▶ my ($symbol, $letter) = ($3 || $4, $5);
Condition0%0/3
$3 || $4
l!l&&r!l&&!r
000
MC/DC0%0/2
$3 || $4
$3 $4
Truth table0%0/3
A || B
A= $3B= $4
ABresult
1X1
011
000
1669 0 ▶ $width += $Char_widths{ord $letter} || 0 if defined $letter;
Branch: 1 branch
BranchTrueFalseTotal
if defined $letter 0 0 0
Condition0%0/2
$Char_widths{ord $letter} || 0
l!l
00
MC/DC0%0/1
$Char_widths{ord $letter} || 0
$Char_widths{ord $letter}
Truth table0%0/2
A
A= $Char_widths{ord $letter}
Aresult
11
00
1670 0 ▶ $width += $Char_widths{$symbol} || 0
Branch: 1 branch
BranchTrueFalseTotal
if defined $symbol and $symbol =~ s/^\\?// 0 0 0
Condition0%0/5
defined $symbol and $symbol =~ s/^\\?//
!ll&&!rl&&r
000
$Char_widths{$symbol} || 0
l!l
00
MC/DC0%0/2
defined $symbol and $symbol =~ s/^\\?//
defined $symbol $symbol =~ s/^\\?//
MC/DC0%0/1
$Char_widths{$symbol} || 0
$Char_widths{$symbol}
Truth table0%0/3
A and B
A= defined $symbolB= $symbol =~ s/^\\?//
ABresult
0X0
100
111
Truth table0%0/2
A
A= $Char_widths{$symbol}
Aresult
11
00
1671 if defined $symbol && $symbol =~ s/^\\?//;
1672 }
1673 0 $self->_push($width);
1674 }
1675 }
1676 4 }
1677  
1678 0 ▶sub _function_top {
Subroutine _function_top: not called CC=1 SCAR=6.9
1679 0 my ($self, $token) = @_;
1680  
1681 0 my $arg = $self->_pop;
1682 0 $self->_warning($self->_format_token($arg), 1);
1683 }
1684  
1685 0 ▶sub _function_type {
Subroutine _function_type: not called CC=2 SCAR=17.9
1686 0 my ($self, $token) = @_;
1687  
1688 0 ▶ return $self->_push('""') unless my $bt_entry = $self->{bibtex}{bt_entry};
Branch: 1 branch
BranchTrueFalseTotal
unless my $bt_entry = $self->{"bibtex"}{"bt_entry"} 0 0 0
1689 0 $self->_push('"' . (lc $bt_entry->type) . '"');
1690 }
1691  
1692 0 ▶sub _function_warning {
Subroutine _function_warning: not called CC=2 SCAR=17.9
1693 0 my ($self, $token) = @_;
1694  
1695 0 my $arg = $self->_pop(s => $token, 1);
1696 0 ▶ return if _check_type_warnings();
Branch: 1 branch
BranchTrueFalseTotal
if _check_type_warnings() 0 0 0
1697 0 $arg = _trim_string($arg);
1698 0 $self->_warning("Warning--$arg", 1);
1699 }
1700  
1701 1 ▶sub _function_while {
Subroutine _function_while: called CC=5 SCAR=18.1
1702 1 my ($self, $token) = @_;
1703  
1704 1 my $do = $self->_pop(qx => $token, 1);
1705 1 my $cont = $self->_pop(qx => $token);
1706 1 ▶ return if _check_type_warnings();
Branch: 1 branch
BranchTrueFalseTotal
if _check_type_warnings() 0 1 1
1707 1 my $val;
1708 1 ▶ while ($self->_evaluate($cont) && ($val = $self->_pop(i => $token)) &&
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_evaluate($cont) and $val = $self->_pop("i", $token) and not _check_type_warnings()) 1 0 1
Condition33%2/6
$self->_evaluate($cont) and $val = $self->_pop("i", $token) and not _check_type_warnings()
!ll&&!rl&&r
001
$self->_evaluate($cont) and $val = $self->_pop("i", $token)
!ll&&!rl&&r
001
MC/DC0%0/3
$self->_evaluate($cont) and $val = $self->_pop("i", $token) and not _check_type_warnings()
$self->_evaluate($cont) $val = $self->_pop("i", $token) not _check_type_warnings()
Truth table25%1/4
A and B and not(C)
A= $self->_evaluate($cont)B= $val = $self->_pop("i", $token)C= not _check_type_warnings()
ABCresult
0XX0
10X0
1100
1111
1709 ! _check_type_warnings()) {
1710 1 $self->_evaluate($do);
1711 }
1712 }
1713  
1714 0 ▶sub _function_write {
Subroutine _function_write: not called CC=2 SCAR=17.9
1715 0 my ($self, $token) = @_;
1716  
1717 0 my $out = $self->_pop(s => $token, 1);
1718 0 ▶ if (! _check_type_warnings()) {
Branch: 1 branch
BranchTrueFalseTotal
if (not _check_type_warnings()) 0 0 0
1719 0 $out = _trim_string($out);
1720 0 push @{$self->{output}}, $out;
1721 }
1722 }
1723  
1724 # Pops the top element from the stack, possibly doing type checking.
1725 # If one argument is present, all must be
1726 # Arguments: optional (type(s), token, first_arg)
1727 # The types are a concatenation of
1728 # i: integer
1729 # q: quoted value (e.g., 'a)
1730 # s: string
1731 # x: expression (array ref)
1732 # See the description of _check_type for the optional arguments
1733 123 ▶sub _pop : method {
Subroutine _pop: called CC=4 SCAR=13.9
1734 123 my ($self, $types, $token, $first_arg) = @_;
1735
1736 123 ▶ my $where = defined $token ? " on token '$token'" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $token ? : 119 4 123
1737 123 ▶ return $self->_warning("Stack underflow$where") unless @{$self->{stack}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"stack"};} 0 123 123
1738 123 my $val = pop @{$self->{stack}};
1739 123 ▶ $self->_check_type($val, $types, $token, $first_arg) if $types;
Branch: 1 branch
BranchTrueFalseTotal
if $types 119 4 123
1740 123 return $val;
1741 }
1742  
1743 # Pushes some elements on the stack
1744 189 ▶sub _push : method {
Subroutine _push: called CC=1 SCAR=0.0
1745 189 my $self = shift;
1746  
1747 189 push @{$self->{stack}}, @_;
1748 }
1749  
1750 # Removes the outer "s from a string
1751 # Arguments: string to trim
1752 # Returns: trimmed string
1753 55 ▶sub _trim_string {
Subroutine _trim_string: called CC=1 SCAR=0.0
1754 55 my ($str) = @_;
1755  
1756 55 $str =~ s/^\"(.*)\"$/$1/s;
1757 55 return $str;
1758 }
1759  
1760  
1761 # Routines to protect and unprotect strings within braces
1762 {
1763 4 my %Brackets;
1764 4 my $Bracket_cnt = 0;
1765  
1766 # Protects strings within matching { } pairs
1767 # Arguments
1768 4 ▶sub _protect {
Subroutine _protect: called CC=2 SCAR=6.9
1769 4 my ($s) = @_;
1770  
1771 4 ▶ while ($s =~ s/\{ ([^{}]*) \}/
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ s/\{ ([^{}]*) \}/my $v = sprintf("PROTECT(0D%d)", $Bracket_cnt++); $Brackets{$v} = $1; $v;/ex) 13 4 17
1772 13 my $v = sprintf 'PROTECT(0D%d)', $Bracket_cnt++;
1773 13 $Brackets{$v} = $1; $v/ex) { }
1774 4 return $s;
1775 }
1776  
1777 # Removes a leading left brace to its matching right brace
1778 # Arguments: string
1779 # Returns: removed brace, remaining string
1780 912 ▶sub _remove_matched_brace {
Subroutine _remove_matched_brace: called CC=6 SCAR=18.4
1781 912 my ($str) = @_;
1782  
1783 912 ▶ return ('', $str) unless $str =~ s/^(\{)//;
Branch: 1 branch
BranchTrueFalseTotal
unless $str =~ s/^(\{)// 0 912 912
1784 # Find the matching brace
1785 912 my $nest = 1;
1786 912 my @char = ($1);
1787 912 ▶ 1 while $str =~ s/^(.*?)([{}])/do {
Branch: 1 branch
BranchTrueFalseTotal
$str =~ s/^(.*?)([{}])/do { $nest += $2 eq "{" ? 1 : -1; push @char, $1, $2; "" };/es and $nest > 0 and do { '???' } 124 912 1036
Condition66%2/3
$str =~ s/^(.*?)([{}])/do { $nest += $2 eq "{" ? 1 : -1; push @char, $1, $2; "" };/es and $nest > 0
!ll&&!rl&&r
0912124
MC/DC50%1/2
$str =~ s/^(.*?)([{}])/do { $nest += $2 eq "{" ? 1 : -1; push @char, $1, $2; "" };/es and $nest > 0
$str =~ s/^(.*?)([{}])/do { $nest += $2 eq "{" ? 1 : -1; push @char, $1, $2; "" };/es $nest > 0
Truth table66%2/3
A and B
A= $str =~ s/^(.*?)([{}])/do { $nest += $2 eq "{" ? 1 : -1; push @char, $1, $2; "" };/esB= $nest > 0
ABresult
0X0
100
111
1788 1036 ▶ $nest += $2 eq '{' ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
$2 eq '{' ? : 62 974 1036
1789 1036 push @char, $1, $2;
1790 1036 '' } /es && $nest > 0;
1791 912 ▶ if ($nest > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nest > 0) 0 912 912
1792 0 push @char, $str;
1793 0 $str = '';
1794 }
1795 912 return (join('', @char), $str);
1796 }
1797  
1798 ################# Routines to support output translation
1799  
1800 { # Closure
1801  
1802 4 my %Accents =
1803 ( "'" => {
1804 A => 193, # capital A, acute accent
1805 a => 225, # small a, acute accent
1806 C => 0x106,
1807 c => 0x107,
1808 E => 201, # capital E, acute accent
1809 e => 233, # small e, acute accent
1810 I => 205, # capital I, acute accent
1811 i => 237, # small i, acute accent
1812 L => 0x139,
1813 l => 0x13a,
1814 N => 0x143,
1815 n => 0x144,
1816 O => 211, # capital O, acute accent
1817 o => 243, # small o, acute accent
1818 R => 0x154,
1819 r => 0x155,
1820 S => 0x15a,
1821 s => 0x15b,
1822 U => 218, # capital U, acute accent
1823 u => 250, # small u, acute accent
1824 Y => 221, # capital Y, acute accent
1825 y => 253, # small y, acute accent
1826 Z => 0x179,
1827 z => 0x17a,
1828 },
1829 '`' => {
1830 A => 192, # capital A, grave accent
1831 a => 224, # small a, grave accent
1832 E => 200, # capital E, grave accent
1833 e => 232, # small e, grave accent
1834 I => 204, # capital I, grave accent
1835 i => 236, # small i, grave accent
1836 O => 210, # capital O, grave accent
1837 o => 242, # small o, grave accent
1838 U => 217, # capital U, grave accent
1839 u => 249, # small u, grave accent
1840 },
1841 '^' => {
1842 A => 194, # capital A, circumflex accent
1843 a => 226, # small a, circumflex accent
1844 E => 202, # capital E, circumflex accent
1845 e => 234, # small e, circumflex accent
1846 G => 0x11c,
1847 g => 0x11d,
1848 H => 0x124,
1849 h => 0x125,
1850 I => 206, # capital I, circumflex accent
1851 i => 238, # small i, circumflex accent
1852 J => 0x134,
1853 j => 0x135,
1854 O => 212, # capital O, circumflex accent
1855 o => 244, # small o, circumflex accent
1856 S => 0x15c,
1857 s => 0x15d,
1858 U => 219, # capital U, circumflex accent
1859 u => 251, # small u, circumflex accent
1860 W => 0x174,
1861 w => 0x175,
1862 Y => 0x176,
1863 y => 0x177,
1864 },
1865 '"' => {
1866 A => 196, # capital A, dieresis or umlaut mark
1867 E => 203, # capital E, dieresis or umlaut mark
1868 I => 207, # capital I, dieresis or umlaut mark
1869 O => 214, # capital O, dieresis or umlaut mark
1870 U => 220, # capital U, dieresis or umlaut mark
1871 Y => 376,
1872 a => 228, # small a, dieresis or umlaut mark
1873 e => 235, # small e, dieresis or umlaut mark
1874 i => 239, # small i, dieresis or umlaut mark
1875 o => 246, # small o, dieresis or umlaut mark
1876 u => 252, # small u, dieresis or umlaut mark
1877 y => 255, # small y, dieresis or umlaut mark
1878 },
1879 '~' => {
1880 A => 195, # capital A, tilde
1881 a => 227, # small a, tilde
1882 I => 0x128,
1883 i => 0x129,
1884 N => 209, # capital N, tilde
1885 n => 241, # small n, tilde
1886 O => 213, # capital O, tilde
1887 o => 245, # small o, tilde
1888 U => 0x168,
1889 u => 0x169,
1890 },
1891 '=' => {
1892 A => 0x100,
1893 a => 0x101,
1894 E => 0x112,
1895 e => 0x113,
1896 I => 0x12a,
1897 i => 0x12b,
1898 O => 0x14c,
1899 o => 0x14d,
1900 U => 0x16a,
1901 u => 0x16b,
1902 },
1903 '.' => {
1904 C => 0x10a,
1905 c => 0x10b,
1906 E => 0x116,
1907 e => 0x117,
1908 G => 0x120,
1909 g => 0x121,
1910 L => 0x13f,
1911 l => 0x140,
1912 Z => 0x17b,
1913 z => 0x17c,
1914 },
1915 c => {
1916 A => 0x104,
1917 a => 0x105,
1918 C => 199, # capital C, cedilla
1919 c => 231, # small c, cedilla
1920 E => 0x118,
1921 e => 0x119,
1922 G => 0x122,
1923 g => 0x123,
1924 I => 0x12e,
1925 i => 0x12f,
1926 K => 0x136,
1927 k => 0x137,
1928 L => 0x13b,
1929 l => 0x13c,
1930 N => 0x145,
1931 n => 0x146,
1932 R => 0x156,
1933 r => 0x157,
1934 S => 0x15e,
1935 s => 0x15f,
1936 T => 0x162,
1937 t => 0x163,
1938 U => 0x172,
1939 u => 0x173,
1940 },
1941 H => {
1942 O => 0x150,
1943 o => 0x151,
1944 U => 0x170,
1945 u => 0x171,
1946 },
1947 u => {
1948 A => 0x102,
1949 a => 0x103,
1950 E => 0x114,
1951 e => 0x115,
1952 G => 0x11e,
1953 g => 0x11f,
1954 I => 0x12c,
1955 i => 0x12d,
1956 O => 0x14e,
1957 o => 0x14f,
1958 U => 0x16c,
1959 u => 0x16d,
1960 },
1961 v => {
1962 C => 0x10c,
1963 c => 0x10d,
1964 D => 0x10e,
1965 d => 0x10f,
1966 E => 0x11a,
1967 e => 0x11b,
1968 H => 0x124,
1969 h => 0x125,
1970 J => 0x134,
1971 j => 0x135,
1972 L => 0x13d,
1973 l => 0x13e,
1974 N => 0x147,
1975 n => 0x148,
1976 R => 0x158,
1977 r => 0x159,
1978 S => 0x160,
1979 s => 0x161,
1980 T => 0x164,
1981 t => 0x165,
1982 Z => 0x17d,
1983 z => 0x17e,
1984 }
1985 );
1986  
1987 4 my %Chars =
1988 (
1989 '\AA' => 197, # capital A, ring
1990 '\aa' => 229, # small a, ring
1991 '\AE' => 198, # capital AE diphthong (ligature)
1992 '\ae' => 230, # small ae diphthong (ligature)
1993 '\copyright'=> 0xa9,
1994 '\L' => 0x141,
1995 '\l' => 0x142,
1996 '\O' => 216, # capital O, slash
1997 '\o' => 248, # small o, slash
1998 '\OE' => 0x152,
1999 '\oe' => 0x153,
2000 '\P' => 0xb6,
2001 '\pounds' => 0xa3,
2002 '\S' => 0xa7,
2003 '\ss' => 223, # small sharp s, German (sz ligature)
2004 '\dag' => 0x2020,
2005 '\ddag' => 0x2021,
2006 '!`' => 0xa1,
2007 '?`' => 0xbf,
2008 '``' => 0x201c,
2009 "''" => 0x201d,
2010 '~' => 160, # non breaking space
2011 '--' => 0x2013,
2012 '---' => 0x2014,
2013 );
2014  
2015 # Returns the result of calling the character option subroutine for
2016 # a special character
2017 # Arguments: bst object, Latex string, characters to accent, optional accent
2018 804 ▶sub _character {
Subroutine _character: called CC=3 SCAR=11.0
2019 804 my ($bst, $opts, $latex, $chars, $accent) = @_;
2020 804 ▶ my $unicode = $accent ? $Accents{$accent}{$chars} : $Chars{$chars};
Branch: 1 branch
BranchTrueFalseTotal
$accent ? : 104 700 804
2021 804 ▶ $unicode = chr($unicode) if defined $unicode;
Branch: 1 branch
BranchTrueFalseTotal
if defined $unicode 750 54 804
2022 804 &{$opts->{character}}($bst, $latex, $unicode, $chars, $accent);
2023 }
2024 }
2025  
2026 # Takes a string, removes all \newcommand commands from it, and substitutes
2027 # for all uses of those \newcommand commands.
2028 # Arguments: string
2029 # Returns: substituted string
2030 4 ▶sub _substitute_newcommand {
Subroutine _substitute_newcommand: called CC=6 SCAR=17.9
2031 46 my ($str) = @_;
2032  
2033 46 my %command;
2034 46 ▶ 1 while $str =~ s/\\newcommand\{ \\([a-z]+) \} \[ (\d+) \](\{.*)/do {
Branch: 1 branch
BranchTrueFalseTotal
$str =~ s<\\newcommand\{ \\([a-z]+) \} \[ (\d+) \](\{.*)>[do { () = ($1, $2, $3); ($def, $next) = _remove_matched_brace($next); $def =~ s/^\{(.*)\}$/sub { qq(\Q$1\E) }/s; $def =~ s/\\\#(\d+)/\$_[@{[$1 - 1];}]/g; my $sub = eval "$def"; die "Internal error: $@" if $@; $command{$cmd} = {"args", $args, "def", $def, "code", $sub}; $next };]eisx and do { '???' } 40 46 86
2035 40 my ($cmd, $args, $next, $def) = ($1, $2, $3);
2036 40 ($def, $next) = _remove_matched_brace($next);
2037 40 $def =~ s!^\{(.*)\}$!sub { qq(\Q$1\E) }!s;
2038 40 $def =~ s!\\\#(\d+)!\$_[@{[$1-1]}]!g;
2039 40 my $sub = eval("$def");
2040 40 ▶ die "Internal error: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 40 40
2041 40 $command{$cmd} = { args => $args, def => $def, code => $sub };
2042 40 $next;
2043 }/sexi;
2044  
2045 46 ▶ if (%command) {
Branch: 1 branch
BranchTrueFalseTotal
if (%command) 10 36 46
2046 10 my $cmd_re = join '|', keys %command;
2047 10 ▶ 1 while $str =~ s/\\($cmd_re)(\{.*)/do {
Branch: 1 branch
BranchTrueFalseTotal
$str =~ s[\\($cmd_re)(\{.*)][do { () = ($1, $2); my @args; for (my $i = 0; $i < $command{$cmd}{"args"}; ++$i) { () = _remove_matched_brace($next); $arg =~ s/^\{(.*)\}$/$1/s; push @args, $arg; $next = $next_; } ; &{$command{$cmd}{"code"};}(@args) . $next };]es and do { '???' } 98 10 108
2048 98 my ($cmd, $next) = ($1, $2);
2049 98 my @args;
2050 98 ▶ for (my $i=0; $i < $command{$cmd}{args}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $command{$cmd}{"args"}) 110 98 208
2051 110 my ($arg, $next_) = _remove_matched_brace($next);
2052 110 $arg =~ s!^\{(.*)\}$!$1!s;
2053 110 push @args, $arg;
2054 110 $next = $next_;
2055 }
2056 98 &{$command{$cmd}{code}}(@args) . $next;
2057 }/se;
2058 }
2059  
2060 46 return $str;
2061 }
2062  
2063 16 ▶sub _unprotect {
Subroutine _unprotect: called CC=3 SCAR=11.0
2064 16 my ($s) = @_;
2065  
2066 16 ▶ return $s unless %Brackets;
Branch: 1 branch
BranchTrueFalseTotal
unless %Brackets 0 16 16
2067 16 my $brack_re = join '|', map("\Q$_", keys %Brackets);
2068 16 ▶ while ($s =~ s/($brack_re)/{$Brackets{uc $1}}/igx) { }
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ s/($brack_re)/{$Brackets{uc $1}}/gix) 10 15 25
2069 15 return $s;
2070 }
2071 }
2072  
2073 # Reports a warning
2074 # Arguments: warning string, lineno_suppression
2075 # Rturns: 1
2076 4 ▶sub _warning : method {
Subroutine _warning: called CC=2 SCAR=7.0
2077 20 my ($self, $warning, $no_lineno) = @_;
2078  
2079 20 ▶ my $lineno = $no_lineno ? '' : "$self->{lineno}: ";
Branch: 1 branch
BranchTrueFalseTotal
$no_lineno ? : 0 20 20
2080 20 my $warn = "$lineno$warning";
2081 20 push @{$self->{warnings}}, "$warn\n";
2082 20 carp $warn;
2083 20 return 1;
2084 }
2085  
2086 4 1;
↑ Summary