More flexible filter
| Project: | Signwriter |
| Version: | 6.x-2.x-dev |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | needs review |
Jump to:
When you define a filter, most of the time you want to use some sort of tags that should be stripped away. For example, <signwriter>Header</signwriter> or [signwriter|times|20pt]Header[/signwriter] or whatever catches your fancy.
At this moment, there is special handling for using HTML-like <...> tags, but only for those. So for the first example you could use the filter /<signwriter>.*?<\/signwriter>/>, while the second is impossible to implement.
The attached patch removes that special handling. Instead, using pattern substitution, it takes the first parenthesized part of the pattern and uses that to substitute.
The above examples would become:
/<signwriter>(.*?)<\/signwriter>/>
and
/\[signwriter\|times\|20pt\](.*?)\[\/signwriter\]/
The patch itself is minimal: it removes the special handling in signwriter_title_convert and slightly changes the process part of signwriter_filter.
The impact for existing sites would be adding a pair of parentheses in all signwriter filters. It could even be done automatically by an upgrade script (not included).
Frodo
| Attachment | Size |
|---|---|
| signwriter-filter.diff | 927 bytes |

#1
Thanks for this, will have a look soon.
This has been on my to do list for a while as in my experience the form can have problems with other input filter modules like the WYSIWYG module.
#2
A quite advanced example using my patch: how to replace the text within h3 tags while preserving the h3 and nested tags.
/<h3[^>]*>(?:<[^>]*>)*\K([^<]*)(?=(?:^<[^>]*>)*<\/h3>)/How does this work?
<h3[^>]*>This matches the h3 tag, ending at the closing >. It may have attributes that should be preserved.
(?:<[^>]*>)*This matches any nested tags, right after the opening h3 tag. The ?: is used to mark this as not being the pattern to be replaced.
\KThis is a special marker which means: anything matched upto now should not be replaced but preserved (see the perlre manpage of perl 5.10 or later).
([^<]*)Finally, the text that should be converted into an image. Between parentheses, to indicate this. Basically, anything except a <.
(?=(?:<[^>]*>)*<\/h3>)Any tags and finally the closing h3 tag. The (?= introduces a look-forward. That means that it should be matched, but not replaced.
Not very easy on the eye, but it opens a lot of possibilities - for those of us who are re-wizards, that is...