script/markdown-adoc

filter: Statementstmt 77.3% 92 / 119 Branchbran 58.7% 27 / 46 Conditioncond 37.5% 3 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 93.7% 15 / 16 Podpod n/a totaltotal 70.9% 137 / 193 CC 28 SCAR 20.4
CC
28
Cov
71%
_run
15.6
_check_same_file
7.5
_parse_options
7.0
CRAP 7.7
« blib/lib/Text/MarkdownAdoc/Refs.pm ↑ Summary blib/lib/Text/MarkdownAdoc.pm »
1 #!/usr/bin/env perl
2  
3 8 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 #===========================================================================
8 # CLI wrapper for Text::MarkdownAdoc
9 #===========================================================================
10  
11 8 ▶use Text::MarkdownAdoc;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use Getopt::Long qw(GetOptions);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use Pod::Usage qw(pod2usage);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 Main:
16 {
17 8 my $cli = _parse_options(@ARGV);
18 4 exit _run($cli);
19 }
20  
21 #===========================================================================
22 # Option parsing
23 #===========================================================================
24  
25 sub _parse_options
26 8 ▶{
Subroutine _parse_options: called CC=7 SCAR=19.5
27 8 my (@args) = @_;
28  
29 8 my %opts;
30 8 my $cli = {
31 attributes => {},
32 help => 0,
33 man => 0,
34 version => 0,
35 };
36  
37 8 Getopt::Long::Configure('pass_through');
38 GetOptions(
39 'o|output=s' => \$cli->{output},
40 1 ▶ 'a|attribute=s' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
41 1 my ($name, $val) = @_;
42 1 _parse_attribute($cli->{attributes}, $val);
43 },
44 'imagesdir=s' => \$opts{imagesdir},
45 'auto-ids' => \$opts{auto_ids},
46 'idprefix=s' => \$opts{idprefix},
47 'idseparator=s' => \$opts{idseparator},
48 'wrap=s' => \$opts{wrap},
49 'man' => \$cli->{man},
50 'h|help' => \$cli->{help},
51 'v|version' => \$cli->{version},
52 8 ▶ ) or
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("o|output=s", \$cli->{"output"}, "a|attribute=s", sub { my($name, $val) = @_; _parse_attribute($cli->{"attributes"}, $val); } , "imagesdir=s", \$opts{"imagesdir"}, "auto-ids", \$opts{"auto_ids"}, "idprefix=s", \$opts{"idprefix"}, "idseparator=s", \$opts{"idseparator"}, "wrap=s", \$opts{"wrap"}, "man", \$cli->{"man"}, "h|help", \$cli->{"help"}, "v|version", \$cli->{"version"}) or _error("Invalid options.") 8 0 8
53 _error("Invalid options.");
54  
55 8 ▶ if ($cli->{help})
Branch: 1 branch
BranchTrueFalseTotal
if ($cli->{"help"}) 1 7 8
56 {
57 1 _print_help();
58 1 exit 0;
59 }
60  
61 7 ▶ if ($cli->{man})
Branch: 1 branch
BranchTrueFalseTotal
if ($cli->{"man"}) 1 6 7
62 {
63 1 pod2usage(-verbose => 2, -exitval => 0, -input => $0, -output => \*STDOUT);
64 }
65  
66 6 ▶ if ($cli->{version})
Branch: 1 branch
BranchTrueFalseTotal
if ($cli->{"version"}) 1 5 6
67 {
68 1 _print_version();
69 1 exit 0;
70 }
71  
72 5 my @remaining = @ARGV;
73  
74 5 ▶ if (@remaining == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (@remaining == 0) 1 4 5
75 {
76 1 _error("Missing input file. Use - for stdin.");
77 }
78  
79 4 ▶ if (@remaining > 1)
Branch: 1 branch
BranchTrueFalseTotal
if (@remaining > 1) 0 4 4
80 {
81 0 _error("Too many arguments. Only one input file expected.");
82 }
83  
84 4 $cli->{input} = $remaining[0];
85 4 $cli->{opts} = \%opts;
86  
87 4 return $cli;
88 }
89  
90 # Parse -a KEY=VALUE, -a KEY (empty), -a KEY! (unset)
91 sub _parse_attribute
92 1 ▶{
Subroutine _parse_attribute: called CC=3 SCAR=14.2
93 1 my ($attrs, $val) = @_;
94  
95 1 ▶ if ($val =~ m/^(.*?)\s*=\s*(.*)/)
Branch: 2 branches
BranchTrueFalseTotal
if ($val =~ /^(.*?)\s*=\s*(.*)/u) { } 1 0 1
elsif ($val =~ /^(.+)!$/u) { } 0 0 0
96 {
97 1 my $key = $1;
98 1 my $value = $2;
99 1 $attrs->{$key} = $value;
100 }
101 elsif ($val =~ m/^(.+)!$/)
102 {
103 0 my $key = $1;
104 0 $attrs->{$key} = '';
105 }
106 else
107 {
108 0 $attrs->{$val} = '';
109 }
110 }
111  
112 #===========================================================================
113 # Main run logic
114 #===========================================================================
115  
116 sub _run
117 4 ▶{
Subroutine _run: called CC=12 SCAR=27.5
118 4 my ($cli) = @_;
119  
120 4 my $input_file = $cli->{input};
121 4 my $output_file = $cli->{output};
122 4 my $attrs = $cli->{attributes};
123 4 my $opts = $cli->{opts};
124  
125 # Read input
126 4 my $text;
127 4 ▶ if ($input_file eq '-')
Branch: 1 branch
BranchTrueFalseTotal
if ($input_file eq '-') { } 1 3 4
128 {
129 1 local $/;
130 1 $text = <STDIN>;
131 }
132 else
133 {
134 3 ▶ if (!-f $input_file)
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $input_file) 0 3 3
135 {
136 0 _error("Input file '$input_file' does not exist or is not a regular file.");
137 }
138 3 ▶ open(my $fh, '<', $input_file) or
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $input_file or _error("Cannot open '${input_file}': $!") 3 0 3
139 _error("Cannot open '$input_file': $!");
140 3 local $/;
141 3 $text = <$fh>;
142 3 close($fh);
143 }
144  
145 # Determine output destination
146 4 my %convert_opts;
147 4 ▶ if (%$attrs)
Branch: 1 branch
BranchTrueFalseTotal
if (%$attrs) 1 3 4
148 {
149 1 $convert_opts{attributes} = $attrs;
150 }
151 4 for my $k (qw(imagesdir auto_ids idprefix idseparator wrap))
152 {
153 20 ▶ if (exists $opts->{$k})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{$k}) 20 0 20
154 {
155 20 $convert_opts{$k} = $opts->{$k};
156 }
157 }
158  
159 # Build converter and convert
160 4 my $converter = Text::MarkdownAdoc->new;
161 4 my $result = $converter->convert($text, \%convert_opts);
162  
163 # Write output
164 4 ▶ if (defined $output_file)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $output_file) { } 3 1 4
165 {
166 3 ▶ if ($output_file eq '-')
Branch: 1 branch
BranchTrueFalseTotal
if ($output_file eq '-') { } 2 1 3
167 {
168 2 print $result;
169 }
170 else
171 {
172 1 _check_same_file($input_file, $output_file);
173 0 ▶ open(my $fh, '>', $output_file) or
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $output_file or _error("Cannot write to '${output_file}': $!") 0 0 0
174 _error("Cannot write to '$output_file': $!");
175 0 print $fh $result;
176 0 close($fh);
177 }
178 }
179 else
180 {
181 1 ▶ if ($input_file eq '-')
Branch: 1 branch
BranchTrueFalseTotal
if ($input_file eq '-') { } 1 0 1
182 {
183 1 print $result;
184 }
185 else
186 {
187 0 my $out = _default_output($input_file);
188 0 _check_same_file($input_file, $out);
189 0 ▶ open(my $fh, '>', $out) or
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $out or _error("Cannot write to '${out}': $!") 0 0 0
190 _error("Cannot write to '$out': $!");
191 0 print $fh $result;
192 0 close($fh);
193 }
194 }
195  
196 3 return 0;
197 }
198  
199 #===========================================================================
200 # Helpers
201 #===========================================================================
202  
203 sub _default_output
204 0 ▶{
Subroutine _default_output: not called CC=1 SCAR=6.9
205 0 my ($input) = @_;
206  
207 0 my $out = $input;
208 0 $out =~ s/\.md$/.adoc/;
209 0 $out =~ s/\.markdown$/.adoc/;
210 0 $out =~ s/\.mdown$/.adoc/;
211 0 $out =~ s/\.mkd$/.adoc/;
212 0 $out =~ s/\.mkdn$/.adoc/;
213  
214 0 return $out;
215 }
216  
217 sub _check_same_file
218 1 ▶{
Subroutine _check_same_file: called CC=6 SCAR=20.2
219 1 my ($input, $output) = @_;
220  
221 1 ▶ return if $input eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $input eq "-" 0 1 1
222 1 ▶ return if $output eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $output eq "-" 0 1 1
223  
224 # Resolve both paths
225 1 my ($in_real, $out_real);
226 1 $in_real = _resolve_path($input);
227 1 $out_real = _resolve_path($output);
228  
229 1 ▶ if (defined $in_real && defined $out_real && $in_real eq $out_real)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $in_real and defined $out_real and $in_real eq $out_real) 1 0 1
Condition33%2/6
defined $in_real and defined $out_real and $in_real eq $out_real
!ll&&!rl&&r
001
defined $in_real and defined $out_real
!ll&&!rl&&r
001
MC/DC0%0/3
defined $in_real and defined $out_real and $in_real eq $out_real
defined $in_real defined $out_real $in_real eq $out_real
Truth table25%1/4
A and B and C
A= defined $in_realB= defined $out_realC= $in_real eq $out_real
ABCresult
0XX0
10X0
1100
1111
230 {
231 1 _error("Input and output cannot be the same file: '$input'");
232 }
233 }
234  
235 sub _resolve_path
236 2 ▶{
Subroutine _resolve_path: called CC=3 SCAR=19.2
237 2 my ($path) = @_;
238  
239 2 ▶ return $path if $path =~ m{^/};
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ m[^/]u 2 0 2
240  
241 0 my $cwd;
242 0 chomp($cwd = `pwd`);
243  
244 # Simple resolution: remove ./, ../
245 0 my $resolved = "$cwd/$path";
246 0 $resolved =~ s{/\./}{/}g;
247 0 ▶ while ($resolved =~ s{/[^/]+/\.\./}{/}) { }
Branch: 1 branch
BranchTrueFalseTotal
if ($resolved =~ s[/[^/]+/\.\./][/]u) 0 0 0
248  
249 0 return $resolved;
250 }
251  
252 sub _print_help
253 1 ▶{
Subroutine _print_help: called CC=1 SCAR=0.0
254 1 print <<'HELP';
255 Usage: markdown-adoc [OPTIONS] FILE
256 markdown-adoc [OPTIONS] -
257  
258 Convert Markdown to AsciiDoc.
259  
260 Options:
261 -o, --output=FILE Output file path; use - for stdout.
262 Default: replace .md extension with .adoc.
263 -a, --attribute=KEY=VALUE Set an AsciiDoc attribute (repeatable).
264 KEY (empty value) and KEY! (unset) also accepted.
265 --imagesdir=DIR Set the imagesdir option.
266 --auto-ids Enable auto-generated heading IDs.
267 --idprefix=STRING Set the ID prefix (default: _).
268 --idseparator=CHAR Set the ID separator (default: _).
269 --wrap=preserve|none|ventilate Line wrap mode (default: preserve).
270 --man Display full man-page help and exit.
271 -h, --help Display this help and exit.
272 -v, --version Display version and exit.
273  
274 Input:
275 FILE Input Markdown file to convert.
276 - Read from stdin.
277 HELP
278 }
279  
280 sub _print_version
281 1 ▶{
Subroutine _print_version: called CC=2 SCAR=7.2
282 1 ▶ my $version = $Text::MarkdownAdoc::VERSION // '0.1.1';
Condition50%1/2
$Text::MarkdownAdoc::VERSION // "0.1.1"
l!l
10
MC/DC0%0/1
$Text::MarkdownAdoc::VERSION // "0.1.1"
$Text::MarkdownAdoc::VERSION
Truth table50%1/2
A
A= $Text::MarkdownAdoc::VERSION
Aresult
11
00
283 1 print "markdown-adoc version $version\n";
284 }
285  
286 sub _error
287 2 ▶{
Subroutine _error: called CC=1 SCAR=0.0
288 2 my ($msg) = @_;
289  
290 2 print STDERR "markdown-adoc: $msg\n";
291 2 exit 1;
292 }
293  
294 __END__
« blib/lib/Text/MarkdownAdoc/Refs.pm ↑ Summary blib/lib/Text/MarkdownAdoc.pm »