blib/lib/Data/Edit/Xml/SDL.pm

filter: Statementstmt 77.7% 126 / 162 Branchbran 38.1% 16 / 42 Conditioncond 50.0% 2 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 90.0% 27 / 30 Podpod 57.8% 11 / 19 totaltotal 70.2% 182 / 259 CC 24 SCAR 16.9
CC
24
Cov
69%
__ANON__
20.1
createSDLFileMap
11.2
getImageFile
6.0
CRAP 5.4
↑ Summary
1 #!/usr/bin/perl -I/home/phil/perl/cpan/DataTableText/lib/ -I/home/phil/perl/cpan/DataEditXmlSDL/lib/
2 #-------------------------------------------------------------------------------
3 # Create SDL file map from a set of linted xml files
4 # Philip R Brenan at gmail dot com, Appa Apps Ltd, 2016
5 #-------------------------------------------------------------------------------
6 # podDocumentation
7  
8 package Data::Edit::Xml::SDL;
9 1 ▶use warnings FATAL => qw(all);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Carp qw(confess cluck);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Data::Dump qw(dump);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Data::Edit::Xml::Lint;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Data::Table::Text qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 our $VERSION = 20200109;
16  
17 #1 Constructor # Construct a new SDL file map creator
18  
19 sub new # Create a new SDL file map creator - call this method statically as in Data::Edit::Xml::Lint::new()
20 1 ▶ {bless {sdlVersion=>'13.0.0.0', language=>(qq(en-US))} # Defaults that can be easily overridden
Subroutine new: called CC=1 SCAR=0.0
Pod documented
21 }
22  
23 #2 Attributes # Attributes describing a lint
24  
25 1 genLValueScalarMethods(qw(filePathFolder)); # Prefix this folder (if supplied) to the filepath
26 1 genLValueScalarMethods(qw(fileType)); # The fileType of the file to be processed
27 1 genLValueScalarMethods(qw(filesFlattened)); # Files have been flattened if true
28 1 genLValueScalarMethods(qw(folderHasMixedContent)); # folderHasMixedContent field
29 1 genLValueScalarMethods(qw(ishType)); # IshType field
30 1 genLValueScalarMethods(qw(imagePath)); # Image path relative to sourcebasepath
31 1 genLValueScalarMethods(qw(language)); # The language of the content, defaults to: 'en-US'
32 1 genLValueScalarMethods(qw(lint)); # The lint of the file to be processed
33 1 genLValueScalarMethods(qw(sdlVersion)); # Version of SDL we are using, defaults to: '12.0.0.0'
34 1 genLValueScalarMethods(qw(section)); # Sub folder for file on SDL: maps, topics
35 1 genLValueScalarMethods(qw(sourcebasepath)); # Path to source to be uploaded
36 1 genLValueScalarMethods(qw(targetFolder)); # Input: The SDL target folder to be used in the filemap - the person doing the upload will give this to you.
37 1 genLValueScalarMethods(qw(version)); # Version of the input content
38  
39 #1 SDL File Map # Generate an SDL file map
40  
41 sub xmlLineOne #P Line one of all xml files
42 1 ▶ {'<?xml version="1.0" encoding="utf-8"?>'."\n"
Subroutine xmlLineOne: called CC=1 SCAR=0.0
Pod documented
43 }
44  
45 sub getFileMap #P File map tag
46 1 ▶ {my ($sdl) = @_; # Sdl file map creator
Subroutine getFileMap: called CC=1 SCAR=0.0
Pod documented
47 1 my $s = $sdl->sourcebasepath;
48 1 my $v = $sdl->sdlVersion;
49 <<END
50 <filemap sourcebasepath="$s" version="$v">;
51 END
52 1 }
53  
54 sub getFile($) #P File tag
55 2 ▶ {my ($sdl) = @_; # Sdl file map creator
Subroutine getFile: called CC=5 SCAR=16.4
Pod documented
56 2 my $targetFolder = $sdl->targetFolder;
57 2 my $ishType = $sdl->ishType;
58 2 my $section = $sdl->section;
59 2 my $imagePath = $sdl->imagePath;
60 2 my $lint = $sdl->lint;
61 2 my $project = $lint->project;
62 2 my $guid = $lint->guid;
63 2 my $file = $lint->file;
64 2 ▶ my $title = $lint->title || 'REQUIRED-CLEANUP-TITLE';
Condition50%1/2
$lint->title || 'REQUIRED-CLEANUP-TITLE'
l!l
20
MC/DC0%0/1
$lint->title || 'REQUIRED-CLEANUP-TITLE'
$lint->title
Truth table50%1/2
A
A= $lint->title
Aresult
11
00
65 2 my $mixed = ucfirst $sdl->folderHasMixedContent;
66 2 ▶ $mixed =~ m/\A(True|False)\Z/s or
Branch: 1 branch
BranchTrueFalseTotal
$mixed =~ /\A(True|False)\Z/s or confess("FolderhasMixedContent = (True|False) not $mixed") 2 0 2
67 confess "FolderhasMixedContent = (True|False) not $mixed";
68 2 my $filePrefix = $lint->project; # File name prefix if any
69  
70 2 my (undef, $fileName, $fileExt) = parseFileName($file); # Parse file name
71 2 ▶ $fileExt or confess "No file extension for ".$file;
Branch: 1 branch
BranchTrueFalseTotal
$fileExt or confess('No file extension for ' . $file) 2 0 2
72 # my $relFile = filePathExt($filePrefix, $fileName, $fileExt);
73 2 my $relFile = filePathExt($fileName, $fileExt); ## Fully flattened
74 2 ▶ return <<END unless $sdl->filesFlattened;
Branch: 1 branch
BranchTrueFalseTotal
unless $sdl->filesFlattened 2 0 2
75 <file fileextension=".$fileExt" filename="$fileName.$fileExt" filepath="$filePrefix\\$fileName.$fileExt" filetype="$ishType" folderhasmixedcontent="$mixed" id="$guid" targetfolder="$targetFolder\\$project\\$section" title="$title">
76 END
77  
78 0 return <<END;
79 <file fileextension=".$fileExt" filename="$fileName.$fileExt" filepath="$fileName.$fileExt" filetype="$ishType" folderhasmixedcontent="$mixed" id="$guid" targetfolder="$targetFolder\\$project\\$section" title="$title">
80 END
81 }
82  
83 sub getImageFile($) #P Image file tag
84 0 ▶ {my ($sdl, $file) = @_; # Sdl file map creator, image file name
Subroutine getImageFile: not called CC=2 SCAR=17.9
Pod undocumented
85 0 my $targetFolder = $sdl->targetFolder;
86 0 my $ishType = $sdl->ishType;
87 0 my $section = $sdl->section;
88 0 my $imagePath = $sdl->imagePath;
89 0 my $project = "images";
90 0 my $guid = guidFromMd5(fn $file);
91 0 my $mixed = ucfirst $sdl->folderHasMixedContent;
92 0 ▶ $mixed =~ m/\A(True|False)\Z/s or
Branch: 1 branch
BranchTrueFalseTotal
$mixed =~ /\A(True|False)\Z/s or confess("FolderhasMixedContent = (True|False) not $mixed") 0 0 0
93 confess "FolderhasMixedContent = (True|False) not $mixed";
94  
95 0 my $filePrefix = filePathDir($imagePath); # The image file prefix if we are processing an image
96  
97 0 my (undef, $fileName, $fileExt) = parseFileName($file); # Parse file name
98 # my $relFile = filePathExt($filePrefix, $fileName, $fileExt);
99 0 my $relFile = filePathExt($fileName, $fileExt); ## Fully flattened
100 0 my $r = <<END;
101 <file fileextension=".$fileExt" filename="$relFile" filepath="$relFile" filetype="$ishType" folderhasmixedcontent="$mixed" id="$guid" targetfolder="$targetFolder\\$section" title="$fileName">
102 END
103 0 $r
104 }
105  
106 sub getIshObject #P IshObject tag
107 2 ▶ {my ($sdl) = @_; # Sdl
Subroutine getIshObject: called CC=2 SCAR=7.0
Pod documented
108 2 my $ishType = $sdl->ishType;
109 2 my $lint = $sdl->lint;
110 2 my $guid = $lint->guid;
111 2 ▶ $guid or confess "No guid supplied";
Branch: 1 branch
BranchTrueFalseTotal
$guid or confess("No guid supplied") 2 0 2
112 <<END
113 <ishobject ishref="$guid" ishtype="$ishType">
114 END
115 2 }
116  
117 sub getImageIshObject($$) #P IshObject tag for an image
118 0 ▶ {my ($sdl, $file) = @_; # Sdl, image file
Subroutine getImageIshObject: not called CC=1 SCAR=6.9
Pod undocumented
119 0 my $ishType = $sdl->ishType;
120 0 my $lint = $sdl->lint;
121 0 my $guid = fn $file;
122 <<END
123 <ishobject ishref="$guid" ishtype="$ishType">
124 END
125 0 }
126  
127 sub getFTitle($;$) #P FTITLE tag
128 2 ▶ {my ($sdl, $imageFile) = @_; # Sdl, image file name which might have an image title following the md5 sum
Subroutine getFTitle: called CC=4 SCAR=17.3
Pod documented
129 2 my $lint = $sdl->lint; # Lint
130 2 my $Title = $lint->title; # Title
131  
132 2 ▶ if ($imageFile) # Image files some times have their titles after the md5 sum
Branch: 1 branch
BranchTrueFalseTotal
if ($imageFile) 0 2 2
133 0 {my $i = $imageFile.q(.imageDef);
134 0 ▶ if (-e $i)
Branch: 1 branch
BranchTrueFalseTotal
if (-e $i) 0 0 0
135 0 {$Title = readFile($i);
136 }
137 }
138  
139 # warn "No title in\n".dump($lint)."\n" unless $Title;
140 2 ▶ my $title = $Title || 'REQUIRED-CLEANUP-TITLE';
Condition50%1/2
$Title || 'REQUIRED-CLEANUP-TITLE'
l!l
20
MC/DC0%0/1
$Title || 'REQUIRED-CLEANUP-TITLE'
$Title
Truth table50%1/2
A
A= $Title
Aresult
11
00
141 <<END
142 <ishfield level="logical" name="FTITLE" xml:space="preserve">$title</ishfield>
143 END
144 2 }
145  
146 sub getVersion #P Version tag
147 2 ▶ {my ($sdl) = @_; # Sdl
Subroutine getVersion: called CC=1 SCAR=0.0
Pod documented
148 2 my $v = $sdl->sdlVersion;
149 <<END
150 <ishfield level="version" name="VERSION" xml:space="preserve">$v</ishfield>
151 END
152 2 }
153  
154 sub getDocLanguage #P DOC-LANGUAGE tag
155 2 ▶ {my ($sdl) = @_; # Sdl
Subroutine getDocLanguage: called CC=1 SCAR=0.0
Pod documented
156 2 my $l = $sdl->language;
157 <<END
158 <ishfield level="lng" name="DOC-LANGUAGE" xml:space="preserve">$l</ishfield>
159 END
160 2 }
161  
162 sub getAuthor #P Author tag
163 2 ▶ {my ($sdl) = @_; # Sdl
Subroutine getAuthor: called CC=1 SCAR=0.1
Pod documented
164 2 my $lint = $sdl->lint;
165 my $a = sub
166 2 ▶ {my $a = $lint->author;
Subroutine __ANON__: called CC=2 SCAR=8.1
167 2 ▶ return $a if $a;
Branch: 1 branch
BranchTrueFalseTotal
if $a 2 0 2
168 0 "bill.gearhart";
169 2 }->();
170  
171 <<END
172 <ishfield name="FAUTHOR" level="lng" xml:space="preserve">$a</ishfield>
173 END
174 2 }
175  
176 sub getResolution #P Resolution
177 0 ▶ {my ($sdl) = @_; # Sdl
Subroutine getResolution: not called CC=1 SCAR=6.9
Pod documented
178 <<END
179 <ishfield level="lng" name="FRESOLUTION" xml:space="preserve">High</ishfield>
180 END
181 0 }
182  
183 sub createSDLFileMap($@) # Generate an SDL file map for a selected set of files
184 1 ▶ {my ($sdl, @foldersAndExtensions) = @_; # Sdl, Directory tree search specification
Subroutine createSDLFileMap: called CC=7 SCAR=24.1
Pod documented
185  
186 1 my @files = searchDirectoryTreesForMatchingFiles(@foldersAndExtensions); # Find matching files
187  
188 1 my @map = (xmlLineOne, $sdl->getFileMap); # The generated map
189  
190 1 for my $file(@files) # Each file contributing to the map
191 2 ▶ {next if $file =~ m(\.imageDef\Z)s; # Image definition files
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /\.imageDef\Z/s 0 2 2
192 2 my $lint = Data::Edit::Xml::Lint::read($file); # Linter for the file
193 2 $sdl->lint = $lint;
194  
195 my $ditaType = sub
196 2 ▶ {return $lint->ditaType if $lint->ditaType;
Subroutine __ANON__: called CC=5 SCAR=30.0
Branch: 1 branch
BranchTrueFalseTotal
if $lint->ditaType 2 0 2
197 0 ▶ return q(bookmap) if fe($file) =~ m(ditamap)is;
Branch: 1 branch
BranchTrueFalseTotal
if &fe($file) =~ /ditamap/is 0 0 0
198 0 ▶ return q(image) if fe($file) =~ m((emf|gif|png|jpg|jpeg|pdf|tiff))is;
Branch: 1 branch
BranchTrueFalseTotal
if &fe($file) =~ /(emf|gif|png|jpg|jpeg|pdf|tiff)/is 0 0 0
199 0 ▶ return q(image) if fe($file) =~ m(imageDef); # File has been guidized the corresponding imageDef file tells us its original name
Branch: 1 branch
BranchTrueFalseTotal
if &fe($file) =~ /imageDef/ 0 0 0
200 0 undef;
201 2 }->();
202  
203 2 ▶ $ditaType or confess "DitaType required for file:\n$file";
Branch: 1 branch
BranchTrueFalseTotal
$ditaType or confess("DitaType required for file:\n$file") 2 0 2
204  
205 2 ▶ if ($ditaType =~ m/map/i)
Branch: 3 branches
BranchTrueFalseTotal
if ($ditaType =~ /map/i) { } 1 1 2
elsif ($ditaType =~ /concept|reference|task|troubleShooting/i) { } 1 0 1
elsif ($ditaType =~ /image/i) { } 0 0 0
206 1 {$sdl->ishType = (qq(ISHMasterDoc));
207 1 $sdl->section = (qq(maps));
208 1 $sdl->folderHasMixedContent = (qq(true));
209 1 push @map,
210 $sdl->getFile,
211 $sdl->getIshObject, <<END,
212 <ishfields>
213 END
214 $sdl->getFTitle,
215 $sdl->getVersion,
216 $sdl->getDocLanguage,
217 $sdl->getAuthor,
218 <<END,
219 </ishfields>
220 </ishobject>
221 </file>
222 END
223 }
224  
225 elsif ($ditaType =~ m/concept|reference|task|troubleShooting/i)
226 1 {$sdl->ishType = (qq(ISHModule));
227 1 $sdl->section = (qq(topics));
228 1 $sdl->folderHasMixedContent = (qq(true));
229 1 push @map,
230 $sdl->getFile,
231 $sdl->getIshObject, <<END,
232 <ishfields>
233 END
234 $sdl->getFTitle,
235 $sdl->getVersion,
236 $sdl->getDocLanguage,
237 $sdl->getAuthor,
238 <<END,
239 </ishfields>
240 </ishobject>
241 </file>
242 END
243 }
244  
245 elsif ($ditaType =~ m/image/i)
246 0 {$sdl->ishType = qq(ISHIllustration);
247 0 $sdl->section = qq(images);
248 0 $sdl->imagePath = qq(images);
249 0 $sdl->folderHasMixedContent = qq(false);
250 0 push @map,
251 $sdl->getImageFile($file),
252 $sdl->getImageIshObject($file), <<END,
253 <ishfields>
254 END
255 $sdl->getFTitle($file),
256 $sdl->getVersion,
257 $sdl->getDocLanguage,
258 $sdl->getResolution,
259 q(image), #$sdl->getAuthor,
260 <<END,
261 </ishfields>
262 </ishobject>
263 </file>
264 END
265 }
266 0 else {confess "Unrecognized ditaType $ditaType"}
267 }
268 1 my $T = dateTimeStamp;
269 1 push @map, <<END;
270 </filemap>
271 <!--Created: $T -->
272 END
273 1 join "", @map;
274 } # createSDLFileMap
275  
276 # podDocumentation
277  
278 =pod
279  
280 =encoding utf-8
281  
282 =head1 Name
283  
284 Data::Edit::Xml::SDL - Create SDL file map from a set of linted xml files
285 produced by L<Data::Edit::Xml::Lint>
286  
287 =head1 Synopsis
288  
289 Create an SDL file map from a set of linted xml files produced by
290 L<Data::Edit::Xml::Lint>
291  
292 my $s = Data::Edit::Xml::SDL::new();
293 $s->sourcebasepath = 'C:\frame\batch1\out';
294 $s->targetFolder = qq(RyffineImportSGIfm);
295 $s->imagePath = qq(images);
296 $s->version = 1;
297  
298 say STDERR $s->createSDLFileMap(qw(. xml));
299  
300 Produces:
301  
302 <?xml version="1.0" encoding="utf-8"?>
303 <filemap sourcebasepath="C:\hp\frame\batch1\out" version="12.0.0.0">;
304 <file fileextension=".ditamap" >
305 <ishobject ishref="GUID-D7147C7F-2017-0012-FRMB-000000000002" ishtype="ISHMasterDoc">
306 <ishfields>
307 <ishfield level="logical" name="FTITLE" xml:space="preserve">bm_0003388-002</ishfield>'
308 <ishfield level="version" name="VERSION" xml:space="preserve">1</ishfield>
309 <ishfield level="lng" name="DOC-LANGUAGE" xml:space="preserve">en-US</ishfield>
310 <ishfield name="FAUTHOR" level="lng" xml:space="preserve">bill.gearhart@hpe.com</ishfield>
311 </ishfields>
312 </ishobject>
313 </file>
314  
315 etc.
316  
317 =head1 Description
318  
319 =head2 Constructor
320  
321 Construct a new SDL file map creator
322  
323 =head3 new()
324  
325 Create a new SDL file map creator - call this method statically as in Data::Edit::Xml::Lint::new()
326  
327  
328 =head3 Attributes
329  
330 Attributes describing a lint
331  
332 =head4 filePathFolder :lvalue
333  
334 Prefix this folder (if supplied) to the filepath
335  
336  
337 =head4 fileType :lvalue
338  
339 The fileType of the file to be processed
340  
341  
342 =head4 folderHasMixedContent :lvalue
343  
344 folderHasMixedContent field
345  
346  
347 =head4 ishType :lvalue
348  
349 IshType field
350  
351  
352 =head4 imagePath :lvalue
353  
354 Image path relative to sourcebasepath
355  
356  
357 =head4 language :lvalue
358  
359 The language of the content, defaults to: 'en-US'
360  
361  
362 =head4 lint :lvalue
363  
364 The lint of the file to be processed
365  
366  
367 =head4 sdlVersion :lvalue
368  
369 Version of SDL we are using, defaults to: '12.0.0.0'
370  
371  
372 =head4 section :lvalue
373  
374 Sub folder for file on SDL: maps, topics
375  
376  
377 =head4 sourcebasepath :lvalue
378  
379 Path to source to be uploaded
380  
381  
382 =head4 targetFolder :lvalue
383  
384 The SDL target folder to be used
385  
386  
387 =head4 version :lvalue
388  
389 Version of the input content
390  
391  
392 =head2 SDL File Map
393  
394 Generate an SDL file map
395  
396 =head3 createSDLFileMap($@)
397  
398 Generate an SDL file map for a selected set of files
399  
400 1 $sdl Sdl
401 2 @foldersAndExtensions Directory tree search specification
402  
403  
404 =head1 Private Methods
405  
406 =head2 xmlLineOne()
407  
408 Line one of all xml files
409  
410  
411 =head2 getFileMap()
412  
413 File map tag
414  
415  
416 =head2 getFile($$)
417  
418 File tag
419  
420 1 $sdl Sdl file map creator
421 2 $images Processing an image file or not
422  
423 =head2 getIshObject()
424  
425 IshObject tag
426  
427  
428 =head2 getFTitle()
429  
430 FTITLE tag
431  
432  
433 =head2 getVersion()
434  
435 Version tag
436  
437  
438 =head2 getDocLanguage()
439  
440 DOC-LANGUAGE tag
441  
442  
443 =head2 getAuthor()
444  
445 Author tag
446  
447  
448 =head2 getResolution()
449  
450 Resolution
451  
452  
453  
454 =head1 Index
455  
456  
457 L<createSDLFileMap|/createSDLFileMap>
458  
459 L<filePathFolder|/filePathFolder>
460  
461 L<fileType|/fileType>
462  
463 L<folderHasMixedContent|/folderHasMixedContent>
464  
465 L<getAuthor|/getAuthor>
466  
467 L<getDocLanguage|/getDocLanguage>
468  
469 L<getFile|/getFile>
470  
471 L<getFileMap|/getFileMap>
472  
473 L<getFTitle|/getFTitle>
474  
475 L<getIshObject|/getIshObject>
476  
477 L<getResolution|/getResolution>
478  
479 L<getVersion|/getVersion>
480  
481 L<imagePath|/imagePath>
482  
483 L<ishType|/ishType>
484  
485 L<language|/language>
486  
487 L<lint|/lint>
488  
489 L<new|/new>
490  
491 L<sdlVersion|/sdlVersion>
492  
493 L<section|/section>
494  
495 L<sourcebasepath|/sourcebasepath>
496  
497 L<targetFolder|/targetFolder>
498  
499 L<version|/version>
500  
501 L<xmlLineOne|/xmlLineOne>
502  
503 =head1 Installation
504  
505 This module is written in 100% Pure Perl and, thus, it is easy to read, use,
506 modify and install.
507  
508 Standard Module::Build process for building and installing modules:
509  
510 perl Build.PL
511 ./Build
512 ./Build test
513 ./Build install
514  
515 =head1 Author
516  
517 L<philiprbrenan@gmail.com|mailto:philiprbrenan@gmail.com>
518  
519 L<http://www.appaapps.com|http://www.appaapps.com>
520  
521 =head1 Copyright
522  
523 Copyright (c) 2016-2017 Philip R Brenan.
524  
525 This module is free software. It may be used, redistributed and/or modified
526 under the same terms as Perl itself.
527  
528 =cut
529  
530  
531 # Tests and documentation
532  
533 sub test
534 1 ▶ {my $p = __PACKAGE__;
Subroutine test: called CC=4 SCAR=13.9
Pod undocumented
535 1 ▶ return if eval "eof(${p}::DATA)";
Branch: 1 branch
BranchTrueFalseTotal
if eval "eof(${p}::DATA)" 0 1 1
536 1 my $s = eval "join('', <${p}::DATA>)";
537 1 ▶ $@ and die $@;
Branch: 1 branch
BranchTrueFalseTotal
$@ and die $@ 0 1 1
538 1 ▶ eval $s;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine expectedOutput: called CC=1 SCAR=0.0
Subroutine testBookMap: called CC=1 SCAR=0.0
Subroutine testConcept: called CC=1 SCAR=0.0
Subroutine testImage: called CC=1 SCAR=0.0
Subroutine writeTest: called CC=1 SCAR=0.0
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
539 1 ▶ $@ and die $@;
Branch: 1 branch
BranchTrueFalseTotal
$@ and die $@ 0 1 1
540 }
541  
542 1 ▶test unless caller;
Branch: 1 branch
BranchTrueFalseTotal
unless caller 0 1 1
543  
544 1 1;
545 # podDocumentation
546 __DATA__
↑ Summary