forked from openresty/openresty-devel-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wiki2markdown.pl
executable file
·88 lines (75 loc) · 2.85 KB
/
wiki2markdown.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
#!/usr/bin/env perl
use strict;
use warnings;
my $infile = shift or
die "usage: $0 <infile>\n";
my $name;
if ($infile =~ m{([^/]+)\.wiki$}) {
$name = $1;
if ($name !~ /^[A-Z]/) {
undef $name;
} else {
warn "Using name $name...\n";
}
}
open my $in, $infile or
die "cannot open $infile for reading: $!\n";
my $s = do { local $/; <$in> };
close $in;
#warn sprintf "%02x (%s)", ord(substr($s, 0, 1)), substr($s, 0, 1);
#warn sprintf "%02x (%s)", ord(substr($s, 1, 1)), substr($s, 1, 1);
#warn sprintf "%02x (%s)", ord(substr($s, 2, 1)), substr($s, 2, 1);
$s =~ s/^\x{ef}\x{bb}\x{bf}//s;
$s =~ s/^=\s+(\S[^\n]*?)\s+=$/"$1\n" . ("=" x length $1)/gmse;
$s =~ s/^==\s+(\S[^\n]*?)\s+==$/"$1\n" . ("-" x length $1)/gmse;
$s =~ s/^===\s+(\S[^\n]*?)\s+===$/### $1\n/gms;
$s =~ s/^====\s+(\S[^\n]*?)\s+====$/#### $1\n/gms;
$s =~ s/^=====\s+(\S[^\n]*?)\s+=====$/##### $1\n/gms;
$s =~ s!<geshi[^\n>]*>\n*(.*?)</geshi>!
my $v = $1; if ($v =~ /^ {0,3}\S/m) { $v =~ s/^/ /gm } "\n$v"!gmse;
$s =~ s{^https?://[^\s)(]+}{<$&>}gms;
$s =~ s/^'''([^\n]*?)'''/(my $v = $1) =~ s{<}{\<}g; $v =~ s{>}{\>}g; "**$v**"/egms;
while ($s =~ s/^(\S[^\n]*?)'''([^\n]*?)'''/$1**$2**/gms) {}
$s =~ s/^''([^\n]*?)''/my $v = $1; $v =~ s{<}{\<}g; $v =~ s{>}{\>}g; "*$v*"/egms;
while ($s =~ s/^(\S[^\n]*?)''([^\n]*?)''/my ($p, $v) = ($1, $2); $v =~ s{<}{\<}g; $v =~ s{>}{\>}g; "$p*$v*"/egms) {}
$s =~ s{^<code>([^\n]*?)</code>}{`$1`}gms;
while ($s =~ s{^(\S[^\n]*?)<code>([^\n]*?)</code>}{$1`$2`}gms) {}
$s =~ s! \[\[ (\# [^\|\]\[\n]*) \| ( [^\]\n]+ ) \]\]!
my ($link, $tag) = ($1, $2);
$link =~ s/ /_/g;
$link =~ s/[\$\(\)]/sprintf(".%02x", ord($&))/ge;
if ($name) {
"[$tag](http://wiki.nginx.org/$name$link)"
} else {
"`$tag`"
}
!gmsxe;
$s =~ s{ \[ (https?://[^\|\]\[\s]*) \s+ ( [^\]]+ ) \]}
{[$2]($1)}gmsx;
$s =~ s! \[\[ ([^\|\]\[]*) \| ( [^\]]+ ) \]\]
!
my ($tag, $link) = ($2, $1);
$link =~ s/ /_/g;
$link =~ s/[\$\(\)]/sprintf(".%02x", ord($&))/ge;
#warn "link $link $name";
if ($link =~ /^\#/) {
if ($name) {
"[$tag](http://wiki.nginx.org/$name$link)"
} else {
"`$tag`"
}
} else {
"[$tag](http://wiki.nginx.org/$link)"
}
!gmsxe;
while ($s =~ s{^(\S[^\n]*?)?([^\(\<\n])(https?://[^\s)(\n]+)}{$1$2<$3>}gms) {}
$s =~ s{^\[\[(\w+)\]\]}{\[$1](http://wiki.nginx.org/$1)}gsm;
while ($s =~ s{^(\S[^\n]*?)\[\[(\w+)\]\]}{$1\[$2](http://wiki.nginx.org/$2)}smg) {}
$s =~ s{^(https?://[^\s)(\n]+)}{<$1>}gms;
$s =~ s/^\# (\S)/1. $1/gms;
$s =~ s/^: (\S)/\t$1/gms;
$s =~ s/^\*\* (\S)/\t* $1/gms;
$s =~ s/^\*\*\* (\S)/\t\t* $1/gms;
$s =~ s/\[/[/g;
print "<!---\nDon't edit this file manually! Instead you should generate it by using:\n wiki2markdown.pl /path/to/the/wiki/file\n-->\n\n";
print $s;