DocKnot/Spin/Pointer.pm

filter: Statementstmt 79.1% 95 / 120 Branchbran 35.7% 10 / 28 Conditioncond 70.0% 7 / 10 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 95.4% 21 / 22 Podpod 100% 3 / 3 totaltotal 73.4% 138 / 188 CC 20 SCAR 24.7
CC
20
Cov
71%
_spin_markdown
56.0
spin_pointer
5.3
new
5.2
CRAP 11.8
« DocKnot/Spin/Text.pm ↑ Summary DocKnot/Spin/RSS.pm »
1 # Generate HTML from a pointer to an external file.
2 #
3 # The input tree for spin may contain pointers to external files in various
4 # formats. This module parses those pointer files and performs the conversion
5 # of those external files into HTML.
6 #
7 # SPDX-License-Identifier: MIT
8  
9 ##############################################################################
10 # Modules and declarations
11 ##############################################################################
12  
13 package App::DocKnot::Spin::Pointer v8.0.1;
14  
15 2 ▶use 5.024;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use parent qw(App::DocKnot);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use warnings FATAL => 'utf8';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 ▶use App::DocKnot::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use App::DocKnot::Spin::Text;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use App::DocKnot::Util qw(is_newer);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use Encode qw(decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use File::BaseDir qw(config_files);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use IPC::System::Simple qw(capturex);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use Path::Tiny qw(path);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use Pod::Thread 3.01 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 ▶use Template ();
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 ▶use YAML::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 # The URL to the software page for this package, used to embed a link to the
34 # software that generated the page.
35 2 my $URL = 'https://www.eyrie.org/~eagle/software/docknot/';
36  
37 ##############################################################################
38 # Format conversions
39 ##############################################################################
40  
41 # Convert a Markdown file to HTML.
42 #
43 # $data_ref - Data from the pointer file
44 # path - Path to the Markdown file to convert
45 # style - Style sheet to use
46 # title - Title of the page
47 # $base - Base path of pointer file (for relative paths)
48 # $output - Path to the output file
49 #
50 # Throws: Text exception on conversion failure
51 0 ▶sub _spin_markdown {
Subroutine _spin_markdown: not called CC=7 SCAR=40.3
52 0 my ($self, $data_ref, $base, $output) = @_;
53 0 my $source = path($data_ref->{path})->absolute($base);
54  
55 # Do the Markdown conversion using pandoc.
56 my $html = capturex(
57 0 $self->{pandoc_path}, '--wrap=preserve', '-f', 'markdown',
58 '-t', 'html', $source,
59 );
60  
61 # Pull the title out of the contents of the <h1> header if not set.
62 0 my $title = $data_ref->{title};
63 0 ▶ if (!defined($title)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $title) 0 0 0
64 0 ($title) = $html =~ m{ <h1 [^>]+ > (.*?) </h1> }xms;
65 }
66  
67 # Construct the template variables.
68 0 my ($links, $navbar, $style);
69 0 ▶ if ($self->{sitemap}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"sitemap"}) 0 0 0
70 0 my $page = $output->relative($self->{output});
71 0 my @links = $self->{sitemap}->links($page);
72 0 ▶ if (@links) {
Branch: 1 branch
BranchTrueFalseTotal
if (@links) 0 0 0
73 0 $links = join(q{}, @links);
74 }
75 0 my @navbar = $self->{sitemap}->navbar($page);
76 0 ▶ if (@navbar) {
Branch: 1 branch
BranchTrueFalseTotal
if (@navbar) 0 0 0
77 0 $navbar = join(q{}, @navbar);
78 }
79 }
80 0 ▶ if ($data_ref->{style}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data_ref->{"style"}) 0 0 0
81 0 $style = $self->{style_url} . $data_ref->{style} . '.css';
82 }
83 0 my %vars = (
84 docknot_url => $URL,
85 html => decode('utf-8', $html),
86 links => $links,
87 modified => strftime('%Y-%m-%d', gmtime($source->stat()->[9])),
88 navbar => $navbar,
89 now => strftime('%Y-%m-%d', gmtime()),
90 style => $style,
91 title => $title,
92 );
93  
94 # Construct the output page from those template variables.
95 0 my $result;
96 $self->{template}->process($self->{template_path}, \%vars, \$result)
97 0 ▶ or croak($self->{template}->error());
Branch: 1 branch
BranchTrueFalseTotal
$self->{"template"}->process($self->{"template_path"}, \%vars, \$result) or croak($self->{"template"}->error) 0 0 0
98  
99 # Write the result to the output file.
100 0 $output->spew_utf8($result);
101 0 return;
102 }
103  
104 # Convert a POD file to HTML.
105 #
106 # $data_ref - Data from the pointer file
107 # options - Hash of conversion options
108 # contents - Whether to add a table of contents
109 # navbar - Whether to add a navigation bar
110 # path - Path to the POD file to convert
111 # style - Style sheet to use
112 # title - Title of the page
113 # $base - Base path of pointer file (for relative paths)
114 # $output - Path to the output file
115 #
116 # Throws: Text exception on conversion failure
117 2 ▶sub _spin_pod {
Subroutine _spin_pod: called CC=3 SCAR=11.0
118 2 my ($self, $data_ref, $base, $output) = @_;
119 2 my $source = path($data_ref->{path})->absolute($base);
120  
121 # Construct the Pod::Thread formatter object.
122 my %options = (
123 contents => $data_ref->{options}{contents},
124 style => $data_ref->{style} // 'pod',
125 title => $data_ref->{title},
126 2 ▶ );
Condition50%1/2
$data_ref->{"style"} // "pod"
l!l
02
MC/DC0%0/1
$data_ref->{"style"} // "pod"
$data_ref->{"style"}
Truth table50%1/2
A
A= $data_ref->{"style"}
Aresult
11
00
127 2 ▶ if (exists($data_ref->{options}{navbar})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data_ref->{'options'}{'navbar'}) { } 1 1 2
128 1 $options{navbar} = $data_ref->{options}{navbar};
129 } else {
130 1 $options{navbar} = 1;
131 }
132 2 my $podthread = Pod::Thread->new(%options);
133  
134 # Convert the POD to thread.
135 2 my $data;
136 2 $podthread->output_string(\$data);
137 2 $podthread->parse_file("$source");
138 2 $data = decode('utf-8', $data);
139  
140 # Spin that page into HTML.
141 2 $self->{thread}->spin_thread_output($data, $source, 'POD', $output);
142 2 return;
143 }
144  
145 # Convert a text file to HTML.
146 #
147 # $data_ref - Data form the pointer file
148 # options - Hash of conversion options
149 # modified - Whether to add a last modified subheader
150 # path - Path to the text file to convert
151 # style - Style sheet to use
152 # title - Title of the page
153 # $base - Base path of pointer file (for relative paths)
154 # $output - Path to the output file
155 #
156 # Throws: Text exception on conversion failure
157 2 ▶sub _spin_text {
Subroutine _spin_text: called CC=3 SCAR=11.0
158 2 my ($self, $data_ref, $base, $output) = @_;
159 2 my $source = path($data_ref->{path})->absolute($base);
160  
161 # Determine the style URL.
162 2 ▶ my $style = ($data_ref->{style} // 'faq') . '.css';
Condition100%2/2
$data_ref->{"style"} // "faq"
l!l
11
MC/DC100%1/1
$data_ref->{"style"} // "faq"
$data_ref->{"style"}
Truth table100%2/2
A
A= $data_ref->{"style"}
Aresult
11
00
163 2 ▶ if ($self->{style_url}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"style_url"}) 2 0 2
164 2 $style = $self->{style_url} . $style;
165 }
166  
167 # Create the formatter object.
168 my %options = (
169 modified => $data_ref->{options}{modified},
170 output => $self->{output},
171 sitemap => $self->{sitemap},
172 style => $style,
173 title => $data_ref->{title},
174 2 );
175 2 my $text = App::DocKnot::Spin::Text->new(\%options);
176  
177 # Generate the output page.
178 2 $text->spin_text_file($source, $output);
179 2 return;
180 }
181  
182 ##############################################################################
183 # Public interface
184 ##############################################################################
185  
186 # Create a new HTML converter for pointers. This object can (and should) be
187 # reused for all pointer conversions done while spinning a tree of files.
188 #
189 # $args - Anonymous hash of arguments with the following keys:
190 # output - Root of the output tree
191 # sitemap - App::DocKnot::Spin::Sitemap object
192 # style-url - Partial URL to style sheets
193 # thread - App::DocKnot::Spin::Thread object
194 #
195 # Returns: Newly created object
196 # Throws: Text exception on failure to initialize Template Toolkit
197 5 ▶sub new {
Subroutine new: called CC=5 SCAR=16.5
Pod documented
198 5 my ($class, $args_ref) = @_;
199  
200 # Get the configured path to pandoc, if any.
201 5 my $config_reader = App::DocKnot::Config->new();
202 5 my $global_config_ref = $config_reader->global_config();
203 5 ▶ my $pandoc = $global_config_ref->{pandoc} // 'pandoc';
Condition50%1/2
$global_config_ref->{"pandoc"} // "pandoc"
l!l
05
MC/DC0%0/1
$global_config_ref->{"pandoc"} // "pandoc"
$global_config_ref->{"pandoc"}
Truth table50%1/2
A
A= $global_config_ref->{"pandoc"}
Aresult
11
00
204  
205 # Add a trailing slash to the partial URL for style sheets.
206 5 ▶ my $style_url = $args_ref->{'style-url'} // q{};
Condition50%1/2
$args_ref->{"style-url"} // ""
l!l
50
MC/DC0%0/1
$args_ref->{"style-url"} // ""
$args_ref->{"style-url"}
Truth table50%1/2
A
A= $args_ref->{"style-url"}
Aresult
11
00
207 5 ▶ if ($style_url) {
Branch: 1 branch
BranchTrueFalseTotal
if ($style_url) 5 0 5
208 5 $style_url =~ s{ /* \z }{/}xms;
209 }
210  
211 # Create and return the object.
212 5 ▶ my $tt = Template->new({ ABSOLUTE => 1, ENCODING => 'utf8' })
Branch: 1 branch
BranchTrueFalseTotal
my $tt = "Template"->new({"ABSOLUTE", 1, "ENCODING", "utf8"}) or croak("Template"->error) 5 0 5
213 or croak(Template->error());
214 my $self = {
215 output => $args_ref->{output},
216 pandoc_path => $pandoc,
217 sitemap => $args_ref->{sitemap},
218 style_url => $style_url,
219 template => $tt,
220 thread => $args_ref->{thread},
221 5 };
222 5 bless($self, $class);
223 5 $self->{template_path} = $self->appdata_path('templates', 'html.tmpl');
224 5 return $self;
225 }
226  
227 # Check if the result of a pointer file needs to be regenerated.
228 #
229 # $pointer - Path to pointer file
230 # $output - Path to corresponding output file
231 #
232 # Returns: True if the output file does not exist or has a modification date
233 # older than either the pointer file or the underlying source file,
234 # false otherwise
235 # Throws: YAML::XS exception on invalid pointer
236 15 ▶sub is_out_of_date {
Subroutine is_out_of_date: called CC=2 SCAR=7.2
Pod documented
237 15 my ($self, $pointer, $output) = @_;
238 15 my $data_ref = $self->load_yaml_file($pointer, 'pointer');
239 15 my $path = path($data_ref->{path})->absolute($pointer->parent());
240 15 ▶ if (!$path->exists()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path->exists) 0 15 15
241 0 die "$pointer: path $data_ref->{path} ($path) does not exist\n";
242 }
243 15 return !is_newer($output, $pointer, $path);
244 }
245  
246 # Process a given pointer file.
247 #
248 # $pointer - Path to pointer file to process
249 # $output - Path to corresponding output file
250 #
251 # Throws: YAML::XS exception on invalid pointer
252 # Text exception for missing input file
253 # Text exception on failure to convert the file
254 4 ▶sub spin_pointer {
Subroutine spin_pointer: called CC=5 SCAR=16.7
Pod documented
255 4 my ($self, $pointer, $output, $options_ref) = @_;
256 4 my $data_ref = $self->load_yaml_file($pointer, 'pointer');
257 4 ▶ $data_ref->{options} //= {};
Condition100%2/2
$data_ref->{"options"} //= {}
l!l
22
MC/DC100%1/1
$data_ref->{"options"} //= {}
$data_ref->{"options"}
Truth table100%2/2
A
A= $data_ref->{"options"}
Aresult
11
00
258  
259 # Dispatch to the appropriate conversion function.
260 4 ▶ if ($data_ref->{format} eq 'markdown') {
Branch: 3 branches
BranchTrueFalseTotal
if ($data_ref->{'format'} eq 'markdown') { } 0 4 4
elsif ($data_ref->{'format'} eq 'pod') { } 2 2 4
elsif ($data_ref->{'format'} eq 'text') { } 2 0 2
261 0 $self->_spin_markdown($data_ref, $pointer->parent(), $output);
262 } elsif ($data_ref->{format} eq 'pod') {
263 2 $self->_spin_pod($data_ref, $pointer->parent(), $output);
264 } elsif ($data_ref->{format} eq 'text') {
265 2 $self->_spin_text($data_ref, $pointer->parent(), $output);
266 } else {
267 0 die "$pointer: unknown output format $data_ref->{format}\n";
268 }
269 4 return;
270 }
271  
272 ##############################################################################
273 # Module return value and documentation
274 ##############################################################################
275  
276 2 1;
277  
278 __END__
« DocKnot/Spin/Text.pm ↑ Summary DocKnot/Spin/RSS.pm »