-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmd2txt.pl
377 lines (321 loc) · 8.18 KB
/
md2txt.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
# This file is part of pound.
# Copyright (C) 2018-2025 Sergey Poznyakoff
#
# Pound 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.
#
# Pound 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 pound. If not, see <http://www.gnu.org/licenses/>.
# A simple markdown to text converter. Use "perl md2txt.pl --help",
# or "perldoc md2txt.pl", for a detailed info.
use strict;
use warnings;
use Text::Wrap;
use Getopt::Long qw(:config gnu_getopt no_ignore_case);
my $input_file;
my $output_file;
my $numeric_refs;
my $left_margin = 0;
my $page_width = $Text::Wrap::columns;
my $indent_tab;
my $example_indent = 4;
use constant {
ENV_PARA => 0,
ENV_EXAMPLE => 1,
ENV_ENUM => 2,
ENV_ITEMIZE => 3,
ENV_SECTION => 4,
ENV_REFS => 5
};
GetOptions('numeric-refs|N' => \$numeric_refs,
'left-margin|l=n' => \$left_margin,
'page-width|w=n' => \$page_width,
'output|o=s' => \$output_file)
or exit(1);
$indent_tab = ' ' x $left_margin;
$input_file = shift @ARGV or die "required parameter missing\n";
open(STDIN, '<', $input_file) or die "can't open $input_file: $!\n";
if ($output_file) {
open(STDOUT, '>', $output_file) or die "can't open output file $output_file: $!\n";
}
convert();
exit 0;
#
# Inline markup
#
sub expand_inline {
$_ = join(' ', @_);
s{\*([^*]+)\*}{$1}g;
s{__(.+?)__}{$1}g;
s{\b{wb}_(.+?)_\b{wb}}{$1}g;
s{\`([^`]+)\`}{$1}g;
s{\[([^]]+)\]\((https?://[^)]+)\)}{external_ref($1, $2)}gex;
s{\[(.*?)\]\(#user-content-.*?\)}{$1}g;
return $_;
}
my %refidx;
my $refs;
my @epilogue;
sub external_ref {
my ($name, $url) = @_;
my $refname;
unless (defined($refs)) {
push @epilogue, { env => ENV_SECTION, level => 1, content => ["References"] };
$refs = { env => ENV_REFS, content => [] };
push @epilogue, $refs;
}
if (exists($refidx{$url})) {
$refname = $refs->{content}[$refidx{$url}]->{name}
} else {
$refname = @{$refs->{content}}; # FIXME: take into account $numeric_refs
$refidx{$url} = @{$refs->{content}};
push @{$refs->{content}}, { name => $refname, url => $url }
}
return "$name\[$refname\]";
}
sub format_refs {
my $para = shift;
foreach my $elem (@{$para->{content}}) {
print "$indent_tab\[$elem->{name}\] $elem->{url}\n";
}
print "\n";
}
#
# Converter
#
my %envfun;
BEGIN {
%envfun = (
ENV_SECTION() => {
collect => \&collect_section,
format => \&format_section
},
ENV_PARA() => {
collect => \&collect_para,
format => \&format_para,
},
ENV_ENUM() => {
collect => \&collect_enum,
format => \&format_enum
},
ENV_ITEMIZE() => {
collect => \&collect_itemize,
format => \&format_itemize
},
ENV_EXAMPLE() => {
collect => \&collect_example,
format => \&format_example
},
ENV_REFS() => {
collect => sub { die "Should not happen: collect_refs called!" },
format => \&format_refs
}
);
}
use Data::Dumper;
sub convert {
local $Text::Wrap::columns = $page_width - $left_margin;
local $Text::Wrap::huge = 'overflow';
my $para;
while ($para = collect($para)) {
&{$envfun{$para->{env}}{format}}($para);
}
}
# Collect a single paragraph of text.
sub collect {
my $prev = shift;
my $res = { env => ENV_PARA, content => [] };
if (defined($prev) && exists($prev->{pushback})) {
$_ = $prev->{pushback};
} else {
while (<>) {
chomp;
last unless /^$/;
}
unless (defined($_)) {
return shift @epilogue;
}
}
if (m{^#+\s+\S}) {
$res->{env} = ENV_SECTION;
} elsif (m{^[0-9]+[.)]}) {
$res->{env} = ENV_ENUM;
} elsif (m{^\*\s+}) {
$res->{env} = ENV_ITEMIZE;
} elsif (m{^\s*```}) {
$res->{env} = ENV_EXAMPLE;
}
return &{$envfun{$res->{env}}{collect}}($res, $_);
}
sub collect_section {
my ($res, $init) = @_;
$init =~ m{^(#+)\s+(.*)};
$res->{content}[0] = $2;
$res->{level} = length($1);
return $res;
}
sub collect_para {
my ($res, $init) = @_;
$init =~ s/^\s+//;
push @{$res->{content}}, $init;
while (<>) {
chomp;
return $res if $_ eq '';
s/^\s+//;
push @{$res->{content}}, $_;
}
return $res if @{$res->{content}};
}
sub collect_example {
my ($res, $init) = @_;
while (<>) {
chomp;
return $res if m{^\s*```};
push @{$res->{content}}, $_;
}
}
sub collect_itemized_env {
my ($rx, $res, $text) = @_;
my $lookahead;
$text =~ s{$rx}{};
while (<>) {
chomp;
if ($lookahead) {
if (m/^$/) {
$lookahead .= "\n";
} elsif (s{$rx}{}) {
push @{$res->{content}}, $text . $lookahead;
$text = $_;
$lookahead = undef
} else {
push @{$res->{content}}, $text;
#FIXME: pushback
$res->{pushback} = $_;
return $res;
}
} elsif (s{$rx}{}) {
push @{$res->{content}}, $text;
$text = $_;
} elsif (m/^$/) {
$lookahead = "\n";
} else {
$text .= ' ' . $_
}
}
push @{$res->{content}}, $text unless $text eq '';
return $res if @{$res->{content}};
}
sub collect_enum {
my ($res, $text) = @_;
collect_itemized_env(qr{^[0-9]+[.)]\s*}, $res, $text)
}
sub collect_itemize {
my ($res, $text) = @_;
collect_itemized_env(qr{^\*\s+}, $res, $text)
}
sub format_section {
my $res = shift;
my $h = expand_inline($res->{content}[0]);
my $len = length($h);
if ($len < $Text::Wrap::columns) {
print ' ' x (($Text::Wrap::columns - $len) / 2);
}
print "$h\n";
if ($len < $Text::Wrap::columns) {
print ' ' x (($Text::Wrap::columns - $len) / 2);
}
my $delim;
if ($res->{level} == 1) {
$delim = '=';
} elsif ($res->{level} == 2) {
$delim = '-';
} else {
$delim = '.';
}
print $delim x $len;
print "\n\n";
}
sub format_para {
my $para = shift;
my $text = fill($indent_tab, $indent_tab, expand_inline(@{$para->{content}}));
print $text."\n\n";
}
sub format_example {
my $para = shift;
my $indent = $indent_tab . (' ' x $example_indent);
print $indent, join("\n$indent", @{$para->{content}}), "\n\n";
}
sub format_enum {
my $para = shift;
# print Dumper([$para]);
my $n = @{$para->{content}};
my $indent_len = length("$n. ");
my $indent_pfx = $indent_tab . (' ' x $indent_len);
my $i = 1;
foreach my $elem (@{$para->{content}}) {
print "$indent_tab$i. ";
$i++;
print fill(' ' x (length("$n. ") - $indent_len), $indent_pfx,
expand_inline($elem));
if ($elem =~ m{(\n+)$}sm) {
print $1
} else {
print "\n";
}
}
print "\n";
}
sub format_itemize {
my $para = shift;
foreach my $elem (@{$para->{content}}) {
print fill("$indent_tab* ", "$indent_tab ", expand_inline($elem));
if ($elem =~ m{(\n+)$}sm) {
print $1
} else {
print "\n";
}
}
print "\n";
}
__END__
=head1 NAME
md2txt.pl - a simple markdown to text converter
=head1 SYNOPSIS
B<perl md2txt.pl>
[B<-N>]
[B<-l> I<COLUMN>]
[B<-o> I<FILE>]
[B<-w> I<N>]
[B<--left-margin=>I<COLUMN>]
[B<--numeric-refs>]
[B<--output=>I<FILE>]
[B<--page-width=>I<N>]
I<FILE>
B<perl md2txt.pl> B<-?> | B<--help>
=head1 DESCRIPTION
B<md2txt.pl> converts I<FILE>, written in Markdown format, to a plain text.
By default, the resulting text is written on standard output.
=head1 OPTIONS
=over 4
=item B<-N>, B<--numeric-refs>
Reserved for future use.
=item B<-l>, B<--left-margin=>I<COLUMN>
Sets left margin for the output. Default is 1.
=item B<-o>, B<--output=>I<FILE>
Write output to I<FILE> instead of the standard output.
=item B<-w>, B<--page-width=>I<N>
Sets output page width. Default is 72 columns.
=back
=head1 BUGS
The program is rather ad-hoc. It does not attempt to cover all possible
subtleties of the markdown format. It works neatly for the existing
README.md of the pound project, however. If you use any markup other than
simple headers, references, enumerations and itemized lists, you'd
probably have to modify it in order to handle your changes properly.