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.17 |
*wiki = Storable::retrieve DC::find_rcfile "docwiki.pst"; |
31 |
root |
1.1 |
|
32 |
root |
1.9 |
sub goto_document($) { |
33 |
|
|
$goto_document->(split /\//, $_[0]); |
34 |
|
|
} |
35 |
|
|
|
36 |
root |
1.4 |
sub is_prefix_of($@) { |
37 |
|
|
my ($node, @path) = @_; |
38 |
root |
1.1 |
|
39 |
root |
1.4 |
return 1 unless @path; |
40 |
root |
1.1 |
|
41 |
root |
1.10 |
my $kw = lc pop @path; |
42 |
root |
1.1 |
|
43 |
root |
1.19 |
$node = $node->[N_PARENT] |
44 |
root |
1.4 |
or return 0; |
45 |
root |
1.2 |
|
46 |
root |
1.19 |
return scalar grep $_ eq $kw, @{ $node->[N_KW] }; |
47 |
root |
1.2 |
} |
48 |
|
|
|
49 |
root |
1.4 |
sub find(@) { |
50 |
|
|
my (@path) = @_; |
51 |
root |
1.1 |
|
52 |
root |
1.4 |
return unless @path; |
53 |
root |
1.1 |
|
54 |
root |
1.10 |
my $kw = lc pop @path; |
55 |
root |
1.1 |
|
56 |
root |
1.4 |
# TODO: make sure results are unique |
57 |
root |
1.1 |
|
58 |
root |
1.4 |
grep { is_prefix_of $_, @path } |
59 |
|
|
map @$_, |
60 |
|
|
$kw eq "*" ? @wiki{sort keys %wiki} |
61 |
root |
1.14 |
: $wiki{$kw} || () |
62 |
root |
1.1 |
} |
63 |
|
|
|
64 |
root |
1.4 |
sub full_path_of($) { |
65 |
|
|
my ($node) = @_; |
66 |
root |
1.1 |
|
67 |
root |
1.9 |
my @path; |
68 |
|
|
|
69 |
|
|
# skip toplevel hierarchy pod/, because its not a document |
70 |
root |
1.19 |
while ($node->[N_PARENT]) { |
71 |
root |
1.9 |
unshift @path, $node; |
72 |
root |
1.19 |
$node = $node->[N_PARENT]; |
73 |
root |
1.9 |
} |
74 |
|
|
|
75 |
|
|
@path |
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
sub full_path($) { |
79 |
root |
1.19 |
join "/", map $_->[N_KW][0], &full_path_of |
80 |
root |
1.1 |
} |
81 |
|
|
|
82 |
root |
1.4 |
sub section_of($) { |
83 |
|
|
my ($node) = @_; |
84 |
root |
1.1 |
|
85 |
root |
1.19 |
my $doc = $node->[N_DOC]; |
86 |
|
|
my $par = $node->[N_PAR]; |
87 |
|
|
my $lvl = $node->[N_LEVEL]; |
88 |
root |
1.1 |
|
89 |
root |
1.4 |
my @res; |
90 |
root |
1.1 |
|
91 |
root |
1.4 |
do { |
92 |
|
|
my $p = $doc->[$par]; |
93 |
root |
1.1 |
|
94 |
root |
1.18 |
if (length $p->[P_MARKUP]) { |
95 |
root |
1.7 |
push @res, { |
96 |
root |
1.18 |
markup => $p->[P_MARKUP], |
97 |
|
|
indent => $p->[P_INDENT], |
98 |
root |
1.7 |
}; |
99 |
root |
1.4 |
} |
100 |
root |
1.18 |
} while $doc->[++$par][P_LEVEL] > $lvl; |
101 |
root |
1.1 |
|
102 |
root |
1.4 |
@res |
103 |
root |
1.1 |
} |
104 |
|
|
|
105 |
root |
1.4 |
sub section(@) { |
106 |
|
|
map section_of $_, &find |
107 |
root |
1.2 |
} |
108 |
|
|
|
109 |
root |
1.7 |
sub thaw_section(\@\%) { |
110 |
|
|
for (@{$_[0]}) { |
111 |
|
|
$_->{markup} =~ s{ |
112 |
|
|
$MA_BEG |
113 |
|
|
([^$MA_END]+) |
114 |
|
|
$MA_END |
115 |
|
|
}{ |
116 |
|
|
my ($type, @arg) = split /$MA_SEP/o, $1; |
117 |
|
|
|
118 |
|
|
$_[1]{$type}($_, @arg) |
119 |
|
|
}ogex; |
120 |
|
|
} |
121 |
|
|
} |
122 |
|
|
|
123 |
|
|
my %as_label = ( |
124 |
|
|
image => sub { |
125 |
|
|
my ($par, $path) = @_; |
126 |
|
|
|
127 |
|
|
"<small>img</small>" |
128 |
|
|
}, |
129 |
|
|
link => sub { |
130 |
root |
1.8 |
my ($par, $text, $link) = @_; |
131 |
root |
1.7 |
|
132 |
root |
1.17 |
"<span foreground='#ffff00'>↺</span><span foreground='#c0c0ff' underline='single'>" . (DC::asxml $text) . "</span>" |
133 |
root |
1.7 |
}, |
134 |
|
|
); |
135 |
|
|
|
136 |
root |
1.4 |
sub as_label(@) { |
137 |
root |
1.7 |
thaw_section @_, %as_label; |
138 |
|
|
|
139 |
root |
1.6 |
my $text = |
140 |
|
|
join "\n", |
141 |
|
|
map +("\xa0" x ($_->{indent} / 4)) . $_->{markup}, |
142 |
|
|
@_; |
143 |
|
|
|
144 |
|
|
$text =~ s/^\s+//; |
145 |
|
|
$text =~ s/\s+$//; |
146 |
|
|
|
147 |
|
|
$text |
148 |
|
|
} |
149 |
|
|
|
150 |
root |
1.7 |
my %as_paragraphs = ( |
151 |
|
|
image => sub { |
152 |
root |
1.12 |
my ($par, $path, $flags) = @_; |
153 |
root |
1.7 |
|
154 |
root |
1.17 |
push @{ $par->{widget} }, new DC::UI::Image path => $path, |
155 |
root |
1.13 |
$flags & 1 ? (max_h => $::FONTSIZE) : (); |
156 |
root |
1.7 |
|
157 |
root |
1.8 |
"\x{fffc}" |
158 |
root |
1.7 |
}, |
159 |
|
|
link => sub { |
160 |
root |
1.8 |
my ($par, $text, $link) = @_; |
161 |
root |
1.7 |
|
162 |
root |
1.17 |
push @{ $par->{widget} }, new DC::UI::Label |
163 |
|
|
markup => "<span foreground='#ffff00'>↺</span><span foreground='#c0c0ff' underline='single'>" . (DC::asxml $text) . "</span>", |
164 |
root |
1.11 |
fontsize => 0.8, |
165 |
root |
1.8 |
can_hover => 1, |
166 |
|
|
can_events => 1, |
167 |
|
|
padding_x => 0, |
168 |
|
|
padding_y => 0, |
169 |
root |
1.17 |
tooltip => "Go to <i>" . (DC::asxml $link) . "</i>", |
170 |
root |
1.8 |
on_button_up => sub { |
171 |
root |
1.9 |
goto_document $link; |
172 |
root |
1.8 |
}; |
173 |
|
|
|
174 |
|
|
"\x{fffc}" |
175 |
root |
1.7 |
}, |
176 |
|
|
); |
177 |
|
|
|
178 |
|
|
sub as_paragraphs(@) { |
179 |
|
|
thaw_section @_, %as_paragraphs; |
180 |
|
|
|
181 |
|
|
@_ |
182 |
|
|
} |
183 |
|
|
|
184 |
|
|
sub section_paragraphs(@) { |
185 |
|
|
as_paragraphs §ion |
186 |
|
|
} |
187 |
|
|
|
188 |
root |
1.6 |
sub section_label(@) { |
189 |
|
|
as_label §ion |
190 |
root |
1.1 |
} |
191 |
|
|
|
192 |
root |
1.4 |
1 |