ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Pod.pm
Revision: 1.24
Committed: Sat Apr 3 02:58:25 2010 UTC (14 years, 1 month ago) by root
Branch: MAIN
CVS Tags: rel-2_11, HEAD
Changes since 1.23: +1 -2 lines
Log Message:
*** empty log message ***

File Contents

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