1 |
package DC::Pod; |
2 |
|
3 |
use strict; |
4 |
use utf8; |
5 |
|
6 |
use Storable; |
7 |
|
8 |
our $VERSION = 1.03; |
9 |
|
10 |
our $goto_document = sub { }; |
11 |
our %wiki; |
12 |
|
13 |
my $MA_BEG = "\x{fcd0}"; |
14 |
my $MA_SEP = "\x{fcd1}"; |
15 |
my $MA_END = "\x{fcd2}"; |
16 |
|
17 |
# 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 |
our %wiki; |
31 |
|
32 |
sub load_docwiki { |
33 |
*wiki = Storable::retrieve $_[0]; |
34 |
} |
35 |
|
36 |
sub goto_document($) { |
37 |
$goto_document->(split /\//, $_[0]); |
38 |
} |
39 |
|
40 |
sub is_prefix_of($@) { |
41 |
my ($node, @path) = @_; |
42 |
|
43 |
return 1 unless @path; |
44 |
|
45 |
my $kw = lc pop @path; |
46 |
|
47 |
$node = $node->[N_PARENT] |
48 |
or return 0; |
49 |
|
50 |
#TODO: maybe get rid of lowercasing? |
51 |
return scalar grep lc eq $kw, @{ $node->[N_KW] }; |
52 |
} |
53 |
|
54 |
sub find(@) { |
55 |
my (@path) = @_; |
56 |
|
57 |
return unless @path; |
58 |
|
59 |
my $kw = lc pop @path; |
60 |
|
61 |
# TODO: make sure results are unique |
62 |
|
63 |
grep { is_prefix_of $_, @path } |
64 |
map @$_, |
65 |
$kw eq "*" ? @wiki{sort keys %wiki} |
66 |
: $wiki{$kw} || () |
67 |
} |
68 |
|
69 |
sub full_path_of($) { |
70 |
my ($node) = @_; |
71 |
|
72 |
my @path; |
73 |
|
74 |
# skip toplevel hierarchy pod/, because its not a document |
75 |
while ($node->[N_PARENT]) { |
76 |
unshift @path, $node; |
77 |
$node = $node->[N_PARENT]; |
78 |
} |
79 |
|
80 |
@path |
81 |
} |
82 |
|
83 |
sub full_path($) { |
84 |
join "/", map $_->[N_KW][0], &full_path_of |
85 |
} |
86 |
|
87 |
sub 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 |
|
110 |
sub section(@) { |
111 |
map section_of $_, &find |
112 |
} |
113 |
|
114 |
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 |
my %as_label = ( |
129 |
image => sub { |
130 |
my ($par, $path) = @_; |
131 |
|
132 |
"<small>img</small>" |
133 |
}, |
134 |
link => sub { |
135 |
my ($par, $text, $link) = @_; |
136 |
|
137 |
"<span foreground='#ffff00'>↺</span><span foreground='#c0c0ff' underline='single'>" . (DC::asxml $text) . "</span>" |
138 |
}, |
139 |
); |
140 |
|
141 |
sub as_label(@) { |
142 |
thaw_section @_, %as_label; |
143 |
|
144 |
my $text = |
145 |
join "\n", |
146 |
map +("\xa0" x ($_->{indent} / 4)) . $_->{markup}, |
147 |
@_; |
148 |
|
149 |
$text =~ s/^\s+//; |
150 |
$text =~ s/\s+$//; |
151 |
|
152 |
$text |
153 |
} |
154 |
|
155 |
my %as_paragraphs = ( |
156 |
image => sub { |
157 |
my ($par, $path, $flags) = @_; |
158 |
|
159 |
push @{ $par->{widget} }, new DC::UI::Image path => $path, |
160 |
$flags & 1 ? (max_h => $::FONTSIZE) : (); |
161 |
|
162 |
"\x{fffc}" |
163 |
}, |
164 |
link => sub { |
165 |
my ($par, $text, $link) = @_; |
166 |
|
167 |
push @{ $par->{widget} }, new DC::UI::Label |
168 |
markup => "<span foreground='#ffff00'>↺</span><span foreground='#c0c0ff' underline='single'>" . (DC::asxml $text) . "</span>", |
169 |
fontsize => 0.8, |
170 |
can_hover => 1, |
171 |
can_events => 1, |
172 |
padding_x => 0, |
173 |
padding_y => 0, |
174 |
tooltip => "Go to <i>" . (DC::asxml $link) . "</i>", |
175 |
on_button_up => sub { |
176 |
goto_document $link; |
177 |
}; |
178 |
|
179 |
"\x{fffc}" |
180 |
}, |
181 |
); |
182 |
|
183 |
sub as_paragraphs(@) { |
184 |
thaw_section @_, %as_paragraphs; |
185 |
|
186 |
@_ |
187 |
} |
188 |
|
189 |
sub section_paragraphs(@) { |
190 |
as_paragraphs §ion |
191 |
} |
192 |
|
193 |
sub section_label(@) { |
194 |
as_label §ion |
195 |
} |
196 |
|
197 |
1 |