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