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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines