Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amend HTML Grammar based on attributes in TextMate

I've recently started experimenting with jQuery Templates, which rely on your ability to wrap HTML within SCRIPT tags.

<script id="movieTemplate" type="text/x-jquery-tmpl">
    <li>
        <b>${Name}</b> (${ReleaseYear})
    </li>
</script>

The problem is, TextMate naturally assumes that anything within SCRIPT tags is JavaScript. I'm sure it's possible to make TextMate treat the content differently based on the type attribute, but I'm struggling with some of the grammar being used in the bundle. I'm pretty confident that the line below is key, but I'm not sure where to start.

begin = '(?:^\s+)?(<)((?i:script))\b(?![^>]*/>)';

Has anyone already dealt with a similar scenario? Would someone be able to point me in the right direction?

Rich

like image 773
kim3er Avatar asked Apr 07 '11 08:04

kim3er


3 Answers

begin = '(?:^\s+)?(<)((?i:script))\b(?!([^>]*text/x-jquery-tmpl[^>]*|[^>]*/>))';

will stop treating script tags with "text/x-jquery-tmpl" in them as javascript

like image 188
dimus Avatar answered Nov 16 '22 03:11

dimus


That's a regular expression. You could extend it to check for the type text/javascript like that:

begin = '(?:^\s+)?(<)((?i:script))\b(.*?type="text/javascript")(?![^>]*/>.*)';

I have only tested it with if, but it seems to work. When the type is text/javascript TextMate expands it to Javascript for every other type it uses PHP. (Just like outside of script tags.)

You can read more about how TextMate uses regular expressions here: Regex (TextMate Manual)

like image 38
benjy Avatar answered Nov 16 '22 02:11

benjy


The matching groups are meaningful. You need to change it to this:

begin = '(?:^\s+)?(<)((?i:script))\b(?:.*?type="text/javascript")(?![^>]*/>)';

In order to keep the current matching group configuration.

like image 21
kenbritton Avatar answered Nov 16 '22 01:11

kenbritton