1 | package CFPlus::Pod; |
1 | package DC::Pod; |
2 | |
2 | |
3 | use strict; |
3 | use common::sense; |
4 | |
4 | |
5 | use Storable; |
5 | use Storable; |
6 | |
6 | |
7 | our $VERSION = 1; |
7 | our $VERSION = 1.03; |
8 | |
8 | |
|
|
9 | our $goto_document = sub { }; |
9 | our %wiki; |
10 | our %wiki; |
10 | |
11 | |
11 | my $MA_BEG = "\x{fcd0}"; |
12 | my $MA_BEG = "\x{fcd0}"; |
12 | my $MA_SEP = "\x{fcd1}"; |
13 | my $MA_SEP = "\x{fcd1}"; |
13 | my $MA_END = "\x{fcd2}"; |
14 | my $MA_END = "\x{fcd2}"; |
14 | |
15 | |
15 | *wiki = Storable::retrieve CFPlus::find_rcfile "docwiki.pst"; |
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 | } |
16 | |
38 | |
17 | sub is_prefix_of($@) { |
39 | sub is_prefix_of($@) { |
18 | my ($node, @path) = @_; |
40 | my ($node, @path) = @_; |
19 | |
41 | |
20 | return 1 unless @path; |
42 | return 1 unless @path; |
21 | |
43 | |
22 | my $kw = pop @path; |
44 | my $kw = pop @path; |
23 | |
45 | |
24 | $node = $node->{parent} |
46 | $node = $node->[N_PARENT] |
25 | or return 0; |
47 | or return 0; |
26 | |
48 | |
27 | return ! ! grep $_ eq $kw, @{ $node->{kw} }; |
49 | return scalar grep $_ eq $kw, @{ $node->[N_KW] }; |
28 | } |
50 | } |
29 | |
51 | |
30 | sub find(@) { |
52 | sub find(@) { |
31 | my (@path) = @_; |
53 | my (@path) = @_; |
32 | |
54 | |
33 | return unless @path; |
55 | return unless @path; |
34 | |
56 | |
35 | my $kw = pop @path; |
57 | my $kw = pop @path; |
36 | |
58 | |
37 | # TODO: make sure results are unique |
59 | my %res = map +($_, $_), |
38 | |
|
|
39 | grep { is_prefix_of $_, @path } |
60 | grep { is_prefix_of $_, @path } |
40 | map @$_, |
61 | map @$_, |
41 | $kw eq "*" ? @wiki{sort keys %wiki} |
62 | $kw eq "*" ? values %wiki |
42 | : grep $_, $wiki{$kw} |
63 | : $wiki{$kw} || (); |
|
|
64 | |
|
|
65 | values %res |
43 | } |
66 | } |
44 | |
67 | |
45 | sub full_path_of($) { |
68 | sub full_path_of($) { |
46 | my ($node) = @_; |
69 | my ($node) = @_; |
47 | |
70 | |
48 | my $path = $node->{kw}[0]; |
71 | my @path; |
49 | $path = "$node->{kw}[0]/$path" while $node = $node->{parent}; |
72 | |
|
|
73 | while ($node) { |
|
|
74 | unshift @path, $node; |
|
|
75 | $node = $node->[N_PARENT]; |
|
|
76 | } |
|
|
77 | |
50 | $path |
78 | @path |
|
|
79 | } |
|
|
80 | |
|
|
81 | sub full_path($) { |
|
|
82 | join "/", map $_->[N_KW][0], &full_path_of |
51 | } |
83 | } |
52 | |
84 | |
53 | sub section_of($) { |
85 | sub section_of($) { |
54 | my ($node) = @_; |
86 | my ($node) = @_; |
55 | |
87 | |
56 | my $doc = $node->{doc}; |
88 | my $doc = $node->[N_DOC]; |
57 | my $par = $node->{par}; |
89 | my $par = $node->[N_PAR]; |
58 | my $lvl = $node->{level}; |
90 | my $lvl = $node->[N_LEVEL]; |
59 | |
91 | |
60 | my @res; |
92 | my @res; |
61 | |
93 | |
62 | do { |
94 | do { |
63 | my $p = $doc->[$par]; |
95 | my $p = $doc->[$par]; |
64 | |
96 | |
65 | if (length $p->{markup}) { |
97 | if (length $p->[P_MARKUP]) { |
66 | push @res, { |
98 | push @res, { |
67 | markup => $p->{markup}, |
99 | markup => $p->[P_MARKUP], |
68 | indent => $p->{indent}, |
100 | indent => $p->[P_INDENT], |
69 | }; |
101 | }; |
70 | } |
102 | } |
71 | } while $doc->[++$par]{level} > $lvl; |
103 | } while $doc->[++$par][P_LEVEL] > $lvl; |
72 | |
104 | |
73 | @res |
105 | @res |
74 | } |
106 | } |
75 | |
107 | |
76 | sub section(@) { |
108 | sub section(@) { |
… | |
… | |
89 | $_[1]{$type}($_, @arg) |
121 | $_[1]{$type}($_, @arg) |
90 | }ogex; |
122 | }ogex; |
91 | } |
123 | } |
92 | } |
124 | } |
93 | |
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 | |
94 | my %as_label = ( |
138 | my %as_label = ( |
|
|
139 | %as_common, |
95 | image => sub { |
140 | image => sub { |
96 | my ($par, $path) = @_; |
141 | my ($par, $path) = @_; |
97 | |
142 | |
98 | "<small>img</small>" |
143 | "<small>img</small>" |
99 | }, |
144 | }, |
100 | link => sub { |
145 | link => sub { |
101 | my ($par, $link) = @_; |
146 | my ($par, $text, $link) = @_; |
102 | |
147 | |
103 | "<big>" . (CFPlus::asxml $link) . "</big>" |
148 | "<span foreground='#ffff00'>↺</span><span foreground='#c0c0ff' underline='single'>" . (DC::asxml $text) . "</span>" |
104 | }, |
149 | }, |
105 | ); |
150 | ); |
106 | |
151 | |
107 | sub as_label(@) { |
152 | sub as_label(@) { |
108 | thaw_section @_, %as_label; |
153 | thaw_section @_, %as_label; |
… | |
… | |
117 | |
162 | |
118 | $text |
163 | $text |
119 | } |
164 | } |
120 | |
165 | |
121 | my %as_paragraphs = ( |
166 | my %as_paragraphs = ( |
|
|
167 | %as_common, |
122 | image => sub { |
168 | image => sub { |
123 | my ($par, $path) = @_; |
169 | my ($par, $path, $flags) = @_; |
124 | |
170 | |
125 | push @{ $par->{widget} }, new CFPlus::UI::Image path => $path; |
171 | push @{ $par->{widget} }, new DC::UI::Image path => $path, |
|
|
172 | $flags & 1 ? (max_h => $::FONTSIZE) : (); |
126 | |
173 | |
127 | "\x{FFFC}" |
174 | "\x{fffc}" |
128 | }, |
175 | }, |
129 | link => sub { |
176 | link => sub { |
130 | my ($par, $link) = @_; |
177 | my ($par, $text, $link) = @_; |
131 | |
178 | |
132 | "<big>" . (CFPlus::asxml $link) . "</big>" |
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}" |
133 | }, |
192 | }, |
134 | ); |
193 | ); |
135 | |
194 | |
136 | sub as_paragraphs(@) { |
195 | sub as_paragraphs(@) { |
137 | thaw_section @_, %as_paragraphs; |
196 | thaw_section @_, %as_paragraphs; |