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