forked from openresty/openresty-devel-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wiki2pod.pl
executable file
·140 lines (122 loc) · 3.33 KB
/
wiki2pod.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
#!/usr/bin/env perl
use strict;
use warnings;
use bytes;
my @nl_counts;
my $last_nl_count_level;
my @bl_counts;
my $last_bl_count_level = -1;
sub fmt_pos ($) {
(my $s = $_[0]) =~ s{\#(.*)}{/"$1"};
$s;
}
sub fmt_mark ($$) {
my ($tag, $s) = @_;
my $max_level = 0;
while ($s =~ /([<>])\1*/g) {
my $level = length $&;
if ($level > $max_level) {
$max_level = $level;
}
}
my $times = $max_level + 1;
if ($times > 1) {
$s = " $s ";
}
return $tag . ('<' x $times) . $s . ('>' x $times);
}
print "=encoding utf-8\n\n";
while (<>) {
if ($. == 1) {
# strip the leading U+FEFF byte in MS-DOS text files
my $first = ord(substr($_, 0, 1));
#printf STDERR "0x%x", $first;
#my $second = ord(substr($_, 2, 1));
#printf STDERR "0x%x", $second;
if ($first == 0xEF) {
substr($_, 0, 1, '');
#warn "Hit!";
}
}
s{\[(http[^ \]]+) ([^\]]*)\]}{$2 (L<$1>)}gi;
s{ \[\[ ( [^\]\|]+ ) \| ([^\]]*) \]\] }{"L<$2|" . fmt_pos($1) . ">"}gixe;
s{<code>(.*?)</code>}{fmt_mark('C', $1)}gie;
s{'''(.*?)'''}{fmt_mark('B', $1)}ge;
s{''(.*?)''}{fmt_mark('I', $1)}ge;
if (s{^\s*<[^>]+>\s*$}{}) {
next;
}
if (/^\s*$/) {
print "\n";
next;
}
=begin cmt
if ($. == 1) {
warn $_;
for my $i (0..length($_) - 1) {
my $chr = substr($_, $i, 1);
warn "chr ord($i): ".ord($chr)." \"$chr\"\n";
}
}
=end cmt
=cut
if (/(=+) (.*) \1$/) {
#warn "HERE! $_" if $. == 1;
my ($level, $title) = (length $1, $2);
collapse_lists();
print "\n=head$level $title\n\n";
} elsif (/^(\#+) (.*)/) {
my ($level, $txt) = (length($1) - 1, $2);
if (defined $last_nl_count_level && $level != $last_nl_count_level) {
print "\n=back\n\n";
}
$last_nl_count_level = $level;
$nl_counts[$level] ||= 0;
if ($nl_counts[$level] == 0) {
print "\n\n=over\n\n";
}
$nl_counts[$level]++;
print "\n=item $nl_counts[$level].\n\n";
print "$txt\n";
} elsif (/^(\*+) (.*)/) {
my ($level, $txt) = (length($1) - 1, $2);
if ($level != $last_bl_count_level) {
if ($level < $last_bl_count_level) {
my $i = $level;
while ($i < $last_bl_count_level) {
#warn "!!! $_: $level < $last_bl_count_level\n";
print "\n\n=back\n\n";
$i++;
}
} else {
print "\n\n=over\n\n";
}
}
$last_bl_count_level = $level;
print "\n=item *\n\n";
print "$txt\n";
} elsif (/^: (.*)/) {
my $txt = $1;
$txt =~ s/\[/[/g;
print $txt;
} else {
collapse_lists();
s/\[/[/g;
print;
}
}
collapse_lists();
sub collapse_lists {
while (defined $last_nl_count_level && $last_nl_count_level >= 0) {
print "\n\n=back\n\n";
$last_nl_count_level--;
}
undef $last_nl_count_level;
undef @nl_counts;
while (defined $last_bl_count_level && $last_bl_count_level >= 0) {
print "\n\n=back\n\n";
$last_bl_count_level--;
}
$last_bl_count_level = -1;
undef @bl_counts;
}