ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pod2wiki
Revision: 1.17
Committed: Wed Apr 2 13:27:55 2008 UTC (16 years, 1 month ago) by root
Branch: MAIN
Changes since 1.16: +4 -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/devel/cvs/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 $path = $1;
91 (my $base = $path) =~ s/.*\///;
92 -f "$ARCH/$path" && system "rsync -av -c \Q$ARCH/$path\E \Qresources/arch/$base";
93 ::special image => "arch/$base", 1;
94 } else {
95 ::special link => $text, $link
96 }
97 }
98
99 sub view_verbatim {
100 push @result, [ $indent * 16, $level, "<tt>" . (::asxml $_[1]) . "</tt>\n" ];
101 ()
102 }
103
104 sub view_textblock {
105 push @result, [ $indent * 16, $level, "$_[1]\n" ];
106 ()
107 }
108
109 sub view_head1 {
110 push @result, [ $indent * 16, $level ];
111 my $title = $_[1]->title->present ($_[0]);
112 $result[-1][P_MARKUP] = ::special h1 => $title if length $title;
113 $title = ::flatten $title;
114 unshift @{ $result[-1][P_INDEX] }, $title
115 if !$result[-1][P_INDEX];
116 local $level = $level + 1;
117 $_[1]->content->present ($_[0]);
118 ()
119 };
120
121 sub view_head2 {
122 push @result, [ $indent * 16, $level ];
123 my $title = $_[1]->title->present ($_[0]);
124 $result[-1][P_MARKUP] = ::special h2 => $title if length $title;
125 $title = ::flatten $title;
126 unshift @{ $result[-1][P_INDEX] }, $title
127 if !$result[-1][P_INDEX];
128 local $level = $level + 1;
129 $_[1]->content->present ($_[0]);
130 ()
131 };
132
133 sub view_head3 {
134 push @result, [ $indent * 16, $level ];
135 my $title = $_[1]->title->present ($_[0]);
136 $result[-1][P_MARKUP] = ::special h3 => $title if length $title;
137 $title = ::flatten $title;
138 unshift @{ $result[-1][P_INDEX] || [] }, $title
139 if !$result[-1][P_INDEX];
140 local $level = $level + 1;
141 $_[1]->content->present ($_[0]);
142 ()
143 };
144
145 sub view_over {
146 local $indent = $indent + $_[1]->indent;
147 push @result, [ $indent, $level ];
148 $_[1]->content->present ($_[0]);
149 ()
150 }
151
152 sub view_item {
153 push @result, [ $indent * 8, $level ];
154 my $title = $_[1]->title->present ($_[0]);
155 $result[-1][P_MARKUP] = "$title\n" if length $title;
156 $title = ::flatten $title;
157 unshift @{ $result[-1][P_INDEX] || [] }, $title
158 if !$result[-1][P_INDEX];
159 local $level = $level + 1;
160 $_[1]->content->present ($_[0]);
161 ()
162 }
163
164 sub view_for {
165 if ($_[1]->format eq "image") {
166 push @result, [
167 $indent * 16,
168 $level,
169 (::special image => "pod/" . $_->text),
170 ];
171 }
172 ()
173 }
174
175 sub view_begin {
176 ()
177 }
178
179 sub view {
180 my ($self, $type, $item) = @_;
181
182 $item->content->present ($self);
183 }
184
185 #############################################################################
186
187 sub as_paragraphs($) {
188 my ($pom) = @_;
189
190 local $indent = 0;
191 local $level = 2;
192 local @result = ( [] );
193
194 $pom->present ("AsParagraphs");
195
196 [grep $_->[P_INDEX] || defined $_->[P_MARKUP], @result]
197 }
198
199 #############################################################################
200
201 my %wiki;
202
203 sub add_node($) {
204 my ($node) = @_;
205
206 for (@{ $node->[N_KW] || {} }) {
207 push @{$wiki{lc $_}}, $node;
208 }
209 }
210
211 my $root;
212 $root->[N_KW] = ["Documents", "pod"];
213 $root->[N_DOC] = [[0, 0, ::special link => "All Documents", "pod/*"]];
214
215 for my $path (@ARGV) {
216 $path =~ /([^\/\\]+)\.pod$/ or die "$path: illegal pathname";
217 my $base = $1;
218 my $pom = Pod::POM->new->parse_text (do {
219 local $/;
220 open my $pod, "<:utf8", $path
221 or die "$path: $!";
222 <$pod>
223 });
224
225 my $para = as_paragraphs $pom;
226
227 my $pod;
228 $pod->[N_PARENT] = $root;
229 $pod->[N_PAR] = 0;
230 $pod->[N_LEVEL] = 1;
231 $pod->[N_KW] = [$base];
232 $pod->[N_DOC] = $para;
233
234 my @parent = ($pod);
235
236 for my $idx (0 .. $#$para) {
237 my $par = $para->[$idx];
238
239 while ($parent[-1][N_LEVEL] >= $par->[P_LEVEL]) {
240 pop @parent;
241 }
242
243 if ($par->[P_INDEX]) {
244 my $node;
245 $node->[N_PARENT] = $parent[-1];
246 $node->[N_PAR] = $idx;
247 $node->[N_LEVEL] = $par->[P_LEVEL];
248 $node->[N_KW] = $par->[P_INDEX];
249 $node->[N_DOC] = $para;
250 push @parent, $node;
251 add_node $node;
252 }
253 }
254
255 add_node $pod;
256 }
257
258 add_node $root;
259
260 Storable::nstore \%wiki, "docwiki.pst";
261