File Coverage

blib/lib/Pandoc/Metadata.pm
Criterion Covered Total %
statement 68 70 97.1
branch 31 34 91.1
condition 13 14 92.8
subroutine 20 21 95.2
pod n/a
total 132 139 94.9


line stmt bran cond sub pod time code
1             package Pandoc::Metadata;
2 30     30   282 use strict;
  30         87  
  30         1223  
3 30     30   194 use warnings;
  30         71  
  30         1143  
4 30     30   828 use 5.010001;
  30         131  
5              
6 30     30   206 use Pandoc::Elements;
  30         79  
  30         9711  
7 30     30   257 use Scalar::Util qw(blessed reftype);
  30         91  
  30         1873  
8 30     30   24777 use JSON::PP;
  30         510728  
  30         3864  
9              
10             # packages and methods
11              
12             {
13             # key-value map of metadata fields
14             package Pandoc::Document::Metadata;
15              
16             {
17 30     30   469 no warnings 'once';
  30         90  
  30         36347  
18             *to_json = \&Pandoc::Document::Element::to_json;
19             }
20              
21             sub TO_JSON {
22 18     18   358 return { map { $_ => $_[0]->{$_} } keys %{ $_[0] } };
  12         490  
  18         137  
23             }
24              
25             sub value {
26 26     26   69 my $map = { c => shift };
27 26         65 Pandoc::Document::MetaMap::value( $map, @_ )
28             }
29             }
30              
31             {
32             # metadata element parent class
33             package Pandoc::Document::Meta;
34             our @ISA = ('Pandoc::Document::Element');
35 9     9   331 sub is_meta { 1 }
36 0     0   0 sub value { shift->value(@_) }
37             }
38              
39             # methods
40              
41             sub _value_args {
42 91     91   165 my $content = shift->{c};
43 91 100       327 my ($pointer, %opts) = @_ % 2 ? @_ : (undef, @_);
44              
45 91   100     399 $opts{pointer} = $pointer // $opts{pointer} // '';
      100        
46              
47 91         339 return ($content, %opts);
48             }
49              
50             sub Pandoc::Document::MetaString::value {
51 11     11   23 my ($content, %opts) = _value_args(@_);
52 11 50       133 $opts{pointer} eq '' ? $content : undef;
53             }
54              
55             sub Pandoc::Document::MetaBool::set_content {
56 15 100 100 15   138 $_[0]->{c} = $_[1] && $_[1] ne 'false' && $_[1] ne 'FALSE' ? 1 : 0;
57             }
58              
59             sub Pandoc::Document::MetaBool::TO_JSON {
60             return {
61             t => 'MetaBool',
62 11 100   11   53 c => $_[0]->{c} ? JSON::true() : JSON::false(),
63             };
64             }
65              
66             sub Pandoc::Document::MetaBool::value {
67 13     13   28 my ($content, %opts) = _value_args(@_);
68 13 100       36 return if $opts{pointer} ne '';
69              
70 12 100 100     40 if (($opts{boolean} // '') eq 'JSON::PP') {
71 3 100       17 $content ? JSON::true() : JSON::false();
72             } else {
73 9 100       31 $content ? 1 : 0;
74             }
75             }
76              
77             sub Pandoc::Document::MetaMap::value {
78 38     38   81 my ($map, %opts) = _value_args(@_);
79              
80 38 100       109 if ($opts{pointer} eq '') {
81 9         28 return { map { $_ => $map->{$_}->value(%opts) } keys %$map };
  26         131  
82             } else {
83 29         160 my ($key, @fields) = split '/', $opts{pointer};
84 29         73 $key =~ s!~1!/!g;
85 29         44 $key =~ s/~0/~/g;
86 29         73 $opts{pointer} = join '/', @fields;
87 29 100       122 return $map->{$key} ? $map->{$key}->value(%opts) : undef;
88             }
89             }
90              
91             sub Pandoc::Document::MetaList::value {
92 9     9   18 my ($content, %opts) = _value_args(@_);
93 9 100       38 if ($opts{pointer} =~ /^[1-9]*[0-9]$/) {
    100          
94 3         10 my $value = $content->[$opts{pointer}];
95 3 100       16 defined $value ? $value->value(%opts, pointer => '') : undef;
96             } elsif ($opts{pointer} eq '') {
97 5         13 [ map { $_->value(%opts) } @$content ]
  9         29  
98             } else {
99             undef
100 1         7 }
101             }
102              
103             sub Pandoc::Document::MetaInlines::value {
104 16     16   31 my ($content, %opts) = _value_args(@_);
105 16 50       39 return if $opts{pointer} ne '';
106              
107 16 50 50     71 if ($opts{element} // '' eq 'keep') {
108 0         0 $content;
109             } else {
110 16         37 join '', map { $_->string } @$content;
  16         54  
111             }
112             }
113              
114             sub Pandoc::Document::MetaBlocks::string {
115 3     3   9 join "\n\n", map { $_->string } @{$_[0]->content};
  6         24  
  3         96  
116             }
117              
118             sub Pandoc::Document::MetaBlocks::value {
119 4     4   9 my ($content, %opts) = _value_args(@_);
120 4 100       25 return if $opts{pointer} ne '';
121              
122 3 100 100     15 if ($opts{element} // '' eq 'keep') {
123 1         7 $content;
124             } else {
125 2         7 $_[0]->string;
126             }
127             }
128              
129             1;
130             __END__