English plugin dev 1 6 - Hiranyaloka/Documentation GitHub Wiki
A global modifier does post-tag processing. It can make all text upper-case or lower-case, can encode strings (i.e. URLs) and more.
For example, ‘<MTEntryTitle lower_case=“1”>’ will give us the entry title, lower cased.
As we just read the Test Driven Development chapter, obviously we are going to write test first and then the plugin itself
- Enable encrypting tag content using “rot13 encryption”, as described in: http://ja.wikipedia.org/wiki/ROT13
- Should work only when
rot13="1"is specified as tag parameter
This is essentially the same as MyPlugin03 of the previous chapter
use strict;
use lib qw( t/lib lib extlib );
use warnings;
use MT;
use Test::More tests => 5;
use MT::Test;
ok(MT->component ('MyPlugin04'), "MyPlugin04 plugin loaded correctry");
require_ok('MyPlugin04::L10N');
require_ok('MyPlugin04::L10N::ja');
require_ok('MyPlugin04::L10N::en_us');
require_ok('MyPlugin04::Tags');
1;
We will modify this test from the previous chapter. Here we will take the last entry, and apply the rot13 modifier to it
In this test we will have four cases:
- As always, for empty string we expect empty response
- Check that the title without any modifier is what we expect
- Check that the title with the modifier disabled is unchanged (rot13=“0”)
- Check that the title with the modifier enabled is changed (rot13=“1”)
(...snip...)
#===== Edit here
my $test_json = <<'JSON';
[
{ "r" : "1", "t" : "", "e" : ""},
{ "r" : "1", "t" : "<MTEntries lastn=\"1\"><MTEntryTitle></MTEntries>", "e" : "A Rainy Day"},
{ "r" : "1", "t" : "<MTEntries lastn=\"1\"><MTEntryTitle rot13=\"0\"></MTEntries>", "e" : "A Rainy Day"},
{ "r" : "1", "t" : "<MTEntries lastn=\"1\"><MTEntryTitle rot13=\"1\"></MTEntries>", "e" : "N Enval Qnl"}
]
JSON
#=====
(...snip...)
Again, we are modifying the previous MyPlugin03
Add to the file: “tags”=> “modifier”=> => $::
id: MyPlugin04
name: <__trans phrase="Sample Plugin rot13 globale modifier">
version: 1.0
description: <__trans phrase="_PLUGIN_DESCRIPTION">
author_name: <__trans phrase="_PLUGIN_AUTHOR">
author_link: http://www.example.com/about/
doc_link: http://www.example.com/docs/
l10n_class: MyPlugin04::L10N
tags:
modifier:
rot13: $MyPlugin04::MyPlugin04::Tags::_hdlr_rot13
package MyPlugin04::L10N; use strict; use base 'MT::Plugin::L10N'; 1;
package MyPlugin04::L10N::en_us;
use strict;
use base 'MyPlugin04::L10N';
use vars qw( %Lexicon );
%Lexicon = (
'_PLUGIN_DESCRIPTION' => 'Sample rot13 global modifier',
'_PLUGIN_AUTHOR' => 'Plugin author',
);
1;
package MyPlugin04::L10N::ja;
use strict;
use base 'MyPlugin04::L10N::en_us';
use vars qw( %Lexicon );
%Lexicon = (
'Sample Plugin rot13 globale modifier' => 'サンプルプラグイン rot13 グローバル・モディファイア',
'_PLUGIN_DESCRIPTION' => 'rot13 テストプラグイン',
'_PLUGIN_AUTHOR' => 'プラグイン作者',
);
1;
The implementation of rot13. if the $arg is not set to 1, return. if it is 1, then scramble the text using rot13 and return
package MyPlugin04::Tags;
use strict;
sub _hdlr_rot13 {
my ($str, $arg, $ctx) = @_;
return $str if $arg != 1;
$str =~ tr/a-zA-Z/n-za-mN-ZA-M/;
return $str;
}
1;
$MT_DIR/
|__ plugins/
|__ MyPlugin04/
|__ config.yaml
|__ lib/
| |_ MyPlugin04/
| |__ L10N.pm
| |_ L10N/
| | |_ en_us.pm
| | |_ ja.pm
| |_ Tags.pm
|__ t/
|_00-compile.t
|_01-tags.t
The file name specify that we have a global modifier whose name is rot13. simple.
<?php
function smarty_modifier_rot13($str, $args) {
if ($args != 1) {
return $str;
}
return str_rot13($str);
}
?>
The function name is according the the smarty framework, smarty_modifier_rot13
To make our life easier, we use the PHP function (from version 4.2) str_rot13()
$MT_DIR/
|__ plugins/
|__ MyPlugin04/
|__ config.yaml
|__ lib/
| |_ MyPlugin04/
| |__ L10N.pm
| |_ L10N/
| | |_ en_us.pm
| | |_ ja.pm
| |_ Tags.pm
|__ php/
| |_modifier.rot13.php
|__ t/
|_00-compile.t
|_01-tags.t
So lets run the tests, to see that we got it right
$ perl plugins/MyPlugin04/t/00-compile.t 1..5 ok 1 - MyPlugin04 plugin loaded correctry ok 2 - require MyPlugin04::L10N; ok 3 - require MyPlugin04::L10N::ja; ok 4 - require MyPlugin04::L10N::en_us; ok 5 - require MyPlugin04::Tags;
$ perl plugins/MyPlugin04/t/01-tags.t 1..7 ok 1 - 'blog-name' template found ok 2 - Test blog loaded ok 3 - Test entry loaded ok 4 - perl test 1 A Rainy Day ok 5 - perl test 2 N Enval Qnl ok 6 - perl test 3 ok 7 - ok - php test 1 ok - php test 2 ok - php test 3
$ prove plugins/MyPlugin04/t/*.t plugins/MyPlugin04/t/00-compile....ok plugins/MyPlugin04/t/01-tags.......ok All tests successful. Files=2, Tests=12, 24 wallclock secs (12.23 cusr + 4.98 csys = 17.21 CPU)
Pass on the first try. excellent!
Global Modifiers have their place in the Movable Type template framework (see the complete list of core modifiers at: http://www.movabletype.org/documentation/appendices/modifiers/ ) and sometimes implementing one is better then implementing a new tag. Of course, each case for itself
Now that we are professional plugin writers, the next chapters will talk more about tags, block tags and conditional tags
Prev:Test driven development of plugins << Index >> Next:Developing Function Tags