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