md2html.pl untested

filter: Statementstmt 0.0% 0 / 56 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 2 Podpod n/a totaltotal 0.0% 0 / 117 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #!/pro/bin/perl
2  
3 use 5.012001;
4 use warnings;
5  
6 our $VERSION = "0.05 - 20230104";
7  
8 my $fmd = "Checklist.md";
9 my $fhtm = "Checklist.html";
10  
11 my %c = map { $_ => (stat $_)[9] || 0 } $fmd, $fhtm;
12 $c{$fmd} && $c{$fhtm} && $c{$fhtm} - $c{$fmd} > 2 and
13 die "Did you edit $fhtm instead of $fmd\n";
14  
15 -d ".git" or exit 0;
16  
17 my @m = stat $fmd;
18 my @h = stat $fhtm;
19  
20 $m[9] && $h[9] && $h[9] >= $m[9] and exit 0;
21  
22 print "Converting $fmd to $fhtm\n";
23  
24 open my $fh, ">", $fhtm or die "Cannot make $fhtm: $!\n";
25 print $fh <<"EOH";
26 <?xml version="1.0" encoding="iso-8859-1"?>
27 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"
28 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
29 <head>
30 <title>Release Checklist</title>
31 </head>
32 <body>
33 EOH
34  
35 my $fhx = $fhtm . "_x";
36 my $done = 0;
37 if (grep { -x } map { "$_/pandoc" } grep { $_ && -d } split m/:+/ => $ENV{PATH}) {
38 say "Converting with pandoc";
39 system "pandoc", "-t", "html", "-o", $fhx, $fmd;
40 if (open my $xh, "<", $fhx) {
41 print $fh (<$xh>);
42 print $fh "</body></html>\n";
43 close $fh;
44 $done = 1;
45 }
46 else {
47 warn "pandoc failed to create HTML\n";
48 }
49 }
50 if (!$done and grep { -x } map { "$_/cmark" } grep { $_ && -d } split m/:+/ => $ENV{PATH}) {
51 say "Converting with cmark";
52 if (my @h = `cmark -t html $fmd`) {
53 print $fh @h;
54 print $fh "</body></html>\n";
55 close $fh;
56 $done = 1;
57 }
58 }
59 unless ($done) {
60 say "Converting with multimarkdown";
61 system "multimarkdown", "-o", $fhx, $fmd;
62  
63 open my $xh, "<", $fhx or die "multimarkdown failed!\n";
64 print $fh do { local $/; <$xh>; }
65 =~ s{<p><code>(\w+)}{<pre class="$1">}gr
66 =~ s{<pre><code class="(\w+)">}{<pre class="$1">\n}gr
67 =~ s{<p><code>}{<pre>}gr
68 =~ s{<pre><code>}{<pre>}gr
69 =~ s{</code></p>}{</pre>}gr
70 =~ s{</code></pre>}{</pre>}gr
71 =~ s{<pre>\K(?=.)}{\n}gr
72 =~ s{<(?:li|p)>\K }{}gr,
73 "</body></html>";
74 close $xh;
75 close $fh;
76 unlink $fhx;
77 }
78  
79 my $t = (stat $fmd)[9] + 1;
80 utime $t, $t, $fhtm;