blib/lib/Pandoc/Elements.pm

filter: Statementstmt 80.8% 485 / 600 Branchbran 46.8% 191 / 408 Conditioncond 67.0% 63 / 94 MC/DCmcdc 46.4% 26 / 56 Subroutinesub 87.9% 80 / 91 Podpod 31.5% 6 / 19 totaltotal 67.1% 851 / 1268 SCAR 66.3
CC
132
Cov
67%
_sections
132.0
to_pandoc
20.0
Document
16.1
CRAP 754.8
↑ Summary blib/lib/Pandoc/Document/Element.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.37';
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 sub _as_pandoc_version {
33 32 ▶ (blessed $_[0] and $_[0]->isa('Pandoc::Version'))
Subroutine _as_pandoc_version: called CC=3 SCAR=11.1
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 sub pandoc_version {
38 55 ▶ if (@_) {
Subroutine pandoc_version: called CC=5 SCAR=16.1
Pod documented
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%3/3
_minimum_pandoc_api_for(@_) // croak("pandoc version not supported")
l!l&&r!l&&!r
711
MC/DC0%0/2
_minimum_pandoc_api_for(@_) // croak("pandoc version not supported")
_minimum_pandoc_api_for(@_) croak("pandoc version not supported")
Truth table33%1/3
A // B
A= _minimum_pandoc_api_for(@_)B= croak("pandoc version not supported")
ABresult
1X1
011
000
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 sub _minimum_pandoc_version_for_api {
55 28 ▶ my $api = shift;
Subroutine _minimum_pandoc_version_for_api: called CC=7 SCAR=19.5
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 sub _minimum_pandoc_api_for {
75 9 ▶ my $version = _as_pandoc_version(shift);
Subroutine _minimum_pandoc_api_for: called CC=6 SCAR=17.9
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 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 sub {
170 608 ▶ croak "$name expects $numargs arguments, but given " . scalar @_
Subroutine __ANON__: called CC=3 SCAR=11.0
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/:\[(.+)\]$// ? : 31 1643 1674
187 ? " [ map { bless \$_, 'Pandoc::Document::$1' } \@{$member} ];"
188 : " $member;";
189 1674 for ( split '/', $accessors[$i] ) {
190 ## no critic
191 4 ▶ *{"Pandoc::Document::${name}::$_"} = eval "sub { $code }";
Subroutine __ANON__: called CC=3 SCAR=16.0
Branch: 114 branches
BranchTrueFalseTotal
if @_ 0 4 4
1 == @_ ? : 0 0 0
if @_ 0 1 1
1 == @_ ? : 0 0 0
if @_ 0 0 0
1 == @_ ? : 0 0 0
if @_ 0 24 24
1 == @_ ? : 0 0 0
if @_ 0 0 0
1 == @_ ? : 0 0 0
if @_ 0 0 0
1 == @_ ? : 0 0 0
if @_ 0 3 3
1 == @_ ? : 0 0 0
if @_ 0 25 25
1 == @_ ? : 0 0 0
if @_ 0 4 4
1 == @_ ? : 0 0 0
if @_ 0 0 0
1 == @_ ? : 0 0 0
if @_ 0 1 1
1 == @_ ? : 0 0 0
if @_ 1 24 25
1 == @_ ? : 1 0 1
if @_ 0 6 6
1 == @_ ? : 0 0 0
if @_ 0 1 1
1 == @_ ? : 0 0 0
if @_ 1 16 17
1 == @_ ? : 1 0 1
if @_ 0 16 16
1 == @_ ? : 0 0 0
if @_ 0 80 80
1 == @_ ? : 0 0 0
if @_ 0 0 0
1 == @_ ? : 0 0 0
if @_ 0 2 2
1 == @_ ? : 0 0 0
if @_ 0 0 0
1 == @_ ? : 0 0 0
if @_ 0 23 23
1 == @_ ? : 0 0 0
if @_ 0 11 11
1 == @_ ? : 0 0 0
if @_ 0 10 10
1 == @_ ? : 0 0 0
if @_ 0 6 6
1 == @_ ? : 0 0 0
if @_ 0 14 14
1 == @_ ? : 0 0 0
if @_ 0 0 0
1 == @_ ? : 0 0 0
if @_ 0 0 0
1 == @_ ? : 0 0 0
if @_ 0 8 8
1 == @_ ? : 0 0 0
if @_ 0 5 5
1 == @_ ? : 0 0 0
if @_ 0 3 3
1 == @_ ? : 0 0 0
if @_ 0 0 0
1 == @_ ? : 0 0 0
if @_ 0 23 23
1 == @_ ? : 0 0 0
if @_ 0 5 5
1 == @_ ? : 0 0 0
if @_ 0 3 3
1 == @_ ? : 0 0 0
if @_ 0 50 50
1 == @_ ? : 0 0 0
if @_ 0 14 14
1 == @_ ? : 0 0 0
if @_ 0 3 3
1 == @_ ? : 0 0 0
if @_ 0 89 89
1 == @_ ? : 0 0 0
if @_ 0 0 0
1 == @_ ? : 0 0 0
if @_ 0 10 10
1 == @_ ? : 0 0 0
if @_ 0 0 0
1 == @_ ? : 0 0 0
if @_ 0 0 0
1 == @_ ? : 0 0 0
if @_ 0 4 4
1 == @_ ? : 0 0 0
if @_ 0 2 2
1 == @_ ? : 0 0 0
if @_ 0 0 0
1 == @_ ? : 0 0 0
if @_ 0 63 63
1 == @_ ? : 0 0 0
if @_ 1 8 9
1 == @_ ? : 1 0 1
if @_ 1 2 3
1 == @_ ? : 1 0 1
if @_ 0 0 0
1 == @_ ? : 0 0 0
if @_ 0 10 10
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 0 0
1 == @_ ? : 0 0 0
if @_ 0 0 0
1 == @_ ? : 0 0 0
if @_ 0 0 0
1 == @_ ? : 0 0 0
192 }
193 }
194 }
195  
196 sub element {
197 4 ▶ my $name = shift;
Subroutine element: called CC=3 SCAR=11.0
Pod documented
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 sub Document {
205  
206 54 ▶ my $from_json;
Subroutine Document: called CC=16 SCAR=27.8
Pod documented
207 54 my $arg = do {
208 54 ▶ if ( @_ == 1 ) {
Branch: 3 branches
BranchTrueFalseTotal
if (@_ == 1) { } 29 25 54
elsif (@_ == 2) { } 9 16 25
elsif (@_ % 2) { } 1 15 16
209 29 $from_json = 1;
210 29 ▶ my $reftype = reftype $_[0] // '';
Condition100%2/2
builtin::reftype($_[0]) // ""
l!l
281
MC/DC100%1/1
builtin::reftype($_[0]) // ""
builtin::reftype($_[0])
Truth table100%2/2
A
A= builtin::reftype($_[0])
Aresult
11
00
211 29 ▶ if ( $reftype eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($reftype eq 'ARRAY') { } 21 8 29
elsif ($reftype eq 'HASH') { } 7 1 8
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 7 $_[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 52 ▶ // $arg->{api_version};
Condition83%5/6
($arg->{"pandoc-api-version"} // $arg->{"pandoc_api_version"}) // $arg->{"api_version"}
l!l&&r!l&&!r
52720
$arg->{"pandoc-api-version"} // $arg->{"pandoc_api_version"}
l!l&&r!l&&!r
5047
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 52 ▶ my $doc = bless { blocks => ( $arg->{blocks} // [] ) }, 'Pandoc::Document';
Condition50%1/2
$arg->{"blocks"} // []
l!l
520
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 52 ▶ my $meta = $arg->{meta} // {};
Condition100%2/2
$arg->{"meta"} // {}
l!l
511
MC/DC100%1/1
$arg->{"meta"} // {}
$arg->{"meta"}
Truth table100%2/2
A
A= $arg->{"meta"}
Aresult
11
00
245 52 ▶ if ($from_json) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from_json) { } 28 24 52
246 28 ▶ croak "Document metadata must be a hash" unless 'HASH' eq reftype $meta;
Branch: 1 branch
BranchTrueFalseTotal
unless "HASH" eq builtin::reftype($meta) 0 28 28
247 $doc->{meta} = bless {
248 28 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 52 ▶ if (!defined $api_version and defined $arg->{pandoc_version}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $api_version and defined $arg->{'pandoc_version'}) { } 9 43 52
Condition100%3/3
not defined $api_version and defined $arg->{"pandoc_version"}
!ll&&!rl&&r
32119
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 43 ▶ $doc->api_version($api_version // $REQUIRED_API[1]);
Condition66%2/3
$api_version // $REQUIRED_API[1]
l!l&&r!l&&!r
32110
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 49 walk $doc, \&_bless_pandoc_element;
262  
263 49 return $doc;
264  
265 }
266  
267 # internal helper method
268 sub _bless_pandoc_element {
269 594 ▶ my $e = shift;
Subroutine _bless_pandoc_element: called CC=7 SCAR=19.5
270 594 ▶ return $e unless ref $e;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $e 0 594 594
271 594 ▶ 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 284 594
Condition66%2/3
builtin::blessed($e) and $e->isa("Pandoc::Document::Element")
!ll&&!rl&&r
2840310
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 284 ▶ if ( 'MetaMap' eq $e->{t} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("MetaMap" eq $e->{"t"}) 5 279 284
275 5 for my $v ( values %{ $e->{c} } ) {
276 15 _bless_pandoc_element( $v );
277 }
278 }
279  
280 284 bless $e, 'Pandoc::Document::' . $e->{t};
281 284 ▶ $e->upgrade($e) if $e->can('upgrade');
Branch: 1 branch
BranchTrueFalseTotal
if $e->can("upgrade") 16 268 284
282 284 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 sub attributes($) {
294  
295 40 ▶ my $e = Span(['',[],[]],[]); # to make use of AttributesRole
Subroutine attributes: called CC=1 SCAR=0.0
Pod documented
296 40 $e->keyvals(@_);
297  
298 40 return $e->attr;
299 }
300  
301 sub citation($) {
302 1 ▶ my $a = shift;
Subroutine citation: called CC=7 SCAR=23.8
Pod documented
303 {
304 citationId => $a->{id} // "missing",
305 citationPrefix => $a->{prefix} // [],
306 citationSuffix => $a->{suffix} // [],
307 citationMode => $a->{mode} // bless(
308 { t => 'NormalCitation', c => [] },
309 'Pandoc::Document::NormalCitation'
310 ),
311 citationNoteNum => $a->{num} // 0,
312 1 ▶ citationHash => $a->{hash} // 1,
Condition50%6/12
$a->{"id"} // "missing"
l!l
10
$a->{"prefix"} // []
l!l
10
$a->{"suffix"} // []
l!l
10
$a->{"mode"} // bless({"t", "NormalCitation", "c", []}, "Pandoc::Document::NormalCitation")
l!l
01
$a->{"num"} // 0
l!l
01
$a->{"hash"} // 1
l!l
01
MC/DC0%0/1
$a->{"id"} // "missing"
$a->{"id"}
MC/DC0%0/1
$a->{"prefix"} // []
$a->{"prefix"}
MC/DC0%0/1
$a->{"suffix"} // []
$a->{"suffix"}
MC/DC0%0/1
$a->{"mode"} // bless({"t", "NormalCitation", "c", []}, "Pandoc::Document::NormalCitation")
$a->{"mode"}
MC/DC0%0/1
$a->{"num"} // 0
$a->{"num"}
MC/DC0%0/1
$a->{"hash"} // 1
$a->{"hash"}
Truth table50%1/2
A
A= $a->{"id"}
Aresult
11
00
Truth table50%1/2
A
A= $a->{"prefix"}
Aresult
11
00
Truth table50%1/2
A
A= $a->{"suffix"}
Aresult
11
00
Truth table50%1/2
A
A= $a->{"mode"}
Aresult
11
00
Truth table50%1/2
A
A= $a->{"num"}
Aresult
11
00
Truth table50%1/2
A
A= $a->{"hash"}
Aresult
11
00
313 };
314 }
315  
316 # XXX: must require rather than use Pandoc::Metadata
317 # or its attempt to use Pandoc::Elements will result in a broken state.
318 31 require Pandoc::Metadata;
319  
320 sub metadata($); ## no critic
321  
322 sub metadata($) { ## no critic
323 49 ▶ my $value = shift;
Subroutine metadata: called CC=14 SCAR=26.5
Pod undocumented
324 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
325 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
326 }
327 elsif ( JSON::is_bool($value) ) {
328 2 MetaBool($value)
329 }
330 elsif ( blessed($value) ) {
331 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
332 9 $value
333 }
334 ▶ 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
335 1 MetaInlines([ $value ])
336 }
337 ▶ 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
338 1 MetaBlocks([ $value ])
339 } elsif ( $value->isa('Pandoc::Document::Metadata') ) {
340 0 MetaMap( { map { $_ => $value->{$_} } keys %$value } )
341 } else {
342 1 MetaString("$value")
343 }
344 }
345 elsif ( reftype $value eq 'ARRAY' ) {
346 2 MetaList( [ map { metadata $_ } @$value ] )
347 }
348 elsif ( reftype $value eq 'HASH' ) {
349 26 MetaMap( { map { $_ => metadata $value->{$_} } keys %$value } )
350 }
351 else {
352 1 MetaString("$value")
353 }
354 }
355  
356 sub pandoc_json($) {
357 23 ▶ shift if $_[0] =~ /^Pandoc::/;
Subroutine pandoc_json: called CC=3 SCAR=11.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /^Pandoc::/ 1 22 23
358  
359 23 my $ast = eval { decode_json( $_[0] ) };
360 23 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 22 23
361 1 $@ =~ s/ at [^ ]+Elements\.pm line \d+//;
362 1 chomp $@;
363 1 croak $@;
364 }
365 22 return Document $ast;
366 }
367  
368 31 *pandoc_query = *Pandoc::Walker::query;
369  
370 # document element packages
371  
372 {
373  
374 package Pandoc::Document;
375 31 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
376 31 ▶ use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
377 31 ▶ use Scalar::Util qw(blessed reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
378 31 ▶ use Pandoc;
Subroutine BEGIN: called CC=1 SCAR=0.0
379 31 our $VERSION = '0.04';
380 31 our @ISA = ('Pandoc::Document::Element');
381 sub blocks;
382 1 ▶ sub name { 'Document' }
Subroutine name: called CC=1 SCAR=0.0
383 sub meta {
384 160 ▶ if (@_ > 1) {
Subroutine meta: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 25 135 160
385 25 ▶ croak "document metadata must be a hash"
Branch: 1 branch
BranchTrueFalseTotal
unless "HASH" eq builtin::reftype($_[1]) 0 25 25
386 unless 'HASH' eq reftype $_[1];
387 25 my $map = Pandoc::Elements::metadata($_[1])->content;
388 25 $_[0]->{meta} = bless $map, 'Pandoc::Document::Metadata';
389 }
390 160 $_[0]->{meta};
391 }
392 sub content {
393 13 ▶ $_[0]->{blocks} = $_[1] if @_ > 1;
Subroutine content: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 13 13
394 13 $_[0]->{blocks};
395 }
396 31 *blocks = \&content;
397 1 ▶ sub is_document { 1 }
Subroutine is_document: called CC=1 SCAR=0.0
398 sub as_block {
399 0 ▶ bless { t => 'Div', c => [ {}, $_[0]->{blocks} ] }, 'Pandoc::Document::Div';
Subroutine as_block: not called CC=1 SCAR=6.9
400 }
401 sub value {
402 108 ▶ shift->meta->value(@_);
Subroutine value: called CC=1 SCAR=0.0
403 }
404 31 *metavalue = \&value;
405 sub string {
406 2 ▶ join '', map { $_->string } @{$_[0]->content}
Subroutine string: called CC=1 SCAR=0.0
407 }
408 sub api_version {
409 110 ▶ my $self = shift;
Subroutine api_version: called CC=4 SCAR=13.9
410 110 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 51 59 110
411 51 my $version = Pandoc::Version->new(shift);
412 51 ▶ croak "api_version must be >= $PANDOC_API_MIN"
Branch: 1 branch
BranchTrueFalseTotal
if $version < $PANDOC_API_MIN 1 50 51
413 if $version < $PANDOC_API_MIN;
414 50 ▶ croak "api_version must have major and minor part"
Branch: 1 branch
BranchTrueFalseTotal
if @$version < 2 0 50 50
415 if @$version < 2;
416 50 $self->{'pandoc-api-version'} = $version;
417 }
418 109 return $self->{'pandoc-api-version'};
419 }
420 31 *pandoc_version = \&Pandoc::Elements::pandoc_version;
421 sub outline {
422 0 ▶ my ($self, $depth) = @_;
Subroutine outline: not called CC=1 SCAR=6.9
423 0 _sections( [@{$self->blocks}], $depth );
424 }
425 sub _sections {
426 0 ▶ my ($list, $depth) = @_;
Subroutine _sections: not called CC=11 SCAR=48.8
427 0 my (@blocks, @sections);
428  
429 # everything up to the first Header
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 !$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
433 }
434 0 push @blocks, shift @$list;
435 }
436  
437 # divide into sections
438 0 ▶ while (@$list) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$list) 0 0 0
439 0 my $header = shift @$list;
440 0 my $level = $header->level;
441  
442 0 my @content;
443 0 ▶ while (@$list) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$list) 0 0 0
444 0 ▶ if ($list->[0]->name eq 'Header') {
Branch: 1 branch
BranchTrueFalseTotal
if ($list->[0]->name eq "Header") 0 0 0
445 0 ▶ last if $list->[0]->level <= $level;
Branch: 1 branch
BranchTrueFalseTotal
if $list->[0]->level <= $level 0 0 0
446 }
447 0 push @content, shift @$list;
448 }
449  
450 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
451 ? { blocks => \@content }
452 : _sections(\@content,$depth);
453 0 push @sections, { header => $header, %$s };
454 }
455  
456 0 return { blocks => \@blocks, sections => \@sections };
457 }
458 sub to_pandoc {
459 0 ▶ my ($self, @args) = @_;
Subroutine to_pandoc: not called CC=4 SCAR=30.0
460 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
461 ? shift(@args) : pandoc;
462  
463 0 my $api_version = $self->api_version; # save
464 0 $self->pandoc_version( $pandoc->version );
465  
466 0 my $in = $self->to_json;
467 0 $self->api_version($api_version); # restore
468  
469 0 $pandoc->run( [ -f => 'json', @args ], { in => \$in, out => \my $out } );
470 0 return $out;
471 }
472 31 foreach my $format (qw(markdown latex html rst plain)) {
473 31 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
474 155 *{ __PACKAGE__ . "::to_$format" } = sub {
475 0 ▶ shift()->to_pandoc( @_, '-t' => $format );
Subroutine __ANON__: not called CC=1 SCAR=6.9
476 }
477 155 }
478 }
479  
480 {
481 31 package Pandoc::Document::Keyword;
482 31 our @ISA = ('Pandoc::Document::Element');
483 }
484  
485 {
486  
487 31 package Pandoc::Document::Element;
488 31 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
489 31 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
490 31 our $VERSION = $Pandoc::Document::VERSION;
491 31 ▶ use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
492 31 ▶ use Scalar::Util qw(reftype blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
493 31 ▶ use Pandoc::Walker ();
Subroutine BEGIN: called CC=1 SCAR=0.0
494 31 ▶ use Pandoc::Selector;
Subroutine BEGIN: called CC=1 SCAR=0.0
495 31 ▶ use subs qw(walk query transform); # Silence syntax warnings
Subroutine BEGIN: called CC=1 SCAR=0.0
496  
497 sub to_json {
498 33 ▶ JSON->new->utf8->canonical->convert_blessed->encode( $_[0] );
Subroutine to_json: called CC=1 SCAR=0.0
Pod undocumented
499 }
500  
501 sub TO_JSON {
502  
503 # Run everything thru this method so arrays/hashes are cloned
504 # and objects without TO_JSON methods are stringified.
505 # Required to ensure correct scalar types for Pandoc.
506  
507 # There is no easy way in Perl to tell if a scalar value is already a string or number,
508 # so we stringify all scalar values and numify/boolify as needed afterwards.
509  
510 613 ▶ my ( $ast, $maybe_blessed ) = @_;
Subroutine TO_JSON: called CC=9 SCAR=22.2
Pod undocumented
511 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
512 199 ▶ return $ast if $ast->can('TO_JSON'); # JSON.pm will convert
Branch: 1 branch
BranchTrueFalseTotal
if $ast->can("TO_JSON") 199 0 199
513 # may have overloaded stringification! Should we check?
514 # require overload;
515 # return "$ast" if overload::Method($ast, q/""/) or overload::Method($ast, q/0+/);
516 # carp "Non-stringifiable object $ast";
517 0 return "$ast";
518 }
519 elsif ( 'ARRAY' eq reftype $ast ) {
520 183 ▶ return [ map { ref($_) ? TO_JSON( $_, 1 ) : "$_"; } @$ast ];
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 269 36 305
521 }
522 elsif ( 'HASH' eq reftype $ast ) {
523 231 my %ret = %$ast;
524 231 ▶ while ( my ( $k, $v ) = each %ret ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %ret) 462 231 693
525 462 ▶ $ret{$k} = ref($v) ? TO_JSON( $v, 1 ) : "$v";
Branch: 1 branch
BranchTrueFalseTotal
ref $v ? : 113 349 462
526 }
527 231 return \%ret;
528 }
529 0 else { return "$ast" }
530 }
531  
532 719 ▶ sub name { $_[0]->{t} }
Subroutine name: called CC=1 SCAR=0.0
Pod undocumented
533 sub content {
534 0 ▶ my $e = shift;
Subroutine content: not called CC=2 SCAR=17.9
Pod undocumented
535 0 ▶ $e->set_content(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
536 $e->{c}
537 0 }
538 sub set_content { # TODO: document this
539 592 ▶ my $e = shift;
Subroutine set_content: called CC=2 SCAR=6.9
Pod undocumented
540 592 ▶ $e->{c} = @_ == 1 ? $_[0] : [@_]
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 429 163 592
541 }
542 1 ▶ sub is_document { 0 }
Subroutine is_document: called CC=1 SCAR=0.0
Pod undocumented
543 2 ▶ sub is_block { 0 }
Subroutine is_block: called CC=1 SCAR=0.0
Pod undocumented
544 3 ▶ sub is_inline { 0 }
Subroutine is_inline: called CC=1 SCAR=0.0
Pod undocumented
545 3 ▶ sub is_meta { 0 }
Subroutine is_meta: called CC=1 SCAR=0.0
Pod undocumented
546 sub as_block {
547 0 ▶ bless { t => 'Null', c => [] }, 'Pandoc::Document::Null';
Subroutine as_block: not called CC=1 SCAR=6.9
Pod undocumented
548 }
549 31 *walk = *Pandoc::Walker::walk;
550 31 *query = *Pandoc::Walker::query;
551 31 *transform = *Pandoc::Walker::transform;
552  
553 sub string {
554  
555 # TODO: fix issue #4 to avoid this duplication
556 60 ▶ if ( $_[0]->name =~ /^(Str|Code|CodeBlock|Math|MetaString)$/ ) {
Subroutine string: called CC=3 SCAR=11.0
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
if ($_[0]->name =~ /^(Str|Code|CodeBlock|Math|MetaString)$/) { } 41 19 60
elsif ($_[0]->name =~ /^(LineBreak|SoftBreak|Space)$/) { } 1 18 19
557 41 return $_[0]->content;
558 }
559 elsif ( $_[0]->name =~ /^(LineBreak|SoftBreak|Space)$/ ) {
560 1 return ' ';
561 }
562 join '', @{
563 18 $_[0]->query(
564 {
565 31 ▶ 'Str|Code|CodeBlock|Math|MetaString' => sub { $_->content },
Subroutine __ANON__: called CC=1 SCAR=0.0
566 14 ▶ 'LineBreak|Space|SoftBreak' => sub { ' ' },
Subroutine __ANON__: called CC=1 SCAR=0.0
567 }
568 18 );
569 };
570 }
571  
572 sub match {
573 167 ▶ my $self = shift;
Subroutine match: called CC=2 SCAR=6.9
Pod undocumented
574 167 ▶ my $selector = blessed $_[0] ? shift : Pandoc::Selector->new(shift);
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[0]) ? : 1 166 167
575 167 $selector->match($self);
576 }
577 }
578  
579 {
580  
581 31 package Pandoc::Document::AttributesRole;
582 31 ▶ use Hash::MultiValue;
Subroutine BEGIN: called CC=1 SCAR=0.0
583 31 ▶ use Scalar::Util qw(reftype blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
584 31 ▶ use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
585  
586 sub id {
587 110 ▶ $_[0]->attr->[0] = defined $_[1] ? "$_[1]" : "" if @_ > 1;
Subroutine id: called CC=3 SCAR=11.0
Branch: 2 branches
BranchTrueFalseTotal
if @_ > 1 20 90 110
defined $_[1] ? : 19 1 20
588 110 $_[0]->attr->[0]
589 }
590  
591 sub classes {
592 0 ▶ my $e = shift;
Subroutine classes: not called CC=2 SCAR=17.9
593 0 ▶ croak 'Method classes() is not a setter' if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
594 0 warn "->classes is deprecated. Use [ split ' ', \$e->class ] instead\n";
595 0 $e->attr->[1]
596 }
597  
598 sub class {
599 24 ▶ my $e = shift;
Subroutine class: called CC=4 SCAR=13.9
600 24 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 23 24
601 $e->attr->[1] = [
602 5 grep { $_ ne '' }
603 4 map { split qr/\s+/, $_ }
604 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
605 @_
606 ];
607 }
608 24 join ' ', @{$e->attr->[1]}
609 }
610  
611 sub add_attribute {
612 42 ▶ my ($e, $key, $value) = @_;
Subroutine add_attribute: called CC=6 SCAR=17.9
613 42 ▶ if ($key eq 'id') {
Branch: 2 branches
BranchTrueFalseTotal
if ($key eq 'id') { } 15 27 42
elsif ($key eq 'class') { } 15 12 27
614 15 $e->id($value);
615 } elsif ($key eq 'class') {
616 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
617 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
618 15 push @{$e->attr->[1]}, grep { $_ ne '' } map { split qr/\s+/, $_ } @$value;
619 } else {
620 12 push @{$e->attr->[2]}, [ $key, "$value" ];
621 }
622 }
623  
624 sub keyvals {
625 50 ▶ my $e = shift;
Subroutine keyvals: called CC=8 SCAR=20.8
626 50 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 45 5 50
627 45 ▶ my $attrs = @_ == 1 ? shift : Hash::MultiValue->new(@_);
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 43 2 45
628 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
629 42 $attrs = Hash::MultiValue->new(%$attrs);
630 }
631 45 ▶ $e->attr->[1] = [] if exists $attrs->{class};
Branch: 1 branch
BranchTrueFalseTotal
if exists $attrs->{"class"} 14 31 45
632 45 $e->attr->[2] = [];
633 45 ▶ $attrs->each(sub { $e->add_attribute(@_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
634 }
635 50 my @h;
636 50 ▶ push @h, id => $e->id if $e->id ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $e->id ne "" 16 34 50
637 50 ▶ push @h, class => $e->class if @{$e->attr->[1]};
Branch: 1 branch
BranchTrueFalseTotal
if @{$e->attr->[1];} 17 33 50
638 50 Hash::MultiValue->new( @h, map { @$_ } @{$e->attr->[2]} );
639 }
640  
641 }
642  
643 {
644  
645 31 package Pandoc::Document::Block;
646 31 our $VERSION = $PANDOC::Document::VERSION;
647 31 our @ISA = ('Pandoc::Document::Element');
648 3 ▶ sub is_block { 1 }
Subroutine is_block: called CC=1 SCAR=0.0
649 1 ▶ sub as_block { $_[0] }
Subroutine as_block: called CC=1 SCAR=0.0
650 sub null { # TODO: document this (?)
651 1 ▶ %{$_[0]} = (t => 'Null', c => []);
Subroutine null: called CC=1 SCAR=0.0
652 1 bless $_[0], 'Pandoc::Document::Null';
653 }
654 }
655  
656 {
657  
658 31 package Pandoc::Document::Inline;
659 31 our $VERSION = $PANDOC::Document::VERSION;
660 31 our @ISA = ('Pandoc::Document::Element');
661 13 ▶ sub is_inline { 1 }
Subroutine is_inline: called CC=1 SCAR=0.0
662 sub as_block {
663 1 ▶ bless { t => 'Plain', c => [ $_[0] ] }, 'Pandoc::Document::Plain';
Subroutine as_block: called CC=1 SCAR=0.0
664 }
665 }
666  
667 {
668  
669 31 package Pandoc::Document::LinkageRole;
670 31 our $VERSION = $PANDOC::Document::VERSION;
671  
672 31 for my $Element (qw[ Link Image ]) {
673 31 ▶ no strict 'refs'; #no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
674 62 unshift @{"Pandoc::Document::${Element}::ISA"}, __PACKAGE__; # no critic
675 }
676  
677 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
678 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
679  
680 sub upgrade {
681 # prepend attributes to old-style ast
682 2 unshift @{ $_[0]->{c} }, [ "", [], [] ]
683 16 ▶ if 2 == @{ $_[0]->{c} };
Subroutine upgrade: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if 2 == @{$_[0]{"c"};} 2 14 16
684 }
685 }
686  
687 # Special TO_JSON methods to coerce data to int/number/Boolean as appropriate
688 # and to downgrade document model depending on pandoc_version
689  
690 31 sub Pandoc::Document::to_json {
691 17 ▶ my ($self) = @_;
Subroutine to_json: called CC=2 SCAR=7.0
692  
693 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
694 $Pandoc::Elements::PANDOC_VERSION // $self->pandoc_version;
695  
696 17 return Pandoc::Document::Element::to_json( $self->TO_JSON );
697 }
698  
699 sub Pandoc::Document::TO_JSON {
700 21 ▶ my ( $self ) = @_;
Subroutine TO_JSON: called CC=2 SCAR=6.9
701 return $self->api_version >= 1.17
702 ? { %$self }
703 21 ▶ : [ { unMeta => $self->{meta} }, $self->{blocks} ]
Branch: 1 branch
BranchTrueFalseTotal
$self->api_version >= 1.17 ? : 7 14 21
704 }
705  
706 sub Pandoc::Document::SoftBreak::TO_JSON {
707 14 ▶ if ( pandoc_version() < '1.16' ) {
Subroutine TO_JSON: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (pandoc_version() < '1.16') { } 4 10 14
708 4 return { t => 'Space', c => [] };
709 } else {
710 10 return { t => 'SoftBreak', c => [] };
711 }
712 }
713  
714 sub Pandoc::Document::LinkageRole::TO_JSON {
715 4 ▶ my $ast = Pandoc::Document::Element::TO_JSON( $_[0] );
Subroutine TO_JSON: called CC=2 SCAR=6.9
716 4 ▶ if ( pandoc_version() < 1.16 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (pandoc_version() < "1.16") 2 2 4
717 # remove attributes
718 2 $ast->{c} = [ @{ $ast->{c} }[ 1, 2 ] ];
719 }
720 4 return $ast;
721 }
722  
723 sub Pandoc::Document::Header::TO_JSON {
724 11 ▶ my $ast = Pandoc::Document::Element::TO_JSON( $_[0] );
Subroutine TO_JSON: called CC=1 SCAR=0.0
725  
726 # coerce heading level to int
727 11 $ast->{c}[0] = int( $ast->{c}[0] );
728 11 return $ast;
729 }
730  
731 sub Pandoc::Document::OrderedList::TO_JSON {
732 0 ▶ my $ast = Pandoc::Document::Element::TO_JSON( $_[0] );
Subroutine TO_JSON: not called CC=1 SCAR=6.9
733  
734 # coerce first item number to int
735 0 $ast->{c}[0][0] = int( $ast->{c}[0][0] );
736 0 return $ast;
737 }
738  
739 sub Pandoc::Document::Table::TO_JSON {
740 0 ▶ my $ast = Pandoc::Document::Element::TO_JSON( $_[0] );
Subroutine TO_JSON: not called CC=2 SCAR=17.9
741  
742 # coerce column widths to numbers (floats)
743 0 $_ += 0 for @{ $ast->{c}[2] }; # faster than map
744 0 return $ast;
745 }
746  
747 sub Pandoc::Document::Cite::TO_JSON {
748 0 ▶ my $ast = Pandoc::Document::Element::TO_JSON( $_[0] );
Subroutine TO_JSON: not called CC=3 SCAR=24.8
749 0 for my $citation ( @{ $ast->{c}[0] } ) {
750 0 for my $key (qw[ citationHash citationNoteNum ]) {
751  
752 # coerce to int
753 0 $citation->{$key} = int( $citation->{$key} );
754 }
755 }
756 0 return $ast;
757 }
758  
759 sub Pandoc::Document::LineBlock::TO_JSON {
760 2 ▶ my $ast = Pandoc::Document::Element::TO_JSON( $_[0] );
Subroutine TO_JSON: called CC=5 SCAR=16.3
761 2 my $content = $ast->{c};
762  
763 2 for my $line ( @$content ) {
764  
765 # Convert spaces at the beginning of each line
766 # to Unicode non-breaking spaces, because pandoc does.
767 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
768 6 $line->[0]->{c} =~ s{^(\x{20}+)}{ "\x{a0}" x length($1) }e;
769 }
770  
771 2 ▶ return $ast if pandoc_version() >= 1.18;
Branch: 1 branch
BranchTrueFalseTotal
if pandoc_version() >= "1.18" 1 1 2
772  
773 1 my $c = [ map { ; @$_, LineBreak() } @{$content} ];
774 1 pop @$c; # remove trailing line break
775 1 return Para( $c )->TO_JSON;
776 }
777  
778 31 1;
779 __END__
↑ Summary blib/lib/Pandoc/Document/Element.pm »