Skip to content

English plugin dev 1 6

semuel edited this page Nov 15, 2011 · 7 revisions

Developing Global Modifier Plugin

What is a Global Modifier?

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.

Writing the Plugin and test cases

As we just read the Test Driven Development chapter, obviously we are going to write test first and then the plugin itself

Plugin Specification

  1. Enable encrypting tag content using “rot13 encryption”, as described in: http://ja.wikipedia.org/wiki/ROT13
  2. Should work only when rot13="1" is specified as tag parameter

Test Case: 00-compile.t

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;

Test case: 01-tags.t

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:

  1. As always, for empty string we expect empty response
  2. Check that the title without any modifier is what we expect
  3. Check that the title with the modifier disabled is unchanged (rot13=“0”)
  4. 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...)

Developing the rot13 global modifier (Perl)

Again, we are modifying the previous MyPlugin03

config.yaml

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

L10N.pm

package MyPlugin04::L10N;
use strict;
use base 'MT::Plugin::L10N';

1;

L10N/en_us.pm

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;

L10N/ja.pm

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;

Tags.pm

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;

Directory structure

$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

Developing the rot13 global modifier (PHP)

modifier.rot13.php

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()

Directory structure

$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

Running the tests

So lets run the tests, to see that we got it right

00-compile.t

$ 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;

01-tags.t

$ 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 

Or using the prove command

$ 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!

Summary

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

Plugin Download

MyPlugin04.zip(5.94KB)

Navigation

Prev:Test driven development of plugins << Index >> Next:Developing Function Tags

Clone this wiki locally