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.1 by root, Sun Jul 23 16:11:12 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; # 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::AsXML; 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_code = 32sub load_docwiki {
22*view_seq_bold = sub { "<b>$_[1]</b>" }; 33 *wiki = Storable::retrieve $_[0];
23*view_seq_italic = sub { "<i>$_[1]</i>" };
24*view_seq_space =
25*view_seq_link =
26*view_seq_index = sub { CFClient::asxml $_[1] };
27
28sub view_seq_text {
29 my $text = $_[1];
30 $text =~ s/\s+/ /g;
31 CFClient::asxml $text
32} 34}
33 35
34sub view_item { 36sub goto_document($) {
35 ("\t" x ($indent / 4)) 37 $goto_document->(split /\//, $_[0]);
36 . $_[1]->title->present ($_[0])
37 . "\n\n"
38 . $_[1]->content->present ($_[0])
39} 38}
40 39
41sub view_verbatim { 40sub is_prefix_of($@) {
42 (join "", 41 my ($node, @path) = @_;
43 map +("\t" x ($indent / 2)) . "<tt>$_</tt>\n", 42
44 split /\n/, CFClient::asxml $_[1]) 43 return 1 unless @path;
45 . "\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] };
46} 52}
47 53
48sub view_textblock { 54sub find(@) {
49 ("\t" x ($indent / 2)) . "$_[1]\n\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} || ()
50} 67}
51 68
52sub view_head1 { 69sub full_path_of($) {
53 "\n\n<span foreground='#ffff00' size='x-large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n" 70 my ($node) = @_;
54 . $_[1]->content->present ($_[0])
55};
56 71
57sub view_head2 { 72 my @path;
58 "\n<span foreground='#ccccff' size='large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n"
59 . $_[1]->content->present ($_[0])
60};
61 73
62sub view_head3 { 74 # skip toplevel hierarchy pod/, because its not a document
63 "\n<span size='large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n" 75 while ($node->[N_PARENT]) {
64 . $_[1]->content->present ($_[0]) 76 unshift @path, $node;
65}; 77 $node = $node->[N_PARENT];
78 }
66 79
67sub view_over { 80 @path
68 local $indent = $indent + $_[1]->indent;
69 $_[1]->content->present ($_[0])
70} 81}
71 82
72package CFClient::Pod::AsParagraphs; 83sub full_path($) {
73 84 join "/", map $_->[N_KW][0], &full_path_of
74use strict;
75
76use base "Pod::POM::View";
77
78*view_seq_code =
79*view_seq_bold = sub { "<b>$_[1]</b>" };
80*view_seq_italic = sub { "<i>$_[1]</i>" };
81*view_seq_space =
82*view_seq_link =
83*view_seq_index = sub { CFClient::asxml $_[1] };
84
85sub view_seq_text {
86 my $text = $_[1];
87 $text =~ s/\s+/ /g;
88 CFClient::asxml $text
89} 85}
90 86
91sub view_item { 87sub section_of($) {
88 my ($node) = @_;
89
90 my $doc = $node->[N_DOC];
91 my $par = $node->[N_PAR];
92 my $lvl = $node->[N_LEVEL];
93
94 my @res;
95
96 do {
97 my $p = $doc->[$par];
98
99 if (length $p->[P_MARKUP]) {
92 push @result, { 100 push @res, {
93 indent => $indent * 8, 101 markup => $p->[P_MARKUP],
94 text => $_[1]->title->present ($_[0]) . "\n\n", 102 indent => $p->[P_INDENT],
95 }; 103 };
96 $_[1]->content->present ($_[0]); 104 }
97 () 105 } while $doc->[++$par][P_LEVEL] > $lvl;
106
107 @res
98} 108}
99 109
100sub view_verbatim { 110sub section(@) {
101 push @result, { 111 map section_of $_, &find
102 indent => $indent * 16,
103 text => "<tt>" . (CFClient::asxml $_[1]) . "</tt>",
104 };
105 ()
106} 112}
107 113
108sub view_textblock { 114sub thaw_section(\@\%) {
109 push @result, { 115 for (@{$_[0]}) {
110 indent => $indent * 16, 116 $_->{markup} =~ s{
111 text => "$_[1]\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;
112 }; 125 }
113 ()
114} 126}
115 127
116sub view_head1 { 128my %as_label = (
117 push @result, { 129 image => sub {
118 indent => $indent * 16, 130 my ($par, $path) = @_;
119 text => "\n\n<span foreground='#ffff00' size='x-large'>" . $_[1]->title->present ($_[0]) . "</span>\n",
120 };
121 $_[1]->content->present ($_[0]);
122 ()
123};
124 131
125sub view_head2 { 132 "<small>img</small>"
126 push @result, {
127 indent => $indent * 16,
128 text => "\n\n<span foreground='#ccccff' size='large'>" . $_[1]->title->present ($_[0]) . "</span>\n",
129 }; 133 },
130 $_[1]->content->present ($_[0]); 134 link => sub {
131 () 135 my ($par, $text, $link) = @_;
132};
133 136
134sub view_head3 { 137 "<span foreground='#ffff00'>↺</span><span foreground='#c0c0ff' underline='single'>" . (DC::asxml $text) . "</span>"
135 push @result, {
136 indent => $indent * 16,
137 text => "\n\n<span size='large'>" . $_[1]->title->present ($_[0]) . "</span>\n",
138 }; 138 },
139 $_[1]->content->present ($_[0]); 139);
140 ()
141};
142 140
143sub view_over { 141sub as_label(@) {
144 local $indent = $indent + $_[1]->indent; 142 thaw_section @_, %as_label;
145 push @result, { indent => $indent }; 143
146 $_[1]->content->present ($_[0]); 144 my $text =
147 () 145 join "\n",
146 map +("\xa0" x ($_->{indent} / 4)) . $_->{markup},
147 @_;
148
149 $text =~ s/^\s+//;
150 $text =~ s/\s+$//;
151
152 $text
148} 153}
149 154
150sub view_for { 155my %as_paragraphs = (
151 if ($_[1]->format eq "image") { 156 image => sub {
152 push @result, { 157 my ($par, $path, $flags) = @_;
153 indent => $indent * 16, 158
154 text => "\x{fffc}", 159 push @{ $par->{widget} }, new DC::UI::Image path => $path,
155 obj => [new CFClient::UI::Image path => "pod/" . $_[1]->text], 160 $flags & 1 ? (max_h => $::FONTSIZE) : ();
156 }; 161
162 "\x{fffc}"
157 } 163 },
158 () 164 link => sub {
165 my ($par, $text, $link) = @_;
166
167 push @{ $par->{widget} }, new DC::UI::Label
168 markup => "<span foreground='#ffff00'>↺</span><span foreground='#c0c0ff' underline='single'>" . (DC::asxml $text) . "</span>",
169 fontsize => 0.8,
170 can_hover => 1,
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 @_
159} 187}
160 188
161sub view { 189sub section_paragraphs(@) {
162 my ($self, $type, $item) = @_; 190 as_paragraphs &section
163
164 $item->content->present ($self);
165} 191}
166 192
167package CFClient::Pod; 193sub section_label(@) {
168 194 as_label &section
169my $pod_cache = CFClient::db_table "pod_cache";
170
171sub load($$$$) {
172 my ($path, $filtertype, $filterversion, $filtercb) = @_;
173
174 stat $path
175 or die "$path: $!";
176
177 my $phash = join ",", $filterversion, $VERSION, (stat _)[7,9];
178
179 my ($chash, $pom) = eval { @{ Storable::thaw $pod_cache->get ("$path/$filtertype") } };
180
181 return $pom if $chash eq $phash;
182
183 my $pod = do {
184 local $/;
185 open my $pod, "<:utf8", $_[0]
186 or die "$_[0]: $!";
187 <$pod>
188 };
189
190 #utf8::downgrade $pod;
191
192 $pom = $filtercb->(Pod::POM->new->parse_text ($pod));
193
194 $pod_cache->put ("$path/$filtertype" => Storable::nfreeze [$phash, $pom]);
195
196 $pom
197} 195}
198 196
199sub as_xml($) { 1971
200 my ($pom) = @_;
201
202 local $indent = 0;
203
204 $pom->present ("CFClient::Pod::AsXML")
205}
206
207sub as_paragraphs($) {
208 my ($pom) = @_;
209
210 local @result = ( { } );
211 local $indent = 0;
212
213 $pom->present ("CFClient::Pod::AsParagraphs");
214
215 [grep exists $_->{text}, @result]
216}
217

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines