ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cf.schmorp.de/Deliantra-Client/bin/pod2wiki
Revision: 1.20
Committed: Mon Jun 21 22:51:58 2010 UTC (14 years, 1 month ago) by root
Branch: MAIN
CVS Tags: HEAD
Changes since 1.19: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #! perl
2
3 # convert given .pod files to wiki style
4
5 # base path of arch tree, only used for new arch graphics
6 my $ARCH = "/root/src/cf.schmorp.de/arch";
7
8 use strict;
9
10 use Storable;
11 use Pod::POM;
12
13 our @result;
14 our $indent;
15 our $level;
16
17 my $MA_BEG = "\x{fcd0}";
18 my $MA_SEP = "\x{fcd1}";
19 my $MA_END = "\x{fcd2}";
20
21 sub asxml($) {
22 local $_ = $_[0];
23
24 s/&/&/g;
25 s/>/>/g;
26 s/</&lt;/g;
27
28 $_
29 }
30
31 sub flatten($) {
32 local $_ = $_[0];
33
34 s/<[^>]+>//g;
35 s/^\s+//;
36 s/\s+$//;
37 s/\s+/ /g;
38
39 $_
40 }
41
42 sub special {
43 $MA_BEG . (join $MA_SEP, @_) . $MA_END
44 }
45
46 package AsParagraphs;
47
48 use strict;
49
50 use base "Pod::POM::View";
51
52 # nodes (order must stay as it is)
53 sub N_PARENT (){ 0 }
54 sub N_PAR (){ 1 }
55 sub N_LEVEL (){ 2 }
56 sub N_KW (){ 3 }
57 sub N_DOC (){ 4 }
58
59 # paragraphs (order must stay as it is)
60 sub P_INDENT (){ 0 }
61 sub P_LEVEL (){ 1 }
62 sub P_MARKUP (){ 2 }
63 sub P_INDEX (){ 3 }
64
65 *view_seq_file =
66 *view_seq_code =
67 *view_seq_bold = sub { "<b>$_[1]</b>" };
68 *view_seq_italic = sub { "<i>$_[1]</i>" };
69 *view_seq_zero = sub { };
70 *view_seq_space = sub { my $text = $_[1]; $text =~ s/ /&#160;/g; $text };
71 *view_seq_index = sub { push @{ $result[-1][P_INDEX] }, $_[1]; "" };
72
73 sub view_seq_text {
74 my $text = $_[1];
75 $text =~ s/\s+/ /g;
76 ::asxml $text
77 }
78
79 sub view_seq_link {
80 my (undef, $link) = @_;
81
82 $link =~ s/^(.*)\|//
83 or $link =~ /([^\/]*)$/;
84
85 my $text = $1;
86
87 if ($link =~ /http:/) {
88 "<u>" . (::asxml $link) . "</u>"
89 } elsif ($link =~ /^\$ARCH\/(.+\....)$/) {
90 my $file = $1;
91
92 unless (-f "resources/arch/$file.png") {
93 print "ARCHIMG $file is missing, trying to supply... ";
94 my ($path) = split /\x00/, qx<find \Q$ARCH\E -name \Q$file.64x64.png*\E -print0>;
95 -f $path or die "$file: could not find arch image";
96 print "$path\n";
97 system "rsync -a \Q$path\E resources/arch/\Q$file.png"
98 and die "rsync failed: $?";
99 system "cvs add -kb resources/arch/\Q$file.png"
100 and ((unlink "resources/arch/$file.png"), die "cvs add failed: $?");
101 }
102
103 ::special image => "arch/$file.png", 1;
104 } else {
105 ::special link => $text, $link
106 }
107 }
108
109 sub view_verbatim {
110 push @result, [ $indent * 16, $level, "<tt>" . (::asxml $_[1]) . "</tt>\n" ];
111 ()
112 }
113
114 sub view_textblock {
115 push @result, [ $indent * 16, $level, "$_[1]\n" ];
116 ()
117 }
118
119 sub view_head1 {
120 push @result, [ $indent * 16, $level ];
121 my $title = $_[1]->title->present ($_[0]);
122 $result[-1][P_MARKUP] = ::special h1 => $title if length $title;
123 $title = ::flatten $title;
124 unshift @{ $result[-1][P_INDEX] }, $title
125 if !$result[-1][P_INDEX];
126 local $level = $level + 1;
127 $_[1]->content->present ($_[0]);
128 ()
129 };
130
131 sub view_head2 {
132 push @result, [ $indent * 16, $level ];
133 my $title = $_[1]->title->present ($_[0]);
134 $result[-1][P_MARKUP] = ::special h2 => $title if length $title;
135 $title = ::flatten $title;
136 unshift @{ $result[-1][P_INDEX] }, $title
137 if !$result[-1][P_INDEX];
138 local $level = $level + 1;
139 $_[1]->content->present ($_[0]);
140 ()
141 };
142
143 sub view_head3 {
144 push @result, [ $indent * 16, $level ];
145 my $title = $_[1]->title->present ($_[0]);
146 $result[-1][P_MARKUP] = ::special h3 => $title if length $title;
147 $title = ::flatten $title;
148 unshift @{ $result[-1][P_INDEX] || [] }, $title
149 if !$result[-1][P_INDEX];
150 local $level = $level + 1;
151 $_[1]->content->present ($_[0]);
152 ()
153 };
154
155 sub view_over {
156 local $indent = $indent + $_[1]->indent;
157 push @result, [ $indent, $level ];
158 $_[1]->content->present ($_[0]);
159 ()
160 }
161
162 sub view_item {
163 push @result, [ $indent * 8, $level ];
164 my $title = $_[1]->title->present ($_[0]);
165 $result[-1][P_MARKUP] = "$title\n" if length $title;
166 $title = ::flatten $title;
167 unshift @{ $result[-1][P_INDEX] || [] }, $title
168 if !$result[-1][P_INDEX];
169 local $level = $level + 1;
170 $_[1]->content->present ($_[0]);
171 ()
172 }
173
174 sub view_for {
175 if ($_[1]->format eq "image") {
176 push @result, [
177 $indent * 16,
178 $level,
179 (::special image => "pod/" . $_->text),
180 ];
181 }
182 ()
183 }
184
185 sub view_begin {
186 ()
187 }
188
189 sub view {
190 my ($self, $type, $item) = @_;
191
192 $item->content->present ($self);
193 }
194
195 #############################################################################
196
197 sub as_paragraphs($) {
198 my ($pom) = @_;
199
200 local $indent = 0;
201 local $level = 2;
202 local @result = ( [] );
203
204 $pom->present ("AsParagraphs");
205
206 [grep $_->[P_INDEX] || defined $_->[P_MARKUP], @result]
207 }
208
209 #############################################################################
210
211 $| = 1;
212
213 my %wiki;
214
215 sub add_node($) {
216 my ($node) = @_;
217
218 for (@{ $node->[N_KW] || {} }) {
219 push @{$wiki{$_}}, $node;
220 }
221 }
222
223 my $root;
224 $root->[N_KW] = ["Documents", "pod"];
225 $root->[N_DOC] = [[0, 0, ::special link => "All Documents", "pod/*"]];
226
227 for my $path (@ARGV) {
228 $path =~ /([^\/\\]+)\.pod$/ or die "$path: illegal pathname";
229 my $base = $1;
230 my $pom = Pod::POM->new->parse_text (do {
231 local $/;
232 open my $pod, "<:perlio", $path
233 or die "$path: $!";
234 <$pod>
235 });
236
237 my $para = as_paragraphs $pom;
238
239 my $pod;
240 $pod->[N_PARENT] = $root;
241 $pod->[N_PAR] = 0;
242 $pod->[N_LEVEL] = 1;
243 $pod->[N_KW] = [$base];
244 $pod->[N_DOC] = $para;
245
246 my @parent = ($pod);
247
248 for my $idx (0 .. $#$para) {
249 my $par = $para->[$idx];
250
251 while ($parent[-1][N_LEVEL] >= $par->[P_LEVEL]) {
252 pop @parent;
253 }
254
255 if ($par->[P_INDEX]) {
256 my $node;
257 $node->[N_PARENT] = $parent[-1];
258 $node->[N_PAR] = $idx;
259 $node->[N_LEVEL] = $par->[P_LEVEL];
260 $node->[N_KW] = $par->[P_INDEX];
261 $node->[N_DOC] = $para;
262 push @parent, $node;
263 add_node $node;
264 }
265 }
266
267 add_node $pod;
268 }
269
270 add_node $root;
271
272 Storable::nstore \%wiki, "docwiki.pst";
273