Edgewall Software

Opened 18 years ago

Last modified 18 years ago

#85 new enhancement

Static Matching Implementation

Reported by: hodgestar+genshi@… Owned by: cmlenz
Priority: minor Milestone:
Component: Template processing Version: devel
Keywords: static match Cc: davidf@…

Description

The syntax for using py:smatch is the same as that for py:match. The py:smatch XPath query is performed immediately after the templated is parsed whereas the py:match query is performed when the template is generated/rendered. In both cases the replacement markup is only created on template generation.

Py:smatch avoids performing XPath matches when the template is generated at the cost of not being able to access the context in the XPath expression or match XML not present in the template file (for example, attributes generated by py:attr).

The implementation includes two new directives SmatchDirective? and StaticMatchDirective?. SmatchDirective? is registered and represents the point in the stream where the py:smatch tag occurs. After parsing the template, a filter (_smatch) is run which removes SmatchDirectives? and places StaticMatchDirectives? at the points in the stream matched by the SmatchDirective? XPath expression. StaticMatchDirectives? then create the new content on template generation.

Note that py:smatch is not as generally useful as I first thought it might be. Not being able to match generated content is in practice a really strong restriction. If others think it's useful, feel free to include it though. :)

The patch (against genshi trunk) includes three tests.

Attachments (2)

pysmatch.diff (10.8 KB) - added by hodgestar+genshi@… 18 years ago.
Patch against http://svn.edgewall.org/repos/genshi/trunk r466 which implements py:smatch.
pysmatch.2.diff (13.2 KB) - added by hodgestar+genshi@… 18 years ago.
Patch against http://svn.edgewall.org/repos/genshi/trunk r466 which implements a py:smatch which works with subprograms.

Download all attachments as: .zip

Change History (7)

Changed 18 years ago by hodgestar+genshi@…

Patch against http://svn.edgewall.org/repos/genshi/trunk r466 which implements py:smatch.

comment:1 Changed 18 years ago by hodgestar+genshi@…

  • Priority changed from major to minor
  • Type changed from defect to enhancement

Oops. This is an enhancement not a defect. And not having it is probably not a major thing for anyone.

comment:2 Changed 18 years ago by anonymous

  • Cc davidf@… added

comment:3 Changed 18 years ago by hodgestar+genshi@…

The current patch seems not to work well with sub programmes. For example py:smatch="td" fails to match <td py:attrs="{'foo':'bar'}" />. However it does match <td py:content="foo" />. Will investigate.

Changed 18 years ago by hodgestar+genshi@…

Patch against http://svn.edgewall.org/repos/genshi/trunk r466 which implements a py:smatch which works with subprograms.

comment:4 Changed 18 years ago by hodgestar+genshi@…

Problems with SUB programs (and especially py:attrs solved). The new py:smatch is quite a lot niftier (for example it does the right thing when matching against something like <td py:for="..." />).

The patch unfortunately requires a small change to genshi.core.path to allow select('./text()') to match EXPR nodes which generate text. I don't think I've quite done the right thing (since presumably EXPR nodes can occur in places TEXT nodes can't). Perhaps EXPR nodes which occur inside text nodes should in fact be wrapped with TEXT node objects? I'd appreciate someone who knows Genshi streams better than I do taking a look at this.

comment:5 Changed 18 years ago by cmlenz

  • Milestone 0.4 deleted

Interesting work, thanks! This will have to wait for after 0.4 though.

Note: See TracTickets for help on using tickets.