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.3 by root, Sun Jul 30 13:16:44 2006 UTC vs.
Revision 1.19 by root, Tue Mar 25 02:12:35 2008 UTC

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