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