root/lang/perl/blosxom/plugins/rwtt2

Revision 77, 3.9 kB (checked in by cho45, 4 years ago)

lang/perl/blosxom,
lang/perl/blosxom/plugins:

Added for blosxom plugins.

Line 
1# Blosxom Plugin: rwtt2
2# Author(s): cho45 <cho45@lowreal.net>
3# Version: 2006-09-10
4# Documentation: See the bottom of this file or type: perldoc rwtt2
5# vim:set ft=perl:
6
7package rwtt2;
8
9use strict;
10
11# --- Configurable variables -----------
12
13our $timezone = "Asia/Tokyo";
14
15our $config = {
16        EVAL_PERL  => 1,
17        POST_CHOMP => 1,
18};
19
20# --- Plug-in package variables --------
21
22# --------------------------------------
23
24use Template;
25use Template::Parser;
26use Template::Context;
27use Template::Document;
28use DateTime;
29use IO::File;
30
31use Data::Dumper;
32
33our ($vars, $story, $date, $debug) = ({}, {}, {}, 0);
34
35our ($template, $params, $doc, $content_type);
36
37sub start {
38        print "content-type: text/plain\n\n" if $debug;
39
40        $vars->{entries} = [];
41
42        $template     = load_template("$blosxom::datadir/template.tt.$blosxom::flavour");
43        $content_type = load_template("$blosxom::datadir/content-type.$blosxom::flavour");
44
45        # doc は後で使う
46        ($params, $doc) = parse($template);
47
48        return 1;
49}
50
51
52sub template {
53        return sub {
54                return '';
55        };
56}
57
58sub filter {
59        my ($pkg, $files_ref, $others_ref) = @_;
60
61        return 1;
62}
63
64sub skip {
65        return 0;
66}
67
68sub interpolate {
69        return sub {
70                return '';
71        };
72}
73
74sub head {
75        my ($pkg, $path, $head_ref) = @_;
76
77        return 1;
78}
79
80sub date {
81        my ($pkg, $path, $date_ref, $mtime, $dw, $mo, $mo_num, $da, $ti, $yr) = @_;
82
83        $date = DateTime->from_epoch(epoch => $mtime);
84        $date->set_time_zone( $timezone );
85        return 1;
86}
87
88sub story {
89        my ($pkg, $path, $fn, $story_ref, $title_ref, $body_ref) = @_;
90
91        print Dumper ["story", $path, $fn] if $debug;
92        push @{ $vars->{entries} }, {
93                path   => $path,
94                fn     => $fn,
95                title  => $$title_ref,
96                body   => $$body_ref,
97                date   => $date,
98                plugin => get_current_variables(),
99        };
100
101        return 1;
102}
103
104sub foot {
105        my ($pkg, $path, $foot_ref) = @_;
106
107        return 1;
108}
109
110sub last {
111        $vars->{b} = get_current_variables();
112        print Dumper $vars if $debug;
113
114        my $output = '';
115
116        $config->{VARIABLES} = $vars,
117        my $c = Template::Context->new($config) || die $Template::Context::ERROR;;
118       
119        $doc = Template::Document->new($doc) || die $Template::Document::ERROR;
120        eval {
121                $output = $doc->process($c);
122        }; if ($@) {
123                $output =  join "\n", "Content-Type: text/plain\n", $@, $template;
124        }
125       
126
127
128        $blosxom::header->{-type} = $content_type;
129        $blosxom::output = $output;
130
131        return 1;
132}
133
134sub load_template {
135        my $fn = shift;
136        my $ret;
137        my $fh = IO::File->new($fn, "r");
138        if (defined $fh) {
139                $ret = join "", $fh->getlines;
140                $fh->close;
141        } else {
142                die "template not found: $fn";
143        }
144        return $ret;
145}
146
147sub parse {
148        my ($text, $config) = @_;
149        my $parser = Template::Parser->new($config);
150
151        my $doc    = $parser->parse($text);
152        my $code   = $doc->{BLOCK};
153        my @params;
154
155        while ($code =~ /\$stash->get\( ( \[ [^)]+ \] ) \)/gx) {
156                print Dumper $1 if $debug;
157                my $a = eval($1);
158                my $i = 0;
159                for (@{ $a }) {
160                        push @params, $_ unless $i % 2;
161                        $i++;
162                }
163        }
164
165        my %seen;
166        my @params = grep !$seen{$_}++, @params;
167
168        print Dumper \@params if $debug;
169
170        return (\@params, $doc);
171}
172
173
174sub get_current_variables {
175        my $data = {};
176        for my $plugin (@blosxom::plugins) {
177                next if ($blosxom::plugins{$plugin} < 0);
178                next if ($plugin eq __PACKAGE__);
179
180                foreach my $var (@{ $params }){
181                        my $v = eval "\$${plugin}::${var}";
182                        $data->{$plugin}->{$var} = $v if $v;
183                        my $v = eval "\$blosxom::${var}";
184                        $data->{blosxom}->{$var}  = $v if $v;
185                }
186        }
187        return $data;
188}
189
1901;
191
192__END__
193
194=head1 NAME
195
196Blosxom Plug-in: rwtt2
197
198=head1 SYNOPSIS
199
200atodekaku
201
202=head1 VERSION
203
2042006-09-10
205
206=head1 AUTHOR
207
208cho45 <cho45>, http://lowreal.net/
209
210=head1 DESCRIPTION
211
212
213=head1 INSTALLATION
214
215Drop rwtt2 into your plugins directory.
216
217=head1 CONFIGURATION
218
219
220=head1 REQUIREMENTS
221
222C<Template>
223
224=head1 SEE ALSO
225
226Blosxom Home/Docs/Licensing: http://www.blosxom.com/
227all about blosxom (lang=ja): http://blosxom.info/
228
229=head1 LICENSE
230
231
232rwtt2 Blosxom plugin Copyright 2006 cho45
233
234This plugin is licensed under Creative-Commons by 2.1 jp.
235
236See the below URL:
237http://creativecommons.org/licenses/by/2.1/jp/
238
Note: See TracBrowser for help on using the browser.