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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines