ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cf.schmorp.de/Deliantra-Client/DC/Pod.pm
Revision: 1.24
Committed: Sat Apr 3 02:58:25 2010 UTC (14 years, 3 months ago) by root
Branch: MAIN
CVS Tags: rel-2_11, HEAD
Changes since 1.23: +1 -2 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 package DC::Pod;
2
3 use common::sense;
4
5 use Storable;
6
7 our $VERSION = 1.03;
8
9 our $goto_document = sub { };
10 our %wiki;
11
12 my $MA_BEG = "\x{fcd0}";
13 my $MA_SEP = "\x{fcd1}";
14 my $MA_END = "\x{fcd2}";
15
16 # nodes (order must stay as it is)
17 sub N_PARENT (){ 0 }
18 sub N_PAR (){ 1 }
19 sub N_LEVEL (){ 2 }
20 sub N_KW (){ 3 }
21 sub N_DOC (){ 4 }
22
23 # paragraphs (order must stay as it is)
24 sub P_INDENT (){ 0 }
25 sub P_LEVEL (){ 1 }
26 sub P_MARKUP (){ 2 }
27 sub P_INDEX (){ 3 }
28
29 our %wiki;
30
31 sub load_docwiki {
32 *wiki = Storable::retrieve $_[0];
33 }
34
35 sub goto_document($) {
36 $goto_document->(split /\//, $_[0]);
37 }
38
39 sub is_prefix_of($@) {
40 my ($node, @path) = @_;
41
42 return 1 unless @path;
43
44 my $kw = pop @path;
45
46 $node = $node->[N_PARENT]
47 or return 0;
48
49 return scalar grep $_ eq $kw, @{ $node->[N_KW] };
50 }
51
52 sub find(@) {
53 my (@path) = @_;
54
55 return unless @path;
56
57 my $kw = pop @path;
58
59 my %res = map +($_, $_),
60 grep { is_prefix_of $_, @path }
61 map @$_,
62 $kw eq "*" ? values %wiki
63 : $wiki{$kw} || ();
64
65 values %res
66 }
67
68 sub full_path_of($) {
69 my ($node) = @_;
70
71 my @path;
72
73 while ($node) {
74 unshift @path, $node;
75 $node = $node->[N_PARENT];
76 }
77
78 @path
79 }
80
81 sub full_path($) {
82 join "/", map $_->[N_KW][0], &full_path_of
83 }
84
85 sub section_of($) {
86 my ($node) = @_;
87
88 my $doc = $node->[N_DOC];
89 my $par = $node->[N_PAR];
90 my $lvl = $node->[N_LEVEL];
91
92 my @res;
93
94 do {
95 my $p = $doc->[$par];
96
97 if (length $p->[P_MARKUP]) {
98 push @res, {
99 markup => $p->[P_MARKUP],
100 indent => $p->[P_INDENT],
101 };
102 }
103 } while $doc->[++$par][P_LEVEL] > $lvl;
104
105 @res
106 }
107
108 sub section(@) {
109 map section_of $_, &find
110 }
111
112 sub thaw_section(\@\%) {
113 for (@{$_[0]}) {
114 $_->{markup} =~ s{
115 $MA_BEG
116 ([^$MA_END]+)
117 $MA_END
118 }{
119 my ($type, @arg) = split /$MA_SEP/o, $1;
120
121 $_[1]{$type}($_, @arg)
122 }ogex;
123 }
124 }
125
126 my %as_common = (
127 h1 => sub {
128 "\n\n<span foreground='#ffff00' size='x-large'>$_[1]</span>\n"
129 },
130 h2 => sub {
131 "\n\n<span foreground='#ccccff' size='large'>$_[1]</span>\n"
132 },
133 h3 => sub {
134 "\n\n<span size='large'>$_[1]</span>\n"
135 },
136 );
137
138 my %as_label = (
139 %as_common,
140 image => sub {
141 my ($par, $path) = @_;
142
143 "<small>img</small>"
144 },
145 link => sub {
146 my ($par, $text, $link) = @_;
147
148 "<span foreground='#ffff00'>↺</span><span foreground='#c0c0ff' underline='single'>" . (DC::asxml $text) . "</span>"
149 },
150 );
151
152 sub as_label(@) {
153 thaw_section @_, %as_label;
154
155 my $text =
156 join "\n",
157 map +("\xa0" x ($_->{indent} / 4)) . $_->{markup},
158 @_;
159
160 $text =~ s/^\s+//;
161 $text =~ s/\s+$//;
162
163 $text
164 }
165
166 my %as_paragraphs = (
167 %as_common,
168 image => sub {
169 my ($par, $path, $flags) = @_;
170
171 push @{ $par->{widget} }, new DC::UI::Image path => $path,
172 $flags & 1 ? (max_h => $::FONTSIZE) : ();
173
174 "\x{fffc}"
175 },
176 link => sub {
177 my ($par, $text, $link) = @_;
178
179 push @{ $par->{widget} }, new DC::UI::Label
180 markup => "<span foreground='#ffff00'>↺</span><span foreground='#c0c0ff' underline='single'>" . (DC::asxml $text) . "</span>",
181 fontsize => 0.8,
182 can_hover => 1,
183 can_events => 1,
184 padding_x => 0,
185 padding_y => 0,
186 tooltip => "Go to <i>" . (DC::asxml $link) . "</i>",
187 on_button_up => sub {
188 goto_document $link;
189 };
190
191 "\x{fffc}"
192 },
193 );
194
195 sub as_paragraphs(@) {
196 thaw_section @_, %as_paragraphs;
197
198 @_
199 }
200
201 sub section_paragraphs(@) {
202 as_paragraphs &section
203 }
204
205 sub section_label(@) {
206 as_label &section
207 }
208
209 1