blib/lib/Pandoc/Document/Element.pm untested

filter: Statementstmt 0.0% 0 / 66 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 6 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 136 SCAR 58.3
CC
18
Cov
0%
TO_JSON
56.0
match_simple
56.0
string
12.0
CRAP 342.0
1 package Pandoc::Document::Element;
2 use strict;
3 use warnings;
4 our $VERSION = $Pandoc::Document::VERSION;
5 use JSON ();
6 use Scalar::Util qw(reftype blessed);
7 use Pandoc::Walker ();
8 use subs qw(walk query transform); # Silence syntax warnings
9  
10 sub to_json {
11 JSON->new->utf8->canonical->convert_blessed->encode( $_[0] );
12 }
13  
14 sub TO_JSON {
15  
16 # Run everything thru this method so arrays/hashes are cloned
17 # and objects without TO_JSON methods are stringified.
18 # Required to ensure correct scalar types for Pandoc.
19  
20 # There is no easy way in Perl to tell if a scalar value is already a string or number,
21 # so we stringify all scalar values and numify/boolify as needed afterwards.
22  
23 my ( $ast, $maybe_blessed ) = @_;
24 if ( $maybe_blessed && blessed $ast ) {
25 return $ast if $ast->can('TO_JSON'); # JSON.pm will convert
26 # may have overloaded stringification! Should we check?
27 # require overload;
28 # return "$ast" if overload::Method($ast, q/""/) or overload::Method($ast, q/0+/);
29 # carp "Non-stringifiable object $ast";
30 return "$ast";
31 }
32 elsif ( 'ARRAY' eq reftype $ast ) {
33 return [ map { ref($_) ? TO_JSON( $_, 1 ) : "$_"; } @$ast ];
34 }
35 elsif ( 'HASH' eq reftype $ast ) {
36 my %ret = %$ast;
37 while ( my ( $k, $v ) = each %ret ) {
38 $ret{$k} = ref($v) ? TO_JSON( $v, 1 ) : "$v";
39 }
40 return \%ret;
41 }
42 else { return "$ast" }
43 }
44  
45 sub name { $_[0]->{t} }
46 sub content {
47 my $e = shift;
48 $e->set_content(@_) if @_;
49 $e->{c}
50 }
51 sub set_content { # TODO: document this
52 my $e = shift;
53 $e->{c} = @_ == 1 ? $_[0] : [@_]
54 }
55 sub is_document { 0 }
56 sub is_block { 0 }
57 sub is_inline { 0 }
58 sub is_meta { 0 }
59 *walk = *Pandoc::Walker::walk;
60 *query = *Pandoc::Walker::query;
61 *transform = *Pandoc::Walker::transform;
62  
63 sub string {
64  
65 # TODO: fix issue #4 to avoid this duplication
66 if ( $_[0]->name =~ /^(Str|Code|CodeBlock|Math|MetaString)$/ ) {
67 return $_[0]->content;
68 }
69 elsif ( $_[0]->name =~ /^(LineBreak|SoftBreak|Space)$/ ) {
70 return ' ';
71 }
72 join '', @{
73 $_[0]->query(
74 {
75 'Str|Code|CodeBlock|Math|MetaString' => sub { $_->content },
76 'LineBreak|Space|SoftBreak' => sub { ' ' },
77 }
78 );
79 };
80 }
81  
82 # TODO: replace by new class Pandoc::Selector with compiled code
83 sub match {
84 my $self = shift;
85 foreach my $selector ( split /\|/, shift ) {
86 return 1 if $self->match_simple($selector);
87 }
88 return 0;
89 }
90  
91 sub match_simple {
92 my ( $self, $selector ) = @_;
93 $selector =~ s/^\s+|\s+$//g;
94  
95 # name
96 return 0
97 if $selector =~ s/^([a-z]+)\s*//i and lc($1) ne lc( $self->name );
98 return 1 if $selector eq '';
99  
100 # type
101 if ( $selector =~ s/^:(document|block|inline|meta)\s*// ) {
102 my $method = "is_$1";
103 return 0 unless $self->$method;
104 return 1 if $selector eq '';
105 }
106  
107 # id and/or classes
108 return 0 unless $self->can('match_attributes');
109 return $self->match_attributes($selector);
110 }
111