root/trunk/plagger/lib/Plagger/Plugin/Publish/JavaScript.pm

Revision 934 (checked in by miyagawa, 14 years ago)

Publish::JavaScript?: code tweak and docs

  • Property svn:keywords set to Id Revision
Line 
1 package Plagger::Plugin::Publish::JavaScript;
2 use strict;
3 use base qw( Plagger::Plugin );
4
5 use File::Spec;
6 use Template::Plugin::JavaScript;
7
8 sub register {
9     my($self, $context) = @_;
10     $context->register_hook(
11         $self,
12         'publish.feed' => \&feed,
13     );
14 }
15
16 sub init {
17     my $self = shift;
18     $self->SUPER::init(@_);
19
20     my $dir = $self->conf->{dir};
21     unless (-e $dir && -d _) {
22         mkdir $dir, 0755 or Plagger->context->error("mkdir $dir: $!");
23     }
24 }
25
26 sub feed {
27     my($self, $context, $args) = @_;
28
29     my $file = $self->gen_filename($args->{feed}, $self->conf->{filename} || '%i.js');
30     my $path = File::Spec->catfile($self->conf->{dir}, $file);
31     $context->log(info => "writing output to $path");
32
33     my $body = $self->templatize($context, $args->{feed});
34
35     open my $out, ">:utf8", $path or $context->error("$path: $!");
36     print $out $body;
37     close $out;
38 }
39
40 my %formats = (
41     'u' => sub { my $s = $_[0]->url;  $s =~ s!^https?://!!; $s },
42     'l' => sub { my $s = $_[0]->link; $s =~ s!^https?://!!; $s },
43     't' => sub { $_[0]->title },
44     'i' => sub { $_[0]->id },
45 );
46
47 my $format_re = qr/%(u|l|t|i)/;
48
49 sub gen_filename {
50     my($self, $feed, $file) = @_;
51
52     $file =~ s{$format_re}{
53         $self->safe_filename($formats{$1}->($feed))
54     }egx;
55
56     $file;
57 }
58
59 sub safe_filename {
60     my($self, $path) = @_;
61     $path =~ s![^\w\s]+!_!g;
62     $path =~ s!\s+!_!g;
63     $path;
64 }
65
66 sub templatize {
67     my($self, $context, $feed) = @_;
68     my $tt = $context->template();
69     $tt->process('javascript.tt', {
70         feed => $feed,
71     }, \my $out) or $context->error($tt->error);
72     $out;
73 }
74
75 1;
76
77 __END__
78
79 =head1 NAME
80
81 Plagger::Plugin::Publish::JavaScript - publish links to entries as JavaScript
82
83 =head1 SYNOPSIS
84
85   - module: Publish::JavaScript
86     config:
87       dir: /path/to/www/js
88       filename: %t.js
89
90 =head1 DESCRIPTION
91
92 This plugin publishes links to feed entries as an HTML embedable
93 JavaScript file. The JS file contains document.write() calls, and can
94 be easily included in any HTML page using:
95
96   <script src="/path/to/file.js"></script>
97
98 in any place, like Blog sidebar widgets.
99
100 The HTML emitted by the JavaScript code has exactly the same structure
101 with Movable Type's standard sidebar module, so you can easily style
102 using CSS.
103
104 =head1 CONFIG
105
106 =over 4
107
108 =item dir
109
110 Directory to save JS files in.
111
112 =item filename
113
114 Filename to be used to create JS files. It defaults to C<%i.js>, but
115 could be configured using the following formats like strftime:
116
117 =over 8
118
119 =item * %u url
120
121 =item * %l link
122
123 =item * %t title
124
125 =item * %i id
126
127 =back
128
129 =back
130
131 =head1 AUTHOR
132
133 Tatsuhiko Miyagawa
134
135 =head1 SEE ALSO
136
137 L<Plagger>, L<Plagger::Plugin::Publish::MTWidget>
138
139 =cut
Note: See TracBrowser for help on using the browser.