Compiler/Source.pm

filter: Statementstmt 97.6% 42 / 43 Branchbran 87.5% 14 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 95.4% 63 / 66 CC 10 SCAR 14.8
CC
10
Cov
95%
_build_parsed
7.0
_build_has_metadata
4.0
BEGIN
1.0
CRAP 4.4
« Compiler/Target/Plogitty.pm ↑ Summary Compiler/Lexer.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 67 ▶sub _build_parsed {
Subroutine _build_parsed: called CC=7 SCAR=19.5
49 67 my ( $self ) = @_;
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 # First line, see if we have an alternative metatype.
87 12 ▶ if ( $is_first_line ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_first_line) 2 10 12
88 2 ▶ if ( $line =~ /^---\s+(\S+)\s?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^---\s+(\S+)\s?$/) 0 2 2
89 0 $parsed->{metatype} = $1;
90 }
91 2 $is_first_line = 0;
92 2 next;
93 }
94  
95 # Now each line from the second line, until we close
96 # the metadata section.
97 10 ▶ if ( $is_metadata_section ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_metadata_section) 6 4 10
98 6 ▶ if ( $line =~ /^---\s*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^---\s*$/) 2 4 6
99 2 $is_metadata_section = 0; # No longer processing metadata.
100 2 next;
101 }
102 # Still processing metadata, add line and then move to the next
103 # line.
104 4 $parsed->{metadata} .= $line;
105 4 next;
106 }
107  
108 # Now we are in the lines after the metadata section of the file.
109 4 $parsed->{body} .= $line;
110 }
111  
112 2 return $parsed;
113 }
114  
115 67 ▶sub _build_has_metadata {
Subroutine _build_has_metadata: called CC=4 SCAR=13.9
116 67 my ( $self ) = @_;
117  
118 # Get the first three chars from the source.
119 67 my $marker = substr($self->source,0,3);
120  
121 67 ▶ return 0 unless $marker;
Branch: 1 branch
BranchTrueFalseTotal
unless $marker 0 67 67
122 67 ▶ return 0 unless length($marker) == 3;
Branch: 1 branch
BranchTrueFalseTotal
unless length $marker == 3 15 52 67
123 52 ▶ return 0 unless $marker eq '---';
Branch: 1 branch
BranchTrueFalseTotal
unless $marker eq "---" 50 2 52
124  
125 2 return 1;
126 }
127  
128 18 1;
« Compiler/Target/Plogitty.pm ↑ Summary Compiler/Lexer.pm »