[Templates] Add regex support to custom TAGS

Andy Wardley abw@wardley.org
Thu, 03 May 2007 11:02:58 +0100


Paul Seamons wrote:
> I propose allowing the passing of the keyword "unquoted" to the TAGS
> directive.

I propose adding native /regular expression/ quoting syntax to TT3.
That would allow you to do this:

   [% TAGS /[<({]%/ /%[})>]/ %]     # regexen

For the TAGS directive, we would also need to support explicit 'string
quoting' in case a literal (non-regex) pattern starts with a /

   [% TAGS '/*' '*/' %]             # explicit quoting

As well as supporting the current unquoted style:

   [% TAGS [* *] %]                 # unquoted - same as TT2

Building regex quoting into the core language should also make passing
regexen to methods (like text.match) a little more intuitive when it
comes to escaping characters.  At present, regexen must be string 'quoted'
and that can get confusing when you have to escape certain characters.
Within /regex/ quotes, the normal escaping rules for regexen apply.

e.g.
   [% text.match(/<a href=["'].*["']>/)  %]     # TT3
   [% text.match('<a href=["\'].*["\']>') %]    # TT2

It also gives us a natural place to put flags rather than
having to embed them in the pattern...

   [% text.match(/foo/i) %]                     # TT3
   [% text.match('(?i)foo') %]                  # TT2

...or requiring additional parameters like the 'global' flag in match()

   [% text.match(/foo/g) %]                     # TT3
   [% text.match('foo', 1) %]                   # TT2

(I'm not quite sure how that last example is going to work yet, so consider
it "just an idea" at this stage)

Once again, it's something that's too hard/painful/costly to do
with the table-drive parser in TT2, but is relatively easy in TT3.

Cheers
A