-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathgithub_md2html
More file actions
executable file
·258 lines (224 loc) · 8.09 KB
/
github_md2html
File metadata and controls
executable file
·258 lines (224 loc) · 8.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/usr/bin/perl -w
# $Id: github_md2html 1189 2019-07-13 16:41:07Z mueller $
# SPDX-License-Identifier: GPL-3.0-or-later
# Copyright 2016-2019 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
#
# Revision History:
# Date Rev Version Comment
# 2019-07-13 1189 1.1.5 drop superfluous exists for $opts
# 2018-12-18 1089 1.1.4 add and use bailout
# 2018-10-19 1057 1.1.3 add --verbose; don't list up-to-date files anymore
# 2018-07-02 1033 1.1.2 use non-greedy match in -stand code
# 2018-05-13 1021 1.1.1 handle fragment identifiers in -standalone mapping
# 2017-01-02 837 1.1 add -standalone and -trace; add rate wait
# 2016-12-17 823 1.0 Initial version
#
use 5.14.0; # require Perl 5.14 or higher
use strict; # require strict checking
use Getopt::Long;
use LWP::UserAgent;
use JSON::XS;
my %opts = ();
GetOptions(\%opts,
"context:s", "force", "standalone", "trace", "verbose",
"dump", "help")
or bailout("bad command options");
my $url_ghapi_md = "https://api.github.com/markdown";
my $url_ghapi_mdraw = "https://api.github.com/markdown/raw";
my $url_css_ghmd = "https://wfjm.github.io/css/github-markdown.css";
my $url_css_ghmdbody = "https://wfjm.github.io/css/github-markdown-body.css";
autoflush STDOUT 1 if (-p STDOUT); # autoflush if output into pipe
autoflush STDOUT 1 if (-t STDOUT); # autoflush if output into term
if ($opts{help}) {
print_help();
exit 0;
}
my @flist;
foreach my $arg (@ARGV) {
if (! -e $arg) {
print STDERR "github_md2html-E: file or directory '$arg' not found\n";
} elsif (-f $arg) {
push @flist, $arg;
} elsif (-d $arg) {
open (FFILE, "find $arg -name '*.md' -type f | sort |")
or bailout("Failed to run 'find $arg': $!");
while (<FFILE>) {
chomp;
push @flist, $_;
}
close FFILE;
} elsif (-l $arg) {
print STDERR "github_md2html-W: symlink '$arg' ignored\n";
} else {
print STDERR "github_md2html-E: '$arg' not file or directory, ignored\n";
}
}
unless (scalar @flist) {
print STDERR "github_md2html-E: no files specified of found\n";
print_help();
exit 1;
}
foreach my $file (@flist) {
do_md2html($file);
}
#-------------------------------------------------------------------------------
sub do_request {
my ($ifile,$ua,$req) = @_;
for (my $nretry=0; $nretry<10; $nretry++) {
my $res = $ua->request($req);
if ($opts{dump}) {
print "------------------------------------------------------\n";
print "response for $ifile\n";
print $res->as_string,"\n";
print "------------------------------------------------------\n";
}
return $res if $res->is_success;
my $rate_limit = $res->header('X-RateLimit-Limit');
my $rate_remain = $res->header('X-RateLimit-Remaining');
my $rate_reset = $res->header('X-RateLimit-Reset');
if ($res->status_line eq '403 Forbidden' &&
defined $rate_limit && defined $rate_remain && defined $rate_reset &&
$rate_remain == 0) {
my $twait = $rate_reset - time;
my $twait_min = $twait / 60;
my $twait_sec = $twait % 60;
printf "github_md2html-I: rate limit %d/hr reached, wait %2dm%02ds\n",
$rate_limit,
$twait_min, $twait_sec;
sleep $twait+1;
} else {
print STDERR "github_md2html-E: api error:'" . $res->status_line . "'\n";
print STDERR "response for $ifile\n";
print STDERR $res->as_string,"\n";
return undef;
}
}
print STDERR "github_md2html-E: retry limit reached\n";
return undef;
}
#-------------------------------------------------------------------------------
sub do_md2html {
my ($ifile) = @_;
my $ofile = "$ifile.html";
my $doit = $opts{force} || (not -e $ofile);
unless ($doit) {
# -M returns <script_start_time> - <file_modify_time> in fractional days
# --> thus file age in days relative to now
$doit = -M $ofile > -M $ifile; # output older than input
}
unless ($doit) {
print "$ifile: ok\n" if $opts{verbose};
return;
}
# get file path and name
my $ifile_path = '.';
my $ifile_name = $ifile;
if ($ifile =~ m|^(.+)/(.+)|) {
$ifile_path = $1;
$ifile_name = $2;
}
# read input file
my $idata;
{
local $/; # slurp file ...
open IFILE, $ifile or bailout("file read open for '$ifile' failed: $!");
$idata = <IFILE>;
close IFILE;
}
# prepare request
my $ua = LWP::UserAgent->new;
my $req;
# with context --> use markdown api (json based)
if (exists $opts{context}) {
$req = HTTP::Request->new('POST', $url_ghapi_md);
my %apireq = ('mode' => 'gfm',
'context' => $opts{context},
'text' => $idata
);
my $apireq_json = encode_json(\%apireq);
$req->content($apireq_json);
# no context --> use markdown/raw api
} else {
$req = HTTP::Request->new('POST', $url_ghapi_mdraw);
$req->content_type('text/x-markdown');
$req->content($idata);
}
if ($opts{dump}) {
print "------------------------------------------------------\n";
print "request for $ifile\n";
print $req->as_string,"\n";
}
my $res = do_request($ifile, $ua, $req);
return unless defined $res;
my $html = $res->decoded_content;
if ($opts{standalone}) {
$html =~ s{<a(.*?)href="(.+?)"}{ # non-greedy matches !!
my $hopt = $1;
my $href = "$2";
unless ($href =~ m|^https?://|) {
if ($href =~ m|^(.+\.md)(#.*)?$|) {
$href = $1 . '.html';
$href .= $2 if defined $2;
print " hmap: $href\n" if $opts{trace};
} else {
if (-d "$ifile_path/$href") {
$href =~ s|/$||; # drop trailing '/'
if (-r "$ifile_path/$href/README.md") {
$href .= '/README.md.html';
print " dmap: $href: ok\n" if $opts{trace};
} else {
print " dmap: $href: skipped\n" if $opts{trace};
}
}
}
}
"<a${hopt}href=\"$href\"";
}gex;
}
open OFILE, ">$ofile" or bailout("file write open for '$ofile' failed: $!");
print OFILE '<!DOCTYPE html>',"\n";
print OFILE '<html>',"\n";
print OFILE '<head>',"\n";
print OFILE ' <meta http-equiv="Content-Type"',
' content="text/html; charset=UTF-8">',"\n";
print OFILE ' <meta name="generator"',
' content="github_md2html via github api">',"\n";
print OFILE ' <link rel="stylesheet"',
' href="',$url_css_ghmd,'">',"\n";
print OFILE ' <link rel="stylesheet"',
' href="',$url_css_ghmdbody,'">',"\n";
print OFILE '</head>',"\n";
print OFILE '<body class="markdown-body">',"\n";
if ($opts{standalone} && $ifile_name eq 'README.md') {
print OFILE '<p><i>Directory README.',"\n";
print OFILE 'To <a href=".">local directory listing</a></i>.</p>',"\n";
}
print OFILE $html,"\n";
print OFILE '</body>',"\n";
print OFILE '</html>',"\n";
close OFILE;
my $rate_limit = $res->header('X-RateLimit-Limit');
my $rate_remain = $res->header('X-RateLimit-Remaining');
my $rate_reset = $res->header('X-RateLimit-Reset');
my $time_reset = $rate_reset - time;
my $reset_min = $time_reset / 60;
my $reset_sec = $time_reset % 60;
printf "%s: done, rate %d of %d for %2dm%02ds\n",
$ifile, $rate_remain, $rate_limit, $reset_min, $reset_sec;
}
#-------------------------------------------------------------------------------
sub bailout {
my ($msg) = @_;
print STDERR "github_md2html-F: $msg\n";
exit 1;
}
#-------------------------------------------------------------------------------
sub print_help {
print "usage: github_md2html [opts] files...\n";
print " --force update all (default: check timestamps)\n";
print " --standalone modify links for local browser usage\n";
print " --trace trace link mapping in standalone mode\n";
print " --context c uses context and markdown api (default: raw api)\n";
print " --dump print HTTP request and response\n";
print " --help this message\n";
}