Sticker/Scribe/YamlPrefix.pm

filter: Statementstmt 66.6% 4 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 75.0% 6 / 8 CC 1 SCAR 1.4
CC
1
Cov
67%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
« Sticker/Scribe/Yaml.pm ↑ Summary Sticker/Scribe/Epub.pm »
1 package File::Sticker::Scribe::YamlPrefix;
2 $File::Sticker::Scribe::YamlPrefix::VERSION = '4.9';
3 =head1 NAME
4  
5 File::Sticker::Scribe::YamlPrefix - write and standardize meta-data from YAML file
6  
7 =head1 VERSION
8  
9 version 4.9
10  
11 =head1 SYNOPSIS
12  
13 use File::Sticker::Scribe::YamlPrefix;
14  
15 my $obj = File::Sticker::Scribe::YamlPrefix->new(%args);
16  
17 my %meta = $obj->read_meta($filename);
18  
19 $obj->write_meta(%args);
20  
21 =head1 DESCRIPTION
22  
23 This will read and write meta-data from plain text files where the first part
24 of the file contains YAML data, set up as if it is a YAML stream. That is, the
25 file starts with '---' on one line, then there is YAML data, then there is
26 another '---' line, and all content after that is ignored. Then it will
27 standardize it to a common nomenclature, such as "tags" for things called tags,
28 or Keywords or Subject etc.
29  
30 This format can be useful as a way of storing meta-data in documents or in
31 wiki pages.
32  
33 =cut
34  
35 1 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 0 ▶use File::LibMagic;
Subroutine BEGIN: called CC=1 SCAR=2.6
37 use YAML::Any;
38  
39 use parent qw(File::Sticker::Scribe);
40  
41 # FOR DEBUGGING
42 =head1 DEBUGGING
43  
44 =head2 whoami
45  
46 Used for debugging info
47  
48 =cut
49 sub whoami { ( caller(1) )[3] }
50  
51 =head1 METHODS
52  
53 =head2 priority
54  
55 The priority of this scribe. Scribes with higher priority get tried first.
56  
57 =cut
58  
59 sub priority {
60 my $class = shift;
61 return 1;
62 } # priority
63  
64 =head2 allowed_file
65  
66 If this scribe can be used for the given file, then this returns true.
67 File must be plain text and NOT end with '.yml'
68 If the file does not exist, it cannot be written to.
69 If it does exist, the YAML-prefix area must exist also.
70  
71 =cut
72  
73 sub allowed_file {
74 my $self = shift;
75 my $file = shift;
76 say STDERR whoami() if $self->{verbose} > 2;
77  
78 my $ft = $self->{file_magic}->info_from_filename($file);
79 # This needs to be a plain text file
80 # We don't want to include .yml files because they are dealt with separately
81 if (-r $file
82 and $ft->{mime_type} =~ m{^text/plain}
83 and $file !~ /\.yml$/)
84 {
85 say STDERR 'Scribe ' . $self->name() . ' allows filetype ' . $ft->{mime_type} . ' of ' . $file if $self->{verbose} > 1;
86 return 1;
87 }
88 return 0;
89 } # allowed_file
90  
91 =head2 allowed_fields
92  
93 If this scribe can be used for the known and wanted fields, then this returns true.
94 For YAML, this always returns true.
95  
96 if ($scribe->allowed_fields())
97 {
98 ....
99 }
100  
101 =cut
102  
103 sub allowed_fields {
104 my $self = shift;
105  
106 return 1;
107 } # allowed_fields
108  
109 =head2 known_fields
110  
111 Returns the fields which this scribe knows about.
112 This scribe has no limitations.
113  
114 my $known_fields = $scribe->known_fields();
115  
116 =cut
117  
118 sub known_fields {
119 my $self = shift;
120  
121 if ($self->{wanted_fields})
122 {
123 return $self->{wanted_fields};
124 }
125 return {};
126 } # known_fields
127  
128 =head2 read_meta
129  
130 Read the meta-data from the given file.
131  
132 my $meta = $obj->read_meta($filename);
133  
134 =cut
135  
136 sub read_meta {
137 my $self = shift;
138 my $filename = shift;
139 say STDERR whoami() if $self->{verbose} > 2;
140  
141 my ($yaml_str,$more) = $self->_yaml_and_more($filename);
142 my %meta = ();
143 my $info;
144 eval {$info = Load($yaml_str);};
145 if ($@)
146 {
147 warn __PACKAGE__, " Load of data failed: $@";
148 say "======\n$yaml_str\n=====" if $self->{verbose} > 1;
149 return \%meta;
150 }
151 if (!$info)
152 {
153 warn __PACKAGE__, " no legal YAML";
154 return \%meta;
155 }
156 # now standardize the meta-data
157 foreach my $key (sort keys %{$info})
158 {
159 my $val = $info->{$key};
160 if ($val)
161 {
162 if ($key eq 'tags')
163 {
164 $meta{tags} = $val;
165 # If there are no commas, change spaces to commas. This is
166 # because if we are using commas to separate, we allow
167 # multi-word tags with spaces in them, so we don't want to turn
168 # those spaces into commas!
169 if ($meta{tags} !~ /,/)
170 {
171 $meta{tags} =~ s/ /,/g; # spaces to commas
172 }
173 }
174 elsif ($key eq 'dublincore.source')
175 {
176 $meta{'url'} = $val;
177 }
178 elsif ($key eq 'dublincore.title')
179 {
180 $meta{'title'} = $val;
181 }
182 elsif ($key eq 'dublincore.creator')
183 {
184 $meta{'creator'} = $val;
185 }
186 elsif ($key eq 'dublincore.description')
187 {
188 $meta{'description'} = $val;
189 }
190 elsif ($key eq 'private')
191 {
192 # deal with this after tags
193 }
194 else
195 {
196 $meta{$key} = $val;
197 }
198 }
199 }
200 if ($info->{private})
201 {
202 $meta{tags} .= ",private";
203 }
204  
205 # Check for wiki-specific meta-data in the "more" part
206 if ($more =~ m/\[\[\!meta title="([^"]+)"\]\]/)
207 {
208 $meta{title} = $1 if !$meta{title};
209 $more =~ s/\[\[\!meta title="([^"]+)"\]\]//;
210 }
211 if ($more =~ m/\[\[\!meta description="([^"]+)"\]\]/)
212 {
213 $meta{description} = $1 if !$meta{description};
214 $more =~ s/\[\[\!meta description="([^"]+)"\]\]//;
215 }
216
217 return \%meta;
218 } # read_meta
219  
220 =head2 delete_field_from_file
221  
222 Completely remove the given field.
223 This does no checking for multi-valued fields, it just deletes the whole thing.
224  
225 $scribe->delete_field_from_file(filename=>$filename,field=>$field);
226  
227 =cut
228  
229 sub delete_field_from_file {
230 my $self = shift;
231 my %args = @_;
232 say STDERR whoami() if $self->{verbose} > 2;
233  
234 my $filename = $args{filename};
235 my $field = $args{field};
236  
237 my $info = $self->_load_meta($filename);
238 delete $info->{$field};
239 $self->_write_meta(filename=>$filename,meta=>$info);
240
241 } # delete_field_from_file
242  
243 =head2 replace_all_meta
244  
245 Overwrite the existing meta-data with that given.
246  
247 (This supercedes the parent method because we can do it more efficiently this way)
248  
249 $scribe->replace_all_meta(filename=>$filename,meta=>\%meta);
250  
251 =cut
252  
253 sub replace_all_meta {
254 my $self = shift;
255 my %args = @_;
256 say STDERR whoami() if $self->{verbose} > 2;
257  
258 my $filename = $args{filename};
259 my $meta = $args{meta};
260  
261 $self->_write_meta(filename=>$filename,meta=>$meta);
262
263 } # replace_all_meta
264  
265 =head2 replace_one_field
266  
267 Overwrite the given field. This does no checking.
268  
269 $scribe->replace_one_field(filename=>$filename,field=>$field,value=>$value);
270  
271 =cut
272  
273 sub replace_one_field {
274 my $self = shift;
275 my %args = @_;
276 say STDERR whoami() if $self->{verbose} > 2;
277  
278 my $filename = $args{filename};
279 my $field = $args{field};
280 my $value = $args{value};
281  
282 my $info = $self->_load_meta($filename);
283 $info->{$field} = $value;
284 $self->_write_meta(filename=>$filename,meta=>$info);
285 } # replace_one_field
286  
287 =head1 Helper Functions
288  
289 Private interface.
290  
291 =head2 _has_yaml
292  
293 The file has YAML if the FIRST line is '---'
294  
295 =cut
296 sub _has_yaml {
297 my $self = shift;
298 my $filename = shift;
299  
300 my $fh;
301 if (!open($fh, '<', $filename))
302 {
303 die __PACKAGE__, " Unable to open file '" . $filename ."': $!\n";
304 }
305  
306 my $first_line = <$fh>;
307 close($fh);
308 return 0 if !$first_line;
309  
310 chomp $first_line;
311 return ($first_line eq '---');
312 } # _has_yaml
313  
314 =head2 _yaml_and_more
315  
316 Get the YAML part of the file (if any)
317 by reading the stuff between the first set of --- lines
318 and also the rest of the file as a separate part.
319  
320 =cut
321 sub _yaml_and_more {
322 my $self = shift;
323 my $filename = shift;
324 say STDERR whoami() if $self->{verbose} > 2;
325  
326 my $fh;
327 if (!open($fh, '<', $filename))
328 {
329 die __PACKAGE__, " Unable to open file '" . $filename ."': $!\n";
330 }
331  
332 my $yaml_str = '';
333 my $more_str = '';
334 my $yaml_started = 0;
335 my $yaml_finished = 0;
336 while (<$fh>) {
337 if (/^---$/) {
338 # There could be "---" lines after the YAML is finished!
339 if (!$yaml_started and !$yaml_finished)
340 {
341 $yaml_started = 1;
342 next;
343 }
344 elsif (!$yaml_finished) # end of the YAML part
345 {
346 $yaml_started = 0;
347 $yaml_finished = 1;
348 next;
349 }
350 }
351 if ($yaml_started)
352 {
353 $yaml_str .= $_;
354 }
355 elsif ($yaml_finished)
356 {
357 $more_str .= $_;
358 }
359 }
360 close($fh);
361 return ($yaml_str,$more_str);
362 } # _yaml_and_more
363  
364 =head2 _load_meta
365  
366 Quick non-checking loading of the meta-data. Does not standardize any fields.
367  
368 =cut
369 sub _load_meta {
370 my $self = shift;
371 my $filename = shift;
372 say STDERR whoami() if $self->{verbose} > 2;
373  
374 my ($yaml_str,$more) = $self->_yaml_and_more($filename);
375 my $meta;
376 eval {$meta = Load($yaml_str);};
377 if ($@)
378 {
379 warn __PACKAGE__, " Load of data failed: $@";
380 return {};
381 }
382 if (!$meta)
383 {
384 warn __PACKAGE__, " no legal YAML";
385 return {};
386 }
387 return $meta;
388 } # _load_meta
389  
390 =head2 _write_meta
391  
392 Overwrites the file completely with the given metadata
393 plus the rest of its contents
394 This saves multi-value comma-separated fields as arrays.
395  
396 =cut
397 sub _write_meta {
398 my $self = shift;
399 my %args = @_;
400  
401 my $filename = $args{filename};
402 my $meta = $args{meta};
403 # restore multi-value comma-separated fields to arrays
404 foreach my $fn (keys %{$self->{wanted_fields}})
405 {
406 if ($self->{wanted_fields}->{$fn} eq 'MULTI'
407 and exists $meta->{$fn}
408 and defined $meta->{$fn}
409 and $meta->{$fn} =~ /,/)
410 {
411 my @vals = split(/,/, $meta->{$fn});
412 $meta->{$fn} = \@vals;
413 }
414 }
415  
416 my ($yaml_str,$file_rest) = $self->_yaml_and_more($filename);
417 my $fh;
418 if (!open($fh, '>', $filename))
419 {
420 die __PACKAGE__, " Unable to open file '" . $filename ."': $!\n";
421 }
422 print $fh Dump($meta);
423 print $fh "---\n";
424 print $fh $file_rest;
425 close $fh;
426 } # _write_meta
427  
428 =head1 BUGS
429  
430 Please report any bugs or feature requests to the author.
431  
432 =cut
433  
434 1; # End of File::Sticker::Scribe
435 __END__
« Sticker/Scribe/Yaml.pm ↑ Summary Sticker/Scribe/Epub.pm »