script/md-utils.pl untested

filter: Statementstmt 0.0% 0 / 99 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 160 CC 10 SCAR 27.9
CC
10
Cov
0%
cmd_render
56.0
get_git_user
12.0
init
6.0
CRAP 16.3
1 #!/usr/bin/env perl
2 package Markdown::Render::CLI;
3  
4 use strict;
5 use warnings;
6  
7 use Carp;
8 use Cwd;
9 use Config::Tiny;
10 use CLI::Simple::Constants qw(:chars :booleans);
11 use Data::Dumper;
12 use English qw(-no_match_vars);
13 use File::Basename;
14 use Getopt::Long qw(:config no_ignore_case auto_abbrev);
15 use Readonly;
16  
17 use Markdown::Render;
18  
19 our $VERSION = '2.0.4';
20  
21 use parent qw(CLI::Simple);
22  
23 ########################################################################
24 sub choose(&) { ## no critic
25 ########################################################################
26 return $_[0]->();
27 }
28  
29 ########################################################################
30 sub cmd_version {
31 ########################################################################
32 my ($self) = @_;
33  
34 my ($name) = File::Basename::fileparse( $PROGRAM_NAME, qr/[.][^.]+$/xsm );
35  
36 print {*STDOUT} "$name $VERSION\n";
37  
38 return $SUCCESS;
39 }
40  
41 ########################################################################
42 sub get_git_user {
43 ########################################################################
44 my ($self) = @_;
45  
46 my ( $git_user, $git_email );
47  
48 for ( ( getcwd . '.git/config' ), "$ENV{HOME}/.gitconfig" ) {
49 next if !-e $_;
50  
51 my $config = eval { Config::Tiny->read($_); };
52  
53 ( $git_user, $git_email ) = @{ $config->{user} }{qw(name email)};
54  
55 last if $git_user && $git_email;
56 }
57  
58 return ( $git_user, $git_email );
59 }
60  
61 ########################################################################
62 sub new {
63 ########################################################################
64 my ( $class, @args ) = @_;
65  
66 # spoof CLI::Simple - force command render and preserve argument position
67 unshift @ARGV, 'render';
68  
69 return $class->SUPER::new(@args);
70 }
71  
72 ########################################################################
73 sub init {
74 ########################################################################
75 my ($self) = @_;
76  
77 if ( $self->get_raw ) {
78 $self->set_body($FALSE);
79 }
80  
81 return;
82 }
83  
84 ########################################################################
85 sub cmd_render {
86 ########################################################################
87 my ($self) = @_;
88  
89 my $markdown;
90  
91 my $infile = $self->get_infile;
92  
93 if ( !$infile ) {
94 ($infile) = $self->get_args;
95  
96 my $fh = choose {
97 return *STDIN
98 if !$infile && !-t STDIN;
99  
100 open my $fh, '<', $infile
101 or croak sprintf "ERROR: could not open %s for reading\n%s", $infile, $OS_ERROR;
102  
103 return $fh;
104 };
105  
106 local $RS = undef;
107  
108 $markdown = <$fh>;
109 }
110  
111 my ( $git_user, $git_email ) = $self->get_git_user();
112  
113 my $md = Markdown::Render->new(
114 markdown => $markdown,
115 body => $self->get_body,
116 css => $self->get_css,
117 engine => $self->get_engine,
118 git_email => $git_email,
119 user => $git_user,
120 html => $self->get_html,
121 mode => $self->get_mode,
122 no_title => $self->get_no_title,
123 raw => $self->get_raw,
124 render => $self->get_render,
125 title => $self->get_title,
126 );
127  
128 my $ofh = choose {
129 my $outfile = $self->get_outfile;
130  
131 return *STDOUT
132 if !$outfile;
133  
134 open my $fh, '>', $outfile
135 or croak sprintf "ERROR: could not open output file\n%s", $self->get_outfile, $OS_ERROR;
136  
137 return $fh;
138 };
139  
140 eval {
141 if ( $self->get_both ) {
142 $md->finalize_markdown->render_markdown;
143  
144 $md->print_html(
145 fh => $ofh,
146 body => $self->get_body,
147 css => $self->get_css,
148 title => $self->get_title,
149 );
150 }
151 elsif ( $self->get_render ) {
152 $md->render_markdown;
153 $md->print_html(
154 fh => $ofh,
155 body => $self->get_body,
156 css => $self->get_css,
157 title => $self->get_title,
158 );
159 }
160 else {
161 $md->finalize_markdown;
162 print {$ofh} $md->get_markdown;
163 }
164 };
165  
166 croak "ERROR: $EVAL_ERROR"
167 if $EVAL_ERROR;
168  
169 close $ofh;
170  
171 return $SUCCESS;
172 }
173  
174 ########################################################################
175 sub main {
176 ########################################################################
177  
178 my @option_specs = qw(
179 body|B!
180 both|b
181 css=s
182 debug
183 engine=s
184 help
185 infile=s
186 mode=s
187 no-title
188 nocss|N
189 outfile=s
190 raw|R
191 render|r
192 title=s
193 version
194 );
195  
196 my %commands = ( version => \&cmd_version, render => \&cmd_render );
197  
198 my $cli = Markdown::Render::CLI->new(
199 option_specs => \@option_specs,
200 commands => \%commands,
201 default_options => { body => $TRUE, },
202 extra_options => [qw(git_user git_email html)],
203 );
204  
205 return $cli->run();
206  
207 }
208  
209 exit main();
210  
211 __END__