-
Notifications
You must be signed in to change notification settings - Fork 4
/
jodoc
executable file
·422 lines (364 loc) · 11.6 KB
/
jodoc
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#!/usr/bin/env perl
# Everything moved into one file, does things the right way
use strict;
use warnings;
use Getopt::Long;
use IPC::Open2;
# parse the input options
my $markdown_bin;
my $outputdir;
my $title;
my $toc;
my $template;
my $smartypants_bin;
my $use_smartypants;
my $use_tidy;
my $tidy_bin;
my $template_html = do { local $/; <DATA> };
my %h1s = ();
my %processed = ();
my %files_to_h1s = ();
my @fileset = ();
my $result = GetOptions(
"markdown|m=s" => \$markdown_bin,
"output|o=s" => \$outputdir,
"title|t=s" => \$title,
"toc=s" => \$toc,
"template=s" => \$template,
"s" => \$use_smartypants,
"smartypants=s" => \$smartypants_bin,
"tidy" => \$use_tidy,
"htmltidy=s" => \$tidy_bin
);
# Exit nicer than an empty die
exit(-1) unless ($result);
die "You need to specify --output or -o if you use --toc.\n"
if ( $toc && !defined($outputdir) );
$title = "joDoc" unless ( defined($title) );
if ($template) {
$template_html = read_file($template);
}
# grab comments out of incoming text
sub docker {
my $input = shift;
my @output = $input =~ m{/\*\*(?:.|[\r\n])*?\*/}g;
s{(\*/|/\*\*)}{}g for @output;
s{([\r\n]+)\s}{$1}g for @output;
return join( "\n", @output );
}
# put a nice header on the html output
# templates can use $body, $title
sub html_head {
my $body = shift;
my $output = $template_html;
$output =~ s{\$title}{$title}g;
$output =~ s{\$body}{$body}g;
return $output;
}
# Read file into a string
sub read_file {
my $in = shift;
my $out;
open( my $fh, '<', $in ) or die $!;
{
# Deactivate input record separator
local $/;
$out = <$fh>;
}
close $fh or die $!;
return $out;
}
# Write a file out
sub write_file {
my $file = shift;
my $outstring = shift;
open( my $fh, '>', $file ) or die $!;
print $fh $outstring;
close $fh or die $!;
return;
}
# link together all the html output, make an index at the bottom of each page
# will link between all files in the output
sub autolink {
my @keywords = keys %h1s;
# Make the most specific keywords appear first, very important
@keywords = reverse sort { lc($a) cmp lc($b) } @keywords;
# let whitespace match correctly in an h1 tag
s{\s}{\\s}g for @keywords;
# prepare for the regex
my $keys = join( "|", @keywords );
for my $file ( keys %processed ) {
my $input = $processed{$file};
# put an external css class on outbound links
$input =~
s{(\<a)\s+(href=\"(?:http|mailto|ftp))}{$1 class=\"external\" $2}g;
if (@keywords) {
# make anything refing a keywork link to it
if ($outputdir) {
$input =~ s/
(\W+) # The word should be delimited by something
($keys) # Search for any of the keywords
(?!\<\/a|\w+) # Except if if it's in a tag or the prefix to another word
/$1\<a href=\"$h1s{$2}\#$2\"\>$2\<\/a\>/gx
; # Link it to the proper page and h1 tag
# make a #tag for the keyword h1 tag
$input =~ s{\<h1\>\<a href="[^\#\>]*#}{\<h1\>\<a name="}g;
}
else {
$input =~ s/
(\W+) # The word should be delimited by something
($keys) # Search for any of the keywords
(?!\<\/a|\w+) # Except if if it's in a tag or the prefix to another word
/$1\<a href=\"\#$2\"\>$2\<\/a\>/gx
; # Link it to the proper h1 tag
# make a #tag for the keyword h1 tag
$input =~ s{\<h1\>\<a href="#}{\<h1\>\<a name="}g;
}
}
$processed{$file} = $input;
}
return;
}
# Make an alphasorted index
sub indexer {
my @keywords = keys %h1s;
my $index = qq(\n\n<hr>\n\n<h1>Index</h1>\n<div id="index">\n);
my $lastletter = "ZZ";
for my $i ( sort { lc($a) cmp lc($b) } @keywords ) {
my $letter = uc( substr( $i, 0, 1 ) );
if ( $letter ne $lastletter ) {
if ( $lastletter ne "ZZ" ) {
$index .= "</ul>\n";
}
$index .= "\n<h2>$letter</h2>\n";
$index .= "\n<ul>\n";
$lastletter = $letter;
}
if ($outputdir) {
$index .= qq(<li><a href="$h1s{$i}#$i">$i</a></li>\n);
}
else {
$index .= qq(<li><a href="#$i">$i</a></li>\n);
}
}
$index .= "</ul></div>\n\n";
return $index;
}
# Pipe stuff through markdown for parsing
sub markdown_pipe {
my $in = shift;
my @out = ();
# Use pipes instead of temp files
# The magic here is what you expect open(HANDLE, "| $markdown_bin |") to do.
my $pid = open2( my $chld_out, my $chld_in, $markdown_bin ) or die $!;
print $chld_in $in;
close $chld_in or die $!;
while (<$chld_out>) {
push( @out, $_ );
}
close $chld_out or die $!;
# We don't leave a process behind!
waitpid( $pid, 0 );
return join( "", @out );
}
sub smartypants_pipe {
my $in = shift;
my @out = ();
my $pid = open2( my $chld_out, my $chld_in, "$smartypants_bin" )
or die $!;
print $chld_in $in;
close $chld_in or die $!;
while (<$chld_out>) {
push( @out, $_ );
}
close $chld_out or die $!;
# We don't leave a process behind!
waitpid( $pid, 0 );
return join( "", @out );
}
sub tidy_pipe {
my $in = shift;
my @out = ();
my $pid = open2( my $chld_out, my $chld_in, "$tidy_bin" )
or die $!;
print $chld_in $in;
close $chld_in or die $!;
while (<$chld_out>) {
push( @out, $_ );
}
close $chld_out or die $!;
# We don't leave a process behind!
waitpid( $pid, 0 );
return join( "", @out );
}
# Find all the h1tags for all files
sub h1finder {
%h1s = ();
%files_to_h1s = ();
for my $file ( keys %processed ) {
my @h1 = $processed{$file} =~ m{\<h1\>([^\<]+)\</h1\>}g;
$h1s{$_} = $file for @h1;
$files_to_h1s{$file} = \@h1;
}
return;
}
# Expand {} blocks in table of contents
sub toclinker {
my $indent = shift;
my $pathpart = shift;
my @matching_files = keys %files_to_h1s;
@matching_files = grep { m{$pathpart} } @matching_files;
my @matching_h1s = ();
push( @matching_h1s, @{ $files_to_h1s{$_} } ) for @matching_files;
@matching_h1s = sort { lc($a) cmp lc($b) } @matching_h1s;
$_ = $indent . '* ' . $_ . "\n" for @matching_h1s;
# escape _ characters to make markdown happy
my $match = join( "", @matching_h1s );
$match =~ s/\_/\\_/g;
return $match;
}
# munge output file names
sub munge {
my %munged_processed = ();
for my $file ( keys %processed ) {
$munged_processed{munge_filename($file)} = $processed{$file};
}
%processed = %munged_processed;
return;
}
sub munge_filename {
my $file = shift;
my @path_parts = split( m{/}, $file );
s{^\.+}{}g for @path_parts;
# Remove empty path parts, looks prettier that way
@path_parts = grep { $_ ne "" } @path_parts;
my $path = join( '_', @path_parts );
my $munged = $path . '.html';
return $munged;
}
# Create a JSON output of the index
sub index_to_json {
my @keywords = keys %h1s;
@keywords = sort { lc($a) cmp lc($b) } @keywords;
$_ = qq({"term":"$_","url":"$h1s{$_}"}) for @keywords;
return '[' . join( ',', @keywords ) . ']';
}
sub main {
# If not specified, find a markdown parser in the path
chomp( $markdown_bin = qx(which markdown) ) unless $markdown_bin;
chomp( $smartypants_bin = qx(which smartypants) ) unless $smartypants_bin;
chomp( $tidy_bin = qx(which tidy) ) unless $tidy_bin;
# We can't do anything if we can't call markdown
die "Markdown parser not found!\n" unless ( -x $markdown_bin );
die "Smartypants prettifier not found!\n"
if ( $use_smartypants && !( -x $smartypants_bin ) );
die "HTMLTidy prettifier not found!\n"
if ( $use_tidy && !( -x $tidy_bin ) );
# Slurp all the files and process them
# recurse current directory if no input arguments
my @files = @ARGV;
@files = glob("./*") unless ( $ARGV[0] );
for my $file (@files) {
unless ( -e $file ) {
warn "$file not found!\n";
next;
}
# recurse
if ( -d $file ) {
my @subdir = glob("$file/*");
push( @files, @subdir );
next;
}
if ( $file =~ m{\.(js|css|mdown|html|md|markdown|htm)$}i ) {
my $content = read_file($file);
# javascript and css files get "cleaned"
if ( $file =~ m{\.(js|css)$} ) {
$content = docker($content);
}
# pure htmlfiles don't have markdown applied
unless ( $file =~ m{\.html$} ) {
$content = markdown_pipe($content);
}
$processed{$file} = $content;
push(@fileset, munge_filename($file));
}
}
#TODO: This is a nasty hack, but it works. Fix when times are better.
if ($toc) {
h1finder;
my @toclinked;
open( my $fh, '<', $toc ) or die $!;
my @content = <$fh>;
close $fh or die $!;
for my $line (@content) {
if ( $line =~ m/(\s*).\s*{(.+)}/ ) {
$line = toclinker( $1, $2 );
}
push( @toclinked, $line );
}
$processed{_content} = markdown_pipe( join( "", @toclinked ) );
}
munge;
h1finder;
autolink;
# Print docs to separate files in the same hierarchy as the input
if ($outputdir) {
$processed{"_index.html"} = indexer;
mkdir $outputdir;
for my $file ( keys %processed ) {
# if the processed output doesn't have at least ONE WORD CHARACTER, then it's blank.
if ( $processed{$file} =~ m{\w} ) {
# Don't wrap html files with <html> tags
unless ( $file =~ m{\.htm[l]?\.html$}i ) {
$processed{$file} = html_head( $processed{$file} );
}
if ($use_tidy) {
$processed{$file} = tidy_pipe( $processed{$file} );
}
if ($use_smartypants) {
$processed{$file} = smartypants_pipe( $processed{$file} );
}
write_file( $outputdir . '/' . $file, $processed{$file} );
}
else {
warn "File $file contained no content, not creating file\n";
}
}
write_file( $outputdir . '/_index.json', index_to_json );
}
# Print to stdout
else {
my $tempout = '';
for my $i (@fileset) {
$tempout .= $processed{$i};
}
# my $tempout = join( "", values %processed );
$tempout = html_head($tempout) . indexer;
if ($use_tidy) {
$tempout = tidy_pipe($tempout);
}
if ($use_smartypants) {
$tempout = smartypants_pipe($tempout);
}
print $tempout;
}
return;
}
main;
# default html template, mobile compatible
__DATA__
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>$title</title>
<meta name="generator" content="joDoc">
<link rel="stylesheet" type="text/css" href="docbody.css">
<link rel="stylesheet" type="text/css" href="doc.css">
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no, width=device-width">
<meta name="format-detection" content="false">
</head>
<body>
$body
</body>
</html>