MT Regex howto
DESCRIPTION
This plugin allows you to define search and replace parameters for
elements you produce from Movable Type templates.
Tags made available through this plugin:
can be used in conjunction with the global 'regex' post processor
attribute.
either a constant value or a regular expression pattern.
These attributes are allowed:
* name
If given, the regular expression can be individually referenced
by name.
* no_html
If specified for a replacement regular expression, it will ignore
any tags
Perl regular expressions are incredibly powerful constructs that can
be used to manipulate text in a variety of ways.
Here's an example (place at the top of your template):
|g
The above will replace all occurrances of :-D with a tag that
displays a smiley.gif instead.
In order to activate the search and replace for any given <$MT$>
variable, use the global "regex" modifier:
<$MTEntryBody regex="1"$>
This will 'turn on' the regex operation for that output.
Sometimes you might want to selectively pick and choose from various
regular expression patterns. To do that, name the expressions like
this:
Note: "patt2" shown above gives you the ability to write like this
in your blog entry:
google(some text)
That produces a "Google This" link right in your entry. This kind of
thing is what makes this plugin so useful!
Now, with your <$MT$> vars, you can specify things like this:
<$MTEntryTitle regex="patt1"$> (only applies patt1 to this part)
<$MTEntryBody regex="patt2"$> (only applies patt2 to this part)
<$MTEntryBody regex="1"$> (applies all regex patterns)
<$MTEntryBody regex="patt1 patt2"$> (applies both patt1 and patt2)
You can also specify an inline regex like this:
<$MTEntryBody regex="s/this/that/g"$>
You can only specify a single regular expression using that technique.
Also, you cannot currently use the letter '$' inside an inline
expression due to a parsing limitation in the current (2.21) version
of Movable Type. In order to use '$' in your expressions, create a
named expression using the MTRegexDefine tag.
This tag allows you to output content based on whether it matches
a value or regular expression.
These attributes are allowed:
* var
If specified, var is evaluated as a Movable Type variable.
The result is used to compare against the constant 'val'
attribute value or the regular expression provided with
the 'pattern' attribute value.
If unspecified, the tag will use the contained data as the
value to compare against the constant/pattern.
* expr
An alternative to 'var' that allows for any Movable Type
expression (use [ and ] instead of < and > for the tag
delimiters).
* value
If specified, value is a string constant to compare with.
* pattern
If specified, pattern defines a named regular expression
or a matching pattern to compare against.
Usage examples:
.. other tags/values here-- outputs data only if content includes the
string 'something' somewhere inside ..
--
.. output entry data (only outputs data authored by Brad, Ben and
Mena ..
--
--
.. other tags/values-- outputs data only if content includes
a match for the named regular expression 'somematch' ..
This tag allows you to process a block with previously defined regex
patterns, or an individual regex pattern.
These attributes are allowed:
* pattern
Can either be a matching regex which will select named regex
patterns that are selected using that pattern, or a replacement
regex pattern which will be applied by itself.
* no_html
Forces the regex operations to exclude any tags.A
Please note: Perl regular expressions are very powerful, but if you're
not familiar with them already, it will take some time to learn how to
use them. The links provided below are very helpful. Also, be patient
as you are creating them-- if they aren't working, don't assume that
this plugin is at fault-- more than likely, it is an improperly
constructed expression.
This tag allows you to search the block contained by the Grep tag.
Lines that match the given pattern(s) are taken and joined together
using the 'glue' attribute given (or the newline character if no glue
attribute is specified).
These attributes are allowed:
* pattern
Can either be a matching regex which would be used for matching
the text block or a space-delimited list of named patterns (specified
previously using the RegexDefine tag).
* glue
Text to be used to piece the matches together.
* default
Text to be returned in case no matches are found.
FOR MORE INFORMATION
Documenting Perl regular expressions goes way beyond the scope of this
readme.txt file. For a tutorial on using Perl regular expressions
visit this page:
http://www.perldoc.com/perl5.6.1/pod/perlretut.html
And for advanced documentation, look here:
http://www.perldoc.com/perl5.6.1/pod/perlre.html