11

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

kim3er
  • 6,306
  • 4
  • 41
  • 69
  • You didn't state what it is that you want TextMate to do with this snippet. When I put this in an HTML file I can do any HTML and JS related actions between the ` – romainl Apr 08 '11 at 11:52
  • @romainl, thanks for the reply. Currently anything within a `script` tag is treated as JavaScript. I'd like the contents `script` tag with a `type` of `text/x-jquery-tmpl` be treated as HTML. – kim3er Apr 09 '11 at 16:39
  • Why do you want that part of the document to be treated as HTML by TextMate? What can't you do with this snippet between ` – romainl Apr 09 '11 at 17:02
  • @romainl - If you're trying to ask why aren't I just putting up with the existing functionality, then I really do know how to answer you. I believe it is possible to have TextMate work the way I would like it too. I would just like a pointer in the right direction. – kim3er Apr 09 '11 at 18:11

3 Answers3

6

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

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

dimus
  • 8,712
  • 10
  • 45
  • 56
  • 3
    Thank you very much! this worked like a charm for me.... I am using it in SublimeText2 HTML.tmLanguage as: (?:^\s+)?(<)((?i:script))\b(?!([^>]*text/x-jquery-tmpl[^>]*|[^>]*/>)) – José F. Romaniello Jan 16 '12 at 20:22
2

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)

benjy
  • 876
  • 2
  • 8
  • 11
  • Hi, thanks for the response. The problem with this method is that it breaks the syntax highlighting on the script tag. All the attributes within the matched script tag (up to and including the `type` attribute turn white. Trading syntax highlighting support in the content, or support in the tag itself. I want both! :) – kim3er Apr 17 '11 at 08:15
1

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.

kenbritton
  • 46
  • 2
  • The problem I'm having with both suggestions is that the attribute names in the script tag are not being differentiated in colour. – kim3er Aug 03 '11 at 16:16
  • I'm seeing green attribute values for the text/x-jquery-tmpl and and white for the text/javascript. What colour do you expect for each? – kenbritton Aug 04 '11 at 03:13
  • That's what I'm getting. Just looking for consistency on the script tag, regardless of type. It is the content of the tag that should change. – kim3er Aug 04 '11 at 12:27