ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Pod.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/Pod.pm (file contents):
Revision 1.5 by root, Sun Aug 13 03:20:56 2006 UTC vs.
Revision 1.23 by root, Sun Mar 30 13:02:26 2008 UTC

1package CFPlus::Pod; 1package DC::Pod;
2 2
3use strict; 3use strict;
4use utf8;
4 5
5use Storable; 6use Storable;
6 7
7our $VERSION = 1; 8our $VERSION = 1.03;
8 9
10our $goto_document = sub { };
9our %wiki; 11our %wiki;
10 12
11*wiki = Storable::retrieve CFPlus::find_rcfile "docwiki.pst"; 13my $MA_BEG = "\x{fcd0}";
14my $MA_SEP = "\x{fcd1}";
15my $MA_END = "\x{fcd2}";
16
17# nodes (order must stay as it is)
18sub N_PARENT (){ 0 }
19sub N_PAR (){ 1 }
20sub N_LEVEL (){ 2 }
21sub N_KW (){ 3 }
22sub N_DOC (){ 4 }
23
24# paragraphs (order must stay as it is)
25sub P_INDENT (){ 0 }
26sub P_LEVEL (){ 1 }
27sub P_MARKUP (){ 2 }
28sub P_INDEX (){ 3 }
29
30our %wiki;
31
32sub load_docwiki {
33 *wiki = Storable::retrieve $_[0];
34}
35
36sub goto_document($) {
37 $goto_document->(split /\//, $_[0]);
38}
12 39
13sub is_prefix_of($@) { 40sub 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 = pop @path;
19 46
20 $node = $node->{parent} 47 $node = $node->[N_PARENT]
21 or return 0; 48 or return 0;
22 49
23 return ! ! grep $_ eq $kw, @{ $node->{kw} }; 50 return scalar grep $_ eq $kw, @{ $node->[N_KW] };
24} 51}
25 52
26sub find(@) { 53sub find(@) {
27 my (@path) = @_; 54 my (@path) = @_;
28 55
29 return unless @path; 56 return unless @path;
30 57
31 my $kw = pop @path; 58 my $kw = pop @path;
32 59
33 # TODO: make sure results are unique 60 my %res = map +($_, $_),
34
35 grep { is_prefix_of $_, @path } 61 grep { is_prefix_of $_, @path }
36 map @$_, 62 map @$_,
37 $kw eq "*" ? @wiki{sort keys %wiki} 63 $kw eq "*" ? values %wiki
38 : grep $_, $wiki{$kw} 64 : $wiki{$kw} || ();
65
66 values %res
39} 67}
40 68
41sub full_path_of($) { 69sub 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 while ($node) {
75 unshift @path, $node;
76 $node = $node->[N_PARENT];
77 }
78
46 $path 79 @path
80}
81
82sub full_path($) {
83 join "/", map $_->[N_KW][0], &full_path_of
47} 84}
48 85
49sub section_of($) { 86sub section_of($) {
50 my ($node) = @_; 87 my ($node) = @_;
51 88
52 my $doc = $node->{doc}; 89 my $doc = $node->[N_DOC];
53 my $par = $node->{par}; 90 my $par = $node->[N_PAR];
54 my $lvl = $node->{level}; 91 my $lvl = $node->[N_LEVEL];
55 92
56 my @res; 93 my @res;
57 94
58 do { 95 do {
59 my $p = $doc->[$par]; 96 my $p = $doc->[$par];
60 97
61 if (exists $p->{markup}) { 98 if (length $p->[P_MARKUP]) {
62 my %para = ( 99 push @res, {
63 markup => $p->{markup}, 100 markup => $p->[P_MARKUP],
64 indent => $p->{indent}, 101 indent => $p->[P_INDENT],
65 );
66
67 for (@{ $p->{widget} || [] }) {
68 my ($class, @args) = @$_;
69 push @{ $para{widget} }, $class->new (@args);
70 } 102 };
71
72 push @res, \%para;
73 } 103 }
74 } while $doc->[++$par]{level} > $lvl; 104 } while $doc->[++$par][P_LEVEL] > $lvl;
75 105
76 @res 106 @res
77} 107}
78 108
79sub section(@) { 109sub section(@) {
80 map section_of $_, &find 110 map section_of $_, &find
81} 111}
82 112
113sub 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
127my %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
139my %as_label = (
140 %as_common,
141 image => sub {
142 my ($par, $path) = @_;
143
144 "<small>img</small>"
145 },
146 link => sub {
147 my ($par, $text, $link) = @_;
148
149 "<span foreground='#ffff00'>↺</span><span foreground='#c0c0ff' underline='single'>" . (DC::asxml $text) . "</span>"
150 },
151);
152
83sub as_label(@) { 153sub as_label(@) {
154 thaw_section @_, %as_label;
155
156 my $text =
84 join "\n", 157 join "\n",
85 map +("\xa0" x ($_->{indent} / 4)) . $_->{markup}, 158 map +("\xa0" x ($_->{indent} / 4)) . $_->{markup},
86 @_ 159 @_;
160
161 $text =~ s/^\s+//;
162 $text =~ s/\s+$//;
163
164 $text
165}
166
167my %as_paragraphs = (
168 %as_common,
169 image => sub {
170 my ($par, $path, $flags) = @_;
171
172 push @{ $par->{widget} }, new DC::UI::Image path => $path,
173 $flags & 1 ? (max_h => $::FONTSIZE) : ();
174
175 "\x{fffc}"
176 },
177 link => sub {
178 my ($par, $text, $link) = @_;
179
180 push @{ $par->{widget} }, new DC::UI::Label
181 markup => "<span foreground='#ffff00'>↺</span><span foreground='#c0c0ff' underline='single'>" . (DC::asxml $text) . "</span>",
182 fontsize => 0.8,
183 can_hover => 1,
184 can_events => 1,
185 padding_x => 0,
186 padding_y => 0,
187 tooltip => "Go to <i>" . (DC::asxml $link) . "</i>",
188 on_button_up => sub {
189 goto_document $link;
190 };
191
192 "\x{fffc}"
193 },
194);
195
196sub as_paragraphs(@) {
197 thaw_section @_, %as_paragraphs;
198
199 @_
200}
201
202sub section_paragraphs(@) {
203 as_paragraphs &section
204}
205
206sub section_label(@) {
207 as_label &section
87} 208}
88 209
891 2101

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines