Muster/LeafFile/mdwn.pm

filter: Statementstmt 21.5% 23 / 107 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 46.6% 7 / 15 Podpod 100% 8 / 8 totaltotal 21.9% 38 / 173 CC 21 SCAR 33.6
CC
21
Cov
16%
get_content_part
72.0
get_yaml_part
42.0
build_meta
20.0
CRAP 28.7
« Muster/Hook/Shortcut.pm ↑ Summary Muster/Scanner.pm »
1 package Muster::LeafFile::mdwn;
2 1 $Muster::LeafFile::mdwn::VERSION = '0.96';
3 #ABSTRACT: Muster::LeafFile::mdwn - a Markdown file in a Muster content tree
4 =head1 NAME
5  
6 Muster::LeafFile::mdwn - a Markdown file in a Muster content tree
7  
8 =head1 VERSION
9  
10 version 0.96
11  
12 =head1 DESCRIPTION
13  
14 File nodes represent files in a Muster::Content content tree.
15 This is a markdown file.
16  
17 =cut
18  
19 1 ▶use Mojo::Base 'Muster::LeafFile';
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use Mojo::Util 'decode';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use Text::Markdown::Discount 'markdown';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use Encode qw{encode};
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use Hash::Merge;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 # use a fast YAML
27 1 ▶use YAML::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 =head2 is_this_a_binary
30  
31 Test if this type of file which just contains binary data.
32  
33 =cut
34 0 ▶sub is_this_a_binary {
Subroutine is_this_a_binary: not called CC=1 SCAR=6.9
Pod documented
35 0 my $self = shift;
36  
37 0 return undef;
38 }
39  
40 =head2 build_meta
41  
42 Fill in the meta-data for this file.
43  
44 =cut
45 0 ▶sub build_meta {
Subroutine build_meta: not called CC=4 SCAR=30.0
Pod documented
46 0 my $self = shift;
47  
48 0 my $meta = $self->SUPER::build_meta();
49  
50 # add the wordcount to the default meta
51 0 $meta->{wordcount} = $self->wordcount;
52  
53 # if there's no YAML in the file then there's no further meta-data
54 0 ▶ if (!$self->has_yaml())
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->has_yaml) 0 0 0
55 {
56 0 return $meta;
57 }
58  
59 # We can't just use YAML streams straight, because the second part is
60 # generally not YAML-compatible.
61 0 my $yaml_str = $self->get_yaml_part();
62 0 my $ydata;
63 0 eval {$ydata = Load(encode('UTF-8', $yaml_str));};
64 0 ▶ if ($@)
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
65 {
66 0 warn __PACKAGE__, " Load of data failed: $@";
67 0 return $meta;
68 }
69 0 ▶ if (!$ydata)
Branch: 1 branch
BranchTrueFalseTotal
unless ($ydata) 0 0 0
70 {
71 0 warn __PACKAGE__, " no legal YAML";
72 0 return $meta;
73 }
74  
75 # what is in the YAML overrides the defaults
76 0 my $merge = Hash::Merge->new('RIGHT_PRECEDENT');
77 0 my $new_meta = $merge->merge($meta, $ydata);
78 0 $meta = $new_meta;
79  
80 0 return $meta;
81 }
82  
83 =head2 has_yaml
84  
85 The file has YAML if the FIRST line is '---'
86  
87 =cut
88 0 ▶sub has_yaml {
Subroutine has_yaml: not called CC=3 SCAR=24.8
Pod documented
89 0 my $self = shift;
90  
91 0 my $fh;
92 0 ▶ if (!open($fh, '<', $self->filename))
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fh, "<", $self->filename) 0 0 0
93 {
94 0 croak __PACKAGE__, " Unable to open file '" . $self->filename ."': $!\n";
95 }
96  
97 0 my $first_line = <$fh>;
98 0 close($fh);
99 0 ▶ return 0 if !$first_line;
Branch: 1 branch
BranchTrueFalseTotal
unless $first_line 0 0 0
100  
101 0 chomp $first_line;
102 0 return ($first_line eq '---');
103 }
104  
105 =head2 build_raw
106  
107 Get the "raw" data of the file.
108  
109 =cut
110 0 ▶sub build_raw {
Subroutine build_raw: not called CC=1 SCAR=6.9
Pod documented
111 0 my $self = shift;
112  
113 0 return $self->get_content_part();
114 }
115  
116 =head2 get_yaml_part
117  
118 Get the YAML part of a file (if any)
119 by reading the stuff between the first set of --- lines
120 Don't load the whole file!
121 When we want the YAML, we are scanning, and we don't want the content.
122  
123 =cut
124 0 ▶sub get_yaml_part {
Subroutine get_yaml_part: not called CC=6 SCAR=37.4
Pod documented
125 0 my $self = shift;
126  
127 0 my $fh;
128 0 ▶ if (!open($fh, '<', $self->filename))
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fh, "<", $self->filename) 0 0 0
129 {
130 0 croak __PACKAGE__, " Unable to open file '" . $self->filename ."': $!\n";
131 }
132  
133 0 my $yaml_str = '';
134 0 my $yaml_started = 0;
135 0 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
136 0 ▶ if (/^---$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^---$/u) 0 0 0
137 0 ▶ if (!$yaml_started)
Branch: 1 branch
BranchTrueFalseTotal
if (not $yaml_started) { } 0 0 0
138 {
139 0 $yaml_started = 1;
140 0 next;
141 }
142 else # end of the yaml part
143 {
144 0 last;
145 }
146 }
147 0 ▶ if ($yaml_started)
Branch: 1 branch
BranchTrueFalseTotal
if ($yaml_started) 0 0 0
148 {
149 0 $yaml_str .= $_;
150 }
151 }
152 0 close($fh);
153 0 return $yaml_str;
154 }
155  
156 =head2 get_content_part
157  
158 Get the content part of the file
159  
160 =cut
161 0 ▶sub get_content_part {
Subroutine get_content_part: not called CC=8 SCAR=42.8
Pod documented
162 0 my $self = shift;
163  
164 0 my $fh;
165 0 ▶ if (!$self->has_yaml())
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->has_yaml) 0 0 0
166 {
167 # read the whole file
168 0 my $fn = $self->filename;
169 0 ▶ open $fh, '<:encoding(UTF-8)', $fn or croak "couldn't open $fn: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<:encoding(UTF-8)", $fn or croak("couldn't open ${fn}: $!") 0 0 0
170  
171 # slurp
172 0 return do { local $/; <$fh> };
173 }
174  
175 0 ▶ if (!open($fh, '<', $self->filename))
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fh, "<", $self->filename) 0 0 0
176 {
177 0 croak __PACKAGE__, " Unable to open file '" . $self->filename ."': $!\n";
178 }
179  
180 0 my $content = '';
181 0 my $yaml_started = 0;
182 0 my $content_started = 0;
183 0 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
184 0 ▶ if (/^---$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^---$/u) 0 0 0
185 0 ▶ if (!$yaml_started)
Branch: 1 branch
BranchTrueFalseTotal
if (not $yaml_started) { } 0 0 0
186 {
187 0 $yaml_started = 1;
188 }
189 else # end of the yaml part
190 {
191 0 $content_started = 1;
192 }
193 0 next;
194 }
195 0 ▶ if ($content_started)
Branch: 1 branch
BranchTrueFalseTotal
if ($content_started) 0 0 0
196 {
197 0 $content .= $_;
198 }
199 }
200 0 close($fh);
201 0 return $content;
202 }
203  
204 =head2 build_html
205  
206 Convert the content into HTML format.
207  
208 =cut
209 0 ▶sub build_html {
Subroutine build_html: not called CC=3 SCAR=24.8
Pod documented
210 0 my $self = shift;
211  
212 0 my $content = $self->cooked();
213 # if the output is going to be text, don't process it
214 0 ▶ if (defined $self->meta->{render_format}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->meta->{"render_format"} and $self->meta->{"render_format"} eq "txt") 0 0 0
Condition0%0/3
defined $self->meta->{"render_format"} and $self->meta->{"render_format"} eq "txt"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->meta->{"render_format"} and $self->meta->{"render_format"} eq "txt"
defined $self->meta->{"render_format"} $self->meta->{"render_format"} eq "txt"
Truth table0%0/3
A and B
A= defined $self->meta->{"render_format"}B= $self->meta->{"render_format"} eq "txt"
ABresult
0X0
100
111
215 and $self->meta->{render_format} eq 'txt')
216 {
217 0 return $content;
218 }
219 0 return markdown($content);
220 }
221  
222 =head2 wordcount
223  
224 Calculate the word-count of the content.
225  
226 =cut
227 0 ▶sub wordcount {
Subroutine wordcount: not called CC=2 SCAR=17.9
Pod documented
228 0 my $self = shift;
229  
230 0 ▶ if (!exists $self->{wordcount})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"wordcount"}) 0 0 0
231 {
232 0 my $content = $self->raw();
233  
234 # count the words in the content
235 0 $content =~ s/<[^>]+>/ /gs; # remove html tags
236 # Remove everything but letters + spaces
237 # This is so that things like apostrophes don't make one
238 # word count as two words
239 0 $content =~ s/[^\w\s]//gs;
240  
241 0 my @matches = ($content =~ m/\b[\w]+/gs);
242 0 $self->{wordcount} = scalar @matches;
243 }
244  
245 0 return $self->{wordcount};
246 } # wordcount
247  
248 1 1;
249  
250 __END__
« Muster/Hook/Shortcut.pm ↑ Summary Muster/Scanner.pm »