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.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; # 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 return scalar grep $_ eq $kw, @{ $node->[N_KW] };
46} 51}
47 52
48sub view_textblock { 53sub find(@) {
49 ("\t" x ($indent / 2)) . "$_[1]\n\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} || ()
50} 66}
51 67
52sub view_head1 { 68sub full_path_of($) {
53 "\n\n<span foreground='#ffff00' size='x-large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n" 69 my ($node) = @_;
54 . $_[1]->content->present ($_[0])
55};
56 70
57sub view_head2 { 71 my @path;
58 "\n<span foreground='#ccccff' size='large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n"
59 . $_[1]->content->present ($_[0])
60};
61 72
62sub view_head3 { 73 # skip toplevel hierarchy pod/, because its not a document
63 "\n<span size='large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n" 74 while ($node->[N_PARENT]) {
64 . $_[1]->content->present ($_[0]) 75 unshift @path, $node;
65}; 76 $node = $node->[N_PARENT];
77 }
66 78
67sub view_over { 79 @path
68 local $indent = $indent + $_[1]->indent;
69 $_[1]->content->present ($_[0])
70} 80}
71 81
72package CFClient::Pod::AsParagraphs; 82sub full_path($) {
73 83 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} 84}
90 85
91sub view_item { 86sub section_of($) {
87 my ($node) = @_;
88
89 my $doc = $node->[N_DOC];
90 my $par = $node->[N_PAR];
91 my $lvl = $node->[N_LEVEL];
92
93 my @res;
94
95 do {
96 my $p = $doc->[$par];
97
98 if (length $p->[P_MARKUP]) {
92 push @result, { 99 push @res, {
93 indent => $indent * 8, 100 markup => $p->[P_MARKUP],
94 text => $_[1]->title->present ($_[0]) . "\n\n", 101 indent => $p->[P_INDENT],
95 }; 102 };
96 $_[1]->content->present ($_[0]); 103 }
97 () 104 } while $doc->[++$par][P_LEVEL] > $lvl;
105
106 @res
98} 107}
99 108
100sub view_verbatim { 109sub section(@) {
101 push @result, { 110 map section_of $_, &find
102 indent => $indent * 16,
103 text => "<tt>" . (CFClient::asxml $_[1]) . "</tt>",
104 };
105 ()
106} 111}
107 112
108sub view_textblock { 113sub thaw_section(\@\%) {
109 push @result, { 114 for (@{$_[0]}) {
110 indent => $indent * 16, 115 $_->{markup} =~ s{
111 text => "$_[1]\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;
112 }; 124 }
113 ()
114} 125}
115 126
116sub view_head1 { 127my %as_label = (
117 push @result, { 128 image => sub {
118 indent => $indent * 16, 129 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 130
125sub view_head2 { 131 "<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 }; 132 },
130 $_[1]->content->present ($_[0]); 133 link => sub {
131 () 134 my ($par, $text, $link) = @_;
132};
133 135
134sub view_head3 { 136 "<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 }; 137 },
139 $_[1]->content->present ($_[0]); 138);
140 ()
141};
142 139
143sub view_over { 140sub as_label(@) {
144 local $indent = $indent + $_[1]->indent; 141 thaw_section @_, %as_label;
145 push @result, { indent => $indent }; 142
146 $_[1]->content->present ($_[0]); 143 my $text =
147 () 144 join "\n",
145 map +("\xa0" x ($_->{indent} / 4)) . $_->{markup},
146 @_;
147
148 $text =~ s/^\s+//;
149 $text =~ s/\s+$//;
150
151 $text
148} 152}
149 153
150sub view_for { 154my %as_paragraphs = (
151 if ($_[1]->format eq "image") { 155 image => sub {
152 push @result, { 156 my ($par, $path, $flags) = @_;
153 indent => $indent * 16, 157
154 text => "\x{fffc}", 158 push @{ $par->{widget} }, new DC::UI::Image path => $path,
155 obj => [new CFClient::UI::Image path => "pod/" . $_[1]->text], 159 $flags & 1 ? (max_h => $::FONTSIZE) : ();
156 }; 160
161 "\x{fffc}"
157 } 162 },
158 () 163 link => sub {
164 my ($par, $text, $link) = @_;
165
166 push @{ $par->{widget} }, new DC::UI::Label
167 markup => "<span foreground='#ffff00'>↺</span><span foreground='#c0c0ff' underline='single'>" . (DC::asxml $text) . "</span>",
168 fontsize => 0.8,
169 can_hover => 1,
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 @_
159} 186}
160 187
161sub view { 188sub section_paragraphs(@) {
162 my ($self, $type, $item) = @_; 189 as_paragraphs &section
163
164 $item->content->present ($self);
165} 190}
166 191
167package CFClient::Pod; 192sub section_label(@) {
168 193 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} 194}
198 195
199sub as_xml($) { 1961
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