Movable Type Docs > Developer > Plugins

Defining Custom Text Filters

One of the very first Movable Type plugins was a simple text filter. Text filters are used to transform the plain text input by a user into the entry editing interface, or through a commenting form into some other format. Movable Type ships with several built in text filters, including:

  • Markdown
  • Textile
  • SmartyPants
  • Convert line breaks

Each of the above text filters converts the text written in an alternate syntax into the corresponding HTML syntax. However, there are many other possible uses for a text transformation plugin including one that may auto-correct spelling, automatically link certain words to specific websites, etc.

Note: The text entered by the user is stored in the database in its original form. The transformation occurs at the time the entry is published only.

To register a text filter, use the following code sample:

sub init\_registry {
    my $plugin = shift;
    $plugin->registry({
        text\_filters  => {
            'mytransform' => {
                label   => 'My Text Transfom',
                handler => \&transform,
                condition => \&transform\_when,
            },
        },
    });
}

There are a number of properties for a text filter that can used when registering the text filter with Movable Type. These options are:

  • label - the display name of the text filter as it will appear in the Movable Type user interface
  • handler - a reference to a subroutine responsible for handling the transformation
  • condition - in the event that the text filter should only be applied under specific conditions, one can identify a reference to a subroutine that will return true if the filter can be applied, and false otherwise.

Conditionalizing Text Filtering

In the code sample above where a text filter is registered with Movable Type, a condition is defined under which text filtering is allowed. The call back determines whether or not the text filter can be selected by a user via the web interface. The callback is called with a single parameter, the type of the object being transformed. Allowable values for the object type are:

  • entry
  • comment

The callback should return "1" if filtering is allowed, and "0" otherwise.

# Will only allow the text filter to be applied to comments
sub transform_when {
    my ($obj_type) = @_;
    return 1 if $obj_type && ($obj_type eq 'comment');
}

The Transformation Callback

The handler or callback for the text filter can be any subroutine. When Movable Type calls the subroutine to transform a piece of text, the text being transformed is passed to the handler as a parameter, along with the complete context for the transformation. The handler would then perform the transformation and returns the transformed text back to Movable Type.

sub transform {
    my ($str, $ctx) = @_;
    return "HELLO! - $str";
}

The above text filter simply appends the text "HELLO! - " to the beginning of the string being transformed or filtered.

This page was last updated on 2008-05-14, 11:28.  

Submit a User Contributed Note

User contributed notes are a great way to share the knowledge you have gained in using Movable Type.

If you have a technical question or problem, please visit Movable Type Support.

(If you haven't left a note here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)