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

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

Merge from refactoring-planet. YAY!

  • 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('javascript.tt', { feed => $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 1;
67
68 __END__
69
70 =head1 NAME
71
72 Plagger::Plugin::Publish::JavaScript - publish links to entries as JavaScript
73
74 =head1 SYNOPSIS
75
76   - module: Publish::JavaScript
77     config:
78       dir: /path/to/www/js
79       filename: %t.js
80
81 =head1 DESCRIPTION
82
83 This plugin publishes links to feed entries as an HTML embedable
84 JavaScript file. The JS file contains document.write() calls, and can
85 be easily included in any HTML page using:
86
87   <script src="/path/to/file.js"></script>
88
89 in any place, like Blog sidebar widgets.
90
91 The HTML emitted by the JavaScript code has exactly the same structure
92 with Movable Type's standard sidebar module, so you can easily style
93 using CSS.
94
95 =head1 CONFIG
96
97 =over 4
98
99 =item dir
100
101 Directory to save JS files in.
102
103 =item filename
104
105 Filename to be used to create JS files. It defaults to C<%i.js>, but
106 could be configured using the following formats like strftime:
107
108 =over 8
109
110 =item * %u url
111
112 =item * %l link
113
114 =item * %t title
115
116 =item * %i id
117
118 =back
119
120 =back
121
122 =head1 AUTHOR
123
124 Tatsuhiko Miyagawa
125
126 =head1 SEE ALSO
127
128 L<Plagger>, L<Plagger::Plugin::Publish::MTWidget>
129
130 =cut
Note: See TracBrowser for help on using the browser.