ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Pod.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/Pod.pm (file contents):
Revision 1.2 by root, Mon Jul 24 08:23:28 2006 UTC vs.
Revision 1.21 by root, Sun Mar 30 10:18:16 2008 UTC

1package CFClient::Pod; 1package DC::Pod;
2 2
3use strict; 3use strict;
4use utf8;
4 5
5use Pod::POM; 6use Storable;
6 7
7use CFClient; 8our $VERSION = 1.03;
8use CFClient::UI;
9 9
10our $VERSION = 1.02; # bump if resultant formatting changes 10our $goto_document = sub { };
11our %wiki;
11 12
12our @result; 13my $MA_BEG = "\x{fcd0}";
13our $indent; 14my $MA_SEP = "\x{fcd1}";
15my $MA_END = "\x{fcd2}";
14 16
15package CFClient::Pod::AsMarkup; 17# nodes (order must stay as it is)
18sub N_PARENT (){ 0 }
19sub N_PAR (){ 1 }
20sub N_LEVEL (){ 2 }
21sub N_KW (){ 3 }
22sub N_DOC (){ 4 }
16 23
17use strict; 24# paragraphs (order must stay as it is)
25sub P_INDENT (){ 0 }
26sub P_LEVEL (){ 1 }
27sub P_MARKUP (){ 2 }
28sub P_INDEX (){ 3 }
18 29
19use base "Pod::POM::View::Text"; 30our %wiki;
20 31
21*view_seq_file = 32sub load_docwiki {
22*view_seq_code = 33 *wiki = Storable::retrieve $_[0];
23*view_seq_bold = sub { "<b>$_[1]</b>" };
24*view_seq_italic = sub { "<i>$_[1]</i>" };
25*view_seq_space =
26*view_seq_link = sub { CFClient::asxml $_[1] };
27*view_seq_zero =
28*view_seq_index = sub { };
29
30sub view_seq_text {
31 my $text = $_[1];
32 $text =~ s/\s+/ /g;
33 CFClient::asxml $text
34} 34}
35 35
36sub view_item { 36sub goto_document($) {
37 ("\t" x ($indent / 4)) 37 $goto_document->(split /\//, $_[0]);
38 . $_[1]->title->present ($_[0])
39 . "\n\n"
40 . $_[1]->content->present ($_[0])
41} 38}
42 39
43sub view_verbatim { 40sub is_prefix_of($@) {
44 (join "", 41 my ($node, @path) = @_;
45 map +("\t" x ($indent / 2)) . "<tt>$_</tt>\n", 42
46 split /\n/, CFClient::asxml $_[1]) 43 return 1 unless @path;
47 . "\n" 44
45 my $kw = lc pop @path;
46
47 $node = $node->[N_PARENT]
48 or return 0;
49
50 #TODO: maybe get rid of lowercasing?
51 return scalar grep lc eq $kw, @{ $node->[N_KW] };
48} 52}
49 53
50sub view_textblock { 54sub find(@) {
51 ("\t" x ($indent / 2)) . "$_[1]\n" 55 my (@path) = @_;
56
57 return unless @path;
58
59 my $kw = lc pop @path;
60
61 # TODO: make sure results are unique
62
63 grep { is_prefix_of $_, @path }
64 map @$_,
65 $kw eq "*" ? @wiki{sort keys %wiki}
66 : $wiki{$kw} || ()
52} 67}
53 68
54sub view_head1 { 69sub full_path_of($) {
55 "\n\n<span foreground='#ffff00' size='x-large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n" 70 my ($node) = @_;
56 . $_[1]->content->present ($_[0])
57};
58 71
59sub view_head2 { 72 my @path;
60 "\n<span foreground='#ccccff' size='large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n"
61 . $_[1]->content->present ($_[0])
62};
63 73
64sub view_head3 { 74 # skip toplevel hierarchy pod/, because its not a document
65 "\n<span size='large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n" 75 while ($node->[N_PARENT]) {
66 . $_[1]->content->present ($_[0]) 76 unshift @path, $node;
67}; 77 $node = $node->[N_PARENT];
78 }
68 79
69sub view_over { 80 @path
70 local $indent = $indent + $_[1]->indent;
71 $_[1]->content->present ($_[0])
72} 81}
73 82
74package CFClient::Pod::AsParagraphs; 83sub full_path($) {
75 84 join "/", map $_->[N_KW][0], &full_path_of
76use strict;
77
78use base "Pod::POM::View";
79
80*view_seq_file =
81*view_seq_code =
82*view_seq_bold = sub { "<b>$_[1]</b>" };
83*view_seq_italic = sub { "<i>$_[1]</i>" };
84*view_seq_zero = sub { };
85*view_seq_space = sub { my $text = $_[1]; $text =~ s/ /&#160;/g; $text };
86*view_seq_index = sub { warn "index<@_>\n"; $result[-1]{index}{$_[1]} = undef };
87
88sub view_seq_text {
89 my $text = $_[1];
90 $text =~ s/\s+/ /g;
91 CFClient::asxml $text
92} 85}
93 86
94sub view_seq_link { 87sub section_of($) {
95 my (undef, $link) = @_; 88 my ($node) = @_;
96 89
97 # TODO: 90 my $doc = $node->[N_DOC];
98 # http://... 91 my $par = $node->[N_PAR];
99 # ref 92 my $lvl = $node->[N_LEVEL];
100 # pod/ref
101 93
102 "<u>" . (CFClient::asxml $_[1]) . "</u>"; 94 my @res;
95
96 do {
97 my $p = $doc->[$par];
98
99 if (length $p->[P_MARKUP]) {
100 push @res, {
101 markup => $p->[P_MARKUP],
102 indent => $p->[P_INDENT],
103 };
104 }
105 } while $doc->[++$par][P_LEVEL] > $lvl;
106
107 @res
103} 108}
104 109
105sub view_item { 110sub section(@) {
106 push @result, { 111 map section_of $_, &find
107 indent => $indent * 8,
108 markup => $_[1]->title->present ($_[0]) . "\n\n",
109 };
110 $_[1]->content->present ($_[0]);
111 ()
112} 112}
113 113
114sub view_verbatim { 114sub thaw_section(\@\%) {
115 push @result, { 115 for (@{$_[0]}) {
116 indent => $indent * 16, 116 $_->{markup} =~ s{
117 markup => "<tt>" . (CFClient::asxml $_[1]) . "</tt>\n", 117 $MA_BEG
118 ([^$MA_END]+)
119 $MA_END
120 }{
121 my ($type, @arg) = split /$MA_SEP/o, $1;
122
123 $_[1]{$type}($_, @arg)
124 }ogex;
118 }; 125 }
119 ()
120} 126}
121 127
122sub view_textblock { 128my %as_label = (
123 push @result, { 129 image => sub {
124 indent => $indent * 16, 130 my ($par, $path) = @_;
125 markup => "$_[1]\n", 131
132 "<small>img</small>"
126 }; 133 },
127 () 134 link => sub {
135 my ($par, $text, $link) = @_;
136
137 "<span foreground='#ffff00'>↺</span><span foreground='#c0c0ff' underline='single'>" . (DC::asxml $text) . "</span>"
138 },
139);
140
141sub as_label(@) {
142 thaw_section @_, %as_label;
143
144 my $text =
145 join "\n",
146 map +("\xa0" x ($_->{indent} / 4)) . $_->{markup},
147 @_;
148
149 $text =~ s/^\s+//;
150 $text =~ s/\s+$//;
151
152 $text
128} 153}
129 154
130sub view_head1 { 155my %as_paragraphs = (
131 push @result, { 156 image => sub {
132 indent => $indent * 16, 157 my ($par, $path, $flags) = @_;
133 markup => "\n\n<span foreground='#ffff00' size='x-large'>" . $_[1]->title->present ($_[0]) . "</span>\n",
134 };
135 $_[1]->content->present ($_[0]);
136 ()
137};
138 158
139sub view_head2 { 159 push @{ $par->{widget} }, new DC::UI::Image path => $path,
140 push @result, { 160 $flags & 1 ? (max_h => $::FONTSIZE) : ();
141 indent => $indent * 16,
142 markup => "\n\n<span foreground='#ccccff' size='large'>" . $_[1]->title->present ($_[0]) . "</span>\n",
143 };
144 $_[1]->content->present ($_[0]);
145 ()
146};
147 161
148sub view_head3 { 162 "\x{fffc}"
149 push @result, {
150 indent => $indent * 16,
151 markup => "\n\n<span size='large'>" . $_[1]->title->present ($_[0]) . "</span>\n",
152 }; 163 },
153 $_[1]->content->present ($_[0]); 164 link => sub {
154 () 165 my ($par, $text, $link) = @_;
155};
156 166
157sub view_over { 167 push @{ $par->{widget} }, new DC::UI::Label
158 local $indent = $indent + $_[1]->indent; 168 markup => "<span foreground='#ffff00'>↺</span><span foreground='#c0c0ff' underline='single'>" . (DC::asxml $text) . "</span>",
159 push @result, { indent => $indent }; 169 fontsize => 0.8,
160 $_[1]->content->present ($_[0]); 170 can_hover => 1,
161 () 171 can_events => 1,
172 padding_x => 0,
173 padding_y => 0,
174 tooltip => "Go to <i>" . (DC::asxml $link) . "</i>",
175 on_button_up => sub {
176 goto_document $link;
177 };
178
179 "\x{fffc}"
180 },
181);
182
183sub as_paragraphs(@) {
184 thaw_section @_, %as_paragraphs;
185
186 @_
162} 187}
163 188
164sub view_for { 189sub section_paragraphs(@) {
165 if ($_[1]->format eq "image") { 190 as_paragraphs &section
166 push @result, {
167 indent => $indent * 16,
168 markup => "\x{fffc}",
169 widget => [new CFClient::UI::Image path => "pod/" . $_[1]->text],
170 };
171 }
172 ()
173} 191}
174 192
175sub view { 193sub section_label(@) {
176 my ($self, $type, $item) = @_; 194 as_label &section
177
178 $item->content->present ($self);
179} 195}
180 196
181package CFClient::Pod; 1971
182
183my $pod_cache = CFClient::db_table "pod_cache";
184
185sub load($$$$) {
186 my ($path, $filtertype, $filterversion, $filtercb) = @_;
187
188 stat $path
189 or die "$path: $!";
190
191 my $phash = join ",", $filterversion, $VERSION, (stat _)[7,9];
192
193 my ($chash, $pom) = eval {
194 local $SIG{__DIE__};
195 @{ Storable::thaw $pod_cache->get ("$path/$filtertype") }
196 };
197
198 return $pom if $chash eq $phash;
199
200 my $pod = do {
201 local $/;
202 open my $pod, "<:utf8", $_[0]
203 or die "$_[0]: $!";
204 <$pod>
205 };
206
207 #utf8::downgrade $pod;
208
209 $pom = $filtercb->(Pod::POM->new->parse_text ($pod));
210
211 $pod_cache->put ("$path/$filtertype" => Storable::nfreeze [$phash, $pom]);
212
213 $pom
214}
215
216sub section($$) {
217 my ($pod, $section) = @_;
218}
219
220sub as_markup($) {
221 my ($pom) = @_;
222
223 local $indent = 0;
224
225 $pom->present ("CFClient::Pod::AsMarkup")
226}
227
228sub as_paragraphs($) {
229 my ($pom) = @_;
230
231 local @result = ( { } );
232 local $indent = 0;
233
234 $pom->present ("CFClient::Pod::AsParagraphs");
235
236 [grep exists $_->{markup}, @result]
237}
238
239sub pod_paragraphs($) {
240 load CFClient::find_rcfile "pod/$_[0].pod",
241 pod_paragraphs => 1, sub { as_paragraphs $_[0] };
242}
243

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines