Document/PageId.pm

filter: Statementstmt 8.4% 8 / 95 Branchbran 0.0% 0 / 40 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 12.5% 2 / 16 Podpod 100% 12 / 12 totaltotal 13.5% 22 / 163 CC 22 SCAR 39.4
CC
22
Cov
6%
parse
110.0
_init
56.0
_update
56.0
CRAP 51.6
« Document/PageIterator.pm ↑ Summary File/Spec/PublicationPage.pm »
1 #================================ PageId.pm ==================================
2 # Filename: PageId.pm
3 # Description: Page Identifier Class.
4 # Original Author: Dale Amon
5 # Revised by: $Author: amon $
6 # Date: $Date: 2008-08-28 23:26:17 $
7 # Version: $Revision: 1.5 $
8 # License: LGPL 2.1, Perl Artistic or BSD
9 #
10 # NOTE * Order matters in the switch statements as a format
11 # will hide those after it if they are subsumed by it.
12 #
13 #=============================================================================
14 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 package Document::PageId;
17 1 ▶use vars qw{@ISA};
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 @ISA = qw( UNIVERSAL );
19  
20 #=============================================================================
21 # Class Methods
22 #=============================================================================
23  
24 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
25 0 my ($class, $pageid) = @_;
26 0 my $self = bless {}, $class;
27  
28 0 return $self->_init ($pageid);
29 }
30  
31 #-----------------------------------------------------------------------------
32  
33 0 ▶sub parse {
Subroutine parse: not called CC=10 SCAR=47.0
Pod documented
34 0 my ($class, $lexeme, $flg) = @_;
35 0 my (@vals) = ("", $lexeme,);
36 0 ▶ defined $flg || ($flg = 0);
Branch: 1 branch
BranchTrueFalseTotal
defined $flg or $flg = 0 0 0 0
37  
38 0 ▶ my $delim = ($flg) ? "p" : "";
Branch: 1 branch
BranchTrueFalseTotal
$flg ? : 0 0 0
39 0 my ($lpar,$rpar) = ($lexeme =~ /^($delim)(.*)/);
40 0 ▶ defined $lpar || ($lpar = "");
Branch: 1 branch
BranchTrueFalseTotal
defined $lpar or $lpar = "" 0 0 0
41 0 ▶ $lpar eq $delim || (return @vals);
Branch: 1 branch
BranchTrueFalseTotal
$lpar eq $delim or return @vals 0 0 0
42
43 0 SWITCH: {$_ = $rpar;
44 # Example: 001.04b
45 0 ▶ if (/^(\d+\.\d+[a-z])(.*)/) { @vals = ($1,$2); last SWITCH;}
Branch: 1 branch
BranchTrueFalseTotal
if (/^(\d+\.\d+[a-z])(.*)/) 0 0 0
46  
47 # Example: 001.04
48 0 ▶ if (/^(\d+\.\d+)(.*)/) { @vals = ($1,$2); last SWITCH;}
Branch: 1 branch
BranchTrueFalseTotal
if (/^(\d+\.\d+)(.*)/) 0 0 0
49  
50 # Example: 000.spine
51 0 ▶ if (/^(0+.spine)(.*)/) { @vals = ($1,$2); last SWITCH;}
Branch: 1 branch
BranchTrueFalseTotal
if (/^(0+.spine)(.*)/) 0 0 0
52  
53 # Example: 000a
54 0 ▶ if (/^(\d+[a-z])(.*)/) { @vals = ($1,$2); last SWITCH;}
Branch: 1 branch
BranchTrueFalseTotal
if (/^(\d+[a-z])(.*)/) 0 0 0
55  
56 # Example: 001
57 0 ▶ if (/^(\d+)(.*)/) { @vals = ($1,$2); last SWITCH;}
Branch: 1 branch
BranchTrueFalseTotal
if (/^(\d+)(.*)/) 0 0 0
58 }
59 0 return @vals;
60 }
61  
62 #=============================================================================
63 # Object Methods
64 #=============================================================================
65  
66 0 ▶sub get {my ($self) = @_; $self->_update; return $self->{'pageid'};}
Subroutine get: not called CC=1 SCAR=6.9
Pod documented
67 0 ▶sub canonical {my ($self) = @_; $self->_update; return $self->{'pageid'};}
Subroutine canonical: not called CC=1 SCAR=6.9
Pod documented
68  
69 #-----------------------------------------------------------------------------
70  
71 0 ▶sub pagenumber {return shift->{'pagenum'};}
Subroutine pagenumber: not called CC=1 SCAR=6.9
Pod documented
72 0 ▶sub subpagenumber {return shift->{'subpagenum'};}
Subroutine subpagenumber: not called CC=1 SCAR=6.9
Pod documented
73 0 ▶sub side {return shift->{'side'};}
Subroutine side: not called CC=1 SCAR=6.9
Pod documented
74 0 ▶sub pagealpha {return shift->{'side'};}
Subroutine pagealpha: not called CC=1 SCAR=6.9
Pod documented
75 0 ▶sub ispairable {return shift->{'pairable'};}
Subroutine ispairable: not called CC=1 SCAR=6.9
Pod documented
76  
77 0 ▶sub haspagenumber {return defined shift->{'pagenum'};}
Subroutine haspagenumber: not called CC=1 SCAR=6.9
Pod documented
78 0 ▶sub hassubpagenumber {return defined shift->{'subpagenum'};}
Subroutine hassubpagenumber: not called CC=1 SCAR=6.9
Pod documented
79 0 ▶sub haspagealpha {return defined shift->{'side'};}
Subroutine haspagealpha: not called CC=1 SCAR=6.9
Pod documented
80  
81 #=============================================================================
82 # Internal Methods
83 #=============================================================================
84  
85 0 ▶sub _init {
Subroutine _init: not called CC=7 SCAR=40.3
86 0 my ($self, $pageid) = @_;
87 0 my @vals;
88  
89 0 SWITCH: foreach ($pageid) {
90 # Example: 001
91 0 ▶ if (/^(\d+)$/) {@vals = ($1, $1, undef, undef,
Branch: 1 branch
BranchTrueFalseTotal
if (/^(\d+)$/) 0 0 0
92 length $1, 0, 1, 1);
93 0 last SWITCH;}
94  
95 # Example: 000a
96 0 ▶ if (/^(\d+)([a-z])$/) {@vals = ($1 . $2, $1, $2, undef,
Branch: 1 branch
BranchTrueFalseTotal
if (/^(\d+)([a-z])$/) 0 0 0
97 length $1, 0, 1, 2);
98 0 last SWITCH;}
99  
100 # Example: 000.spine
101 0 ▶ if (/^(0+)\.(spine)$/) {@vals = ("000.spine", $1, $2, undef,
Branch: 1 branch
BranchTrueFalseTotal
if (/^(0+)\.(spine)$/) 0 0 0
102 length $1, 0, 0, 3);
103 0 last SWITCH;}
104  
105 # Example: 001.04
106 0 ▶ if (/^(\d+)\.(\d+)$/) {@vals = ($1 . "." . $2 , $1, undef, $2,
Branch: 1 branch
BranchTrueFalseTotal
if (/^(\d+)\.(\d+)$/) 0 0 0
107 length $1, length $2, 1, 4);
108 0 last SWITCH;}
109 # Example: 001.04b
110 0 ▶ if (/^(\d+)\.(\d+)([a-z])$/) {@vals = ($1 . "." . $2 . $3, $1, $3, $2,
Branch: 1 branch
BranchTrueFalseTotal
if (/^(\d+)\.(\d+)([a-z])$/) 0 0 0
111 length $1, length $2, 1, 5);
112 0 last SWITCH;}
113 0 return undef;
114 }
115  
116 0 @$self{'pageid','pagenum','side','subpagenum',
117 'pn_digits','spn_digits','pairable', 'type'} = @vals;
118  
119 0 $self->{'_dirty'} = 0;
120 0 return $self;
121 }
122  
123 #-----------------------------------------------------------------------------
124  
125 0 ▶sub _update {
Subroutine _update: not called CC=7 SCAR=40.3
126 0 my $self = shift;
127 0 ▶ return $self if (!$self->{'_dirty'});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_dirty"} 0 0 0
128  
129 0 my $pageid;
130 0 my ($p, $s) = @$self{'pn_digits','spn_digits'};
131 SW: {
132 0 $_ = $self->{'type'};
133 0 ▶ if (/1/) {$pageid = sprintf ("%0${p}d", $self->{'pagenum'});
Branch: 1 branch
BranchTrueFalseTotal
if (/1/) 0 0 0
134 0 last SW;}
135 0 ▶ if (/2/) {$pageid = sprintf ("%0${p}d%1s", @$self{'pagenum','side'});
Branch: 1 branch
BranchTrueFalseTotal
if (/2/) 0 0 0
136 0 last SW;}
137 0 ▶ if (/3/) {$pageid = "000.spine";
Branch: 1 branch
BranchTrueFalseTotal
if (/3/) 0 0 0
138 0 last SW;}
139 0 ▶ if (/4/) {$pageid = sprintf ("%0${p}d.%0${s}d",
Branch: 1 branch
BranchTrueFalseTotal
if (/4/) 0 0 0
140 0 @$self{'pagenum','subpagenum'});
141 0 last SW;}
142 0 ▶ if (/5/) {$pageid = sprintf ("%0${p}d.%0${s}d%1s",
Branch: 1 branch
BranchTrueFalseTotal
if (/5/) 0 0 0
143 0 @$self{'pagenum','subpagenum','side'});
144 0 last SW;}
145 }
146 0 @$self{'pageid','_dirty'} = ($pageid,0);
147 0 return $self;
148 }
149
150 #=============================================================================
151 # Pod Documentation
152 #=============================================================================
153 # You may extract and format the documention section with the 'perldoc' cmd.
154  
155 =head1 NAME
156  
157 Document::PageId - Page Identifier Class.
158  
159 =head1 SYNOPSIS
160  
161 use Document::PageId;
162  
163 $obj = Document::PageId->new ($string);
164 ($pageid,$rest) = Document::PageId->parse ($lexeme,$pflag);
165  
166 $string = $obj->get;
167 $string = $obj->canonical;
168 $pagenum = $obj->pagenumber;
169 $subpagenum = $obj->subpagenumber;
170 $side = $obj->side;
171 $side = $obj->pagealpha;
172 $flg = $obj->ispairable;
173 $flg = $obj->haspagenumber;
174 $flg = $obj->hassubpagenumber;
175 $flg = $obj->haspagealpha;
176  
177 =head1 Inheritance
178  
179 UNIVERSAL
180  
181 =head1 Description
182  
183 Page identifiers will usually be a simple format like "001", but over the
184 years I've found need for variants such as "010.01" and "000a". Not yet
185 even thought about are the many locational descriptions for pageid's which
186 should be moved out of the basic filename: foo-a-toprt, foo-b-botleft, and
187 all sorts of naming conventions I've used for cases where a single document
188 page has been scanned as multiple sub-pages. It will probably be a major
189 effort to sort this out.
190  
191 Just as a warning... do not get in the habit of directly accessing the
192 pageid ivar. This class and subclasses do lazy evaluation, so if you do
193 not retrieve the value through the approved methods, you may not get what
194 you expect.
195  
196 =head1 Examples
197  
198 None.
199  
200 =head1 Class Variables
201  
202 None.
203  
204 =head1 Instance Variables
205  
206 pageid Page identification string, eg 050, 000a , 10.1, 100.01b.
207 Default is undef.
208 pagenum Integer page number part of page id, eg 100.01b
209 subpagenum Integer subpage number, eg 100.01b
210 side Page side, , eg 100.01b
211 pn_digits Number of digits in the page number. Not accessible at
212 present.
213 spn_digits Number of digits in the subpage number. Not accessible at
214 present.
215 type Type of format of this pageid. Internal use.
216  
217 =head1 Class Methods
218  
219 =over 4
220  
221 =item B<$obj = Document::PageId-E<gt>new ($string)>
222  
223 Create a new pageid object around $string. It returns undef and does not
224 create an object if $string cannot be parsed as a pageid.
225  
226 Will I need a last page Class as well? Perhaps subclass first and last? I
227 might either detect the p here and set a switch or outside and not worry
228 about it. That might be the cleanest option. I will need a way to autodetect
229 the field widths of pagenum and subpagenum. I still have not started to deal
230 with segmented pages, top/mid/bot/left/right nomenclature. Sometimes a and b
231 are front and back; sometimes they are a fill in for publications with page 1
232 many pages inside: so I have 00a...00g,01 as page numbers in the front.
233  
234 =item B<($pageid,$rest) = Document::PageId-E<gt>parse ($lexeme,$pflag)>
235  
236 Parse the string contained in $lexeme.. If $pflag is set, page number formats
237 assume a leading 'p', eg. 'p001'. The default is to not require the leading
238 'p'.
239  
240 If $lexeme contains a right justified pageid string, that is returned as
241 $pageid and any remaining chars are placed in $rest. If no pageid is found,
242 $pageid is and all of $lexeme is return in $rest; if $lexeme is empty or
243 undef to start with, both values are .
244  
245 =back 4
246  
247 =head1 Instance Methods
248  
249 =over 4
250  
251 =item B<$string = $obj-E<gt>canonical>
252  
253 Return the pageid in a canonical format. Currently the same as verbatim. This
254 will be useful when I start constructing new pageid's on the fly and want to
255 generate the "perfect" string according to current rules.
256  
257 [Also useful if I someday want to update all files to use a standard format.
258 Use get for the original page and canonical for the way it should be.]
259  
260 =item B<$string = $obj-E<gt>get>
261  
262 Return the original pageid.
263  
264 =item B<$flg = $obj-E<gt>haspagealpha>
265  
266 True if there is an alpha part to the pageid.
267  
268 =item B<$flg = $obj-E<gt>haspagenumber>
269  
270 True if there is a page number part to the pageid.
271  
272 =item B<$flg = $obj-E<gt>hassubpagenumber>
273  
274 True if there is a subpage number part to the pageid..
275  
276 =item B<$side = $obj-E<gt>ispairable>
277  
278 Returns true if the pageid contained in this object may be part of a
279 sequential pair like p001-002. At present the only non-pairable pageid
280 is 000.spine.
281  
282 =item B<$side = $obj-E<gt>pagealpha>
283  
284 Return the page side or alphabetic used for otherwise unnumbered pages within
285 a page numbered document. In most cases a single sheet of paper or a flyer has
286 a side a and a side b; however I also use this as a way of handling publications
287 whose page 1 is 10 pages from the front cover. So I just have a lot of page
288 000's: 000a,000b,000c....001.
289  
290 =item B<$pagenum = $obj-E<gt>pagenumber>
291  
292 Return the page number.
293  
294 =item B<$side = $obj-E<gt>side>
295  
296 A synonym for the pagealpha method.
297  
298 =item B<$subpagenum = $obj-E<gt>subpagenumber >
299  
300 Return the sub-page number. This is often used for inserted booklets in
301 magazines, or updates in manuals.
302  
303 =back 4
304  
305 =head1 Private Instance Methods
306  
307 None.
308  
309 =head1 Private Instance Methods
310  
311 =over 4
312  
313 =item B<$obj = $obj-E<gt>_init ($pageid)>
314  
315 Subclass use only. This method does the bulk of the work for the new class
316 method. Parses $pageid into components and sets up all the associated ivars.
317 Returns undef if the pageid is not parseable.
318  
319 =item B<$obj = $obj-E<gt>_update>
320  
321 Subclass use only. Using the format information recorded by _init, recreate
322 the pageid from its parts. This is a way to rebuild the pageid after a
323 subclass modifies one of its components.
324  
325 =back 4
326  
327 =head1 KNOWN BUGS
328  
329 See TODO.
330  
331 =head1 SEE ALSO
332  
333 None.
334  
335 =head1 AUTHOR
336  
337 Dale Amon <amon@vnl.com>
338  
339 =cut
340
341 #=============================================================================
342 # CVS HISTORY
343 #=============================================================================
344 # $Log: PageId.pm,v $
345 # Revision 1.5 2008-08-28 23:26:17 amon
346 # perldoc section regularization.
347 #
348 # Revision 1.4 2008-08-13 21:04:32 amon
349 # Third phase of reformatting to current standard.
350 #
351 # Revision 1.3 2008-08-13 20:56:53 amon
352 # Second phase of reformatting to current standard.
353 #
354 # Revision 1.2 2008-08-13 14:50:02 amon
355 # First phase of reformatting to current standard.
356 #
357 # Revision 1.1.1.1 2006-07-05 12:11:24 amon
358 # Manages a Document directory
359 #
360 # 20060705 Dale Amon <amon@islandone.org>
361 # A page of (001,01,a) was printing as 00101a because
362 # the dot was misplaced in the format statement.
363 # 20040816 Dale Amon <amon@islandone.org>
364 # Added support for a page named 000.spine which cannot
365 # exist as part of a pageid pair like 'p001-002'. Added
366 # the ispairable method to support this restriction.
367 #
368 # 20040813 Dale Amon <amon@islandone.org>
369 # Moved to Document:: from Archivist::
370 # to make it easier to enforce layers.
371 #
372 # 20021211 Dale Amon <amon@vnl.com>
373 # Created.
374 1 1;
« Document/PageIterator.pm ↑ Summary File/Spec/PublicationPage.pm »