wion / tWriter.tmbundle

An extended Textile bundle for TextMate.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

grammar scope: Escape brackets

wion opened this issue · comments

commented

It is necessary to escape superscripts and subscripts in Textile with straight brackets if you don't want any space between them and surrounding punctuation or words.

But the added brackets start looking a little busy in regular text if all those characters are foreground color. So the objective is to scope for the straight brackets only in those specific conditions where interior wrapping pairs are either ^...^ or ~...~.

This regex gets the whole strings, but not the brackets alone:

(\[(\^|~)|(\^|~)\])

Need to figure out how to target just the brackets.

commented

After trying to understand lookarounds in regular expressions, this adjusted regex scope seems to do the job:

((?=\[(\^|~))\[)|((?<=(\^|~))\])

It's a combination of a positive lookahead, (?=\[(\^|~))\[), and a positive lookbehind ((?<=(\^|~))\]), with an or operator, |, between them. So it's actually looking for one side or the other, but since it always will find both, it treats both.

So I guess | actually functions as an and/or operator?

commented

Realized, of course, these brackets could be needed for other Textile elements, too, so the grammar was changed again and now works in the following situations (excluding the more challenging link combinations for now).

tWriter escape brackets

Note this is different from footnote numbers[n] and endnote references,[#ref] which are greyed out entirely.