ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Pod.pm
Revision: 1.23
Committed: Sun Mar 30 13:02:26 2008 UTC (16 years, 1 month ago) by root
Branch: MAIN
CVS Tags: rel-2_03, rel-2_02, rel-2_05, rel-2_04, rel-2_0, rel-2_10, rel-0_9972, rel-0_9973, rel-0_9974, rel-0_9975, rel-0_9976, rel-0_9977, rel-0_9978, rel-0_9971, rel-1_21
Changes since 1.22: +10 -11 lines
Log Message:
*** empty log message ***

File Contents

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