-
Notifications
You must be signed in to change notification settings - Fork 8
/
man-to-md.pl
executable file
·783 lines (666 loc) · 21.2 KB
/
man-to-md.pl
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
#!/usr/bin/perl
# man-to-md -- Converts nroff man pages to Markdown.
# Copyright © 2016-2020 Maximilian Eul
#
# This file is part of man-to-md.
#
# man-to-md is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# man-to-md is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with man-to-md. If not, see <http://www.gnu.org/licenses/>.
use strict;
use warnings;
use utf8;
use Getopt::Long qw(:config no_getopt_compat bundling);
use File::Basename qw(dirname basename);
chdir dirname($0);
use constant {
PROGNAME => basename($0),
PROGVER => '0.17.0',
PROGDATE => '2020-12-18',
DEFAULT_COMMENT => "This file was autogenerated from the man page with 'make README.md'",
};
my ($section, $subsection, $prev_section);
my ($is_synopsis, $in_list, $start_list_item, $is_desclist, $in_rawblock, $in_preblock);
my ($in_urltitle, $in_mailtitle);
my ($progname, $mansection, $version, $is_bare_version, $verdate, $description);
my ($lineopt, $line_did_set_options);
my $headline_prefix = '# ';
my $section_prefix = '# ';
my $subsection_prefix = '### ';
my $re_token = qr/(?:"(?:\.|[^"])*+"|(?:\\.|[^\s"])(?:\\.|\S)*+)/; # matches one token, with or without "enclosure".
my $re_urlprefix = qr/(?:https?:|s?ftp:|www)/;
my $re_url = qr/${re_urlprefix}.+?/;
my $re_email = qr/(?:\w[\w\-_\.\+]*@[\w\-_\+\.]+?\.[\w\-]+)/;
my $re_punctuation = qr/[\s,;\.\?!]/;
my $replacement_token = "\001kXXfQ6Yd" . int(10000 * rand);
my %paste_after_section = ( ); # ('section' => ['filename'...], ...)
my %paste_before_section = ( );
my $plain_dashes = 1;
my $code_formatting = 0;
my $add_comment;
my %strings = ( );
my %words = ( );
my %stopwords = map { $_ => 1 } (qw(
a an the
as at and but by for from nor or so yet while if on of off to it its it's
on in onto into with within unless while after before once since until when since
));
sub Syntax (;$) {
printf STDERR <<EOT, PROGNAME;
syntax: %s [OPTIONS] < input.nroff > output.md
Converts nroff man pages to Markdown.
Options:
-p, --paste-section-after SECTION:FILENAME
Pastes the contents of FILENAME after the input SECTION
and adds the filename as section title.
-P, --paste-section-before SECTION:FILENAME
Pastes the contents of FILENAME right before the input SECTION
and adds the filename as section title.
--paste-after SECTION:FILENAME Like -p, but does not add a section title.
--paste-before SECTION:FILENAME Like -P, but does not add a section title.
-c, --comment [COMMENT] Adds an invisible comment as first line.
Uses a default comment without its argument.
--escaped-dashes Don't remove the backslash from escaped dashes (\\-).
-w, --word WORD Adds a word to the list of words
not to be titlecased in chapter titles.
-f, --formatted-code Allow formatting in nf/fi code blocks and Synopsis line.
-h, --help Show program help
-V, --version Show program version
EOT
exit ($_[0] // 0);
}
sub Version () {
printf <<EOT, PROGNAME, PROGVER, PROGDATE;
%s v%s
Written by Maximilian Eul <maximilian\@eul.cc>, %s.
License GPLv3+: GNU GPL Version 3 or later <http://gnu.org/licenses/gpl.html>
EOT
exit;
}
GetOptions(
'paste-after=s@' => sub{ add_paste_file('after', split /:/, $_[1]) },
'paste-before=s@' => sub{ add_paste_file('before', split /:/, $_[1]) },
'p|paste-section-after=s@' => sub{ add_paste_file('after', split(/:/, $_[1]), 1) },
'P|paste-section-before=s@' => sub{ add_paste_file('before', split(/:/, $_[1]), 1) },
'escaped-dashes' => sub{ $plain_dashes = 0 },
'c|comment:s' => sub{ $add_comment = (length $_[1]) ? $_[1] : DEFAULT_COMMENT },
'f|formatted-code' => sub{ $code_formatting = 1 },
'w|word=s' => sub{ $words{ lc $_[1] } = $_[1] },
'h|help' => sub{ Syntax 0 },
'V|version' => sub{ Version },
);
sub add_paste_file ($$$) {
my ($op, $section, $filename, $add_section_title) = @_;
die "file not readable: $filename" unless (-f $filename && -r $filename);
my $addto = ($op eq 'after')
? \%paste_after_section
: \%paste_before_section;
push @{ $addto->{$section} }, {file => $filename, add_section_title => $add_section_title};
}
# Install postprocessing function for all output:
sub {
my $pid = open(STDOUT, '|-');
return if $pid > 0;
die "cannot fork: $!" unless defined $pid;
# process entire output at once:
local $/;
local $_ = <STDIN>;
utf8::decode($_);
# merge code blocks:
s#(?:\n```\n```\n|</code></pre>\n<pre><code>|</code>\n<code>\n?)# #g;
s#(?:</code><code>|</pre><pre>)##g;
s#(?:\n</synopsis>\n<synopsis>\n|\n</synopsisFormatted>\n<synopsisFormatted>\n)# #g;
# ensure correct synposis format:
s#(<(synopsis(?:Formatted)?)>.*</\2>)#postprocess_synopsis($1)#se;
# URLs:
s/(\[[^\]]+) (?=\]\((?:$re_urlprefix|mailto:))/$1/g; # remove trailing spaces in link titles
s/^(.+)(?<!>)(?<!>)(?:$)\n^(?:[\[\(]\*{0,2}($re_url)\*{0,2}[\]\)])($re_punctuation*)$/[$1]($2)$3/gm;
# Line breaks;
s/\n *${replacement_token}#BRK#/ \n/g;
# Internal links:
s=${replacement_token}#INTERNAL-LINK#\n? *(?:((?!<|<)[^\n]+)\n)? *(?:<|<)([^\n]+?)(?:>|>)($re_punctuation*)$=
'[' . ($1 // $2) . '](' . $2 . ')' . $3
=gme;
s=${replacement_token}#LINK-TO#([^#]+)#\n? *(<|<|“|‘|")?([^\n]+?)((?:>|>|”|’|")?$re_punctuation*)?$=
($2 // '') . '[' . $3 . '](#' . section_slug($1) . ')' . ($4 // '')
=gme;
# 1 target
# 2 prefix
# 3 link text
# 4 suffix
# Clean up remaining markers:
s/${replacement_token}#[\w\-]+#\n?//g;
# There should never be a linebreak after a NBSP, it defeats the entire purpose.
s/(?<= )\n//g;
utf8::encode($_);
print;
exit;
}->();
# nextline(keep_blanklines=false)
# Fetch next input line into $_.
# Returns true if there was an input line, false if EOF.
# If the first argument (keep_blanklines) is true,
# blank lines will be returned;
# by default, blank lines will be skipped.
# This function also removes all line comments (\")
# and block comments (.ig).
sub nextline {
my $keep_blanklines = $_[0] // 0;
my $in_comment;
do {{
$_ = <>;
return 0 unless defined;
# options for following line(s):
$line_did_set_options = 0;
if (s/^\.?\s*\\"\s*(PLAIN)\s*$//) {
$line_did_set_options = 1;
add_lineopt($1);
}
# special markers in comments:
s/^\.?\s*\\"\s*INTERNAL-LINK.*$/${replacement_token}#INTERNAL-LINK#/s or
s/^\.?\s*\\"\s*LINK-TO\s+([^\s#][^#\r\n]*)\s*$/${replacement_token}#LINK-TO#$1#/s or
s/^\.\\".*$// # remove line comment commands
or
s/^((?:\\[^"]|[^\\])++)\\".*$/$1/; # remove line comments
if (m/^\.ig/ || $in_comment) {
# block comment
$in_comment = ! m/^\.\./;
redo;
}
# Keep reading if line ends with “\<NEWLINE>”, it's not truly finished yet:
while (!eof && s/((?:\\{2})*)\\[\r\n]+$/$1/) {
$_ .= <>;
}
}} while (line_empty() && !$keep_blanklines);
utf8::decode($_);
1
}
sub line_empty { m/^\s*$/ }
sub has_lineopt ($) { defined($lineopt) && $lineopt =~ m/\b$_[0]\b/ }
sub add_lineopt ($) { $lineopt .= " $_[0] " }
sub clr_lineopt () { undef $lineopt }
sub strip_highlighting {
# remove remaining highlighting:
s/(?:^\.[BIR] |\\f[BIRP1234])//g unless $_[0];
# get rid of .BR formatting, but correctly unquote its arguments:
s/^\.[BIR]{2} *(.+)/alternating_highlighting('R', 'R', $1)/ge unless $_[0];
# paragraphs:
if (m/^\.br/i) {
$_ = "${replacement_token}#BRK#";
return
} elsif (m/^\.(LP|P|PP|sp)\b/) {
$_ = "\n"; # one blank line
$in_list = 0;
}
# known special characters:
s/\\\(lq/“/g;
s/\\\(rq/”/g;
s/\\\(oq/‘/g;
s/\\\(cq/’/g;
s/\\\(ga/`/g;
s/\\\(aq/'/g;
s/\\\(dq/"/g;
s/\\\(fm/′/g;
s/\\\(sd/″/g;
s/\\\(Fo/«/g;
s/\\\(Fc/»/g;
s/\\\(fo/‹/g;
s/\\\(fc/›/g;
s/\\\(hy/-/g;
s/\\\(en/–/g;
s/\\\(em/—/g;
s/\\\(ha/^/g;
s/\\\(lh/☜/g;
s/\\\(rh/☞/g;
s/\\\(at/@/g;
s/\\\(bu/·/g;
s/\\\(ci/○/g;
s/\\\(CR/↵/g;
s/\\\(de/°/g;
s/\\\(dg/†/g;
s/\\\(dd/‡/g;
s/\\\(lz/◊/g;
s/\\\(mc/µ/g;
s/\\\(OK/✓/g;
s/\\\(ps/¶/g;
s/\\\(ru/_/g;
s/\\\(sc/§/g;
s/\\\(sh/#/g;
s/\\\(sq/□/g;
s/\\\(ti/~/g;
s/\\\(ct/¢/g;
s/\\\(Do/\$/;
s/\\\([Ee]u/€/g;
s/\\\(Ye/¥/g;
s/\\\(Po/£/g;
s/\\\(Cs/¤/g;
s/\\\(co/©/g;
s/\\\(rg/®/g;
s/\\\(tm/™/g;
s/\\\(ff/ff/g;
s/\\\(fi/fi/g;
s/\\\(Fi/ffi/g;
s/\\\(fl/fl/g;
s/\\\(Fl/ffl/g;
s/\\\(12/½/g;
s/\\\(14/¼/g;
s/\\\(34/¾/g;
s/\\\(38/⅜/g;
s/\\\(58/⅝/g;
s/\\\(78/⅞/g;
s/\\\(S1/¹/g;
s/\\\(S2/²/g;
s/\\\(S3/³/g;
# unicode characters:
s/\\\[u0*?([0-9a-fA-F]+)\]/ chr hex $1 /ge;
s/\\[ ~]/ /g; # non-breakable space
# s/\\-/‑/g; # non-breakable hyphen
s/\\%//g; # hyphenation command
# other special characters, except "\\":
s/`/\\`/g;
s/\\ / /g;
s/\\-/-/g if $plain_dashes;
# s/\\(.)/$1/g;
# non-printing zero-width characters, used to mask strings that are not commands:
s/\\[&\)]//g;
s/\\://g; # ZWSP
# other unprintables and control characters:
s/\\[\/,]//g;
# unknown \*X or \*(XX string usages not previously defined with .ds:
s/^(?:\\[^\*]|[^\\])*?\K\\\*[^\s\(]//g;
s/^(?:\\[^\*]|[^\\])*?\K\\\*\([^\s]{2}//g;
# These regexes look a bit weird.
# They prevent removal of non-string-sequence input like ...**\\\\**...
# but excluding there patterns with a negative look-behind
# won't work because it's not a fixed-length match.
# TODO: Apply similar exclusions to all other backslash-escaped replacements in this sub?
utf8::encode($_);
}
sub strip_html {
# avoid accidental html output:
my @result = map{
s/</</g;
s/>/>/g;
$_ }
($#_ >= 0 ? @_ : ($_));
wantarray ? @result : $result[0]
}
sub section_title {
# If the current line contains a section title,
# this function sets $section, $prev_section, and the $is_... flags accordingly
# and returns true.
return 0 unless m/^\.SH +(.+)$/m;
$in_list = 0;
$prev_section = $section // '';
$section = qtok($1);
undef $subsection;
$is_synopsis = ($section eq 'SYNTAX' || $section eq 'SYNOPSIS');
1
}
sub subsection_title {
return 0 unless m/^\.SS +(.+)$/m;
$in_list = 0;
$subsection = qtok($1);
1
}
sub postprocess_synopsis {
local $_ = $_[0];
# Turn fake block tags into correct markup:
s#<synopsis>(.*)</synopsis>#```$1```#s ||
s#^<synopsisFormatted>\n(.*)\n</synopsisFormatted>#<pre><code>$1</code></pre>#s;
# Synopsis blocks are processed line-by-line, then merged by the global output postprocessing function.
# This may cause spaces to be inserted at unexpected places. Remove them:
s/ *${replacement_token}#BRK# */\n/gs;
$_
}
sub reformat_syntax {
# commands to be ignored:
if (m/^\.(?:PD|hy|ad|ft|fi|\s|$)/) {
$_ = '';
return
}
# replace .ds strings:
for my $sname (keys %strings) {
if (length $sname == 1) { s/\\\*$sname/$strings{$sname}/g; }
elsif (length $sname == 2) { s/\\\*\($sname/$strings{$sname}/g; }
}
# raw block markers:
if (m/^\.(?:nf|co|cm)/) {
if (has_lineopt('PLAIN')) {
$in_preblock = 2;
} else {
$in_rawblock = 2;
}
if (m/^\.cm(?:\s+($re_token))?/) {
chomp;
$_ = qtok($1);
strip_highlighting();
$_ = "\n**\`$_\`**\n\n"
} elsif (m/^\.co/) {
$_ = "\n"
} else {
$_ = ''
}
return
}
# command invocation in Synopsis section:
if ($is_synopsis && !line_empty()) {
# only code here
chomp;
if ($code_formatting) {
# synopsis content with formatting
$_ = strip_html($_);
reformat_html();
strip_highlighting();
s/\\(.)/$1/g; # in md <pre> blocks, backslashes are not special!
$_ = "<synopsisFormatted>\n$_\n</synopsisFormatted>\n"
} else {
strip_highlighting();
$_ = "<synopsis>\n$_\n</synopsis>\n";
}
return
}
# Usually we can get away with unescaped underscores.
# But they'll lead to problems inside words that use \fI font changes.
# So escape just these occurrences:
s/(?=\S*\\f[IRP12]\S*)_/\\_/g;
# bold and italics:
# (The special cases <b>*</b> and <i>*</i> are handled after the strip_html() call.)
s/(?:\\f[B3])+([^\*_]|.{2,}?)(?:\\f[RP1])+/**$1**/g;
s/(?:\\f[I2])+([^\*_]|.{2,}?)(?:\\f[RP1])+/_$1_/g;
s/(?:\\f4)+([^\*_]|.{2,}?)(?:\\f[RP1])+/**_$1_**/g;
# groff concatenates tokens in .B and .I lines with spaces.
# We still have to tokenize and re-join the line
# to get rid of the token doublequote enclosures.
s/^\.B +([^\*].*)/'**' . join(' ', tokenize($1)) . '**'/ge;
s/^\.I +([^\*].*)/'_' . join(' ', tokenize($1)) . '_'/ge;
s/^\.([BIR])([BIR]) *(.+)/alternating_highlighting($1, $2, $3)/ge;
# other formatting:
strip_highlighting(1);
# escape html tags:
$_ = strip_html($_);
# process highlighting special cases:
s#(?:\\f[B3])+(\*|_)(?:\\f[RP1])+#<b>\\$1</b>#g;
s#(?:\\f[I2])+(\*|_)(?:\\f[RP1])+#<i>\\$1</i>#g;
s#(?:\\f4)+(\*|_)(?:\\f[RP1])+#<b><i>\\$1</i></b>#g;
s#^\.B +(\*.*)#'<b>' . join(' ', tokenize($1)) . '</b>'#ge;
s#^\.I +(\*.*)#'<i>' . join(' ', tokenize($1)) . '</i>'#ge;
# remove remaining highlighting:
s/(?:^\.[BIR]{1,2} |\\f[BIRP1234])//g;
if ($section eq 'AUTHOR' || $section eq 'AUTHORS') {
# convert e-mail address to link:
s/\b($re_email)\b/[$1](mailto:$1)/u;
}
# item lists and description lists:
if (m/^\.IP(?: +($re_token))?/ || m/^\.TP/) {
my $tok = defined($1) ? qtok($1) : undef;
my $is_bullet = (!defined($tok) || $tok eq '' || $tok eq '-' || $tok eq 'o');
$is_desclist = !$is_bullet || (m/^\.TP/ && ($section ne 'EXIT CODES' && $section ne 'EXIT STATUS'));
my $indent = ($in_list > 1)
? ' ' x ($in_list - 1)
: '';
$_ = $indent . '* '; # no trailing break here
if ($is_bullet) {
$start_list_item = 1;
} else {
$_ .= $tok . " \n";
}
if (!$in_list) {
$_ = "\n$_";
$in_list = 1;
}
} elsif ($in_list && m/^\.RS/) {
$in_list++;
$_ = ''
} elsif ($in_list && m/^\.RE/) {
$in_list--;
$_ = ''
} elsif (m/^\.(?:RS|RE)/) {
# ignore
$_ = ''
} elsif ($in_list) {
if ($start_list_item) {
$start_list_item = 0;
# In description list (probably some CLI options).
# Add extra line break after option name:
s/$/ / if $is_desclist;
} else {
my $indent = ' ' x (2 + (4 * ($in_list - 1)));
s/^/$indent/;
}
} elsif (m/\.UR ($re_url)\s*$/) {
$in_urltitle = $1;
$_ = '['
} elsif (m/\.MT ($re_email)\s*$/) {
$in_mailtitle = $1;
$_ = '['
} elsif (defined $in_urltitle && m/\.UE(?: (\S*)\s*)?$/) {
$_ = "]($in_urltitle)" . ($1 // '') . "\n";
undef $in_urltitle
} elsif (defined $in_mailtitle && m/\.ME(?: (\S*)\s*)?$/) {
$_ = "](mailto:$in_mailtitle)" . ($1 // '') . "\n";
undef $in_mailtitle
} elsif (defined $in_urltitle || defined $in_mailtitle) {
s/[\r\n]+/ /g
}
s/$/ / if has_lineopt('BRK');
clr_lineopt() unless $line_did_set_options;
}
sub reformat_html {
s#\\f[B3](.+?)\\f[RP1]#<b>$1</b>#g;
s#\\f[I2](.+?)\\f[RP1]#<i>$1</i>#g;
s#\\f4(.+?)\\f[RP1]#<b><i>$1</i></b>#g;
s#^\.B +(.+)#<b>$1</b>#g;
s#^\.I +(.+)#<i>$1</i>#g;
s/^\.([BIR])([BIR]) *(.+)/alternating_highlighting($1, $2, $3, 1)/ge;
}
# Strips doublequote enclosure from string tokens, if present.
sub qtok {
my @result = map{ defined && m/^"(.+)"$/ ? $1 : $_ } @_;
wantarray ? @result : $result[0]
}
# Extracts all tokens from the input string and returns them in a list.
# Tokens are things enclosed in unescaped doublequotes or any strings without spaces.
sub tokenize { qtok($_[0] =~ m/$re_token/g) }
sub section_slug ($) {
local $_ = lc shift;
s/[^\w\d\-_ ]//g;
s/[ \-]+/-/g;
$_
}
sub section_anchor ($) { "<a name=\"" . section_slug($_[0]) . "\"></a>" }
sub print_section_title ($) {
my $title = strip_html($_[0]);
my $output = sprintf "\n%s\n\n%s%s\n\n", section_anchor($title), $section_prefix, $title;
utf8::encode($output);
print $output
}
sub print_subsection_title ($) {
my $title = strip_html($_[0]);
my $output = sprintf "\n%s\n\n%s%s\n\n", section_anchor($title), $subsection_prefix, $title;
utf8::encode($output);
print $output
}
sub paste_file (%) {
my %args = @_;
return 0 unless -r $args{'file'};
if ($args{'add_section_title'} && $args{'file'} =~ m/^(?:[a-zA-Z0-9_\-]+\/)*(.+)\.md$/) {
my $section_title = $1;
print_section_title $section_title;
}
open FH, "< $args{'file'}";
local $/;
my $content = <FH>;
close FH;
# $content =~ s/\s+$//;
print "$content\n";
1
}
sub alternating_highlighting {
my @hl = @_[0, 1];
my @tokens = tokenize($_[2]);
my $do_html = $_[3] // 0;
my $h = 0;
# groff concatenates tokens in .B and .I lines with spaces,
# but tokens in .[BIR][BIR] lines are concatenated WITHOUT spaces.
# Therefore we have to join('') the tokens here:
return join '', map {
my $highlightkey = $hl[$h++ % 2];
if ($highlightkey eq 'R') {
$_
} elsif ($highlightkey eq 'I') {
($do_html)
? '<i>' . $_ . '</i>'
: '_' . $_ . '_'
} elsif ($highlightkey eq 'B') {
($do_html)
? '<b>' . $_ . '</b>'
: '**' . $_ . '**'
}
} @tokens
}
sub titlecase {
local $_ = $_[0];
my $re_word = '(\pL[\pL\d\'_]*)';
# lowercase stop words, keep casing of known words, else titlecase
s!$re_word!$stopwords{lc $1} ? lc($1) : ($words{lc $1} // ucfirst(lc($1)))!ge;
# capitalize first word following colon or semicolon
s/ ( [:;] \s+ ) $re_word /$1\u$2/x;
# titlecase first word (even a stopword), except if it's a known word
s!^\s*$re_word!$words{lc $1} // ucfirst(lc($1))!e;
$_
}
sub read_version {
if ($_[0] eq '') {
# no version string found
$version = '';
return 1
}
if ($_[0] =~ m/^(?:$progname(?: \(\d\))?\s+)(?:v|ver\.?|version)? ?(\d[\w\.\-\+]*)$/i) {
# found explicit version following known progname
$is_bare_version = 1;
$version = $1;
return 1
}
# found something else
$version = $_[0];
return 1
}
##############################
# eat first line, extract progname, version, and man section
nextline() or die "could not read first line";
m/^.(?:Dd|Dt)\b/ and die "man page is in mdoc format which is not supported";
m/^\.TH\b/ or die "first line does not contain '.TH' macro";
m/^\.TH ($re_token)(?:\s|$)/ or die ".TH line doesn't contain page title";
m/^\.TH ($re_token) ($re_token)(?: ($re_token)(?: ($re_token))?)?/ or die ".TH line doesn't contain man section";
($progname, $mansection, $verdate) = (lc(qtok($1)), qtok($2), qtok($3));
read_version(qtok($4 // ''));
# skip NAME headline, extract description
if (nextline() && section_title() && $section eq 'NAME') {
if (nextline() && m/ \\?(?:-|\\\(em|\\\(en) +(.+)$/) {
$description = $1;
nextline();
}
}
print "[//]: # ($add_comment)\n\n" if defined $add_comment;
printf "%s%s(%s)", $headline_prefix, strip_html($progname), $mansection;
printf " - %s", strip_html($description) if defined $description;
print "\n\n";
# Fake section name 'HEADLINE' can be used
# to paste additional content right after the headline
# (but not before)
if (defined $paste_after_section{'HEADLINE'}) {
paste_file(%$_) foreach (@{ $paste_after_section{'HEADLINE'} });
undef $paste_after_section{'HEADLINE'};
}
if ($version || $verdate) {
if ($version) {
print "Version " if $is_bare_version;
print $version;
}
if ($version && $verdate) {
print ", ";
}
if ($verdate) {
print $verdate;
}
print "\n\n";
}
# skip SYNOPSIS headline
nextline() if (section_title && $is_synopsis);
do {{
PARSELINE:
if ($in_rawblock) {
if (m/^\.(?:fi|SH|cx)/) {
# code block ends
$in_rawblock = 0;
print "</code></pre>\n" if $code_formatting;
print "\n" if m/^\.cx/;
redo if m/^\.SH/; # .nf sections can be ended with .SH, but we still need to print the new section title too
} elsif ($code_formatting) {
# inside code block with limited html formatting
if ($in_rawblock == 2) {
$in_rawblock = 1;
print "<pre><code>";
}
$_ = strip_html($_);
reformat_html;
strip_highlighting;
s/\\(.)/$1/g; # in md <pre> blocks, backslashes are not special!
print
} else {
# inside code block without formatting
strip_highlighting;
s/\\(.)/$1/g; # in md raw blocks, backslashes are not special!
print " $_"
}
} elsif ($in_preblock) {
if (m/^\.fi/) {
# preformatted block ends
$in_preblock = 0;
$_ = '';
} else {
# Add two spaces at EOL to force visible linebreak:
add_lineopt('BRK');
}
reformat_syntax;
print
} elsif (section_title) {
# new section begins
if (defined $paste_after_section{$prev_section}) {
paste_file(%$_) foreach (@{ $paste_after_section{$prev_section} });
undef $paste_after_section{$prev_section};
}
if (defined $paste_before_section{$section}) {
paste_file(%$_) foreach (@{ $paste_before_section{$section} });
undef $paste_before_section{$section};
}
print_section_title titlecase($section)
} elsif (subsection_title) {
# new subsection begins
print_subsection_title $subsection
} elsif (m/^\.ds +(\S{1,2}) +"?(.+)$/) {
$strings{ $1 } = $2
} elsif (m/^\.de\b/) {
# macro definition -- skip completely
1 while (nextline(1) && ! m/^\.\./);
} else {
reformat_syntax;
print
}
}} while (nextline(1));
# Paste section which haven't matched anything yet:
# TODO: print warnings -- they probably should have gone somewhere else
foreach (values %paste_before_section)
{ paste_file(%$_) foreach (@$_) }
foreach (values %paste_after_section)
{ paste_file(%$_) foreach (@$_) }