blib/lib/Dancer/Plugin/Preprocess/Markdown.pm

filter: Statementstmt 77.7% 70 / 90 Branchbran 46.6% 14 / 30 Conditioncond 37.5% 3 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 10 / 10 Podpod n/a totaltotal 68.5% 98 / 143 CC 19 SCAR 37.7
CC
19
Cov
68%
__ANON__
66.9
__ANON__
2.5
BEGIN
1.0
CRAP 43.5
↑ Summary
1 package Dancer::Plugin::Preprocess::Markdown;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # ABSTRACT: Generate HTML content from Markdown files
7  
8 2 our $VERSION = '0.021'; # VERSION
9  
10 2 ▶use Cwd 'abs_path';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Dancer ':syntax';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Dancer::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use File::Spec::Functions qw(catfile file_name_is_absolute);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Text::Markdown qw(markdown);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 my $settings = {
17 layout => setting('layout'),
18 # TODO: It might make more sense to have 1 as the default
19 recursive => 0,
20 save => 0,
21 2 %{plugin_setting()}
22 };
23 2 my $paths;
24  
25 2 ▶if (exists $settings->{paths}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $settings->{"paths"}) 1 1 2
26 1 $paths = $settings->{paths};
27 }
28  
29 my $paths_re = join '|', map {
30 2 my $s = $_;
31 4 $s =~ s{^[^/]}{/$&}; # Add leading slash, if missing
32 4 $s =~ s{/$}{}; # Remove trailing slash
33 4 quotemeta $s;
34 } reverse sort keys %$paths;
35  
36 4 ▶sub _process_markdown_file {
Subroutine _process_markdown_file: called CC=1 SCAR=0.0
37 4 my $md_file = shift;
38  
39 4 open (my $f, '<', $md_file);
40 4 my $contents;
41 {
42 4 local $/;
43 4 $contents = <$f>;
44 }
45 4 close($f);
46  
47 4 return markdown($contents);
48 }
49  
50 2 my $handler_defined;
51  
52 # Postpone setting up the route handler to the time before the first request is
53 # processed, so that other routes defined in the app will take precedence.
54 4 ▶hook on_reset_state => sub {
Subroutine __ANON__: called CC=2 SCAR=9.2
55 4 ▶ return if $handler_defined;
Branch: 1 branch
BranchTrueFalseTotal
if $handler_defined 3 1 4
56  
57 4 ▶ get qr{($paths_re)/(.*)} => sub {
Subroutine __ANON__: called CC=18 SCAR=42.0
58 4 my ($path, $file) = splat;
59  
60 4 $path .= '/';
61 4 my $path_settings;
62  
63 4 for my $path_prefix (reverse sort keys %$paths) {
64 10 (my $path_prefix_slash = $path_prefix) =~ s{([^/])$}{$1/};
65  
66 10 ▶ if (substr($path, 0, length($path_prefix_slash)) eq
Branch: 1 branch
BranchTrueFalseTotal
if (substr($path, 0, length $path_prefix_slash) eq $path_prefix_slash) 4 6 10
67 $path_prefix_slash)
68 {
69 # Found a matching path
70 $path_settings = {
71 # Top-level settings
72 layout => $settings->{layout},
73 recursive => $settings->{recursive},
74 save => $settings->{save},
75 # Path-specific settings (may override top-level ones)
76 4 ▶ %{$paths->{$path_prefix} || {}}
Condition100%2/2
$paths->{$path_prefix} || {}
l!l
31
MC/DC100%1/1
$paths->{$path_prefix} || {}
$paths->{$path_prefix}
Truth table100%2/2
A
A= $paths->{$path_prefix}
Aresult
11
00
77 };
78 4 last;
79 }
80 }
81  
82 # Pass if there was no matching path
83 4 ▶ return pass if (!defined $path_settings);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path_settings 0 4 4
84  
85 # Pass if the requested file appears to be in a subdirectory while
86 # recursive mode is off
87 4 ▶ return pass if (!$path_settings->{recursive} && $file =~ m{/});
Branch: 1 branch
BranchTrueFalseTotal
if not $path_settings->{"recursive"} and $file =~ m[/] 0 4 4
Condition33%1/3
not $path_settings->{"recursive"} and $file =~ m[/]
!ll&&!rl&&r
040
MC/DC0%0/2
not $path_settings->{"recursive"} and $file =~ m[/]
not $path_settings->{"recursive"} $file =~ m[/]
Truth table33%1/3
not(A) and B
A= not $path_settings->{"recursive"}B= $file =~ m[/]
ABresult
0X0
100
111
88  
89 4 ▶ if (!exists $path_settings->{src_dir}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $path_settings->{"src_dir"}) 1 3 4
90 # Source directory not specified -- use default
91 1 $path_settings->{src_dir} = path 'md', 'src', split(m{/}, $path);
92 }
93  
94 # Strip off the ".html" suffix, if present
95 4 $file =~ s/\.html$//;
96  
97 4 my $src_file;
98  
99 4 ▶ if (file_name_is_absolute($path_settings->{src_dir})) {
Branch: 1 branch
BranchTrueFalseTotal
if (file_name_is_absolute($path_settings->{'src_dir'})) { } 0 4 4
100 0 $src_file = path $path_settings->{src_dir}, ($file . '.md');
101 }
102 else {
103 # Assume a non-absolute source directory is relative to appdir
104 $src_file = path abs_path(setting('appdir')),
105 4 $path_settings->{src_dir}, ($file . '.md');
106 }
107  
108 4 ▶ if (!-r $src_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-r $src_file) 0 4 4
109 0 return send_error("Not allowed", 403);
110 }
111  
112 4 my $content;
113  
114 4 ▶ if ($path_settings->{save}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path_settings->{"save"}) 0 4 4
115 0 ▶ if (!exists $path_settings->{dest_dir}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $path_settings->{"dest_dir"}) 0 0 0
116 0 $path_settings->{dest_dir} = path 'md', 'dest',
117 split(m{/}, $path);
118 }
119  
120 0 my $dest_file;
121  
122 0 ▶ if (file_name_is_absolute($path_settings->{dest_dir})) {
Branch: 1 branch
BranchTrueFalseTotal
if (file_name_is_absolute($path_settings->{'dest_dir'})) { } 0 0 0
123 0 $dest_file = path $path_settings->{dest_dir}, ($file . '.html');
124 }
125 else {
126 # Assume a non-absolute destination directory is relative to
127 # appdir
128 $dest_file = path abs_path(setting('appdir')),
129 0 $path_settings->{dest_dir}, ($file . '.html');
130 }
131  
132 0 ▶ if (!-f $dest_file ||
Branch: 1 branch
BranchTrueFalseTotal
if (not -f $dest_file or (stat $dest_file)[9] < (stat $src_file)[9]) { } 0 0 0
Condition0%0/3
not -f $dest_file or (stat $dest_file)[9] < (stat $src_file)[9]
l!l&&r!l&&!r
000
MC/DC0%0/2
not -f $dest_file or (stat $dest_file)[9] < (stat $src_file)[9]
not -f $dest_file (stat $dest_file)[9] < (stat $src_file)[9]
Truth table0%0/3
not(A) or B
A= not -f $dest_fileB= (stat $dest_file)[9] < (stat $src_file)[9]
ABresult
1X1
011
000
133 ((stat($dest_file))[9] < (stat($src_file))[9]))
134 {
135 # Source file is newer than destination file (or the latter does
136 # not exist)
137 0 $content = _process_markdown_file($src_file);
138  
139 0 ▶ if (open(my $f, '>', $dest_file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $f, '>', $dest_file) { } 0 0 0
140 0 print {$f} $content;
141 0 close($f);
142 }
143 else {
144 0 warning __PACKAGE__ .
145 ": Can't open '$dest_file' for writing";
146 }
147 }
148 else {
149 # The HTML file already exists -- read its contents back to the
150 # client
151 0 ▶ if (open (my $f, '<', $dest_file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $f, '<', $dest_file) { } 0 0 0
152 0 local $/;
153 0 $content = <$f>;
154 0 close($f);
155 }
156 else {
157 0 warning __PACKAGE__ .
158 ": Can't open '$dest_file' for reading";
159 }
160 }
161 }
162  
163 4 ▶ if (!defined $content) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $content) 4 0 4
164 4 $content = _process_markdown_file($src_file);
165 }
166  
167 # TODO: Add support for path-specific layouts
168 return (engine 'template')->apply_layout($content, {},
169 4 { layout => $path_settings->{layout} });
170 1 };
171  
172 1 $handler_defined = 1;
173 2 };
174  
175 2 register_plugin;
176  
177 2 1;
178  
179 __END__
↑ Summary