blib/lib/Pandoc/Elements.pm

filter: Statementstmt 80.6% 507 / 629 Branchbran 47.8% 200 / 418 Conditioncond 69.0% 58 / 84 MC/DCmcdc 54.9% 28 / 51 Subroutinesub 78.6% 118 / 150 Podpod 31.5% 6 / 19 totaltotal 67.8% 917 / 1351 CC 240 SCAR 22.0
CC
240
Cov
68%
_sections
132.0
to_pandoc
20.0
Document
16.1
CRAP 9.0
« blib/lib/Pandoc/Document/Element.pm ↑ Summary blib/lib/Pandoc/Walker.pm »
1 package Pandoc::Elements;
2 31 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 ## no critic (ProhibitNoStrict, ProhibitSubroutinePrototypes)
4 31 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 31 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 31 our $VERSION = '0.38';
8  
9 31 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 31 ▶use JSON qw(decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 31 ▶use Scalar::Util qw(blessed reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 31 ▶use Pandoc::Walker qw(walk);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 31 ▶use Pandoc::Version;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 31 our $PANDOC_VERSION; # a string like '1.16'
17 31 ▶$PANDOC_VERSION ||= eval { Pandoc::Version->new($ENV{PANDOC_VERSION}) };
Condition66%2/3
$Pandoc::Elements::PANDOC_VERSION ||= eval { do { "Pandoc::Version"->new($ENV{"PANDOC_VERSION"}) } }
l!l&&r!l&&!r
0130
MC/DC50%1/2
$Pandoc::Elements::PANDOC_VERSION ||= eval { do { "Pandoc::Version"->new($ENV{"PANDOC_VERSION"}) } }
$Pandoc::Elements::PANDOC_VERSION eval { do { "Pandoc::Version"->new($ENV{"PANDOC_VERSION"}) } }
Truth table66%2/3
A ||= B
A= $Pandoc::Elements::PANDOC_VERSIONB= eval { do { "Pandoc::Version"->new($ENV{"PANDOC_VERSION"}) } }
ABresult
1X1
011
000
18  
19 # internal variables
20  
21 31 my $PANDOC_API_MIN = Pandoc::Version->new('1.12.3'); # since pandoc 1.12.1
22 31 my $PANDOC_BIN_MIN = Pandoc::Version->new('1.12.1');
23  
24 # release version => minimal required api version
25 31 my @REQUIRED_API = map { Pandoc::Version->new($_) }
26 '1.19' => '1.17', # pandoc 1.19 has api 1.17.0.4, compatible with api 1.17
27 '1.18' => '1.17', # pandoc 1.18 has api 1.17.0.4, compatible with api 1.17
28 '1.16' => '1.16', # pandoc 1.16 has api 1.16
29 '1.17' => '1.16', # pandoc 1.17 has api 1.16
30 ;
31  
32 32 ▶sub _as_pandoc_version {
Subroutine _as_pandoc_version: called CC=3 SCAR=11.1
33 32 ▶ (blessed $_[0] and $_[0]->isa('Pandoc::Version'))
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[0]) && $_[0]->isa('Pandoc::Version') ? : 9 23 32
Condition66%2/3
builtin::blessed($_[0]) and $_[0]->isa("Pandoc::Version")
!ll&&!rl&&r
2309
MC/DC50%1/2
builtin::blessed($_[0]) and $_[0]->isa("Pandoc::Version")
builtin::blessed($_[0]) $_[0]->isa("Pandoc::Version")
Truth table66%2/3
A and B
A= builtin::blessed($_[0])B= $_[0]->isa("Pandoc::Version")
ABresult
0X0
100
111
34 ? $_[0] : Pandoc::Version->new($_[0])
35 }
36  
37 55 ▶sub pandoc_version {
Subroutine pandoc_version: called CC=5 SCAR=16.1
Pod documented
38 55 ▶ if (@_) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 30 25 55
elsif (defined $Pandoc::Elements::PANDOC_VERSION) { } 23 2 25
39 30 my $doc = shift;
40 30 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 9 21 30
41 9 ▶ $doc->api_version(
Condition100%2/2
_minimum_pandoc_api_for(@_) // croak("pandoc version not supported")
l!l
72
MC/DC100%1/1
_minimum_pandoc_api_for(@_) // croak("pandoc version not supported")
_minimum_pandoc_api_for(@_)
Truth table100%2/2
A
A= _minimum_pandoc_api_for(@_)
Aresult
11
00
42 _minimum_pandoc_api_for(@_)
43 // croak "pandoc version not supported"
44 );
45 }
46 28 _minimum_pandoc_version_for_api($doc->api_version);
47 } elsif (defined $PANDOC_VERSION) {
48 23 _as_pandoc_version($PANDOC_VERSION)
49 } else {
50 2 $REQUIRED_API[0]
51 }
52 }
53  
54 28 ▶sub _minimum_pandoc_version_for_api {
Subroutine _minimum_pandoc_version_for_api: called CC=7 SCAR=19.5
55 28 my $api = shift;
56  
57 28 my $version;
58  
59 28 foreach (grep { $_ % 2} 0 .. @REQUIRED_API) { # 1,3,...
60 112 ▶ if ($api->match($REQUIRED_API[$_]) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($api->match($REQUIRED_API[$_])) 50 62 112
61 50 ▶ if (!$version or $version > $REQUIRED_API[$_-1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $version or $version > $REQUIRED_API[$_ - 1]) 40 10 50
Condition100%3/3
not $version or $version > $REQUIRED_API[$_ - 1]
l!l&&r!l&&!r
251510
MC/DC100%2/2
not $version or $version > $REQUIRED_API[$_ - 1]
not $version $version > $REQUIRED_API[$_ - 1]
Truth table100%3/3
not(A) or B
A= not $versionB= $version > $REQUIRED_API[$_ - 1]
ABresult
1X1
011
000
62 40 $version = $REQUIRED_API[$_-1]
63 }
64 }
65 }
66  
67 28 ▶ if (!$version and $api >= $PANDOC_API_MIN) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $version and $api >= $PANDOC_API_MIN) { } 3 25 28
Condition66%2/3
not $version and $api >= $PANDOC_API_MIN
!ll&&!rl&&r
2503
MC/DC50%1/2
not $version and $api >= $PANDOC_API_MIN
not $version $api >= $PANDOC_API_MIN
Truth table66%2/3
not(A) and B
A= not $versionB= $api >= $PANDOC_API_MIN
ABresult
0X0
100
111
68 3 $PANDOC_BIN_MIN;
69 } else {
70 25 return $version;
71 }
72 }
73  
74 9 ▶sub _minimum_pandoc_api_for {
Subroutine _minimum_pandoc_api_for: called CC=6 SCAR=17.9
75 9 my $version = _as_pandoc_version(shift);
76 9 ▶ return if @$version <= 1; # require major.minor
Branch: 1 branch
BranchTrueFalseTotal
if @$version <= 1 0 9 9
77  
78 9 foreach (grep { $_ % 2} 0 .. @REQUIRED_API) { # 1,3,...
79 28 ▶ if ($version->match($REQUIRED_API[$_-1]) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version->match($REQUIRED_API[$_ - 1])) 5 23 28
80 5 return $REQUIRED_API[$_];
81 }
82 }
83  
84 # required version is newer than any known version
85 # return the latest known api version and hope it has not changed
86 4 ▶ if ($version > $REQUIRED_API[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version > $REQUIRED_API[0]) 1 3 4
87 1 return $REQUIRED_API[1];
88 }
89  
90 3 ▶ return $version >= $PANDOC_BIN_MIN ? $PANDOC_API_MIN : undef;
Branch: 1 branch
BranchTrueFalseTotal
$version >= $PANDOC_BIN_MIN ? : 1 2 3
91 }
92  
93 our %ELEMENTS = (
94  
95 # BLOCK ELEMENTS
96 Plain => [ Block => 'content' ],
97 Para => [ Block => 'content' ],
98 CodeBlock => [ Block => qw(attr content) ],
99 RawBlock => [ Block => qw(format content) ],
100 BlockQuote => [ Block => 'content' ],
101 OrderedList => [ Block => qw(attr content/items) ],
102 BulletList => [ Block => 'content/items' ],
103 DefinitionList => [ Block => 'content/items:[DefinitionPair]' ],
104 Header => [ Block => qw(level attr content) ],
105 HorizontalRule => ['Block'],
106 Table => [ Block => qw(caption alignment widths headers rows) ],
107 Div => [ Block => qw(attr content) ],
108 Null => ['Block'],
109 LineBlock => [ Block => qw(content) ],
110  
111 # INLINE ELEMENTS
112 Str => [ Inline => 'content' ],
113 Emph => [ Inline => 'content' ],
114 Strong => [ Inline => 'content' ],
115 Strikeout => [ Inline => 'content' ],
116 Superscript => [ Inline => 'content' ],
117 Subscript => [ Inline => 'content' ],
118 SmallCaps => [ Inline => 'content' ],
119 Quoted => [ Inline => qw(type content) ],
120 Cite => [ Inline => qw(citations:[Citation] content) ],
121 Code => [ Inline => qw(attr content) ],
122 Space => ['Inline'],
123 SoftBreak => ['Inline'],
124 LineBreak => ['Inline'],
125 Math => [ Inline => qw(type content) ],
126 RawInline => [ Inline => qw(format content) ],
127 Link => [ Inline => qw(attr content target) ],
128 Image => [ Inline => qw(attr content target) ],
129 Note => [ Inline => 'content' ],
130 Span => [ Inline => qw(attr content) ],
131  
132 # METADATA ELEMENTS
133 MetaBool => [ Meta => 'content' ],
134 MetaString => [ Meta => 'content' ],
135 MetaMap => [ Meta => 'content' ],
136 MetaInlines => [ Meta => 'content' ],
137 MetaList => [ Meta => 'content' ],
138 MetaBlocks => [ Meta => 'content' ],
139  
140 # TYPE KEYWORDS
141 31 map { $_ => ['Keyword'] }
142 qw(DefaultDelim Period OneParen TwoParens SingleQuote DoubleQuote
143 DisplayMath InlineMath AuthorInText SuppressAuthor NormalCitation
144 AlignLeft AlignRight AlignCenter AlignDefault DefaultStyle Example
145 Decimal LowerRoman UpperRoman LowerAlpha UpperAlpha)
146 );
147  
148 31 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
149 31 our @EXPORT = (
150 keys %ELEMENTS,
151 qw(Document attributes metadata citation pandoc_version pandoc_json pandoc_query)
152 );
153 31 our @EXPORT_OK = ( @EXPORT, 'element' );
154  
155 # create constructor functions
156 31 foreach my $name ( keys %ELEMENTS ) {
157 31 ▶ no strict 'refs'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
158  
159 1891 my ( $parent, @accessors ) = @{ $ELEMENTS{$name} };
160 1891 my $numargs = scalar @accessors;
161 1891 my @parents = map { "Pandoc::Document::$_" } ($parent);
162 2139 $parent = join ' ', map { "Pandoc::Document::$_" } $parent,
163 1891 map { 'AttributesRole' } grep { $_ eq 'attr' } @accessors;
164  
165 ## no critic (ProhibitStringyEval)
166 1891 eval "package Pandoc::Document::$name; our \@ISA = qw($parent);";
167  
168 1891 *{ __PACKAGE__ . "::$name" } = Scalar::Util::set_prototype(
169 608 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
170 608 ▶ croak "$name expects $numargs arguments, but given " . scalar @_
Branch: 1 branch
BranchTrueFalseTotal
if @_ != $numargs 1 607 608
171 if @_ != $numargs;
172 607 ▶ my $self = bless {
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 444 163 607
173 t => $name,
174 c => ( @_ == 1 ? $_[0] : [@_] )
175 }, "Pandoc::Document::$name";
176 607 $self->set_content(@_);
177 607 $self;
178 },
179 1891 '$' x $numargs
180 );
181  
182 1891 ▶ for ( my $i = 0 ; $i < @accessors ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @accessors) 1674 1891 3565
183 1674 ▶ my $member = @accessors == 1 ? "\$e->{c}" : "\$e->{c}->[$i]";
Branch: 1 branch
BranchTrueFalseTotal
@accessors == 1 ? : 620 1054 1674
184 1674 my $code = "my \$e = shift; $member = ( 1 == \@_ ? \$_[0] : [\@_] ) if \@_; return";
185 # auto-bless on access via accessor (TODO: move to constructor?)
186 1674 ▶ $code .= $accessors[$i] =~ s/:\[(.+)\]$//
Branch: 1 branch
BranchTrueFalseTotal
$accessors[$i] =~ s/:\[(.+)\]$// ? : 62 1612 1674
187 ? " [ map { bless \$_, 'Pandoc::Document::$1' } \@{$member} ];"
188 : " $member;";
189 1674 for ( split '/', $accessors[$i] ) {
190 ## no critic
191 0 ▶ *{"Pandoc::Document::${name}::$_"} = eval "sub { $code }";
Subroutine __ANON__: not called CC=3 SCAR=16.3
Subroutine __ANON__: called CC=3 SCAR=16.3
Subroutine __ANON__: called CC=3 SCAR=16.3
Subroutine __ANON__: not called CC=3 SCAR=16.3
Subroutine __ANON__: not called CC=3 SCAR=16.3
Subroutine __ANON__: not called CC=3 SCAR=16.3
Subroutine __ANON__: called CC=3 SCAR=16.3
Subroutine __ANON__: called CC=3 SCAR=16.3
Subroutine __ANON__: not called CC=3 SCAR=16.3
Subroutine __ANON__: not called CC=3 SCAR=16.3
Subroutine __ANON__: called CC=3 SCAR=16.3
Subroutine __ANON__: called CC=3 SCAR=16.3
Subroutine __ANON__: called CC=3 SCAR=16.3
Subroutine __ANON__: not called CC=3 SCAR=16.3
Subroutine __ANON__: not called CC=3 SCAR=16.3
Subroutine __ANON__: not called CC=3 SCAR=16.3
Subroutine __ANON__: not called CC=3 SCAR=16.3
Subroutine __ANON__: called CC=3 SCAR=16.3
Subroutine __ANON__: called CC=3 SCAR=16.3
Subroutine __ANON__: called CC=3 SCAR=16.3
Subroutine __ANON__: called CC=3 SCAR=16.3
Subroutine __ANON__: called CC=3 SCAR=16.3
Subroutine __ANON__: called CC=3 SCAR=16.3
Subroutine __ANON__: called CC=3 SCAR=16.3
Subroutine __ANON__: called CC=3 SCAR=16.3
Subroutine __ANON__: called CC=3 SCAR=16.3
Subroutine __ANON__: called CC=3 SCAR=16.3
Subroutine __ANON__: called CC=3 SCAR=16.3
Subroutine __ANON__: not called CC=3 SCAR=16.3
Subroutine __ANON__: not called CC=3 SCAR=16.3
Subroutine __ANON__: not called CC=3 SCAR=16.3
Subroutine __ANON__: not called CC=3 SCAR=16.3
Subroutine __ANON__: called CC=3 SCAR=16.3
Subroutine __ANON__: called CC=3 SCAR=16.3
Subroutine __ANON__: called CC=3 SCAR=16.3
Subroutine __ANON__: called CC=3 SCAR=16.3
Subroutine __ANON__: not called CC=3 SCAR=16.3
Subroutine __ANON__: called CC=3 SCAR=16.3
Subroutine __ANON__: not called CC=3 SCAR=16.3
Subroutine __ANON__: called CC=3 SCAR=16.3
Subroutine __ANON__: called CC=3 SCAR=16.3
Subroutine __ANON__: called CC=3 SCAR=16.3
Subroutine __ANON__: called CC=3 SCAR=16.3
Subroutine __ANON__: called CC=3 SCAR=16.3
Subroutine __ANON__: called CC=3 SCAR=16.3
Subroutine __ANON__: called CC=3 SCAR=16.3
Subroutine __ANON__: called CC=3 SCAR=16.3
Subroutine __ANON__: not called CC=3 SCAR=16.3
Subroutine __ANON__: called CC=3 SCAR=16.3
Subroutine __ANON__: not called CC=3 SCAR=16.3
Subroutine __ANON__: not called CC=3 SCAR=16.3
Subroutine __ANON__: called CC=3 SCAR=16.3
Subroutine __ANON__: not called CC=3 SCAR=16.3
Subroutine __ANON__: called CC=3 SCAR=16.3
Subroutine __ANON__: called CC=3 SCAR=16.3
Subroutine __ANON__: called CC=3 SCAR=16.3
Subroutine __ANON__: not called CC=3 SCAR=16.3
Branch: 114 branches
BranchTrueFalseTotal
if @_ 0 0 0
1 == @_ ? : 0 0 0
if @_ 0 13 13
1 == @_ ? : 0 0 0
if @_ 0 31 31
1 == @_ ? : 0 0 0
if @_ 0 0 0
1 == @_ ? : 0 0 0
if @_ 0 0 0
1 == @_ ? : 0 0 0
if @_ 0 0 0
1 == @_ ? : 0 0 0
if @_ 0 35 35
1 == @_ ? : 0 0 0
if @_ 0 1 1
1 == @_ ? : 0 0 0
if @_ 0 0 0
1 == @_ ? : 0 0 0
if @_ 0 0 0
1 == @_ ? : 0 0 0
if @_ 0 15 15
1 == @_ ? : 0 0 0
if @_ 0 10 10
1 == @_ ? : 0 0 0
if @_ 0 65 65
1 == @_ ? : 0 0 0
if @_ 0 0 0
1 == @_ ? : 0 0 0
if @_ 0 0 0
1 == @_ ? : 0 0 0
if @_ 0 0 0
1 == @_ ? : 0 0 0
if @_ 0 0 0
1 == @_ ? : 0 0 0
if @_ 0 17 17
1 == @_ ? : 0 0 0
if @_ 0 1 1
1 == @_ ? : 0 0 0
if @_ 0 1 1
1 == @_ ? : 0 0 0
if @_ 0 12 12
1 == @_ ? : 0 0 0
if @_ 0 10 10
1 == @_ ? : 0 0 0
if @_ 0 5 5
1 == @_ ? : 0 0 0
if @_ 0 78 78
1 == @_ ? : 0 0 0
if @_ 0 8 8
1 == @_ ? : 0 0 0
if @_ 0 14 14
1 == @_ ? : 0 0 0
if @_ 0 10 10
1 == @_ ? : 0 0 0
if @_ 0 8 8
1 == @_ ? : 0 0 0
if @_ 0 0 0
1 == @_ ? : 0 0 0
if @_ 0 0 0
1 == @_ ? : 0 0 0
if @_ 0 0 0
1 == @_ ? : 0 0 0
if @_ 0 0 0
1 == @_ ? : 0 0 0
if @_ 0 23 23
1 == @_ ? : 0 0 0
if @_ 1 28 29
1 == @_ ? : 1 0 1
if @_ 1 5 6
1 == @_ ? : 1 0 1
if @_ 0 8 8
1 == @_ ? : 0 0 0
if @_ 0 0 0
1 == @_ ? : 0 0 0
if @_ 0 6 6
1 == @_ ? : 0 0 0
if @_ 0 0 0
1 == @_ ? : 0 0 0
if @_ 0 5 5
1 == @_ ? : 0 0 0
if @_ 0 28 28
1 == @_ ? : 0 0 0
if @_ 1 6 7
1 == @_ ? : 1 0 1
if @_ 0 1 1
1 == @_ ? : 0 0 0
if @_ 0 1 1
1 == @_ ? : 0 0 0
if @_ 0 14 14
1 == @_ ? : 0 0 0
if @_ 0 1 1
1 == @_ ? : 0 0 0
if @_ 0 37 37
1 == @_ ? : 0 0 0
if @_ 0 0 0
1 == @_ ? : 0 0 0
if @_ 0 13 13
1 == @_ ? : 0 0 0
if @_ 0 0 0
1 == @_ ? : 0 0 0
if @_ 0 0 0
1 == @_ ? : 0 0 0
if @_ 0 11 11
1 == @_ ? : 0 0 0
if @_ 0 0 0
1 == @_ ? : 0 0 0
if @_ 0 50 50
1 == @_ ? : 0 0 0
if @_ 1 1 2
1 == @_ ? : 1 0 1
if @_ 0 2 2
1 == @_ ? : 0 0 0
if @_ 0 0 0
1 == @_ ? : 0 0 0
192 }
193 }
194 }
195  
196 4 ▶sub element {
Subroutine element: called CC=3 SCAR=11.0
Pod documented
197 4 my $name = shift;
198 31 ▶ no strict 'refs'; #
Subroutine BEGIN: called CC=1 SCAR=0.0
199 4 ▶ croak "undefined element" unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 4 4
200 4 ▶ croak "unknown element $name" unless $ELEMENTS{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless $Pandoc::Elements::ELEMENTS{$name} 1 3 4
201 3 &$name(@_);
202 }
203  
204 55 ▶sub Document {
Subroutine Document: called CC=16 SCAR=27.8
Pod documented
205  
206 55 my $from_json;
207 55 my $arg = do {
208 55 ▶ if ( @_ == 1 ) {
Branch: 3 branches
BranchTrueFalseTotal
if (@_ == 1) { } 30 25 55
elsif (@_ == 2) { } 9 16 25
elsif (@_ % 2) { } 1 15 16
209 30 $from_json = 1;
210 30 ▶ my $reftype = reftype $_[0] // '';
Condition100%2/2
builtin::reftype($_[0]) // ""
l!l
291
MC/DC100%1/1
builtin::reftype($_[0]) // ""
builtin::reftype($_[0])
Truth table100%2/2
A
A= builtin::reftype($_[0])
Aresult
11
00
211 30 ▶ if ( $reftype eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($reftype eq 'ARRAY') { } 21 9 30
elsif ($reftype eq 'HASH') { } 8 1 9
212 # old JSON format
213 {
214 meta => $_[0]->[0]->{unMeta},
215 21 blocks => $_[0]->[1],
216 api_version => 1.16,
217 }
218 } elsif ( $reftype eq 'HASH' ) {
219 8 $_[0]
220 } else {
221 1 croak 'Document: expect array or hash reference'
222 }
223 } elsif ( @_ == 2 ) {
224 # \%meta, \@blocks
225 9 { meta => $_[0], blocks => $_[1] }
226 } elsif ( @_ % 2 ) {
227 # odd number of args
228 1 croak "Document: too many or ambiguous arguments";
229 } else {
230 # even number of args: api_version as named parameter
231 15 { meta => shift, blocks => shift, @_ }
232 }
233 };
234  
235 # prefer haskell-style key but accept perl-style and abbreviated key
236 my $api_version = $arg->{'pandoc-api-version'}
237 // $arg->{pandoc_api_version}
238 53 ▶ // $arg->{api_version};
Condition83%5/6
($arg->{"pandoc-api-version"} // $arg->{"pandoc_api_version"}) // $arg->{"api_version"}
l!l&&r!l&&!r
62720
$arg->{"pandoc-api-version"} // $arg->{"pandoc_api_version"}
l!l&&r!l&&!r
6047
MC/DC66%2/3
($arg->{"pandoc-api-version"} // $arg->{"pandoc_api_version"}) // $arg->{"api_version"}
$arg->{"pandoc-api-version"} $arg->{"pandoc_api_version"} $arg->{"api_version"}
Truth table75%3/4
A // B // C
A= $arg->{"pandoc-api-version"}B= $arg->{"pandoc_api_version"}C= $arg->{"api_version"}
ABCresult
1XX1
01X1
0011
0000
239  
240 # We copy values here because $arg may not be a pure AST representation
241 53 ▶ my $doc = bless { blocks => ( $arg->{blocks} // [] ) }, 'Pandoc::Document';
Condition50%1/2
$arg->{"blocks"} // []
l!l
530
MC/DC0%0/1
$arg->{"blocks"} // []
$arg->{"blocks"}
Truth table50%1/2
A
A= $arg->{"blocks"}
Aresult
11
00
242  
243 # unblessed metadata in internal format can only come from JSON
244 53 ▶ my $meta = $arg->{meta} // {};
Condition100%2/2
$arg->{"meta"} // {}
l!l
521
MC/DC100%1/1
$arg->{"meta"} // {}
$arg->{"meta"}
Truth table100%2/2
A
A= $arg->{"meta"}
Aresult
11
00
245 53 ▶ if ($from_json) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from_json) { } 29 24 53
246 29 ▶ croak "Document metadata must be a hash" unless 'HASH' eq reftype $meta;
Branch: 1 branch
BranchTrueFalseTotal
unless "HASH" eq builtin::reftype($meta) 0 29 29
247 $doc->{meta} = bless {
248 29 map { $_ => _bless_pandoc_element( $meta->{$_} ) } keys %$meta
249 }, 'Pandoc::Document::Metadata';
250 } else {
251 # otherwise allow user-friendly upgrade via 'metadata' function
252 24 $doc->meta($meta)
253 }
254  
255 53 ▶ if (!defined $api_version and defined $arg->{pandoc_version}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $api_version and defined $arg->{'pandoc_version'}) { } 9 44 53
Condition100%3/3
not defined $api_version and defined $arg->{"pandoc_version"}
!ll&&!rl&&r
33119
MC/DC100%2/2
not defined $api_version and defined $arg->{"pandoc_version"}
not defined $api_version defined $arg->{"pandoc_version"}
Truth table100%3/3
not(A) and B
A= not defined $api_versionB= defined $arg->{"pandoc_version"}
ABresult
0X0
100
111
256 9 $doc->pandoc_version($arg->{pandoc_version});
257 } else {
258 44 ▶ $doc->api_version($api_version // $REQUIRED_API[1]);
Condition66%2/3
$api_version // $REQUIRED_API[1]
l!l&&r!l&&!r
33110
MC/DC0%0/2
$api_version // $REQUIRED_API[1]
$api_version $REQUIRED_API[1]
Truth table66%2/3
A // B
A= $api_versionB= $REQUIRED_API[1]
ABresult
1X1
011
000
259 }
260  
261 50 walk $doc, \&_bless_pandoc_element;
262  
263 50 return $doc;
264  
265 }
266  
267 # internal helper method
268 597 ▶sub _bless_pandoc_element {
Subroutine _bless_pandoc_element: called CC=7 SCAR=19.5
269 597 my $e = shift;
270 597 ▶ return $e unless ref $e;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $e 0 597 597
271 597 ▶ return $e if blessed $e and $e->isa('Pandoc::Document::Element');
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($e) and $e->isa("Pandoc::Document::Element") 310 287 597
Condition66%2/3
builtin::blessed($e) and $e->isa("Pandoc::Document::Element")
!ll&&!rl&&r
2870310
MC/DC50%1/2
builtin::blessed($e) and $e->isa("Pandoc::Document::Element")
builtin::blessed($e) $e->isa("Pandoc::Document::Element")
Truth table66%2/3
A and B
A= builtin::blessed($e)B= $e->isa("Pandoc::Document::Element")
ABresult
0X0
100
111
272  
273 # TODO: run recursively via set_content (don't require 'walk')
274 287 ▶ if ( 'MetaMap' eq $e->{t} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("MetaMap" eq $e->{"t"}) 5 282 287
275 5 for my $v ( values %{ $e->{c} } ) {
276 15 _bless_pandoc_element( $v );
277 }
278 }
279  
280 287 bless $e, 'Pandoc::Document::' . $e->{t};
281 287 ▶ $e->upgrade($e) if $e->can('upgrade');
Branch: 1 branch
BranchTrueFalseTotal
if $e->can("upgrade") 16 271 287
282 287 return $e;
283 }
284  
285  
286 # specific accessors
287  
288 1 ▶sub Pandoc::Document::DefinitionPair::term { $_[0]->[0] }
Subroutine term: called CC=1 SCAR=0.0
289 1 ▶sub Pandoc::Document::DefinitionPair::definitions { $_[0]->[1] }
Subroutine definitions: called CC=1 SCAR=0.0
290  
291 # additional functions
292  
293 40 ▶sub attributes($) {
Subroutine attributes: called CC=1 SCAR=0.0
Pod documented
294  
295 40 my $e = Span(['',[],[]],[]); # to make use of AttributesRole
296 40 $e->keyvals(@_);
297  
298 40 return $e->attr;
299 }
300  
301 2 ▶sub citation($) { Pandoc::Document::Citation->new( @_ ) }
Subroutine citation: called CC=1 SCAR=0.0
Pod documented
302  
303 # XXX: must require rather than use Pandoc::Metadata
304 # or its attempt to use Pandoc::Elements will result in a broken state.
305 31 require Pandoc::Metadata;
306  
307 sub metadata($); ## no critic
308  
309 49 ▶sub metadata($) { ## no critic
Subroutine metadata: called CC=14 SCAR=26.5
Pod undocumented
310 49 my $value = shift;
311 49 ▶ if ( !ref $value ) {
Branch: 5 branches
BranchTrueFalseTotal
if (not ref $value) { } 6 43 49
elsif (&JSON::is_bool($value)) { } 2 41 43
elsif (builtin::blessed($value)) { } 12 29 41
elsif (builtin::reftype($value) eq 'ARRAY') { } 2 27 29
elsif (builtin::reftype($value) eq 'HASH') { } 26 1 27
312 6 ▶ MetaString($value // '')
Condition100%2/2
$value // ""
l!l
51
MC/DC100%1/1
$value // ""
$value
Truth table100%2/2
A
A= $value
Aresult
11
00
313 }
314 elsif ( JSON::is_bool($value) ) {
315 2 MetaBool($value)
316 }
317 elsif ( blessed($value) ) {
318 12 ▶ if ( $value->can('is_meta') and $value->is_meta ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($value->can('is_meta') and $value->is_meta) { } 9 3 12
elsif ($value->can('is_inline') and $value->is_inline) { } 1 2 3
elsif ($value->can('is_block') and $value->is_block) { } 1 1 2
elsif ($value->isa('Pandoc::Document::Metadata')) { } 0 1 1
Condition100%3/3
$value->can("is_meta") and $value->is_meta
!ll&&!rl&&r
129
MC/DC100%2/2
$value->can("is_meta") and $value->is_meta
$value->can("is_meta") $value->is_meta
Truth table100%3/3
A and B
A= $value->can("is_meta")B= $value->is_meta
ABresult
0X0
100
111
319 9 $value
320 }
321 ▶ elsif ( $value->can('is_inline') and $value->is_inline ) {
Condition100%3/3
$value->can("is_inline") and $value->is_inline
!ll&&!rl&&r
111
MC/DC100%2/2
$value->can("is_inline") and $value->is_inline
$value->can("is_inline") $value->is_inline
Truth table100%3/3
A and B
A= $value->can("is_inline")B= $value->is_inline
ABresult
0X0
100
111
322 1 MetaInlines([ $value ])
323 }
324 ▶ elsif ( $value->can('is_block') and $value->is_block ) {
Condition66%2/3
$value->can("is_block") and $value->is_block
!ll&&!rl&&r
101
MC/DC50%1/2
$value->can("is_block") and $value->is_block
$value->can("is_block") $value->is_block
Truth table66%2/3
A and B
A= $value->can("is_block")B= $value->is_block
ABresult
0X0
100
111
325 1 MetaBlocks([ $value ])
326 } elsif ( $value->isa('Pandoc::Document::Metadata') ) {
327 0 MetaMap( { map { $_ => $value->{$_} } keys %$value } )
328 } else {
329 1 MetaString("$value")
330 }
331 }
332 elsif ( reftype $value eq 'ARRAY' ) {
333 2 MetaList( [ map { metadata $_ } @$value ] )
334 }
335 elsif ( reftype $value eq 'HASH' ) {
336 26 MetaMap( { map { $_ => metadata $value->{$_} } keys %$value } )
337 }
338 else {
339 1 MetaString("$value")
340 }
341 }
342  
343 24 ▶sub pandoc_json($) {
Subroutine pandoc_json: called CC=3 SCAR=11.0
Pod documented
344 24 ▶ shift if $_[0] =~ /^Pandoc::/;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /^Pandoc::/ 1 23 24
345  
346 24 my $ast = eval { decode_json( $_[0] ) };
347 24 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 23 24
348 1 $@ =~ s/ at [^ ]+Elements\.pm line \d+//;
349 1 chomp $@;
350 1 croak $@;
351 }
352 23 return Document $ast;
353 }
354  
355 31 *pandoc_query = *Pandoc::Walker::query;
356  
357 # document element packages
358  
359 {
360  
361 package Pandoc::Document;
362 31 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
363 31 ▶ use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
364 31 ▶ use Scalar::Util qw(blessed reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
365 31 ▶ use Pandoc;
Subroutine BEGIN: called CC=1 SCAR=0.0
366 31 our $VERSION = '0.04';
367 31 our @ISA = ('Pandoc::Document::Element');
368 sub blocks;
369 1 ▶ sub name { 'Document' }
Subroutine name: called CC=1 SCAR=0.0
370 160 ▶ sub meta {
Subroutine meta: called CC=3 SCAR=11.0
371 160 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 25 135 160
372 25 ▶ croak "document metadata must be a hash"
Branch: 1 branch
BranchTrueFalseTotal
unless "HASH" eq builtin::reftype($_[1]) 0 25 25
373 unless 'HASH' eq reftype $_[1];
374 25 my $map = Pandoc::Elements::metadata($_[1])->content;
375 25 $_[0]->{meta} = bless $map, 'Pandoc::Document::Metadata';
376 }
377 160 $_[0]->{meta};
378 }
379 13 ▶ sub content {
Subroutine content: called CC=2 SCAR=7.2
380 13 ▶ $_[0]->{blocks} = $_[1] if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 13 13
381 13 $_[0]->{blocks};
382 }
383 31 *blocks = \&content;
384 1 ▶ sub is_document { 1 }
Subroutine is_document: called CC=1 SCAR=0.0
385 0 ▶ sub as_block {
Subroutine as_block: not called CC=1 SCAR=6.9
386 0 bless { t => 'Div', c => [ {}, $_[0]->{blocks} ] }, 'Pandoc::Document::Div';
387 }
388 108 ▶ sub value {
Subroutine value: called CC=1 SCAR=0.0
389 108 shift->meta->value(@_);
390 }
391 31 *metavalue = \&value;
392 2 ▶ sub string {
Subroutine string: called CC=1 SCAR=0.0
393 2 join '', map { $_->string } @{$_[0]->content}
394 }
395 111 ▶ sub api_version {
Subroutine api_version: called CC=4 SCAR=13.9
396 111 my $self = shift;
397 111 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 52 59 111
398 52 my $version = Pandoc::Version->new(shift);
399 52 ▶ croak "api_version must be >= $PANDOC_API_MIN"
Branch: 1 branch
BranchTrueFalseTotal
if $version < $PANDOC_API_MIN 1 51 52
400 if $version < $PANDOC_API_MIN;
401 51 ▶ croak "api_version must have major and minor part"
Branch: 1 branch
BranchTrueFalseTotal
if @$version < 2 0 51 51
402 if @$version < 2;
403 51 $self->{'pandoc-api-version'} = $version;
404 }
405 110 return $self->{'pandoc-api-version'};
406 }
407 31 *pandoc_version = \&Pandoc::Elements::pandoc_version;
408 0 ▶ sub outline {
Subroutine outline: not called CC=1 SCAR=6.9
409 0 my ($self, $depth) = @_;
410 0 _sections( [@{$self->blocks}], $depth );
411 }
412 0 ▶ sub _sections {
Subroutine _sections: not called CC=11 SCAR=48.8
413 0 my ($list, $depth) = @_;
414 0 my (@blocks, @sections);
415  
416 # everything up to the first Header
417 0 ▶ while (@$list) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$list) 0 0 0
418 0 ▶ if ($list->[0]->name eq 'Header') {
Branch: 1 branch
BranchTrueFalseTotal
if ($list->[0]->name eq "Header") 0 0 0
419 0 ▶ last if !$depth or $depth >= $list->[0]->level;
Branch: 1 branch
BranchTrueFalseTotal
if not $depth or $depth >= $list->[0]->level 0 0 0
Condition0%0/3
not $depth or $depth >= $list->[0]->level
l!l&&r!l&&!r
000
MC/DC0%0/2
not $depth or $depth >= $list->[0]->level
not $depth $depth >= $list->[0]->level
Truth table0%0/3
not(A) or B
A= not $depthB= $depth >= $list->[0]->level
ABresult
1X1
011
000
420 }
421 0 push @blocks, shift @$list;
422 }
423  
424 # divide into sections
425 0 ▶ while (@$list) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$list) 0 0 0
426 0 my $header = shift @$list;
427 0 my $level = $header->level;
428  
429 0 my @content;
430 0 ▶ while (@$list) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$list) 0 0 0
431 0 ▶ if ($list->[0]->name eq 'Header') {
Branch: 1 branch
BranchTrueFalseTotal
if ($list->[0]->name eq "Header") 0 0 0
432 0 ▶ last if $list->[0]->level <= $level;
Branch: 1 branch
BranchTrueFalseTotal
if $list->[0]->level <= $level 0 0 0
433 }
434 0 push @content, shift @$list;
435 }
436  
437 0 ▶ my $s = ($depth and $depth < $level)
Branch: 1 branch
BranchTrueFalseTotal
$depth && $depth < $level ? : 0 0 0
Condition0%0/3
$depth and $depth < $level
!ll&&!rl&&r
000
MC/DC0%0/2
$depth and $depth < $level
$depth $depth < $level
Truth table0%0/3
A and B
A= $depthB= $depth < $level
ABresult
0X0
100
111
438 ? { blocks => \@content }
439 : _sections(\@content,$depth);
440 0 push @sections, { header => $header, %$s };
441 }
442  
443 0 return { blocks => \@blocks, sections => \@sections };
444 }
445 0 ▶ sub to_pandoc {
Subroutine to_pandoc: not called CC=4 SCAR=30.0
446 0 my ($self, @args) = @_;
447 0 ▶ my $pandoc = (@args and blessed($args[0]) and $args[0]->isa('Pandoc'))
Branch: 1 branch
BranchTrueFalseTotal
@args && builtin::blessed($args[0]) && $args[0]->isa('Pandoc') ? : 0 0 0
Condition0%0/6
@args and builtin::blessed($args[0]) and $args[0]->isa("Pandoc")
!ll&&!rl&&r
000
@args and builtin::blessed($args[0])
!ll&&!rl&&r
000
MC/DC0%0/3
@args and builtin::blessed($args[0]) and $args[0]->isa("Pandoc")
@args builtin::blessed($args[0]) $args[0]->isa("Pandoc")
Truth table0%0/4
A and B and C
A= @argsB= builtin::blessed($args[0])C= $args[0]->isa("Pandoc")
ABCresult
0XX0
10X0
1100
1111
448 ? shift(@args) : pandoc;
449  
450 0 my $api_version = $self->api_version; # save
451 0 $self->pandoc_version( $pandoc->version );
452  
453 0 my $in = $self->to_json;
454 0 $self->api_version($api_version); # restore
455  
456 0 $pandoc->run( [ -f => 'json', @args ], { in => \$in, out => \my $out } );
457 0 return $out;
458 }
459 31 foreach my $format (qw(markdown latex html rst plain)) {
460 31 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
461 155 ▶ *{ __PACKAGE__ . "::to_$format" } = sub {
Subroutine __ANON__: not called CC=1 SCAR=1.2
462 0 shift()->to_pandoc( @_, '-t' => $format );
463 }
464 155 }
465 }
466  
467 {
468 31 package Pandoc::Document::Keyword;
469 31 our @ISA = ('Pandoc::Document::Element');
470 }
471  
472 {
473  
474 31 package Pandoc::Document::Element;
475 31 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
476 31 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
477 31 our $VERSION = $Pandoc::Document::VERSION;
478 31 ▶ use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
479 31 ▶ use Scalar::Util qw(reftype blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
480 31 ▶ use Pandoc::Walker ();
Subroutine BEGIN: called CC=1 SCAR=0.0
481 31 ▶ use Pandoc::Selector;
Subroutine BEGIN: called CC=1 SCAR=0.0
482 31 ▶ use subs qw(walk query transform); # Silence syntax warnings
Subroutine BEGIN: called CC=1 SCAR=0.0
483  
484 33 ▶ sub to_json {
Subroutine to_json: called CC=1 SCAR=0.0
Pod undocumented
485 33 JSON->new->utf8->canonical->convert_blessed->encode( $_[0] );
486 }
487  
488 613 ▶ sub TO_JSON {
Subroutine TO_JSON: called CC=9 SCAR=22.2
Pod undocumented
489  
490 # Run everything thru this method so arrays/hashes are cloned
491 # and objects without TO_JSON methods are stringified.
492 # Required to ensure correct scalar types for Pandoc.
493  
494 # There is no easy way in Perl to tell if a scalar value is already a string or number,
495 # so we stringify all scalar values and numify/boolify as needed afterwards.
496  
497 613 my ( $ast, $maybe_blessed ) = @_;
498 613 ▶ if ( $maybe_blessed && blessed $ast ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($maybe_blessed and builtin::blessed($ast)) { } 199 414 613
elsif ('ARRAY' eq builtin::reftype($ast)) { } 183 231 414
elsif ('HASH' eq builtin::reftype($ast)) { } 231 0 231
Condition100%3/3
$maybe_blessed and builtin::blessed($ast)
!ll&&!rl&&r
231183199
MC/DC100%2/2
$maybe_blessed and builtin::blessed($ast)
$maybe_blessed builtin::blessed($ast)
Truth table100%3/3
A and B
A= $maybe_blessedB= builtin::blessed($ast)
ABresult
0X0
100
111
499 199 ▶ return $ast if $ast->can('TO_JSON'); # JSON.pm will convert
Branch: 1 branch
BranchTrueFalseTotal
if $ast->can("TO_JSON") 199 0 199
500 # may have overloaded stringification! Should we check?
501 # require overload;
502 # return "$ast" if overload::Method($ast, q/""/) or overload::Method($ast, q/0+/);
503 # carp "Non-stringifiable object $ast";
504 0 return "$ast";
505 }
506 elsif ( 'ARRAY' eq reftype $ast ) {
507 183 ▶ return [ map { ref($_) ? TO_JSON( $_, 1 ) : "$_"; } @$ast ];
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 269 36 305
508 }
509 elsif ( 'HASH' eq reftype $ast ) {
510 231 my %ret = %$ast;
511 231 ▶ while ( my ( $k, $v ) = each %ret ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %ret) 462 231 693
512 462 ▶ $ret{$k} = ref($v) ? TO_JSON( $v, 1 ) : "$v";
Branch: 1 branch
BranchTrueFalseTotal
ref $v ? : 113 349 462
513 }
514 231 return \%ret;
515 }
516 0 else { return "$ast" }
517 }
518  
519 722 ▶ sub name { $_[0]->{t} }
Subroutine name: called CC=1 SCAR=0.0
Pod undocumented
520 0 ▶ sub content {
Subroutine content: not called CC=2 SCAR=17.9
Pod undocumented
521 0 my $e = shift;
522 0 ▶ $e->set_content(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
523 $e->{c}
524 0 }
525 592 ▶ sub set_content { # TODO: document this
Subroutine set_content: called CC=2 SCAR=6.9
Pod undocumented
526 592 my $e = shift;
527 592 ▶ $e->{c} = @_ == 1 ? $_[0] : [@_]
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 429 163 592
528 }
529 1 ▶ sub is_document { 0 }
Subroutine is_document: called CC=1 SCAR=0.0
Pod undocumented
530 2 ▶ sub is_block { 0 }
Subroutine is_block: called CC=1 SCAR=0.0
Pod undocumented
531 3 ▶ sub is_inline { 0 }
Subroutine is_inline: called CC=1 SCAR=0.0
Pod undocumented
532 3 ▶ sub is_meta { 0 }
Subroutine is_meta: called CC=1 SCAR=0.0
Pod undocumented
533 0 ▶ sub as_block {
Subroutine as_block: not called CC=1 SCAR=6.9
Pod undocumented
534 0 bless { t => 'Null', c => [] }, 'Pandoc::Document::Null';
535 }
536 31 *walk = *Pandoc::Walker::walk;
537 31 *query = *Pandoc::Walker::query;
538 31 *transform = *Pandoc::Walker::transform;
539  
540 60 ▶ sub string {
Subroutine string: called CC=3 SCAR=11.0
Pod undocumented
541  
542 # TODO: fix issue #4 to avoid this duplication
543 60 ▶ if ( $_[0]->name =~ /^(Str|Code|CodeBlock|Math|MetaString)$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_[0]->name =~ /^(Str|Code|CodeBlock|Math|MetaString)$/) { } 41 19 60
elsif ($_[0]->name =~ /^(LineBreak|SoftBreak|Space)$/) { } 1 18 19
544 41 return $_[0]->content;
545 }
546 elsif ( $_[0]->name =~ /^(LineBreak|SoftBreak|Space)$/ ) {
547 1 return ' ';
548 }
549 join '', @{
550 18 $_[0]->query(
551 {
552 31 ▶ 'Str|Code|CodeBlock|Math|MetaString' => sub { $_->content },
Subroutine __ANON__: called CC=1 SCAR=0.0
553 14 ▶ 'LineBreak|Space|SoftBreak' => sub { ' ' },
Subroutine __ANON__: called CC=1 SCAR=0.0
554 }
555 18 );
556 };
557 }
558  
559 170 ▶ sub match {
Subroutine match: called CC=2 SCAR=6.9
Pod undocumented
560 170 my $self = shift;
561 170 ▶ my $selector = blessed $_[0] ? shift : Pandoc::Selector->new(shift);
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[0]) ? : 1 169 170
562 170 $selector->match($self);
563 }
564 }
565  
566 {
567  
568 31 package Pandoc::Document::AttributesRole;
569 31 ▶ use Hash::MultiValue;
Subroutine BEGIN: called CC=1 SCAR=0.0
570 31 ▶ use Scalar::Util qw(reftype blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
571 31 ▶ use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
572  
573 110 ▶ sub id {
Subroutine id: called CC=3 SCAR=11.0
574 110 ▶ $_[0]->attr->[0] = defined $_[1] ? "$_[1]" : "" if @_ > 1;
Branch: 2 branches
BranchTrueFalseTotal
if @_ > 1 20 90 110
defined $_[1] ? : 19 1 20
575 110 $_[0]->attr->[0]
576 }
577  
578 0 ▶ sub classes {
Subroutine classes: not called CC=2 SCAR=17.9
579 0 my $e = shift;
580 0 ▶ croak 'Method classes() is not a setter' if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
581 0 warn "->classes is deprecated. Use [ split ' ', \$e->class ] instead\n";
582 0 $e->attr->[1]
583 }
584  
585 24 ▶ sub class {
Subroutine class: called CC=4 SCAR=13.9
586 24 my $e = shift;
587 24 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 23 24
588 $e->attr->[1] = [
589 5 grep { $_ ne '' }
590 4 map { split qr/\s+/, $_ }
591 1 ▶ map { (ref $_ and reftype $_ eq 'ARRAY') ? @$_ : $_ }
Branch: 1 branch
BranchTrueFalseTotal
ref $_ && builtin::reftype($_) eq 'ARRAY' ? : 1 2 3
Condition66%2/3
ref $_ and builtin::reftype($_) eq "ARRAY"
!ll&&!rl&&r
201
MC/DC50%1/2
ref $_ and builtin::reftype($_) eq "ARRAY"
ref $_ builtin::reftype($_) eq "ARRAY"
Truth table66%2/3
A and B
A= ref $_B= builtin::reftype($_) eq "ARRAY"
ABresult
0X0
100
111
592 @_
593 ];
594 }
595 24 join ' ', @{$e->attr->[1]}
596 }
597  
598 42 ▶ sub add_attribute {
Subroutine add_attribute: called CC=6 SCAR=17.9
599 42 my ($e, $key, $value) = @_;
600 42 ▶ if ($key eq 'id') {
Branch: 2 branches
BranchTrueFalseTotal
if ($key eq 'id') { } 15 27 42
elsif ($key eq 'class') { } 15 12 27
601 15 $e->id($value);
602 } elsif ($key eq 'class') {
603 15 ▶ $value //= '';
Condition100%2/2
$value //= ""
l!l
141
MC/DC100%1/1
$value //= ""
$value
Truth table100%2/2
A
A= $value
Aresult
11
00
604 15 ▶ $value = ["$value"] unless (reftype $value // '') eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::reftype($value) // "") eq "ARRAY" 9 6 15
Condition100%2/2
builtin::reftype($value) // ""
l!l
69
MC/DC100%1/1
builtin::reftype($value) // ""
builtin::reftype($value)
Truth table100%2/2
A
A= builtin::reftype($value)
Aresult
11
00
605 15 push @{$e->attr->[1]}, grep { $_ ne '' } map { split qr/\s+/, $_ } @$value;
606 } else {
607 12 push @{$e->attr->[2]}, [ $key, "$value" ];
608 }
609 }
610  
611 50 ▶ sub keyvals {
Subroutine keyvals: called CC=8 SCAR=20.8
612 50 my $e = shift;
613 50 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 45 5 50
614 45 ▶ my $attrs = @_ == 1 ? shift : Hash::MultiValue->new(@_);
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 43 2 45
615 45 ▶ unless (blessed $attrs and $attrs->isa('Hash::MultiValue')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($attrs) and $attrs->isa("Hash::MultiValue")) 42 3 45
Condition66%2/3
builtin::blessed($attrs) and $attrs->isa("Hash::MultiValue")
!ll&&!rl&&r
4203
MC/DC50%1/2
builtin::blessed($attrs) and $attrs->isa("Hash::MultiValue")
builtin::blessed($attrs) $attrs->isa("Hash::MultiValue")
Truth table66%2/3
A and B
A= builtin::blessed($attrs)B= $attrs->isa("Hash::MultiValue")
ABresult
0X0
100
111
616 42 $attrs = Hash::MultiValue->new(%$attrs);
617 }
618 45 ▶ $e->attr->[1] = [] if exists $attrs->{class};
Branch: 1 branch
BranchTrueFalseTotal
if exists $attrs->{"class"} 14 31 45
619 45 $e->attr->[2] = [];
620 45 ▶ $attrs->each(sub { $e->add_attribute(@_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
621 }
622 50 my @h;
623 50 ▶ push @h, id => $e->id if $e->id ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $e->id ne "" 16 34 50
624 50 ▶ push @h, class => $e->class if @{$e->attr->[1]};
Branch: 1 branch
BranchTrueFalseTotal
if @{$e->attr->[1];} 17 33 50
625 50 Hash::MultiValue->new( @h, map { @$_ } @{$e->attr->[2]} );
626 }
627  
628 }
629  
630 {
631  
632 31 package Pandoc::Document::Block;
633 31 our $VERSION = $PANDOC::Document::VERSION;
634 31 our @ISA = ('Pandoc::Document::Element');
635 3 ▶ sub is_block { 1 }
Subroutine is_block: called CC=1 SCAR=0.0
636 1 ▶ sub as_block { $_[0] }
Subroutine as_block: called CC=1 SCAR=0.0
637 1 ▶ sub null { # TODO: document this (?)
Subroutine null: called CC=1 SCAR=0.0
638 1 %{$_[0]} = (t => 'Null', c => []);
639 1 bless $_[0], 'Pandoc::Document::Null';
640 }
641 }
642  
643 {
644  
645 31 package Pandoc::Document::Inline;
646 31 our $VERSION = $PANDOC::Document::VERSION;
647 31 our @ISA = ('Pandoc::Document::Element');
648 13 ▶ sub is_inline { 1 }
Subroutine is_inline: called CC=1 SCAR=0.0
649 1 ▶ sub as_block {
Subroutine as_block: called CC=1 SCAR=0.0
650 1 bless { t => 'Plain', c => [ $_[0] ] }, 'Pandoc::Document::Plain';
651 }
652 }
653  
654 {
655  
656 31 package Pandoc::Document::LinkageRole;
657 31 our $VERSION = $PANDOC::Document::VERSION;
658  
659 31 for my $Element (qw[ Link Image ]) {
660 31 ▶ no strict 'refs'; #no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
661 62 unshift @{"Pandoc::Document::${Element}::ISA"}, __PACKAGE__; # no critic
662 }
663  
664 15 ▶ sub url { my $e = shift; $e->{c}->[-1][0] = shift if @_; return $e->{c}->[-1][0] //= ""; }
Subroutine url: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
if @_ 2 13 15
Condition100%2/2
$e->{"c"}[-1][0] //= ""
l!l
132
MC/DC100%1/1
$e->{"c"}[-1][0] //= ""
$e->{"c"}[-1][0]
Truth table100%2/2
A
A= $e->{"c"}[-1][0]
Aresult
11
00
665 6 ▶ sub title { my $e = shift; $e->{c}->[-1][1] = shift if @_; return $e->{c}->[-1][1] //= ""; }
Subroutine title: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
if @_ 2 4 6
Condition100%2/2
$e->{"c"}[-1][1] //= ""
l!l
42
MC/DC100%1/1
$e->{"c"}[-1][1] //= ""
$e->{"c"}[-1][1]
Truth table100%2/2
A
A= $e->{"c"}[-1][1]
Aresult
11
00
666  
667 16 ▶ sub upgrade {
Subroutine upgrade: called CC=2 SCAR=6.9
668 # prepend attributes to old-style ast
669 2 unshift @{ $_[0]->{c} }, [ "", [], [] ]
670 16 ▶ if 2 == @{ $_[0]->{c} };
Branch: 1 branch
BranchTrueFalseTotal
if 2 == @{$_[0]{"c"};} 2 14 16
671 }
672 }
673  
674 {
675 31 package Pandoc::Document::Citation;
676 31 our $VERSION = $PANDOC::Document::VERSION;
677  
678 31 ▶ use Carp qw[ carp croak ];
Subroutine BEGIN: called CC=1 SCAR=0.0
679  
680 31 my %props = (
681 id => { key => 'citationId', default => '"missing"' },
682 prefix => { key => 'citationPrefix', default => '[]' },
683 suffix => { key => 'citationSuffix', default => '[]' },
684 num => { key => 'citationNoteNum', default => '0' },
685 hash => { key => 'citationHash', default => '1' },
686 mode => {
687 key => 'citationMode',
688 default => q{
689 bless(
690 { t => 'NormalCitation', c => [] },
691 'Pandoc::Document::NormalCitation'
692 )
693 },
694 },
695 );
696  
697 {
698 31 my $template = <<'END_OF_TEMPLATE';
699 #line 1 "method [[[method]]]()"
700 package Pandoc::Document::Citation;
701 sub [[[method]]] {
702 my $self = shift;
703 if ( @_ ) {
704 $self->{[[[key]]]} = [[[coerce]]] ( shift // [[[default]]] );
705 }
706 return [[[coerce]]] ( $self->{[[[key]]]} //= [[[default]]] );
707 }
708 no warnings 'once';
709 *[[[alias]]] = \&[[[method]]];
710 1;
711 END_OF_TEMPLATE
712  
713 31 ▶ while ( my ( $name, $prop ) = each %props ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %props) 186 31 217
714 186 $prop->{name} = $name;
715 186 $prop->{method} = "Pandoc::Document::Citation::$name";
716 186 $prop->{alias} = "Pandoc::Document::Citation::$prop->{key}";
717 $prop->{coerce}
718 = $prop->{default} =~ /^\d$/ ? '0 +'
719 186 ▶ : $prop->{default} =~ /^"/ ? '"" .'
Branch: 2 branches
BranchTrueFalseTotal
$prop->{'default'} =~ /^\d$/ ? : 62 124 186
$prop->{'default'} =~ /^"/ ? : 31 93 124
720 : "";
721 {
722 186 ( my $source = $template ) =~ s/\Q[[[\E(\w+)\Q]]]\E/$prop->{$1}/g;
723 186 local $@;
724 ## no critic
725 186 ▶ eval $source || croak $@ . $source;
Branch: 1 branch
BranchTrueFalseTotal
eval $source or croak($@ . $source) 186 0 186
726 }
727 }
728 }
729  
730 31 my %accessors = map { ; $_->{name} => $_->{key} } values %props;
731  
732 2 ▶ sub new {
Subroutine new: called CC=3 SCAR=11.0
733 2 my ( $class, $arg ) = @_;
734 2 my $self = bless {}, $class;
735 2 ▶ while ( my ( $name, $key ) = each %accessors ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %accessors) 12 2 14
736 # coerce on access
737 12 ▶ $self->$name( $arg->{$key} // $arg->{$name} );
Condition66%2/3
$arg->{$key} // $arg->{$name}
l!l&&r!l&&!r
048
MC/DC50%1/2
$arg->{$key} // $arg->{$name}
$arg->{$key} $arg->{$name}
Truth table66%2/3
A // B
A= $arg->{$key}B= $arg->{$name}
ABresult
1X1
011
000
738 }
739 2 return $self;
740 }
741  
742 31 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
743 31 *TO_JSON = \&Pandoc::Document::Element::TO_JSON;
744 }
745  
746 # Special TO_JSON methods to coerce data to int/number/Boolean as appropriate
747 # and to downgrade document model depending on pandoc_version
748  
749 17 ▶sub Pandoc::Document::to_json {
Subroutine to_json: called CC=2 SCAR=7.0
750 17 my ($self) = @_;
751  
752 17 ▶ local $Pandoc::Elements::PANDOC_VERSION =
Condition66%2/3
$Pandoc::Elements::PANDOC_VERSION // $self->pandoc_version
l!l&&r!l&&!r
4130
MC/DC0%0/2
$Pandoc::Elements::PANDOC_VERSION // $self->pandoc_version
$Pandoc::Elements::PANDOC_VERSION $self->pandoc_version
Truth table66%2/3
A // B
A= $Pandoc::Elements::PANDOC_VERSIONB= $self->pandoc_version
ABresult
1X1
011
000
753 $Pandoc::Elements::PANDOC_VERSION // $self->pandoc_version;
754  
755 17 return Pandoc::Document::Element::to_json( $self->TO_JSON );
756 }
757  
758 21 ▶sub Pandoc::Document::TO_JSON {
Subroutine TO_JSON: called CC=2 SCAR=6.9
759 21 my ( $self ) = @_;
760 return $self->api_version >= 1.17
761 ? { %$self }
762 21 ▶ : [ { unMeta => $self->{meta} }, $self->{blocks} ]
Branch: 1 branch
BranchTrueFalseTotal
$self->api_version >= 1.17 ? : 7 14 21
763 }
764  
765 14 ▶sub Pandoc::Document::SoftBreak::TO_JSON {
Subroutine TO_JSON: called CC=2 SCAR=6.9
766 14 ▶ if ( pandoc_version() < '1.16' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (pandoc_version() < '1.16') { } 4 10 14
767 4 return { t => 'Space', c => [] };
768 } else {
769 10 return { t => 'SoftBreak', c => [] };
770 }
771 }
772  
773 4 ▶sub Pandoc::Document::LinkageRole::TO_JSON {
Subroutine TO_JSON: called CC=2 SCAR=6.9
774 4 my $ast = Pandoc::Document::Element::TO_JSON( $_[0] );
775 4 ▶ if ( pandoc_version() < 1.16 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (pandoc_version() < "1.16") 2 2 4
776 # remove attributes
777 2 $ast->{c} = [ @{ $ast->{c} }[ 1, 2 ] ];
778 }
779 4 return $ast;
780 }
781  
782 11 ▶sub Pandoc::Document::Header::TO_JSON {
Subroutine TO_JSON: called CC=1 SCAR=0.0
783 11 my $ast = Pandoc::Document::Element::TO_JSON( $_[0] );
784  
785 # coerce heading level to int
786 11 $ast->{c}[0] = int( $ast->{c}[0] );
787 11 return $ast;
788 }
789  
790 0 ▶sub Pandoc::Document::OrderedList::TO_JSON {
Subroutine TO_JSON: not called CC=1 SCAR=6.9
791 0 my $ast = Pandoc::Document::Element::TO_JSON( $_[0] );
792  
793 # coerce first item number to int
794 0 $ast->{c}[0][0] = int( $ast->{c}[0][0] );
795 0 return $ast;
796 }
797  
798 0 ▶sub Pandoc::Document::Table::TO_JSON {
Subroutine TO_JSON: not called CC=2 SCAR=17.9
799 0 my $ast = Pandoc::Document::Element::TO_JSON( $_[0] );
800  
801 # coerce column widths to numbers (floats)
802 0 $_ += 0 for @{ $ast->{c}[2] }; # faster than map
803 0 return $ast;
804 }
805  
806 0 ▶sub Pandoc::Document::Cite::TO_JSON {
Subroutine TO_JSON: not called CC=3 SCAR=24.8
807 0 my $ast = Pandoc::Document::Element::TO_JSON( $_[0] );
808 0 for my $citation ( @{ $ast->{c}[0] } ) {
809 0 for my $key (qw[ citationHash citationNoteNum ]) {
810  
811 # coerce to int
812 0 $citation->{$key} = int( $citation->{$key} );
813 }
814 }
815 0 return $ast;
816 }
817  
818 2 ▶sub Pandoc::Document::LineBlock::TO_JSON {
Subroutine TO_JSON: called CC=5 SCAR=16.3
819 2 my $ast = Pandoc::Document::Element::TO_JSON( $_[0] );
820 2 my $content = $ast->{c};
821  
822 2 for my $line ( @$content ) {
823  
824 # Convert spaces at the beginning of each line
825 # to Unicode non-breaking spaces, because pandoc does.
826 6 ▶ next unless @$line and $line->[0]->{t} eq 'Str';
Branch: 1 branch
BranchTrueFalseTotal
unless @$line and $line->[0]{"t"} eq "Str" 0 6 6
Condition33%1/3
@$line and $line->[0]{"t"} eq "Str"
!ll&&!rl&&r
006
MC/DC0%0/2
@$line and $line->[0]{"t"} eq "Str"
@$line $line->[0]{"t"} eq "Str"
Truth table33%1/3
A and B
A= @$lineB= $line->[0]{"t"} eq "Str"
ABresult
0X0
100
111
827 6 $line->[0]->{c} =~ s{^(\x{20}+)}{ "\x{a0}" x length($1) }e;
828 }
829  
830 2 ▶ return $ast if pandoc_version() >= 1.18;
Branch: 1 branch
BranchTrueFalseTotal
if pandoc_version() >= "1.18" 1 1 2
831  
832 1 my $c = [ map { ; @$_, LineBreak() } @{$content} ];
833 1 pop @$c; # remove trailing line break
834 1 return Para( $c )->TO_JSON;
835 }
836  
837 31 1;
838 __END__
« blib/lib/Pandoc/Document/Element.pm ↑ Summary blib/lib/Pandoc/Walker.pm »