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