script/asciidoc-down

filter: Statementstmt 97.0% 97 / 100 Branchbran 77.5% 31 / 40 Conditioncond 53.3% 8 / 15 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 92.8% 13 / 14 Podpod n/a totaltotal 85.3% 151 / 177 CC 3 SCAR 2.7
CC
3
Cov
88%
to_output_path
2.0
split_attr
2.0
usage
2.0
CRAP 1.3
« /usr/local/bin/perldoc ↑ Summary
1 #!/usr/bin/env perl
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 9 ▶use File::Basename qw(dirname basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use File::Temp qw(tempdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use Getopt::Long qw(GetOptions);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use Cwd qw(abs_path getcwd);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use Pod::Usage qw(pod2usage);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 9 ▶use lib File::Spec->catdir(dirname(dirname(abs_path(__FILE__))), 'lib');
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use Text::AsciidocDown;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 9 my @attrs;
17 my $output;
18 9 my $help;
19 9 my $man;
20 9 my $version;
21 9 my $prepublish;
22 9 my $postpublish;
23  
24 9 ▶GetOptions(
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("attribute|a=s\@", \@attrs, "output|o=s", \$output, "help|h", \$help, "man", \$man, "version|v", \$version, "prepublish", \$prepublish, "postpublish", \$postpublish) or usage(1) 9 0 9
25 'attribute|a=s@' => \@attrs,
26 'output|o=s' => \$output,
27 'help|h' => \$help,
28 'man' => \$man,
29 'version|v' => \$version,
30 'prepublish' => \$prepublish,
31 'postpublish' => \$postpublish,
32 ) or
33 usage(1);
34  
35 9 ▶if ($help)
Branch: 1 branch
BranchTrueFalseTotal
if ($help) 1 8 9
36 {
37 1 print "Usage: asciidoc-down [OPTION]... FILE\n";
38 1 print "Convert AsciiDoc FILE to Markdown.\n";
39 1 print " -a, --attribute name=val set attribute (repeatable)\n";
40 1 print " -o, --output path|- output path or stdout\n";
41 1 print " --prepublish convert and hide source\n";
42 1 print " --postpublish restore hidden source and remove output\n";
43 1 print " -h, --help show help\n";
44 1 print " --man show full man-page help\n";
45 1 print " -v, --version show version\n";
46 1 exit 0;
47 }
48  
49 8 ▶if ($man)
Branch: 1 branch
BranchTrueFalseTotal
if ($man) 1 7 8
50 {
51 1 pod2usage(-verbose => 2, -exitval => 0, -input => $0, -output => \*STDOUT);
52 }
53  
54 7 ▶if ($version)
Branch: 1 branch
BranchTrueFalseTotal
if ($version) 1 6 7
55 {
56 1 print $Text::AsciidocDown::VERSION, "\n";
57 1 exit 0;
58 }
59  
60 6 my $input = shift @ARGV;
61 6 ▶$input = 'README.adoc' if !defined($input) && ($prepublish || $postpublish);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $input and $prepublish || $postpublish 4 2 6
Condition66%4/6
not defined $input and $prepublish || $postpublish
!ll&&!rl&&r
204
$prepublish or $postpublish
l!l&&r!l&&!r
220
MC/DC33%1/3
not defined $input and $prepublish || $postpublish
not defined $input $prepublish $postpublish
Truth table75%3/4
not(A) and B or C
A= not defined $inputB= $prepublishC= $postpublish
ABCresult
0XX0
1000
11X1
1011
62 6 ▶usage(1) unless defined $input;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input 0 6 6
63  
64 6 ▶if ($postpublish)
Branch: 1 branch
BranchTrueFalseTotal
if ($postpublish) 2 4 6
65 {
66 2 ▶ my $out = defined($output) ? $output : to_output_path($input);
Branch: 1 branch
BranchTrueFalseTotal
defined $output ? : 0 2 2
67 2 my $hidden = to_hidden_path($input);
68 2 ▶ rename $hidden, $input if -f $hidden;
Branch: 1 branch
BranchTrueFalseTotal
if -f $hidden 2 0 2
69 2 ▶ unlink $out if defined($out) && $out ne '-' && -f $out;
Branch: 1 branch
BranchTrueFalseTotal
if defined $out and $out ne "-" and -f $out 2 0 2
Condition33%2/6
defined $out and $out ne "-" and -f $out
!ll&&!rl&&r
002
defined $out and $out ne "-"
!ll&&!rl&&r
002
MC/DC0%0/3
defined $out and $out ne "-" and -f $out
defined $out $out ne "-" -f $out
Truth table25%1/4
A and B and C
A= defined $outB= $out ne "-"C= -f $out
ABCresult
0XX0
10X0
1100
1111
70 2 exit 0;
71 }
72  
73 4 my %attr_map = map { split_attr($_) } @attrs;
74  
75 4 my $source_text;
76 4 ▶if ($input eq '-')
Branch: 1 branch
BranchTrueFalseTotal
if ($input eq '-') { } 1 3 4
77 {
78 1 local $/;
79 1 $source_text = <STDIN>;
80 }
81 else
82 {
83 3 ▶ open my $fh, '<:encoding(UTF-8)', $input or die "asciidoc-down: $input: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:encoding(UTF-8)", $input or die "asciidoc-down: ${input}: $!\n" 3 0 3
84 3 local $/;
85 3 $source_text = <$fh>;
86 3 close $fh;
87 }
88  
89 4 my $converter = Text::AsciidocDown->new(attributes => \%attr_map);
90 4 ▶my $out_text = $converter->convert(
Branch: 1 branch
BranchTrueFalseTotal
$input eq '-' ? : 1 3 4
91 $source_text,
92 {
93 attributes => \%attr_map,
94 source_path => ($input eq '-' ? undef : abs_path($input)),
95 }
96 ) .
97 "\n";
98  
99 4 ▶my $target = defined($output) ? $output : to_output_path($input);
Branch: 1 branch
BranchTrueFalseTotal
defined $output ? : 2 2 4
100 4 ▶if ($target eq '-')
Branch: 1 branch
BranchTrueFalseTotal
if ($target eq '-') { } 1 3 4
101 {
102 1 print $out_text;
103 }
104 else
105 {
106 3 ▶ open my $fh, '>:encoding(UTF-8)', $target or die "asciidoc-down: $target: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:encoding(UTF-8)", $target or die "asciidoc-down: ${target}: $!\n" 3 0 3
107 3 print {$fh} $out_text;
108 3 close $fh;
109 }
110  
111 4 ▶if ($prepublish && $input ne '-')
Branch: 1 branch
BranchTrueFalseTotal
if ($prepublish and $input ne "-") 2 2 4
Condition66%2/3
$prepublish and $input ne "-"
!ll&&!rl&&r
202
MC/DC50%1/2
$prepublish and $input ne "-"
$prepublish $input ne "-"
Truth table66%2/3
A and B
A= $prepublishB= $input ne "-"
ABresult
0X0
100
111
112 {
113 2 my $hidden = to_hidden_path($input);
114 2 ▶ rename $input, $hidden or die "asciidoc-down: cannot hide $input: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
rename $input, $hidden or die "asciidoc-down: cannot hide ${input}: $!\n" 2 0 2
115 }
116  
117 4 exit 0;
118  
119 sub split_attr
120 2 ▶{
Subroutine split_attr: called CC=2 SCAR=6.9
121 2 my ($raw) = @_;
122 2 my ($k, $v) = split /=/, $raw, 2;
123 2 ▶ $v = '' unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 1 1 2
124 2 return ($k => $v);
125 }
126  
127 sub to_output_path
128 4 ▶{
Subroutine to_output_path: called CC=2 SCAR=7.0
129 4 my ($path) = @_;
130 4 ▶ return '-' if $path eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $path eq "-" 0 4 4
131 4 (my $out = $path) =~ s/\.adoc$/.md/;
132 4 return $out;
133 }
134  
135 sub to_hidden_path
136 4 ▶{
Subroutine to_hidden_path: called CC=1 SCAR=0.0
137 4 my ($path) = @_;
138 4 my $base = basename($path);
139 4 my $dir = dirname($path);
140 4 return File::Spec->catfile($dir, '.' . $base);
141 }
142  
143 sub usage
144 0 ▶{
Subroutine usage: not called CC=1 SCAR=6.9
145 0 my ($code) = @_;
146 0 print STDERR "Usage: asciidoc-down [OPTION]... FILE\n";
147 0 exit $code;
148 }
149  
150 __END__
« /usr/local/bin/perldoc ↑ Summary