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