Regex A Plugin for Movable Type Release 1.61 January 26, 2003 From Brad Choate http://www.bradchoate.com/ Copyright (c) 2002-2003, Brad Choate =========================================================================== INSTALLATION To install, place the 'regex.pl' file in your Movable Type "plugins" directory. The 'regex.pm' and 'postproc.pm' ('postproc.pm' is only required for Movable Type 2.21) files should be placed in a 'bradchoate' subdirectory underneath your Movable Type "extlib" directory. Your installation should look like this: * (mt home)/plugins/regex.pl * (mt home)/extlib/bradchoate/regex.pm * (mt home)/extlib/bradchoate/postproc.pm Refer to the Movable Type documentation for more information regarding plugins. This plugin also includes the 'Custom Post-Processor' plugin (postproc.pm) also available from my web site. http://www.bradchoate.com/past/000654.php =========================================================================== 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: - a utility tag to add a Perl regular expression that can be used in conjunction with the global 'regex' post processor attribute. - a tag for including/excluding content compared with either a constant value or a regular expression pattern. - Container tag for processing any portion of a template. - Returns matches from the contained text. =========================================================================== 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): s|:-D||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: s/Brad/Perl Nut/gi s|google\(([^\)]+)\)|$1|g 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 .. -- WOW! We've reached 1,000 blog entries! -- m/something/ .. 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 =========================================================================== LICENSE Released under the MIT license. Please see http://www.opensource.org/licenses/mit-license.php for details. =========================================================================== SUPPORT If you have any questions or need assistance with this plugin, please visit this page on my site: http://www.bradchoate.com/past/mtregex.php Brad Choate July 27, 2002 =========================================================================== CHANGELOG 1.6 - Added 'no_html' to RegexDefine tag. 1.53 - Compatability fix for older versions of Perl ('undef operator' error); forward compatability update for Movable Type 2.5. Added Regex tag. 1.52 - Correction to pass 'conditional' arguments through properly. 1.51 - Corrected closure tags for embedded expressions. 1.5 - Added 'expr' attribute to IfMatches. 1.4 - Rearchitected code splitting into two files. 1.3 - The global attribute routine now pre-compiles and caches the expressions. This will substantially improve performance when an pattern is used over and over again (as in during a 'rebuild all' or when looping over multiple entries). Also, new in this release is support for another container tag, . This tag allows you to use match-style regular expressions defined by the tag. This release also includes the most current version of the 'Custom Post-Processing Handler' plugin which is required to use this plugin. Install both into your plugins directory. 1.2 - Revised to use the forward-compatible syntax for adding global filters to Movable Type (requires postproc.pm release 1.2). 1.1 - Revised storage of expressions so that if it is invoked using the 'regex="1"' parameter (all patterns), it will do so in the order they were added. The initial release required names for patterns. This was not the intention, but was the result. This release no longer requires names for patterns. Now supports the updating of expressions-- if you add a pattern with a name, you can later set a new pattern for that same name which will cause the original pattern to be replaced with the new one. If you update an existing named pattern with an empty pattern, it will in effect remove the pattern so it will not longer be evaluated. 1.0 - Initial release