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.4 by root, Sun Aug 13 02:43:22 2006 UTC vs.
Revision 1.24 by root, Sat Apr 3 02:58:25 2010 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines