| 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 | |
|---|
| 7 | package rwtt2; |
|---|
| 8 | |
|---|
| 9 | use strict; |
|---|
| 10 | |
|---|
| 11 | # --- Configurable variables ----------- |
|---|
| 12 | |
|---|
| 13 | our $timezone = "Asia/Tokyo"; |
|---|
| 14 | |
|---|
| 15 | our $config = { |
|---|
| 16 | EVAL_PERL => 1, |
|---|
| 17 | POST_CHOMP => 1, |
|---|
| 18 | }; |
|---|
| 19 | |
|---|
| 20 | # --- Plug-in package variables -------- |
|---|
| 21 | |
|---|
| 22 | # -------------------------------------- |
|---|
| 23 | |
|---|
| 24 | use Template; |
|---|
| 25 | use Template::Parser; |
|---|
| 26 | use Template::Context; |
|---|
| 27 | use Template::Document; |
|---|
| 28 | use DateTime; |
|---|
| 29 | use IO::File; |
|---|
| 30 | |
|---|
| 31 | use Data::Dumper; |
|---|
| 32 | |
|---|
| 33 | our ($vars, $story, $date, $debug) = ({}, {}, {}, 0); |
|---|
| 34 | |
|---|
| 35 | our ($template, $params, $doc, $content_type); |
|---|
| 36 | |
|---|
| 37 | sub 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 | |
|---|
| 52 | sub template { |
|---|
| 53 | return sub { |
|---|
| 54 | return ''; |
|---|
| 55 | }; |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | sub filter { |
|---|
| 59 | my ($pkg, $files_ref, $others_ref) = @_; |
|---|
| 60 | |
|---|
| 61 | return 1; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | sub skip { |
|---|
| 65 | return 0; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | sub interpolate { |
|---|
| 69 | return sub { |
|---|
| 70 | return ''; |
|---|
| 71 | }; |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | sub head { |
|---|
| 75 | my ($pkg, $path, $head_ref) = @_; |
|---|
| 76 | |
|---|
| 77 | return 1; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | sub 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 | |
|---|
| 88 | sub 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 | |
|---|
| 104 | sub foot { |
|---|
| 105 | my ($pkg, $path, $foot_ref) = @_; |
|---|
| 106 | |
|---|
| 107 | return 1; |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | sub 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 | |
|---|
| 134 | sub 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 | |
|---|
| 147 | sub 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 | |
|---|
| 174 | sub 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 | |
|---|
| 190 | 1; |
|---|
| 191 | |
|---|
| 192 | __END__ |
|---|
| 193 | |
|---|
| 194 | =head1 NAME |
|---|
| 195 | |
|---|
| 196 | Blosxom Plug-in: rwtt2 |
|---|
| 197 | |
|---|
| 198 | =head1 SYNOPSIS |
|---|
| 199 | |
|---|
| 200 | atodekaku |
|---|
| 201 | |
|---|
| 202 | =head1 VERSION |
|---|
| 203 | |
|---|
| 204 | 2006-09-10 |
|---|
| 205 | |
|---|
| 206 | =head1 AUTHOR |
|---|
| 207 | |
|---|
| 208 | cho45 <cho45>, http://lowreal.net/ |
|---|
| 209 | |
|---|
| 210 | =head1 DESCRIPTION |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | =head1 INSTALLATION |
|---|
| 214 | |
|---|
| 215 | Drop rwtt2 into your plugins directory. |
|---|
| 216 | |
|---|
| 217 | =head1 CONFIGURATION |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | =head1 REQUIREMENTS |
|---|
| 221 | |
|---|
| 222 | C<Template> |
|---|
| 223 | |
|---|
| 224 | =head1 SEE ALSO |
|---|
| 225 | |
|---|
| 226 | Blosxom Home/Docs/Licensing: http://www.blosxom.com/ |
|---|
| 227 | all about blosxom (lang=ja): http://blosxom.info/ |
|---|
| 228 | |
|---|
| 229 | =head1 LICENSE |
|---|
| 230 | |
|---|
| 231 | |
|---|
| 232 | rwtt2 Blosxom plugin Copyright 2006 cho45 |
|---|
| 233 | |
|---|
| 234 | This plugin is licensed under Creative-Commons by 2.1 jp. |
|---|
| 235 | |
|---|
| 236 | See the below URL: |
|---|
| 237 | http://creativecommons.org/licenses/by/2.1/jp/ |
|---|
| 238 | |
|---|