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