blib/lib/Biblio/EndnoteStyle.pm

filter: Statementstmt 80.0% 80 / 100 Branchbran 64.2% 27 / 42 Conditioncond 50.0% 6 / 12 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 75.0% 12 / 16 Podpod 100% 3 / 3 totaltotal 71.6% 129 / 180 CC 28 SCAR 20.1
CC
28
Cov
73%
format
14.7
typename
12.0
new
6.0
CRAP 7.5
↑ Summary
1 # $Id: EndnoteStyle.pm,v 1.12 2016/11/28 22:18:22 mike Exp $
2  
3 package Biblio::EndnoteStyle;
4  
5 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = 0.06;
10  
11 =encoding utf8
12  
13 =head1 NAME
14  
15 Biblio::EndnoteStyle - reference formatting using Endnote-like templates
16  
17 =head1 SYNOPSIS
18  
19 use Biblio::EndnoteStyle;
20 $style = new Biblio::EndnoteStyle();
21 ($text, $errmsg) = $style->format($template, \%fields);
22  
23 =head1 DESCRIPTION
24  
25 This small module provides a way of formatting bibliographic
26 references using style templates similar to those used by the popular
27 reference management software Endnote (http://www.endnote.com/). The
28 API is embarrassingly simple: a formatter object is made using the
29 class's constructor, the C<new()> method; C<format()> may then be
30 repeatedly called on this object, using the same or different
31 templates.
32  
33 (The sole purpose of the object is to cache compiled templates so that
34 multiple C<format()> invocations are more efficient than they would
35 otherwise be. Apart from that, the API might just as well have been a
36 single function.)
37  
38 =head1 METHODS
39  
40 =head2 new()
41  
42 $style = new Biblio::EndnoteStyle();
43  
44 Creates a new formatter object. Takes no arguments.
45  
46 =cut
47  
48 # The object is vacuous except that it knows its class, so that
49 # subclasses can be made that override some of the methods.
50 #
51 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
52 1 my $class = shift();
53  
54 1 return bless {
55 debug => 0,
56 compiled => {}, # cache of compiled templates
57 }, $class;
58 }
59  
60  
61 =head2 debug()
62  
63 $olddebug = $style->debug(1);
64  
65 Turns debugging on or off and returns the old debugging status. If an
66 argument is provided, then debugging is turned either on or off
67 according to whether then argument is true or false. In any case, the
68 old value of the debugging status is returned, so that a call with no
69 argument is a side-effect-free inquiry.
70  
71 When debugging is turned on, compiled templates are dumped to standard
72 error. It is not pretty.
73  
74 =cut
75  
76 0 ▶sub debug {
Subroutine debug: not called CC=2 SCAR=17.9
Pod documented
77 0 my $this = shift();
78 0 my($val) = @_;
79  
80 0 my $old = $this->{debug};
81 0 ▶ $this->{debug} = $val if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 0 0 0
82 0 return $old;
83 }
84  
85  
86 =head2 format()
87  
88 ($text, $errmsg) = $style->format($template, \%fields);
89  
90 Formats a reference, consisting of a hash of fields, according to an
91 Endnote-like template. The template is a string essentially the same
92 as those used in Endnote, as documented in the Endnote X User Guide at
93 http://www.endnote.com/support/helpdocs/EndNoteXWinManual.pdf
94 pages 390ff. In particular, pages 415-210 have details of the recipe
95 format. Because the templates used in this module are plain text, a
96 few special characters are used:
97  
98 =over 4
99  
100 =item ¬
101  
102 Link adjacent words. This is the "non-breaking space"
103 described on page 418 of the EndNote X
104  
105 =item |
106  
107 Forced Separation of elements that would otherwise be dependent.
108  
109 =item ^
110  
111 Separator for singular/plural aternatives.
112  
113 =cut `
114  
115 =item `
116  
117 Used to prevent literal text from being interpreted as a fieldname.
118  
119 =back
120  
121 The hash of fields is passed by reference: keys are fieldnames, and
122 the corresponding values are the data. PLEASE NOTE AN IMPORTANT
123 DIFFERENCE. Keys that do not appear in the hash at all are not
124 considered to be fields, so that if they appear in the template, they
125 will be interpreted as literal text; keys that appear in the hash but
126 whose values are undefined or empty are considered to be fields with
127 no value, and will be formatted as empty with dependent text omitted.
128 So for example:
129  
130 $style->format(";Author: ", { Author => "Taylor" }) eq ":Taylor: "
131 $style->format(";Author: ", { Author => "" }) eq ";"
132 $style->format(";Author: ", { xAuthor => "" }) eq ";Author: "
133  
134 C<format()> returns two values: the formatted reference and an
135 error-message. The error message is defined if and only if the
136 formatted reference is not.
137  
138 =cut
139  
140 9 ▶sub format {
Subroutine format: called CC=3 SCAR=11.0
Pod documented
141 9 my $this = shift();
142 9 my($text, $data) = @_;
143  
144 #use Data::Dumper; print Dumper($data);
145 9 my $template = $this->{compiled}->{$text};
146 9 ▶ if (!defined $template) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $template) 8 1 9
147 8 my $errmsg;
148 ($template, $errmsg) =
149 8 Biblio::EndnoteStyle::Template->new($text, $this->{debug});
150 8 ▶ return (undef, $errmsg) if !defined $template;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $template 0 8 8
151 #print "template '$text'\n", $template->render();
152 8 $this->{compiled}->{$text} = $template;
153 }
154  
155 9 return $template->format($data);
156 }
157  
158  
159 package Biblio::EndnoteStyle::Template;
160  
161 8 ▶sub new {
Subroutine new: called CC=4 SCAR=14.0
162 8 my $class = shift();
163 8 my($text, $debug) = @_;
164  
165 8 my @sequences;
166 8 ▶ while ($text ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($text ne "") 12 8 20
167 12 ▶ if ($text =~ s/^(\s*[^\s|]*\s?)//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ s/^(\s*[^\s|]*\s?)//) { } 12 0 12
168 12 my $sequence = $1;
169 12 my $obj = Biblio::EndnoteStyle::Sequence->new($sequence);
170 12 push @sequences, $obj;
171 12 $text =~ s/^\|//;
172 } else {
173 0 die "unparseable template fragment '$text'";
174 }
175 }
176  
177 8 my $this = bless {
178 text => $text,
179 sequences => \@sequences,
180 }, $class;
181 8 ▶ print STDERR $this->render() if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 8 8
182  
183 8 return $this;
184 }
185  
186 0 ▶sub render {
Subroutine render: not called CC=1 SCAR=6.9
187 0 my $this = shift();
188  
189 0 return join("", map { $_->render() . "\n" } @{ $this->{sequences} });
190 }
191  
192 9 ▶sub format {
Subroutine format: called CC=3 SCAR=11.0
193 9 my $this = shift();
194 9 my($data) = @_;
195  
196 9 my $result = "";
197 9 foreach my $sequence (@{ $this->{sequences} }) {
198 14 my($substr, $errmsg) = $sequence->format($data);
199 14 ▶ return (undef, $errmsg) if !defined $substr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $substr 0 14 14
200 14 $result .= $substr;
201 }
202  
203 9 return $result;
204 }
205  
206  
207 # ----------------------------------------------------------------------------
208  
209 package Biblio::EndnoteStyle::Sequence;
210  
211 15 ▶sub WORD { 290168 }
Subroutine WORD: called CC=1 SCAR=0.0
212 47 ▶sub LITERAL { 120368 }
Subroutine LITERAL: called CC=1 SCAR=0.0
213 0 ▶sub typename {
Subroutine typename: not called CC=3 SCAR=24.8
214 0 my($type) = @_;
215 0 ▶ return "WORD" if $type == WORD;
Branch: 1 branch
BranchTrueFalseTotal
if $type == WORD() 0 0 0
216 0 ▶ return "LITERAL" if $type == LITERAL;
Branch: 1 branch
BranchTrueFalseTotal
if $type == LITERAL() 0 0 0
217 0 return "???";
218 }
219  
220 12 ▶sub new {
Subroutine new: called CC=6 SCAR=17.9
221 12 my $class = shift();
222 12 my($text) = @_;
223  
224 1 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
225 12 ▶ confess("new($class) with text undefined") if !defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 12 12
226 12 my $tail = $text;
227 12 $tail =~ s/¬/ /g;
228 12 my @tokens;
229 12 ▶ while ($tail =~ s/(.*?)([``a-z_0-9]+)//i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tail =~ s/(.*?)([``a-z_0-9]+)//i) 8 12 20
230 8 my($head, $word) = ($1, $2);
231 8 ▶ push @tokens, [ LITERAL, $head ] if $head ne "";
Branch: 1 branch
BranchTrueFalseTotal
if $head ne "" 8 0 8
232 8 ▶ if ($word =~ s/^`(.*)`$/$1/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($word =~ s/^`(.*)`$/$1/) { } 1 7 8
233 1 push @tokens, [ LITERAL, $word ];
234 } else {
235 7 push @tokens, [ WORD, $word ];
236 }
237 }
238 12 ▶ push @tokens, [ LITERAL, $tail ] if $tail ne "";
Branch: 1 branch
BranchTrueFalseTotal
if $tail ne "" 11 1 12
239  
240 12 return bless {
241 text => $text,
242 tokens => \@tokens,
243 }, $class;
244 }
245  
246 0 ▶sub render {
Subroutine render: not called CC=1 SCAR=6.9
247 0 my $this = shift();
248  
249 return (sprintf("%24s: ", ("'" . $this->{text} . "'")) .
250 join(", ", map {
251 0 my($type, $val) = @$_;
252 0 typename($type) . " '$val'";
253 0 } @{ $this->{tokens} }));
254 }
255  
256 14 ▶sub format {
Subroutine format: called CC=13 SCAR=26.9
257 14 my $this = shift();
258 14 my($data) = @_;
259  
260 14 my $gotField = 0;
261 14 my $result = "";
262 14 foreach my $token (@{ $this->{tokens} }) {
263 27 my($type, $val) = @$token;
264 27 ▶ if ($type == LITERAL) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type == LITERAL()) { } 19 8 27
elsif ($type != WORD()) { } 0 8 8
265 19 $result .= $val;
266 } elsif ($type != WORD) {
267 0 die "unexpected token type '$type'";
268 } else {
269 8 my $dval = $data->{$val};
270 8 ▶ $dval = $data->{lc($val)} if !defined $dval;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dval 1 7 8
271 $dval = "" if !defined $dval && (exists $data->{$val} ||
272 8 ▶ exists $data->{lc($val)});
Branch: 1 branch
BranchTrueFalseTotal
if not defined $dval and exists $data->{$val} || exists $$data{lc $val} 0 8 8
Condition50%3/6
not defined $dval and exists $data->{$val} || exists $$data{lc $val}
!ll&&!rl&&r
710
exists $data->{$val} or exists $$data{lc $val}
l!l&&r!l&&!r
001
MC/DC0%0/3
not defined $dval and exists $data->{$val} || exists $$data{lc $val}
not defined $dval exists $data->{$val} exists $$data{lc $val}
Truth table50%2/4
not(A) and B or C
A= not defined $dvalB= exists $data->{$val}C= exists $$data{lc $val}
ABCresult
0XX0
1000
11X1
1011
273 8 ▶ if (!defined $dval) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $dval) { } 1 7 8
elsif (not $gotField and $dval eq '') { } 5 2 7
274 # The word is not a fieldname at all: treat as a literal
275 #print "!defined \$dval\n";
276 1 $result .= $val;
277 ▶ } elsif (!$gotField && $dval eq "") {
Condition66%2/3
not $gotField and $dval eq ""
!ll&&!rl&&r
025
MC/DC50%1/2
not $gotField and $dval eq ""
not $gotField $dval eq ""
Truth table66%2/3
not(A) and B
A= not $gotFieldB= $dval eq ""
ABresult
0X0
100
111
278 #print "\$dval is empty\n";
279 # Field is empty, so whole dependent sequence is omitted
280 5 return "";
281 } else {
282 #print "$dval eq '$dval'\n";
283 2 $gotField = 1;
284 # Loathesome but useful special case
285 2 ▶ $dval = "http://$dval" if $val eq "URL" && $dval !~ /^[a-z]+:/;
Branch: 1 branch
BranchTrueFalseTotal
if $val eq "URL" and not $dval =~ /^[a-z]+:/ 0 2 2
Condition33%1/3
$val eq "URL" and not $dval =~ /^[a-z]+:/
!ll&&!rl&&r
200
MC/DC0%0/2
$val eq "URL" and not $dval =~ /^[a-z]+:/
$val eq "URL" not $dval =~ /^[a-z]+:/
Truth table33%1/3
A and not(B)
A= $val eq "URL"B= not $dval =~ /^[a-z]+:/
ABresult
0X0
100
111
286 2 $result .= $dval;
287 }
288 }
289 }
290  
291 9 return $result;
292 }
293  
294  
295 =head1 AUTHOR
296  
297 Mike Taylor, E<lt>mike@miketaylor.org.ukE<gt>
298  
299 =head1 COPYRIGHT AND LICENCE
300  
301 Copyright (C) 2007 by Mike Taylor.
302  
303 This library is free software; you can redistribute it and/or modify
304 it under the same terms as Perl itself, either Perl version 5.8.4 or,
305 at your option, any later version of Perl 5 you may have available.
306  
307 =cut
308  
309  
310 1 1;
↑ Summary