/[pub]/suikawiki/script/lib/SuikaWiki/Plugin/WikiResource.wps
Suika

Contents of /suikawiki/script/lib/SuikaWiki/Plugin/WikiResource.wps

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.7 - (show annotations) (download)
Fri Jan 16 07:52:29 2004 UTC (21 years, 3 months ago) by wakaba
Branch: MAIN
CVS Tags: HEAD
Changes since 1.6: +0 -0 lines
FILE REMOVED
Some obsoleted files removed

1 Name:
2 WikiResource
3 FullName:
4 WikiResource interface
5 URI:
6 IW:SuikaWiki:"Wiki//Resource"
7 Description:
8 This module provides "WikiResource" support. A resource is a record data
9 in the resource database. Using resource database, human readable text
10 is easily customizable and multilingualizationable (With conneg,
11 user preferred language can be automatically selected).
12 Require:
13 SuikaWiki::Plugin::WikiConst main
14 Initialize:
15 my $HAS_XML = SuikaWiki::Plugin->feature ('SuikaWiki::Markup::XML');
16 my $NS_XHTML1 = 'http://www.w3.org/1999/xhtml';
17
18 {
19 Name:
20 wikiform_input/res
21 FullName:
22 Get text from resource
23 Format:
24 $r = $o->resource ($p->{name},escape=>1);
25 }
26 {
27 Name:
28 wikiview/res
29 wikiview-resource/res
30 wikipage_list_item/res
31 wikipage-link/res
32 wikiwe--edit/res
33 FullName:
34 Get text from resource
35 Format:
36 $r = $o->formatter('view-resource')->replace ($o->resource ($p->{name}), $o);
37 $r = SuikaWiki::Markup::XML->new (type => '#text', value => $r) unless ref $r;
38 }
39
40 {
41 Name:
42 wikiview-resource/-bare_text
43 FullName:
44 HTML escape for bare text
45 Format:
46 if ($HAS_XML) {
47 $r = SuikaWiki::Markup::XML->new (type => '#text', value => $p->{-bare_text});
48 } else {
49 $r = $o->escape ($p->{-bare_text});
50 }
51 }
52 {
53 Name:
54 wikiview-resource/span
55 wikipage-link/span
56 FullName:
57 Give class name
58 Format:
59 $r = SuikaWiki::Markup::XML->new (namespace_uri => $NS_XHTML1, local_name => 'span');
60 $r->set_attribute (class => $p->{class});
61 $r->append_node ($p->{content}, node_or_text => 1);
62 }
63
64 {
65 Name:
66 wikiview-resource/text
67 FullName:
68 Give class name
69 Format:
70 $r = SuikaWiki::Markup::XML->new (type => '#text', value => $p->{cdata});
71 }
72
73 MODULE:
74 my %_Resource;
75 my $default_ns = $main::PageName{ResourceNS};
76 sub get_resource ($$;%) {
77 my (undef, $s, %o) = @_;
78 $o{ns} ||= $default_ns;
79 unless (defined $_Resource{$o{ns}}->{$s}) {
80 $_Resource{'.//option'}->{$o{ns}}->{resource_ns} = $o{ns};
81 $_Resource{$o{ns}}->{$s} = wiki::resource::get ($s, $_Resource{'.//option'}->{$o{ns}});
82 }
83 return $_Resource{$o{ns}}->{$s};
84 }
85 ## TODO: implement inherit default_ns ??
86 sub SuikaWiki::Plugin::resource ($$;%) {
87 my (undef, $s, %o) = @_;
88 my $s = __PACKAGE__->get_resource ($s, %o);
89 $o{escape} ? SuikaWiki::Plugin->escape ($s) : $s;
90 }
91
92 sub main::Resource ($;%) {
93 my ($s, %o) = @_;
94 unless (defined $_Resource{$default_ns}->{$s}) {
95 $_Resource{$default_ns}->{$_[0]} = &wiki::resource::get ($s, $_Resource{'.//option'}->{$default_ns});
96 }
97 $o{escape} ? SuikaWiki::Plugin->escape ($_Resource{$default_ns}->{$s}) : $_Resource{$default_ns}->{$s};
98 }
99 sub wiki::resource::get ($;$) {
100 my ($resname, $option) = @_;
101 $option->{accept_language} ||= &wiki::conneg::get_accept_lang ();
102 $option->{resource} ||= {};
103 $option->{resource_ns} ||= $default_ns;
104 my $v;
105 for my $lang (sort {$option->{accept_language}->{$b} <=> $option->{accept_language}->{$a}} grep {$option->{accept_language}->{$_}!=0} keys %{$option->{accept_language}}) {
106 while (length $lang) {
107 unless ($option->{accept_language}->{defined $option->{accept_language}->{$lang} ? $lang : '*'} == 0) {
108 $option->{resource}->{$lang} ||= SuikaWiki::Plugin::WikiConst::to_hash ($main::database{$option->{resource_ns}.$lang});
109 $v = $option->{resource}->{$lang}->{$resname};
110 last if defined $v;
111 if (defined $SuikaWiki::Plugin::Resource::BaseResource->{$lang}->{''}->{$resname}) {
112 $v = $SuikaWiki::Plugin::Resource::BaseResource->{$lang}->{''}->{$resname};
113 last;
114 }
115 }
116 $lang =~ s/[^+-]*$//; $lang =~ s/[+-]$//;
117 }
118 last if defined $v;
119 }
120 if (defined $v) {
121 return $v;
122 } elsif (defined $SuikaWiki::Plugin::Resource::BaseResource->{und}->{''}->{$resname}) {
123 return $SuikaWiki::Plugin::Resource::BaseResource->{und}->{''}->{$resname};
124 } else {
125 return $resname;
126 }
127 }
128
129 package wiki::conneg;
130 ## BUG: this parser isn't strict.
131 sub get_accept_lang (;$) {
132 my $alang = shift || $main::ENV{HTTP_ACCEPT_LANGUAGE};
133 my %alang = (ja => 0.0002, en => 0.0001);
134 if ($main::UA =~ m#Mozilla/0\.#) {
135 $alang{ja} = 0.00001;
136 }
137 my $i = 0.1;
138 for (split /\s*,\s*/, $alang) {
139 tr/\x09\x0A\x0D\x20//d;
140 if (/((?:(?!;q=).)+)(?:;q="?([0-9.]+)"?)?/) {
141 my $l = lc $1; $l =~ tr/\x22\x5C//d;
142 $alang{$l} = (defined $2 ? $2 : 1.000)*1000;
143 $alang{$l} += $i unless $alang{$l} == 0;
144 $i -= 0.001;
145 }
146 }
147 \%alang;
148 }
149
150 POD:TO DO:
151 - Refine wiki::* functions.
152
153 - Separate conneg functions.
154
155 - Persistent caching of retrived resource.
156
157 POD:LICENSE:
158 Copyright 2002-2003 Wakaba <w@suika.fam.cx>
159
160 %%GNUGPL2%%

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24