script/md-utils.pl untested

filter: Statementstmt 0.0% 0 / 90 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 16 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 147 CC 3 SCAR 13.5
CC
3
Cov
0%
get_git_user
12.0
version
2.0
usage
2.0
CRAP 3.9
1 #!/usr/local/bin/perl
2  
3 use strict;
4 use warnings;
5  
6 use Carp;
7 use Cwd;
8 use Config::Tiny;
9 use Data::Dumper;
10 use English qw(-no_match_vars);
11 use File::Basename;
12 use Getopt::Long qw(:config no_ignore_case auto_abbrev);
13 use Readonly;
14  
15 use Markdown::Render;
16  
17 our $VERSION = $Markdown::Render::VERSION;
18  
19 Readonly our $EMPTY => q{};
20 Readonly our $TRUE => 1;
21 Readonly our $FALSE => 0;
22  
23 ########################################################################
24 sub version {
25 ########################################################################
26 my ($name) = File::Basename::fileparse( $PROGRAM_NAME, qr/[.][^.]+$/xsm );
27  
28 print "$name $VERSION\n";
29  
30 return;
31 }
32  
33 ########################################################################
34 sub usage {
35 ########################################################################
36 print <<'END_OF_USAGE';
37 usage: md-utils options [markdown-file]
38  
39 Utility to add a table of contents and other goodies to your GitHub
40 flavored markdown.
41  
42 - Add @TOC@ where you want to see your TOC.
43 - Add @TOC_BACK@ to insert an internal link to TOC
44 - Add @DATE(format-str)@ where you want to see a formatted date
45 - Add @GIT_USER@ where you want to see your git user name
46 - Add @GIT_EMAIL@ where you want to see your git email address
47 - Use the --render option to render the HTML for the markdown
48  
49 Examples:
50 ---------
51 md-utils README.md.in > README.md
52  
53 md-utils -r README.md.in
54  
55 Options
56 -------
57 -B, --body default is to add body tag, use --nobody to prevent
58 -b, --both interpolates intermediate file and renders HTML
59 -c, --css css file
60 -e, --engine github, text_markdown (default: github)
61 -h help
62 -i, --infile input file, default: STDIN
63 -m, --mode for GitHub API mode is 'gfm' or 'markdown' (default: markdown)
64 -n, --no-titl do not print a title for the TOC
65 -o, --outfile outfile, default: STDOUT
66 -r, --render render only, does NOT interpolate keywords
67 -R, --raw return raw HTML from engine
68 -t, --title string to use for a custom title, default: "Table of Contents"
69 -v, --version version
70  
71 Tips
72 ----
73 * Use !# to prevent a header from being include in the table of contents.
74 Add your own custom back to TOC message @TOC_BACK(Back to Index)@
75  
76 * Date format strings are based on format strings supported by the Perl
77 module 'Date::Format'. The default format is %Y-%m-%d if not format is given.
78  
79 * use the --nobody tag to return the HTML without the <html><body></body></html>
80 wrapper. --raw mode will also return HTML without wrapper
81 END_OF_USAGE
82 return;
83 }
84  
85 ########################################################################
86 sub get_git_user {
87 ########################################################################
88 my ( $git_user, $git_email );
89  
90 for ( ( getcwd . '.git/config' ), "$ENV{HOME}/.gitconfig" ) {
91 next if !-e $_;
92  
93 my $config = eval { Config::Tiny->read($_); };
94  
95 ( $git_user, $git_email ) = @{ $config->{user} }{qw(name email)};
96  
97 last if $git_user && $git_email;
98 }
99  
100 return ( $git_user, $git_email );
101 }
102  
103 # +------------------------ +
104 # | MAIN SCRIPT STARTS HERE |
105 # +------------------------ +
106  
107 my %options;
108  
109 my @options_spec = qw(
110 body|B!
111 both|b
112 css=s
113 debug
114 engine=s
115 help
116 infile=s
117 mode=s
118 no-title
119 outfile=s
120 raw|R
121 render|r
122 title=s
123 version
124 );
125  
126 GetOptions( \%options, @options_spec )
127 or croak 'could not parse options';
128  
129 $options{body} //= $TRUE;
130  
131 if ( $options{raw} ) {
132 $options{body} = $FALSE;
133 }
134  
135 if ( exists $options{help} ) {
136 usage;
137 exit 0;
138 }
139  
140 if ( exists $options{version} ) {
141 version;
142 exit 0;
143 }
144  
145 $options{no_title} = delete $options{'no-title'};
146  
147 my $markdown;
148  
149 if ( !$options{infile} ) {
150  
151 if (@ARGV) {
152 $options{infile} = shift @ARGV;
153 }
154 elsif ( !-t STDIN ) { ## no critic (ProhibitInteractiveTest)
155 local $RS = undef;
156  
157 my $fh = *STDIN;
158  
159 $markdown = <$fh>;
160 }
161 }
162  
163 my ( $git_user, $git_email ) = get_git_user();
164  
165 $options{git_user} = $git_user // $EMPTY;
166 $options{git_email} = $git_email // $EMPTY;
167  
168 my $md = Markdown::Render->new( %options, markdown => $markdown );
169  
170 my $ofh = *STDOUT;
171  
172 if ( exists $options{outfile} ) {
173 open $ofh, '>', $options{outfile} ## no critic (RequireBriefOpen)
174 or croak "could not open output file\n";
175 }
176  
177 eval {
178 if ( $options{both} ) {
179 $md->finalize_markdown->render_markdown;
180 $md->print_html( %options, fh => $ofh );
181 }
182 elsif ( $options{render} ) {
183 $md->render_markdown;
184 $md->print_html( %options, fh => $ofh );
185 }
186 else {
187 $md->finalize_markdown;
188 print {$ofh} $md->get_markdown;
189 }
190 };
191  
192 croak "ERROR: $EVAL_ERROR"
193 if $EVAL_ERROR;
194  
195 close $ofh;
196  
197 exit 0;
198  
199 __END__