Compiler/Source.pm

filter: Statementstmt 97.7% 43 / 44 Branchbran 87.5% 14 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 95.5% 64 / 67 SCAR 23.0
CC
10
Cov
95%
_build_parsed
7.0
_build_has_metadata
4.0
BEGIN
1.0
CRAP 10.0
« Compiler/Test.pm ↑ Summary Compiler.pm »
1 package Markdown::Compiler::Source;
2 18 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 18 has source => (
5 is => 'ro',
6 required => 1,
7 );
8  
9 has default_metatype => (
10 is => 'ro',
11 67 ▶ default => sub { 'YAML' },
Subroutine __ANON__: called CC=1 SCAR=0.0
12 18 );
13  
14 has body => (
15 is => 'ro',
16 lazy => 1,
17 init_arg => undef,
18 65 ▶ default => sub { shift->parsed->{body} },
Subroutine __ANON__: called CC=1 SCAR=0.0
19 18 );
20  
21 has metadata => (
22 is => 'ro',
23 lazy => 1,
24 init_arg => undef,
25 2 ▶ default => sub { shift->parsed->{metadata} },
Subroutine __ANON__: called CC=1 SCAR=0.0
26  
27 18 );
28  
29 has metatype => (
30 is => 'ro',
31 lazy => 1,
32 init_arg => undef,
33 2 ▶ default => sub { shift->parsed->{metatype} },
Subroutine __ANON__: called CC=1 SCAR=0.0
34 18 );
35  
36 18 has has_metadata => (
37 is => 'ro',
38 lazy => 1,
39 builder => '_build_has_metadata',
40 );
41  
42 18 has parsed => (
43 is => 'ro',
44 lazy => 1,
45 builder => '_build_parsed'
46 );
47  
48 sub _build_parsed {
49 67 ▶ my ( $self ) = @_;
Subroutine _build_parsed: called CC=7 SCAR=19.5
50  
51 # If we do not have any metadata, act as if we have a
52 # blank file of the default type.
53 67 ▶ if ( ! $self->has_metadata ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->has_metadata) 65 2 67
54 return {
55 65 metadata => '',
56 metatype => $self->default_metatype,
57 body => $self->source,
58 };
59 }
60  
61 # File spec:
62 # Named metadata:
63 # --- [Name]
64 # metadata.....
65 # ---
66 # body......
67 # Unamed/Default metadata:
68 # ---
69 # metadata.....
70 # ---
71 # body......
72  
73 2 my @source = split /\n/, $self->source;
74 2 my $is_first_line = 1;
75 2 my $is_metadata_section = 1;
76  
77 2 my $parsed = {
78 metadata => '',
79 metatype => $self->default_metatype,
80 body => '',
81 };
82  
83 2 foreach my $line ( split( /\n/, $self->source )) {
84 12 $line = "$line\n";
85  
86 12 print "Looking at line: $line";
87 # First line, see if we have an alternative metatype.
88 12 ▶ if ( $is_first_line ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_first_line) 2 10 12
89 2 ▶ if ( $line =~ /^---\s+(\S+)\s?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^---\s+(\S+)\s?$/) 0 2 2
90 0 $parsed->{metatype} = $1;
91 }
92 2 $is_first_line = 0;
93 2 next;
94 }
95  
96 # Now each line from the second line, until we close
97 # the metadata section.
98 10 ▶ if ( $is_metadata_section ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_metadata_section) 6 4 10
99 6 ▶ if ( $line =~ /^---\s*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^---\s*$/) 2 4 6
100 2 $is_metadata_section = 0; # No longer processing metadata.
101 2 next;
102 }
103 # Still processing metadata, add line and then move to the next
104 # line.
105 4 $parsed->{metadata} .= $line;
106 4 next;
107 }
108  
109 # Now we are in the lines after the metadata section of the file.
110 4 $parsed->{body} .= $line;
111 }
112  
113 2 return $parsed;
114 }
115  
116 sub _build_has_metadata {
117 67 ▶ my ( $self ) = @_;
Subroutine _build_has_metadata: called CC=4 SCAR=13.9
118  
119 # Get the first three chars from the source.
120 67 my $marker = substr($self->source,0,3);
121  
122 67 ▶ return 0 unless $marker;
Branch: 1 branch
BranchTrueFalseTotal
unless $marker 0 67 67
123 67 ▶ return 0 unless length($marker) == 3;
Branch: 1 branch
BranchTrueFalseTotal
unless length $marker == 3 15 52 67
124 52 ▶ return 0 unless $marker eq '---';
Branch: 1 branch
BranchTrueFalseTotal
unless $marker eq "---" 50 2 52
125  
126 2 return 1;
127 }
128  
129 18 1;
« Compiler/Test.pm ↑ Summary Compiler.pm »