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.20 by root, Sun Mar 30 00:25:11 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 return scalar grep $_ eq $kw, @{ $node->[N_KW] };
48} 51}
49 52
50sub view_textblock { 53sub find(@) {
51 ("\t" x ($indent / 2)) . "$_[1]\n" 54 my (@path) = @_;
55
56 return unless @path;
57
58 my $kw = lc pop @path;
59
60 # TODO: make sure results are unique
61
62 grep { is_prefix_of $_, @path }
63 map @$_,
64 $kw eq "*" ? @wiki{sort keys %wiki}
65 : $wiki{$kw} || ()
52} 66}
53 67
54sub view_head1 { 68sub full_path_of($) {
55 "\n\n<span foreground='#ffff00' size='x-large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n" 69 my ($node) = @_;
56 . $_[1]->content->present ($_[0])
57};
58 70
59sub view_head2 { 71 my @path;
60 "\n<span foreground='#ccccff' size='large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n"
61 . $_[1]->content->present ($_[0])
62};
63 72
64sub view_head3 { 73 # skip toplevel hierarchy pod/, because its not a document
65 "\n<span size='large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n" 74 while ($node->[N_PARENT]) {
66 . $_[1]->content->present ($_[0]) 75 unshift @path, $node;
67}; 76 $node = $node->[N_PARENT];
77 }
68 78
69sub view_over { 79 @path
70 local $indent = $indent + $_[1]->indent;
71 $_[1]->content->present ($_[0])
72} 80}
73 81
74package CFClient::Pod::AsParagraphs; 82sub full_path($) {
75 83 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} 84}
93 85
94sub view_seq_link { 86sub section_of($) {
95 my (undef, $link) = @_; 87 my ($node) = @_;
96 88
97 # TODO: 89 my $doc = $node->[N_DOC];
98 # http://... 90 my $par = $node->[N_PAR];
99 # ref 91 my $lvl = $node->[N_LEVEL];
100 # pod/ref
101 92
102 "<u>" . (CFClient::asxml $_[1]) . "</u>"; 93 my @res;
94
95 do {
96 my $p = $doc->[$par];
97
98 if (length $p->[P_MARKUP]) {
99 push @res, {
100 markup => $p->[P_MARKUP],
101 indent => $p->[P_INDENT],
102 };
103 }
104 } while $doc->[++$par][P_LEVEL] > $lvl;
105
106 @res
103} 107}
104 108
105sub view_item { 109sub section(@) {
106 push @result, { 110 map section_of $_, &find
107 indent => $indent * 8,
108 markup => $_[1]->title->present ($_[0]) . "\n\n",
109 };
110 $_[1]->content->present ($_[0]);
111 ()
112} 111}
113 112
114sub view_verbatim { 113sub thaw_section(\@\%) {
115 push @result, { 114 for (@{$_[0]}) {
116 indent => $indent * 16, 115 $_->{markup} =~ s{
117 markup => "<tt>" . (CFClient::asxml $_[1]) . "</tt>\n", 116 $MA_BEG
117 ([^$MA_END]+)
118 $MA_END
119 }{
120 my ($type, @arg) = split /$MA_SEP/o, $1;
121
122 $_[1]{$type}($_, @arg)
123 }ogex;
118 }; 124 }
119 ()
120} 125}
121 126
122sub view_textblock { 127my %as_label = (
123 push @result, { 128 image => sub {
124 indent => $indent * 16, 129 my ($par, $path) = @_;
125 markup => "$_[1]\n", 130
131 "<small>img</small>"
126 }; 132 },
127 () 133 link => sub {
134 my ($par, $text, $link) = @_;
135
136 "<span foreground='#ffff00'>↺</span><span foreground='#c0c0ff' underline='single'>" . (DC::asxml $text) . "</span>"
137 },
138);
139
140sub as_label(@) {
141 thaw_section @_, %as_label;
142
143 my $text =
144 join "\n",
145 map +("\xa0" x ($_->{indent} / 4)) . $_->{markup},
146 @_;
147
148 $text =~ s/^\s+//;
149 $text =~ s/\s+$//;
150
151 $text
128} 152}
129 153
130sub view_head1 { 154my %as_paragraphs = (
131 push @result, { 155 image => sub {
132 indent => $indent * 16, 156 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 157
139sub view_head2 { 158 push @{ $par->{widget} }, new DC::UI::Image path => $path,
140 push @result, { 159 $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 160
148sub view_head3 { 161 "\x{fffc}"
149 push @result, {
150 indent => $indent * 16,
151 markup => "\n\n<span size='large'>" . $_[1]->title->present ($_[0]) . "</span>\n",
152 }; 162 },
153 $_[1]->content->present ($_[0]); 163 link => sub {
154 () 164 my ($par, $text, $link) = @_;
155};
156 165
157sub view_over { 166 push @{ $par->{widget} }, new DC::UI::Label
158 local $indent = $indent + $_[1]->indent; 167 markup => "<span foreground='#ffff00'>↺</span><span foreground='#c0c0ff' underline='single'>" . (DC::asxml $text) . "</span>",
159 push @result, { indent => $indent }; 168 fontsize => 0.8,
160 $_[1]->content->present ($_[0]); 169 can_hover => 1,
161 () 170 can_events => 1,
171 padding_x => 0,
172 padding_y => 0,
173 tooltip => "Go to <i>" . (DC::asxml $link) . "</i>",
174 on_button_up => sub {
175 goto_document $link;
176 };
177
178 "\x{fffc}"
179 },
180);
181
182sub as_paragraphs(@) {
183 thaw_section @_, %as_paragraphs;
184
185 @_
162} 186}
163 187
164sub view_for { 188sub section_paragraphs(@) {
165 if ($_[1]->format eq "image") { 189 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} 190}
174 191
175sub view { 192sub section_label(@) {
176 my ($self, $type, $item) = @_; 193 as_label &section
177
178 $item->content->present ($self);
179} 194}
180 195
181package CFClient::Pod; 1961
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