ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pod2wiki
Revision: 1.11
Committed: Tue Mar 25 01:46:36 2008 UTC (16 years, 2 months ago) by root
Branch: MAIN
Changes since 1.10: +46 -49 lines
Log Message:
make documents level 1, root level 0, also use arrays for paragraphs, for, among other things, speed and memory savings

File Contents

# User Rev Content
1 root 1.1 #! perl
2    
3 root 1.5 # convert given .pod files to wiki style
4 root 1.1
5 root 1.5 # base path of arch tree, only used for new arch graphics
6 elmex 1.7 my $ARCH = "/root/devel/cvs/cf.schmorp.de/arch";
7 root 1.1
8     use strict;
9    
10 root 1.5 use Storable;
11 root 1.1 use Pod::POM;
12    
13     our @result;
14     our $indent;
15     our $level;
16    
17 root 1.3 my $MA_BEG = "\x{fcd0}";
18     my $MA_SEP = "\x{fcd1}";
19     my $MA_END = "\x{fcd2}";
20    
21 root 1.1 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 root 1.8 sub special {
43     $MA_BEG . (join $MA_SEP, @_) . $MA_END
44     }
45    
46 root 1.1 package AsParagraphs;
47    
48     use strict;
49    
50     use base "Pod::POM::View";
51    
52 root 1.11 # 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 root 1.1 *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 root 1.11 *view_seq_index = sub { push @{ $result[-1][P_INDEX] }, $_[1]; "" };
72 root 1.1
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 root 1.4 my $text = $link =~ s/^(.*)\|// ? $1 : $link;
83    
84 root 1.3 if ($link =~ /http:/) {
85     "<u>" . (::asxml $link) . "</u>"
86 root 1.5 } elsif ($link =~ /^\$ARCH\/(.+)$/) {
87     my $path = $1;
88     (my $base = $path) =~ s/.*\///;
89 root 1.6 -f "$ARCH/$path" && system "rsync -av -c \Q$ARCH/$path\E \Qresources/arch/$base";
90 root 1.8 ::special image => "arch/$base", 1;
91 root 1.3 } else {
92 root 1.8 ::special link => $text, $link
93 root 1.3 }
94 root 1.1 }
95    
96     sub view_item {
97 root 1.11 push @result, [
98     $indent * 8,
99     $level,
100     ];
101 root 1.1 my $title = $_[1]->title->present ($_[0]);
102 root 1.11 $result[-1][P_MARKUP] = "$title\n" if length $title;
103     $title = ::flatten $title; unshift @{ $result[-1][P_INDEX] }, $title if length $title;
104 root 1.1 local $level = $level + 1;
105     $_[1]->content->present ($_[0]);
106     ()
107     }
108    
109     sub view_verbatim {
110 root 1.11 push @result, [ $indent * 16, $level, "<tt>" . (::asxml $_[1]) . "</tt>\n" ];
111 root 1.1 ()
112     }
113    
114     sub view_textblock {
115 root 1.11 push @result, [ $indent * 16, $level, "$_[1]\n" ];
116 root 1.1 ()
117     }
118    
119     sub view_head1 {
120 root 1.11 push @result, [ $indent * 16, $level ];
121 root 1.1 my $title = $_[1]->title->present ($_[0]);
122 root 1.11 $result[-1][P_MARKUP] = "\n\n<span foreground='#ffff00' size='x-large'>$title</span>\n" if length $title;
123     $title = ::flatten $title; unshift @{ $result[-1][P_INDEX] }, $title if length $title;
124 root 1.1 local $level = $level + 1;
125     $_[1]->content->present ($_[0]);
126     ()
127     };
128    
129     sub view_head2 {
130 root 1.11 push @result, [ $indent * 16, $level ];
131 root 1.1 my $title = $_[1]->title->present ($_[0]);
132 root 1.11 $result[-1][P_MARKUP] = "\n\n<span foreground='#ccccff' size='large'>$title</span>\n" if length $title;
133     $title = ::flatten $title; unshift @{ $result[-1][P_INDEX] }, $title if length $title;
134 root 1.1 local $level = $level + 1;
135     $_[1]->content->present ($_[0]);
136     ()
137     };
138    
139     sub view_head3 {
140 root 1.11 push @result, [ $indent * 16, $level ];
141 root 1.1 my $title = $_[1]->title->present ($_[0]);
142 root 1.11 $result[-1][P_MARKUP] = "\n\n<span size='large'>$title</span>\n" if length $title;
143     $title = ::flatten $title; unshift @{ $result[-1][P_INDEX] }, $title if length $title;
144 root 1.1 local $level = $level + 1;
145     $_[1]->content->present ($_[0]);
146     ()
147     };
148    
149     sub view_over {
150     local $indent = $indent + $_[1]->indent;
151 root 1.11 push @result, [ $indent, $level ];
152     local $level = $level + 1;
153 root 1.1 $_[1]->content->present ($_[0]);
154     ()
155     }
156    
157     sub view_for {
158     if ($_[1]->format eq "image") {
159 root 1.11 push @result, [
160     $indent * 16,
161     $level,
162     (::special image => "pod/" . $_->text),
163     ];
164 root 1.1 }
165     ()
166     }
167    
168 root 1.10 sub view_begin {
169     ()
170     }
171    
172 root 1.1 sub view {
173     my ($self, $type, $item) = @_;
174    
175     $item->content->present ($self);
176     }
177    
178     #############################################################################
179    
180     sub as_paragraphs($) {
181     my ($pom) = @_;
182    
183     local $indent = 0;
184 root 1.11 local $level = 2;
185     local @result = ( [] );
186 root 1.1
187     $pom->present ("AsParagraphs");
188    
189 root 1.11 [grep $_->[P_INDEX] || defined $_->[P_MARKUP], @result]
190 root 1.1 }
191    
192     #############################################################################
193    
194     my %wiki;
195    
196     sub add_node($) {
197     my ($node) = @_;
198    
199     for (@{ $node->{kw} || {} }) {
200 root 1.5 push @{$wiki{lc $_}}, $node;
201 root 1.1 }
202     }
203    
204     my $root = {
205     kw => ["pod"],
206     };
207    
208     for my $path (@ARGV) {
209     $path =~ /([^\/\\]+)\.pod$/ or die "$path: illegal pathname";
210     my $base = $1;
211     my $pom = Pod::POM->new->parse_text (do {
212     local $/;
213     open my $pod, "<:utf8", $path
214     or die "$path: $!";
215     <$pod>
216     });
217    
218     my $para = as_paragraphs $pom;
219    
220     my @parent = (
221 root 1.11 { parent => $root, kw => [$base], doc => $para, par => 0, level => 1 },
222 root 1.1 );
223     add_node $parent[-1];
224    
225     for my $idx (0 .. $#$para) {
226     my $par = $para->[$idx];
227    
228 root 1.11 while ($parent[-1]{level} >= $par->[P_LEVEL]) {
229 root 1.1 pop @parent;
230     }
231    
232 root 1.11 if ($par->[P_INDEX]) {
233 root 1.1 my $node = {
234 root 1.11 kw => $par->[P_INDEX],
235 root 1.1 parent => $parent[-1],
236     doc => $para,
237     par => $idx,
238 root 1.11 level => $par->[P_LEVEL],
239 root 1.1 };
240     push @parent, $node;
241     add_node $node;
242     }
243     }
244     }
245    
246     Storable::nstore \%wiki, "docwiki.pst";
247