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