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

Revision 674, 5.1 kB (checked in by jallen, 18 months ago)

Removing author's name at the behest of the author.

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