root/trunk/HelloWorld/plugins/HelloWorld/HelloWorld.pl @ 422

Revision 422, 5.5 kB (checked in by gboggs, 22 months ago)

Added Raynes to the POD too. Fixed my email. Cleaned a bit of my crusty comment grammar.

  • Property svn:executable set to *
  • Property svn:keywords set to Id
Line 
1package MT::Plugin::HelloWorld;
2#
3# This is a modified version of the code listed in "Hacking Movable
4# Type" by Allen, Choate, Hammersley, Haughey and Raynes, in chapter
5# 10: Writing Plugins.  Gene Boggs added the plugin manager
6# compatiblity files.
7#
8# If you spot an inaccuracy or improvement in the context of The Book
9# or PluginManager requirements, patches are welcome!
10#
11use strict;
12use warnings;
13
14use MT;
15use MT::Plugin;
16use MT::Template::Context;
17
18use base 'MT::Plugin';
19
20our $plugin;
21our $VERSION = '0.5';
22MT->add_plugin($plugin = __PACKAGE__->new({
23        name            => "Hello World",
24        version         => $VERSION,
25        description     => "Enables friendly greetings.",
26        author_name     => "Ima Hacker",
27        author_link     => "http://example.com/",
28    template_tags => {
29        'HelloWorld' => \&HelloWorld,
30        'HelloWorldGreeted' => \&HelloWorldGreeted,
31    },
32    container_tags => {
33        'HelloWorldContainer' => \&HelloWorldContainer,
34    },
35    conditional_tags => {
36        'HelloWorldIfNameProvided' => \&HelloWorldIfNameProvided,
37    },
38    global_filters => {
39        'to_uppercase' => \&to_uppercase,
40    },
41}));
42
43
44sub HelloWorld {
45    my($ctx, $args) = @_;
46    my $name = _get_name($ctx, $args);
47    return "Hello $name!";
48}
49
50sub HelloWorldContainer {
51    my($ctx, $args) = @_;
52    my $builder = $ctx->stash('builder');
53    my $tokens = $ctx->stash('tokens');
54
55    my $name = _get_name($ctx, $args);
56
57    if( $name =~ s/ \[ ( \/? MT [^\]]+ ) \] /<$1>/gx ) {
58        my $tok = $builder->compile($ctx, $name);
59        $name = $builder->build($ctx, $tok) ||
60            return $ctx->error($builder->errstr);
61    }
62
63    my $glue = $args->{glue} || '[\s,]+';
64    my @names = split /$glue/, $name;
65
66    my $out = '';
67    for my $name (@names) {
68        $ctx->stash('HelloWorldGreeted' => $name);
69        $out .= $builder->build($ctx, $tokens) ||
70            $ctx->error($builder->errstr);
71    }
72
73    return $out;
74}
75
76sub HelloWorldGreeted {
77    my($ctx, $args) = @_;
78    my $out = $ctx->stash('HelloWorldGreeted') 
79           || $ctx->error('MTHelloWorldGreeted must be used in a '.
80                          'MTHelloWorldContainer element.');
81    return $out;
82}
83
84sub HelloWorldIfNameProvided {
85    my($ctx, $args) = @_;
86    my $out = $ctx->stash('HelloWorldGreeted');
87    return $out && $out ne 'world';
88}
89
90sub to_uppercase {
91    my($text, $value, $ctx) = @_;
92    return uc $text;
93}
94
95sub _get_name {
96    my($ctx, $args) = @_;
97    my $e = $ctx->stash('entry');
98
99    return  $args and defined $args->{name}      ?  $args->{name}
100        :   $e && $e->author && $e->author->name ?  $e->author->name
101        :                                           'world';
102   
103}
104
105__END__
106
107=head1 NAME
108
109MT::Plugin::HelloWorld - Hacking Movable Type, Chapter Ten.
110
111=head1 DESCRIPTION
112
113This module is a simple example illustrating plugin basics, from the
114the book, "Hacking Movable Type" and with the MT PluginManager
115requirements - both linked below in the L</SEE ALSO> section.
116
117The code is transcribed from "Hacking Movable Type" by Allen, Choate,
118Hammersley, Haughey and Raynes, Chapter 10: "Writing Plugins."  Slight
119modifications have been made by Gene Boggs.  If you spot an inaccuracy
120or improvement (in the context of The Book or PluginManager
121requirements), patches are welcome!  Please contact Gene (email in
122L</AUTHORS>) or better yet, check-in a patch if you have svn write
123access.
124
125The distribution of this plugin contains xml files that are required
126by PluginManager, to allow automated updating.  Currently, the paths
127specified in these files do not reference physical locations.
128
129=head1 METHODS
130
131=over 4
132
133=item * HelloWorld
134
135=item * HelloWorldContainer
136
137=item * HelloWorldGreeted
138
139=item * HelloWorldIfNameProvided
140
141=item * to_uppercase
142
143=back
144
145=head1 EXAMPLES
146
147    ### To all of you
148
149        <$MTHelloWorld$>
150
151    ### Dirified
152
153        <$MTHelloWorld dirify="1"$>
154
155
156    ### Showing excitement with a global attribute!
157
158        <$MTHelloWorld to_uppercase="1"$>
159
160    ### Hi Morty!
161
162        <$MTHelloWorld name="Mortimer"$>
163
164    ### Containers
165
166    <MTHelloWorldContainer name="Byrne, Anil, Byron, Jay, world">
167        Hello <$MTHelloWorldGreeted$>!
168    </MTHelloWorldContainer>
169
170    ### Containers with a conditional
171
172    <MTHelloWorldContainer name="Byrne, Anil, Byron, Jay, world">
173      <MTHelloWorldIfNameProvided>
174        Hello <$MTHelloWorldGreeted$>! Nice to see you!
175      <MTElse>
176        Oh, it's you again, world.  Hi.
177      </MTElse>
178      </MTHelloWorldIfNameProvided>
179    </MTHelloWorldContainer>
180
181    ### In an entries loop
182
183    <MTEntries>
184        <$MTHelloWorld$>  Thanks for writing this entry.
185    </MTEntries>
186
187    ### Container inside of entry without tag
188
189    <MTEntries>
190    <MTHelloWorldContainer>
191        Hello <$MTHelloWorldGreeted$>!
192    </MTHelloWorldContainer>
193    </MTEntries>
194
195    ### Container inside of entry with tag as name variable
196
197    <MTEntries>
198    <MTHelloWorldContainer name="[MTEntryTags glue=','][MTTagName][/MTEntryTags]">
199        This entry is tagged: <$MTHelloWorldGreeted$>
200    </MTHelloWorldContainer>
201    </MTEntries>
202
203=head1 TO DO
204
205XML files do not reference physical locations.
206
207=head1 SEE ALSO
208
209=head2 Hacking Movable Type
210
211L<http://hackingmt.com/>
212
213L<http://bradchoate.com/weblog/2005/08/01/hacking-movable-type>
214
215=head2 PluginManager
216
217The code: L<http://code.sixapart.com/svn/mtplugins/trunk/PluginManager/>.
218
219The documentation: L<http://bradchoate.com/weblog/2005/08/01/hacking-movable-type>.
220
221=head1 AUTHORS
222
223=over 4
224
225=item Brad Choate E<lt>brad@sixapart.comE<gt>
226
227=item Jay Allen E<lt>jay@jayallen.orgE<gt>
228
229=item Gene Boggs E<lt>gene.boggs@gmail.comE<gt>
230
231=back
232
233=cut
Note: See TracBrowser for help on using the browser.