Skip to content
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 17 commits into from
May 10, 2018
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions components.json
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,11 @@
"require": "markup",
"owner": "Golmote"
},
"tt2": {
"title": "Template Toolkit 2",
"require": ["clike", "markup-templating"],
"owner": "gflohr"
},
"twig": {
"title": "Twig",
"require": "markup",
Expand Down
59 changes: 59 additions & 0 deletions components/prism-tt2.js
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', {
Copy link
Contributor

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 call insertBefore, so that the operator property can effectively be inserted. If it already exists, it will have unexpected behaviour.

Copy link
Contributor Author

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.

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));
61 changes: 61 additions & 0 deletions examples/prism-tt2.html
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 # &lt;aol&gt;me too!&lt;/aol&gt;
%]
</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") %]
&lt;a href="[% post.permalink %]"&gt;[% post.title | html %]&lt;/a&gt;
[% 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>
89 changes: 89 additions & 0 deletions tests/languages/markup+tt2/tt2_in_markup_feature.test
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.
36 changes: 36 additions & 0 deletions tests/languages/tt2/comment_feature.test
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.
18 changes: 18 additions & 0 deletions tests/languages/tt2/delimiter_feature.test
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.
Loading