-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for Template Toolkit 2 #1418
Changes from 3 commits
33e23a0
50cff77
7baa4d3
cf49406
72e1320
51599d9
d8d790a
86995c2
60b7a0e
88e5662
9339e8b
a0b80aa
41f8c2f
34f4ec8
a30d0d6
1479a25
bbd01bd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
(function(Prism) { | ||
|
||
Prism.languages.tt2 = Prism.languages.extend('clike', { | ||
comment: { | ||
pattern: /#.*|\[%#[^]*?%\]/, | ||
lookbehind: true | ||
}, | ||
keyword: /\b(?:GET|CALL|SET|DEFAULT|INSERT|INCLUDE|PROCESS|WRAPPER|BLOCK|IF|UNLESS|ELSIF|ELSE|SWITCH|CASE|FOREACH|IN|WHILE|FILTER|USE|MACRO|RAWPERL|PERL|TRY|THROW|CATCH|FINAL|NEXT|LAST|RETURN|STOP|CLEAR|META|TAGS|DEBUG|END)\b/, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Considering the large number of keywords here, you might want to consider ordering them alphabetically, for easier addition/removal in the future. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They are in the order of the directives documentation. Switch to alphabetical or go with the docs? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, could you then add a link to this doc page, in a comment above the regexp? |
||
punctuation: /[[\]{},()]/ | ||
}); | ||
|
||
var operatorRE = /(?:=>|==|!=|<=|<|>=|>|=|&&|\|\||\||!|and|or|not)/; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could probably combine some patterns here to avoid backtracking. Something like |
||
|
||
Prism.languages.insertBefore('tt2', 'number', { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you should add a line like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, I didn't know that and see whether that also fixes the FIXME below. |
||
operator: operatorRE, | ||
// A colon is not allowed inside variables but we want to catch things | ||
// like [% USE Project::Specials %] | ||
variable: { | ||
pattern: /[a-z][:_a-z0-9]*(?:[\011-\015\040]*\.[\011-\015\040]*(?:\d+|\$?[a-z][:_a-z0-9]*))*/i, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Couldn't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, with syntactically correct code The colon was for fully qualified plug-in names ( |
||
greedy: true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do variables really need to be greedy? The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do variables really need to be greedy? The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With the |
||
} | ||
}); | ||
|
||
// FIXME! Is this repetition really necessary? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See my previous comment regarding the deletion of the |
||
Prism.languages.insertBefore('tt2', 'variable', { | ||
operator: operatorRE | ||
}); | ||
|
||
Prism.languages.insertBefore('tt2', 'keyword', { | ||
'delimiter': { | ||
pattern: /^(?:\[%|%%)-?|-?%]$/, | ||
alias: 'punctuation' | ||
} | ||
}); | ||
|
||
Prism.languages.insertBefore('tt2', 'string', { | ||
'single-quoted-string': { | ||
pattern: /'[^\\']*(?:\\.[^\\']*)*'/, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This regexp is super smart! Love it! It looks like TT2 allows multiline strings. Souldn't the regexp allow to escape line feeds? That is using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, the regexp is very smart. It's from Jeffrey Friedl, see http://regex.info/book.html And, yes, you're right, in the absence of |
||
greedy: true, | ||
alias: 'string' | ||
}, | ||
'double-quoted-string': { | ||
pattern: /"[^\\"]*(?:\\.[^\\"]*)*"/, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment regarding escaped line feeds. |
||
greedy: true, | ||
alias: 'string', | ||
inside: { | ||
variable: { | ||
pattern: /\$(?:[a-z][_a-z0-9]*(?:\.(?:\d+|\$?[a-z][_a-z0-9]*))*)/i, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can replace There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can replace |
||
greedy: true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment here about the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment here about the |
||
} | ||
} | ||
} | ||
}); | ||
|
||
// The different types of TT2 strings "replace" the C-like standard string | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please indent this comment with a tab. |
||
delete Prism.languages.tt2.string; | ||
|
||
Prism.hooks.add('before-tokenize', function(env) { | ||
var tt2Pattern = /\[%[^]+?%\]/g; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment here about |
||
Prism.languages['markup-templating'].buildPlaceholders(env, 'tt2', tt2Pattern); | ||
}); | ||
|
||
Prism.hooks.add('after-tokenize', function(env) { | ||
Prism.languages['markup-templating'].tokenizePlaceholders(env, 'tt2'); | ||
}); | ||
|
||
}(Prism)); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<h2>Comments</h2> | ||
<pre><code class="language-tt2">[%# this entire directive is ignored no | ||
matter how many lines it wraps onto | ||
%] | ||
[% # this is a comment | ||
theta = 20 # so is this | ||
rho = 30 # <aol>me too!</aol> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You'll probably need to escape the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You'll probably need to escape the |
||
%] | ||
</code></pre> | ||
|
||
<h2>Variables</h2> | ||
<pre><code class="language-tt2">[% text %] | ||
[% article.title %] | ||
[%= eat.whitespace.left %] | ||
[% eat.whitespace.right =%] | ||
[%= eat.whitespace.both =%] | ||
[% object.method() %]</code></pre> | ||
|
||
|
||
<h2>Conditionals and Loops</h2> | ||
<pre><code class="language-tt2">[% IF foo = bar %] | ||
this | ||
[% ELSE %] | ||
that | ||
[% END %] | ||
[% FOREACH post IN q.listPosts(lingua = "de") %] | ||
<a href="[% post.permalink %]">[% post.title | html %]</a> | ||
[% END %]</code></pre> | ||
|
||
<h2>Multiple Directives</h2> | ||
<pre><code class="language-tt2">[% IF title; | ||
INCLUDE header; | ||
ELSE; | ||
INCLUDE other/header title="Some Other Title"; | ||
END | ||
%]</code></pre> | ||
|
||
<h2>Operators</h2> | ||
<pre><code class="language-tt2">[% FOREACH post IN q.listPosts(lingua => 'de') %] | ||
[% post.title | myfilter(foo = "bar") %] | ||
[% END %]</code></pre> | ||
|
||
<h2>Known Limitations</h2> | ||
<ul> | ||
<li><a href="http://template-toolkit.org/docs/manual/Syntax.html#section_Outline_Tags"> | ||
Outline tags</a> are not supported.</li> | ||
<li>The arguments to | ||
<a href="http://template-toolkit.org/docs/manual/Directives.html#section_TAGS">TAGS</a> | ||
are usually misinterpreted</li> | ||
<li>In TT2, you can use keywords as identifiers where this is | ||
unambiguous. But these keywords will be highlighted as keywords, not | ||
as variables here.</li> | ||
<li>The | ||
<a href="http://template-toolkit.org/docs/manual/Config.html#section_ANYCASE">ANYCASE</a> | ||
option is not supprted.</li> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "supported" EDIT: Considering the title of the PR, I guess you really don't like the "o" in "support". 😄 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a typo here: "supported". |
||
<li> | ||
Any number of backslashes in front of dollar signs inside of double quoted | ||
strings are ignored since the behavior of Template Toolkit 2.26 seems to be | ||
inconsistent. | ||
</li> | ||
</ul> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<div>[% foo.bar.baz %]</div> | ||
<div>[%- foo.bar.baz %]</div> | ||
<div>[% foo.bar.baz -%]</div> | ||
<div>[%- foo.bar.baz -%]</div> | ||
|
||
---------------------------------------------------- | ||
|
||
[ | ||
["tag", [ | ||
["tag", [ | ||
["punctuation", "<"], | ||
"div" | ||
]], | ||
["punctuation", ">"] | ||
]], | ||
["tt2", [ | ||
["delimiter", "[%"], | ||
["variable", "foo.bar.baz"], | ||
["delimiter", "%]"] | ||
]], | ||
["tag", [ | ||
["tag", [ | ||
["punctuation", "</"], | ||
"div" | ||
]], | ||
["punctuation", ">"] | ||
]], | ||
["tag", [ | ||
["tag", [ | ||
["punctuation", "<"], | ||
"div" | ||
]], | ||
["punctuation", ">"] | ||
]], | ||
["tt2", [ | ||
["delimiter", "[%-"], | ||
["variable", "foo.bar.baz"], | ||
["delimiter", "%]"] | ||
]], | ||
["tag", [ | ||
["tag", [ | ||
["punctuation", "</"], | ||
"div" | ||
]], | ||
["punctuation", ">"] | ||
]], | ||
["tag", [ | ||
["tag", [ | ||
["punctuation", "<"], | ||
"div" | ||
]], | ||
["punctuation", ">"] | ||
]], | ||
["tt2", [ | ||
["delimiter", "[%"], | ||
["variable", "foo.bar.baz"], | ||
["delimiter", "-%]"] | ||
]], | ||
["tag", [ | ||
["tag", [ | ||
["punctuation", "</"], | ||
"div" | ||
]], | ||
["punctuation", ">"] | ||
]], | ||
["tag", [ | ||
["tag", [ | ||
["punctuation", "<"], | ||
"div" | ||
]], | ||
["punctuation", ">"] | ||
]], | ||
["tt2", [ | ||
["delimiter", "[%-"], | ||
["variable", "foo.bar.baz"], | ||
["delimiter", "-%]"] | ||
]], | ||
["tag", [ | ||
["tag", [ | ||
["punctuation", "</"], | ||
"div" | ||
]], | ||
["punctuation", ">"] | ||
]] | ||
] | ||
|
||
---------------------------------------------------- | ||
|
||
Checks for Template Toolkit 2 inside Markup. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
[%# this entire directive is ignored no | ||
matter how many lines it wraps onto | ||
%] | ||
[% # this is a comment | ||
theta = 20 # so is this | ||
rho = 30 # <aol>me too!</aol> | ||
%] | ||
|
||
---------------------------------------------------- | ||
|
||
[ | ||
["tt2", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please indent this line with a tab. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please indent this line with a tab. |
||
[ | ||
["comment", "[%# this entire directive is ignored no\r\n matter how many lines it wraps onto\r\n%]" ] | ||
] | ||
], | ||
["tt2", | ||
[ | ||
["delimiter", "[%"], | ||
["comment", "# this is a comment" ], | ||
["variable", "theta"], | ||
["operator", "="], | ||
["number", "20"], | ||
["comment", "# so is this" ], | ||
["variable", "rho"], | ||
["operator", "="], | ||
["number", "30"], | ||
["comment", "# <aol>me too!</aol>"], | ||
["delimiter", "%]"] | ||
] | ||
] | ||
] | ||
|
||
---------------------------------------------------- | ||
|
||
Checks for single-line and multi-line comments. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[%- %] | ||
[% -%] | ||
[%- -%] | ||
[% | ||
%] | ||
|
||
---------------------------------------------------- | ||
|
||
[ | ||
["tt2", [["delimiter", "[%-"], ["delimiter", "%]"]]], | ||
["tt2", [["delimiter", "[%"], ["delimiter", "-%]"]]], | ||
["tt2", [["delimiter", "[%-"], ["delimiter", "-%]"]]], | ||
["tt2", [["delimiter", "[%"], ["delimiter", "%]"]]] | ||
] | ||
|
||
---------------------------------------------------- | ||
|
||
Checks for delimiters. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We've decided to use
[\s\S]
instead of[^]
in #1107. Could you change this?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I didn't know that.