-
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
Merged
Merged
Changes from 16 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
33e23a0
support for Template Toolkit 2
gflohr 50cff77
optimized quoted string regexes
gflohr 7baa4d3
optimized double-quoted string regex
gflohr cf49406
fully qualified tt2 plug-in names don't work
gflohr 72e1320
do not use empty character class
gflohr 51599d9
put keyword tests in alphabetical order
gflohr d8d790a
ordered tt2 keywords alphabetically
gflohr 86995c2
removed redundant operator rule for tt2
gflohr 60b7a0e
removed possibly existing old rules before inserti
gflohr 88e5662
indentation
gflohr 9339e8b
allow backslash in front of lf in tt2 strings
gflohr a0b80aa
indentation
gflohr 41f8c2f
avoid backtracking
gflohr 34f4ec8
escape xml special characters
gflohr a30d0d6
indent with tab instead of spaces
gflohr 1479a25
greedy is no longer needed for variables
gflohr bbd01bd
re-generated with gulp
gflohr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
(function(Prism) { | ||
|
||
Prism.languages.tt2 = Prism.languages.extend('clike', { | ||
comment: { | ||
pattern: /#.*|\[%#[\s\S]*?%\]/, | ||
lookbehind: true | ||
}, | ||
keyword: /\b(?:BLOCK|CALL|CASE|CATCH|CLEAR|DEBUG|DEFAULT|ELSE|ELSIF|END|FILTER|FINAL|FOREACH|GET|IF|IN|INCLUDE|INSERT|LAST|MACRO|META|NEXT|PERL|PROCESS|RAWPERL|RETURN|SET|STOP|TAGS|THROW|TRY|SWITCH|UNLESS|USE|WHILE|WRAPPER)\b/, | ||
punctuation: /[[\]{},()]/ | ||
}); | ||
|
||
delete Prism.languages.tt2['operator']; | ||
delete Prism.languages.tt2['variable']; | ||
Prism.languages.insertBefore('tt2', 'number', { | ||
operator: /=[>=]?|!=?|<=?|>=?|&&|\|\|?|\b(?:and|or|not)\b/, | ||
variable: { | ||
pattern: /[a-z]\w*(?:\s*\.\s*(?:\d+|\$?[a-z]\w*))*/i | ||
} | ||
}); | ||
|
||
delete Prism.languages.tt2['delimiter']; | ||
Prism.languages.insertBefore('tt2', 'keyword', { | ||
'delimiter': { | ||
pattern: /^(?:\[%|%%)-?|-?%]$/, | ||
alias: 'punctuation' | ||
} | ||
}); | ||
|
||
Prism.languages.insertBefore('tt2', 'string', { | ||
'single-quoted-string': { | ||
pattern: /'[^\\']*(?:\\[\s\S][^\\']*)*'/, | ||
greedy: true, | ||
alias: 'string' | ||
}, | ||
'double-quoted-string': { | ||
pattern: /"[^\\"]*(?:\\[\s\S][^\\"]*)*"/, | ||
greedy: true, | ||
alias: 'string', | ||
inside: { | ||
variable: { | ||
pattern: /\$(?:[a-z]\w*(?:\.(?:\d+|\$?[a-z]\w*))*)/i | ||
} | ||
} | ||
} | ||
}); | ||
|
||
// The different types of TT2 strings "replace" the C-like standard string | ||
delete Prism.languages.tt2.string; | ||
|
||
Prism.hooks.add('before-tokenize', function(env) { | ||
var tt2Pattern = /\[%[\s\S]+?%\]/g; | ||
Prism.languages['markup-templating'].buildPlaceholders(env, 'tt2', tt2Pattern); | ||
}); | ||
|
||
Prism.hooks.add('after-tokenize', function(env) { | ||
Prism.languages['markup-templating'].tokenizePlaceholders(env, 'tt2'); | ||
}); | ||
|
||
}(Prism)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
%] | ||
</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 supported.</li> | ||
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
[ | ||
["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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think you should add a line like
delete Prism.languages.tt2['operator'];
before you callinsertBefore
, so that theoperator
property can effectively be inserted. If it already exists, it will have unexpected behaviour.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.
Okay, I didn't know that and see whether that also fixes the FIXME below.