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.18 by root, Tue Mar 25 01:46:36 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"; 30*wiki = Storable::retrieve DC::find_rcfile "docwiki.pst";
20 31
21*view_seq_code = 32sub goto_document($) {
22*view_seq_bold = sub { "<b>$_[1]</b>" }; 33 $goto_document->(split /\//, $_[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 is_prefix_of($@) {
35 ("\t" x ($indent / 4)) 37 my ($node, @path) = @_;
36 . $_[1]->title->present ($_[0]) 38
37 . "\n\n" 39 return 1 unless @path;
38 . $_[1]->content->present ($_[0]) 40
41 my $kw = lc pop @path;
42
43 $node = $node->{parent}
44 or return 0;
45
46 return scalar grep $_ eq $kw, @{ $node->{kw} };
39} 47}
40 48
41sub view_verbatim { 49sub find(@) {
42 (join "", 50 my (@path) = @_;
43 map +("\t" x ($indent / 2)) . "<tt>$_</tt>\n", 51
44 split /\n/, CFClient::asxml $_[1]) 52 return unless @path;
45 . "\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} || ()
46} 62}
47 63
48sub view_textblock { 64sub full_path_of($) {
49 ("\t" x ($indent / 2)) . "$_[1]\n\n" 65 my ($node) = @_;
66
67 my @path;
68
69 # skip toplevel hierarchy pod/, because its not a document
70 while ($node->{parent}) {
71 unshift @path, $node;
72 $node = $node->{parent};
73 }
74
75 @path
50} 76}
51 77
52sub view_head1 { 78sub full_path($) {
53 "\n\n<span foreground='#ffff00' size='x-large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n" 79 join "/", map $_->{kw}[0], &full_path_of
54 . $_[1]->content->present ($_[0])
55};
56
57sub view_head2 {
58 "\n<span foreground='#ccccff' size='large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n"
59 . $_[1]->content->present ($_[0])
60};
61
62sub view_head3 {
63 "\n<span size='large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n"
64 . $_[1]->content->present ($_[0])
65};
66
67sub view_over {
68 local $indent = $indent + $_[1]->indent;
69 $_[1]->content->present ($_[0])
70} 80}
71 81
72package CFClient::Pod::AsParagraphs; 82sub section_of($) {
83 my ($node) = @_;
73 84
74use strict; 85 my $doc = $node->{doc};
86 my $par = $node->{par};
87 my $lvl = $node->{level};
75 88
76use base "Pod::POM::View"; 89 my @res;
77 90
78*view_seq_code = 91 do {
79*view_seq_bold = sub { "<b>$_[1]</b>" }; 92 my $p = $doc->[$par];
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 93
85sub view_seq_text { 94 if (length $p->[P_MARKUP]) {
86 my $text = $_[1]; 95 push @res, {
87 $text =~ s/\s+/ /g; 96 markup => $p->[P_MARKUP],
88 CFClient::asxml $text 97 indent => $p->[P_INDENT],
98 };
99 }
100 } while $doc->[++$par][P_LEVEL] > $lvl;
101
102 @res
89} 103}
90 104
91sub view_item { 105sub section(@) {
92 push @result, { 106 map section_of $_, &find
93 indent => $indent * 8,
94 text => $_[1]->title->present ($_[0]) . "\n\n",
95 };
96 $_[1]->content->present ($_[0]);
97 ()
98} 107}
99 108
100sub view_verbatim { 109sub thaw_section(\@\%) {
101 push @result, { 110 for (@{$_[0]}) {
102 indent => $indent * 16, 111 $_->{markup} =~ s{
103 text => "<tt>" . (CFClient::asxml $_[1]) . "</tt>", 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;
104 }; 120 }
105 ()
106} 121}
107 122
108sub view_textblock { 123my %as_label = (
109 push @result, { 124 image => sub {
110 indent => $indent * 16, 125 my ($par, $path) = @_;
111 text => "$_[1]\n", 126
127 "<small>img</small>"
112 }; 128 },
113 () 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
114} 148}
115 149
116sub view_head1 { 150my %as_paragraphs = (
117 push @result, { 151 image => sub {
118 indent => $indent * 16, 152 my ($par, $path, $flags) = @_;
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 153
125sub view_head2 { 154 push @{ $par->{widget} }, new DC::UI::Image path => $path,
126 push @result, { 155 $flags & 1 ? (max_h => $::FONTSIZE) : ();
127 indent => $indent * 16,
128 text => "\n\n<span foreground='#ccccff' size='large'>" . $_[1]->title->present ($_[0]) . "</span>\n",
129 };
130 $_[1]->content->present ($_[0]);
131 ()
132};
133 156
134sub view_head3 { 157 "\x{fffc}"
135 push @result, {
136 indent => $indent * 16,
137 text => "\n\n<span size='large'>" . $_[1]->title->present ($_[0]) . "</span>\n",
138 }; 158 },
139 $_[1]->content->present ($_[0]); 159 link => sub {
140 () 160 my ($par, $text, $link) = @_;
141};
142 161
143sub view_over { 162 push @{ $par->{widget} }, new DC::UI::Label
144 local $indent = $indent + $_[1]->indent; 163 markup => "<span foreground='#ffff00'>↺</span><span foreground='#c0c0ff' underline='single'>" . (DC::asxml $text) . "</span>",
145 push @result, { indent => $indent }; 164 fontsize => 0.8,
146 $_[1]->content->present ($_[0]); 165 can_hover => 1,
147 () 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 @_
148} 182}
149 183
150sub view_for { 184sub section_paragraphs(@) {
151 if ($_[1]->format eq "image") { 185 as_paragraphs &section
152 push @result, {
153 indent => $indent * 16,
154 text => "\x{fffc}",
155 obj => [new CFClient::UI::Image path => "pod/" . $_[1]->text],
156 };
157 }
158 ()
159} 186}
160 187
161sub view { 188sub section_label(@) {
162 my ($self, $type, $item) = @_; 189 as_label &section
163
164 $item->content->present ($self);
165} 190}
166 191
167package CFClient::Pod; 1921
168
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}
198
199sub as_xml($) {
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