1 |
root |
1.17 |
package DC::Pod; |
2 |
root |
1.1 |
|
3 |
|
|
use strict; |
4 |
root |
1.8 |
use utf8; |
5 |
root |
1.1 |
|
6 |
root |
1.4 |
use Storable; |
7 |
root |
1.1 |
|
8 |
root |
1.15 |
our $VERSION = 1.03; |
9 |
root |
1.1 |
|
10 |
root |
1.9 |
our $goto_document = sub { }; |
11 |
root |
1.4 |
our %wiki; |
12 |
root |
1.1 |
|
13 |
root |
1.7 |
my $MA_BEG = "\x{fcd0}"; |
14 |
|
|
my $MA_SEP = "\x{fcd1}"; |
15 |
|
|
my $MA_END = "\x{fcd2}"; |
16 |
|
|
|
17 |
root |
1.18 |
# nodes (order must stay as it is) |
18 |
|
|
sub N_PARENT (){ 0 } |
19 |
|
|
sub N_PAR (){ 1 } |
20 |
|
|
sub N_LEVEL (){ 2 } |
21 |
|
|
sub N_KW (){ 3 } |
22 |
|
|
sub N_DOC (){ 4 } |
23 |
|
|
|
24 |
|
|
# paragraphs (order must stay as it is) |
25 |
|
|
sub P_INDENT (){ 0 } |
26 |
|
|
sub P_LEVEL (){ 1 } |
27 |
|
|
sub P_MARKUP (){ 2 } |
28 |
|
|
sub P_INDEX (){ 3 } |
29 |
|
|
|
30 |
root |
1.20 |
our %wiki; |
31 |
|
|
|
32 |
|
|
sub load_docwiki { |
33 |
|
|
*wiki = Storable::retrieve $_[0]; |
34 |
|
|
} |
35 |
root |
1.1 |
|
36 |
root |
1.9 |
sub goto_document($) { |
37 |
|
|
$goto_document->(split /\//, $_[0]); |
38 |
|
|
} |
39 |
|
|
|
40 |
root |
1.4 |
sub is_prefix_of($@) { |
41 |
|
|
my ($node, @path) = @_; |
42 |
root |
1.1 |
|
43 |
root |
1.4 |
return 1 unless @path; |
44 |
root |
1.1 |
|
45 |
root |
1.10 |
my $kw = lc pop @path; |
46 |
root |
1.1 |
|
47 |
root |
1.19 |
$node = $node->[N_PARENT] |
48 |
root |
1.4 |
or return 0; |
49 |
root |
1.2 |
|
50 |
root |
1.21 |
#TODO: maybe get rid of lowercasing? |
51 |
|
|
return scalar grep lc eq $kw, @{ $node->[N_KW] }; |
52 |
root |
1.2 |
} |
53 |
|
|
|
54 |
root |
1.4 |
sub find(@) { |
55 |
|
|
my (@path) = @_; |
56 |
root |
1.1 |
|
57 |
root |
1.4 |
return unless @path; |
58 |
root |
1.1 |
|
59 |
root |
1.10 |
my $kw = lc pop @path; |
60 |
root |
1.1 |
|
61 |
root |
1.4 |
# TODO: make sure results are unique |
62 |
root |
1.1 |
|
63 |
root |
1.4 |
grep { is_prefix_of $_, @path } |
64 |
|
|
map @$_, |
65 |
|
|
$kw eq "*" ? @wiki{sort keys %wiki} |
66 |
root |
1.14 |
: $wiki{$kw} || () |
67 |
root |
1.1 |
} |
68 |
|
|
|
69 |
root |
1.4 |
sub full_path_of($) { |
70 |
|
|
my ($node) = @_; |
71 |
root |
1.1 |
|
72 |
root |
1.9 |
my @path; |
73 |
|
|
|
74 |
|
|
# skip toplevel hierarchy pod/, because its not a document |
75 |
root |
1.19 |
while ($node->[N_PARENT]) { |
76 |
root |
1.9 |
unshift @path, $node; |
77 |
root |
1.19 |
$node = $node->[N_PARENT]; |
78 |
root |
1.9 |
} |
79 |
|
|
|
80 |
|
|
@path |
81 |
|
|
} |
82 |
|
|
|
83 |
|
|
sub full_path($) { |
84 |
root |
1.19 |
join "/", map $_->[N_KW][0], &full_path_of |
85 |
root |
1.1 |
} |
86 |
|
|
|
87 |
root |
1.4 |
sub section_of($) { |
88 |
|
|
my ($node) = @_; |
89 |
root |
1.1 |
|
90 |
root |
1.19 |
my $doc = $node->[N_DOC]; |
91 |
|
|
my $par = $node->[N_PAR]; |
92 |
|
|
my $lvl = $node->[N_LEVEL]; |
93 |
root |
1.1 |
|
94 |
root |
1.4 |
my @res; |
95 |
root |
1.1 |
|
96 |
root |
1.4 |
do { |
97 |
|
|
my $p = $doc->[$par]; |
98 |
root |
1.1 |
|
99 |
root |
1.18 |
if (length $p->[P_MARKUP]) { |
100 |
root |
1.7 |
push @res, { |
101 |
root |
1.18 |
markup => $p->[P_MARKUP], |
102 |
|
|
indent => $p->[P_INDENT], |
103 |
root |
1.7 |
}; |
104 |
root |
1.4 |
} |
105 |
root |
1.18 |
} while $doc->[++$par][P_LEVEL] > $lvl; |
106 |
root |
1.1 |
|
107 |
root |
1.4 |
@res |
108 |
root |
1.1 |
} |
109 |
|
|
|
110 |
root |
1.4 |
sub section(@) { |
111 |
|
|
map section_of $_, &find |
112 |
root |
1.2 |
} |
113 |
|
|
|
114 |
root |
1.7 |
sub thaw_section(\@\%) { |
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; |
122 |
|
|
|
123 |
|
|
$_[1]{$type}($_, @arg) |
124 |
|
|
}ogex; |
125 |
|
|
} |
126 |
|
|
} |
127 |
|
|
|
128 |
root |
1.22 |
my %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 |
|
|
|
140 |
root |
1.7 |
my %as_label = ( |
141 |
root |
1.22 |
%as_common, |
142 |
root |
1.7 |
image => sub { |
143 |
|
|
my ($par, $path) = @_; |
144 |
|
|
|
145 |
|
|
"<small>img</small>" |
146 |
|
|
}, |
147 |
|
|
link => sub { |
148 |
root |
1.8 |
my ($par, $text, $link) = @_; |
149 |
root |
1.7 |
|
150 |
root |
1.17 |
"<span foreground='#ffff00'>↺</span><span foreground='#c0c0ff' underline='single'>" . (DC::asxml $text) . "</span>" |
151 |
root |
1.7 |
}, |
152 |
|
|
); |
153 |
|
|
|
154 |
root |
1.4 |
sub as_label(@) { |
155 |
root |
1.7 |
thaw_section @_, %as_label; |
156 |
|
|
|
157 |
root |
1.6 |
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 |
|
|
|
168 |
root |
1.7 |
my %as_paragraphs = ( |
169 |
root |
1.22 |
%as_common, |
170 |
root |
1.7 |
image => sub { |
171 |
root |
1.12 |
my ($par, $path, $flags) = @_; |
172 |
root |
1.7 |
|
173 |
root |
1.17 |
push @{ $par->{widget} }, new DC::UI::Image path => $path, |
174 |
root |
1.13 |
$flags & 1 ? (max_h => $::FONTSIZE) : (); |
175 |
root |
1.7 |
|
176 |
root |
1.8 |
"\x{fffc}" |
177 |
root |
1.7 |
}, |
178 |
|
|
link => sub { |
179 |
root |
1.8 |
my ($par, $text, $link) = @_; |
180 |
root |
1.7 |
|
181 |
root |
1.17 |
push @{ $par->{widget} }, new DC::UI::Label |
182 |
|
|
markup => "<span foreground='#ffff00'>↺</span><span foreground='#c0c0ff' underline='single'>" . (DC::asxml $text) . "</span>", |
183 |
root |
1.11 |
fontsize => 0.8, |
184 |
root |
1.8 |
can_hover => 1, |
185 |
|
|
can_events => 1, |
186 |
|
|
padding_x => 0, |
187 |
|
|
padding_y => 0, |
188 |
root |
1.17 |
tooltip => "Go to <i>" . (DC::asxml $link) . "</i>", |
189 |
root |
1.8 |
on_button_up => sub { |
190 |
root |
1.9 |
goto_document $link; |
191 |
root |
1.8 |
}; |
192 |
|
|
|
193 |
|
|
"\x{fffc}" |
194 |
root |
1.7 |
}, |
195 |
|
|
); |
196 |
|
|
|
197 |
|
|
sub as_paragraphs(@) { |
198 |
|
|
thaw_section @_, %as_paragraphs; |
199 |
|
|
|
200 |
|
|
@_ |
201 |
|
|
} |
202 |
|
|
|
203 |
|
|
sub section_paragraphs(@) { |
204 |
|
|
as_paragraphs §ion |
205 |
|
|
} |
206 |
|
|
|
207 |
root |
1.6 |
sub section_label(@) { |
208 |
|
|
as_label §ion |
209 |
root |
1.1 |
} |
210 |
|
|
|
211 |
root |
1.4 |
1 |