Skip to content

Commit

Permalink
wip: fix tests and refactor language definition
Browse files Browse the repository at this point in the history
  • Loading branch information
nknapp committed Jan 6, 2020
1 parent f0f19c6 commit c33ae2f
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 52 deletions.
65 changes: 35 additions & 30 deletions src/languages/handlebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Category: template
*/
function (hljs) {

var DEBUG=false
var DEBUG=false;
var BUILT_INS = {
built_in: 'each with if else unless log lookup',
}
Expand All @@ -20,45 +20,43 @@ function (hljs) {

var HASH_PARAM_REGEX = /(".*?"|'.*?'|\[.*?\]|[^\s!"#%&'()*+,.\/;<=>@\[\\\]^`{|}~]+)=/

var IDENTIFIER_PLAIN_OR_QUOTED = {
className: DEBUG && 'identifier',
var HELPER_NAME_OR_PATH_EXPRESSION = {
className: DEBUG && 'expression',
begin: IDENFIFIER_REGEX,
lexemes: /[\w.\/]+/,
};

var EXPRESSION = hljs.inherit(IDENTIFIER_PLAIN_OR_QUOTED, {
className: DEBUG && 'expression',
});


var SUB_EXPRESSION = {
className: DEBUG && 'sub-expression',
illegal: /\}\}/,
begin: /\(/, end: /\)/,
// the "contains" is added below when all necessary sub-modes are defined
};

var HASH = {
// fka "attribute-assignment", parameters of the form 'key=value'
className: DEBUG && 'hash',
illegal: /\}\}/,
begin: HASH_PARAM_REGEX,
relevance: 0,
starts: {
endsWithParent: true,
end: /\s/,
contains: [
EXPRESSION,
HELPER_NAME_OR_PATH_EXPRESSION,
SUB_EXPRESSION,
hljs.NUMBER_MODE
]
}
};

var BLOCK_PARAMS = {
// parameters of the form '{{#with x as | y |}}...{{/with}}'
className: DEBUG && 'block-params',
begin: /as\s+\|/,
keywords: 'as',
end: /\|/,
contains: [
{
// define sub-mode in order to prevent highlighting of block-parameter named "as"
begin: /\w+/,
keywords: ''
}
Expand All @@ -67,42 +65,49 @@ function (hljs) {

var HELPER_PARAMETERS = {
className: DEBUG && 'helper-parameters',
endsWithParent: true,
relevance: 0,
contains: [
BLOCK_PARAMS, HASH, EXPRESSION, SUB_EXPRESSION, hljs.NUMBER_MODE
]
BLOCK_PARAMS, HASH, HELPER_NAME_OR_PATH_EXPRESSION, SUB_EXPRESSION, hljs.NUMBER_MODE
],
returnEnd: true
// the property "end" is defined through inheritance when the mode is used. If depends
// on the surrounding mode, but "endsWithParent" does not work here (i.e. it includes the
// end-token of the surrounding mode)
};

var OPENING_BLOCK_MUSTACHE_CONTENTS = hljs.inherit(EXPRESSION, {
keywords: BUILT_INS,
var SUB_EXPRESSION_CONTENTS = hljs.inherit(HELPER_NAME_OR_PATH_EXPRESSION, {
className: 'name',
starts: HELPER_PARAMETERS
keywords: BUILT_INS,
starts: hljs.inherit(HELPER_PARAMETERS, {
end: /\)/,
})
});

var CLOSING_BLOCK_MUSTACHE_CONTENTS = hljs.inherit(EXPRESSION, {
SUB_EXPRESSION.contains = [
SUB_EXPRESSION_CONTENTS
];

var OPENING_BLOCK_MUSTACHE_CONTENTS = hljs.inherit(HELPER_NAME_OR_PATH_EXPRESSION, {
keywords: BUILT_INS,
className: 'name',
endsWithParent: true
starts: hljs.inherit(HELPER_PARAMETERS, {
end: /}}/,
})
});


var BASIC_MUSTACHE_CONTENTS = hljs.inherit(EXPRESSION, {
className: 'name',
var CLOSING_BLOCK_MUSTACHE_CONTENTS = hljs.inherit(HELPER_NAME_OR_PATH_EXPRESSION, {
keywords: BUILT_INS,
starts: HELPER_PARAMETERS
className: 'name'
});

var SUB_EXPRESSION_CONTENTS = hljs.inherit(EXPRESSION, {

var BASIC_MUSTACHE_CONTENTS = hljs.inherit(HELPER_NAME_OR_PATH_EXPRESSION, {
className: 'name',
keywords: BUILT_INS,
starts: HELPER_PARAMETERS
starts: hljs.inherit(HELPER_PARAMETERS, {
end: /}}/,
})
});

SUB_EXPRESSION.contains = [
SUB_EXPRESSION_CONTENTS
];

var ESCAPE_MUSTACHE_WITH_PRECEEDING_BACKSLASH = {begin: /\\\{\{/, skip: true};
var PREVENT_ESCAPE_WITH_ANOTHER_PRECEEDING_BACKSLASH = {begin: /\\\\(?=\{\{)/, skip: true};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<span class="xml">Block expression variants with literal segments in paths:
...
</span><span class="hljs-template-tag">{{#<span class="hljs-name">abc</span> abcd.[lite"'ral}}segment] }}</span><span class="xml">a</span><span class="hljs-template-tag">{{/<span class="hljs-name">abc}}</span></span><span class="xml">
</span><span class="hljs-template-tag">{{#<span class="hljs-name">abc</span> abcd.[lite"'ral}}segment] }}</span><span class="xml">a</span><span class="hljs-template-tag">{{/<span class="hljs-name">abc</span>}}</span><span class="xml">
...
</span><span class="hljs-template-tag">{{#<span class="hljs-name">abc</span> abcd."lite]'ral}}segment" }}</span><span class="xml">a</span><span class="hljs-template-tag">{{/<span class="hljs-name">abc}}</span></span><span class="xml">
</span><span class="hljs-template-tag">{{#<span class="hljs-name">abc</span> abcd."lite]'ral}}segment" }}</span><span class="xml">a</span><span class="hljs-template-tag">{{/<span class="hljs-name">abc</span>}}</span><span class="xml">
...
</span><span class="hljs-template-tag">{{#<span class="hljs-name">abc</span> abcd.'lite]"ral}}segment' }}</span><span class="xml">a</span><span class="hljs-template-tag">{{/<span class="hljs-name">abc}}</span></span><span class="xml">
</span><span class="hljs-template-tag">{{#<span class="hljs-name">abc</span> abcd.'lite]"ral}}segment' }}</span><span class="xml">a</span><span class="hljs-template-tag">{{/<span class="hljs-name">abc</span>}}</span><span class="xml">
...
Done
</span>
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<span class="xml">Block expression variants with literal segments in helper name:

</span><span class="hljs-template-tag">{{#<span class="hljs-name">[ab}}c]</span> param }}</span><span class="xml">a</span><span class="hljs-template-tag">{{/<span class="hljs-name">[ab}}c]}}</span></span><span class="xml">
</span><span class="hljs-template-tag">{{#<span class="hljs-name">[ab}}c]</span> param }}</span><span class="xml">a</span><span class="hljs-template-tag">{{/<span class="hljs-name">[ab}}c]</span>}}</span><span class="xml">
...
</span><span class="hljs-template-tag">{{#<span class="hljs-name">'ab}}c'</span> param}}</span><span class="xml">a</span><span class="hljs-template-tag">{{/<span class="hljs-name">'ab}}c'}}</span></span><span class="xml">
</span><span class="hljs-template-tag">{{#<span class="hljs-name">'ab}}c'</span> param}}</span><span class="xml">a</span><span class="hljs-template-tag">{{/<span class="hljs-name">'ab}}c'</span>}}</span><span class="xml">
...
</span><span class="hljs-template-tag">{{#<span class="hljs-name">"ab}}c"</span> param}}</span><span class="xml">a</span><span class="hljs-template-tag">{{/<span class="hljs-name">"ab}}c"}}</span></span><span class="xml">
</span><span class="hljs-template-tag">{{#<span class="hljs-name">"ab}}c"</span> param}}</span><span class="xml">a</span><span class="hljs-template-tag">{{/<span class="hljs-name">"ab}}c"</span>}}</span><span class="xml">
...
</span><span class="hljs-template-tag">{{#<span class="hljs-name">""</span> param}}</span><span class="xml">a</span><span class="hljs-template-tag">{{/<span class="hljs-name">""}}</span></span><span class="xml">
</span><span class="hljs-template-tag">{{#<span class="hljs-name">""</span> param}}</span><span class="xml">a</span><span class="hljs-template-tag">{{/<span class="hljs-name">""</span>}}</span><span class="xml">
...
</span><span class="hljs-template-tag">{{#<span class="hljs-name">''</span> param}}</span><span class="xml">a</span><span class="hljs-template-tag">{{/<span class="hljs-name">''}}</span></span><span class="xml">
</span><span class="hljs-template-tag">{{#<span class="hljs-name">''</span> param}}</span><span class="xml">a</span><span class="hljs-template-tag">{{/<span class="hljs-name">''</span>}}</span><span class="xml">
...
</span><span class="hljs-template-tag">{{#<span class="hljs-name">[]</span> param}}</span><span class="xml">a</span><span class="hljs-template-tag">{{/<span class="hljs-name">[]}}</span></span><span class="xml">
</span><span class="hljs-template-tag">{{#<span class="hljs-name">[]</span> param}}</span><span class="xml">a</span><span class="hljs-template-tag">{{/<span class="hljs-name">[]</span>}}</span><span class="xml">
...
Done
</span>
6 changes: 3 additions & 3 deletions test/markup/handlebars/block-parameters-as.expect.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<span class="xml">Block parameters (using "x as | y |"):

</span><span class="hljs-template-tag">{{#<span class="hljs-name"><span class="hljs-built_in">each</span></span> filter <span class="hljs-keyword">as</span> | value index|}}</span><span class="xml"> </span><span class="hljs-template-tag">{{/<span class="hljs-name"><span class="hljs-built_in">each</span>}}</span></span><span class="xml">
</span><span class="hljs-template-tag">{{#<span class="hljs-name"><span class="hljs-built_in">each</span></span> filter <span class="hljs-keyword">as</span> | value index|}}</span><span class="xml"> </span><span class="hljs-template-tag">{{/<span class="hljs-name"><span class="hljs-built_in">each</span></span>}}</span><span class="xml">
...
</span><span class="hljs-template-tag">{{#<span class="hljs-name"><span class="hljs-built_in">with</span></span> as <span class="hljs-keyword">as</span> | as |}}</span><span class="xml"> </span><span class="hljs-template-tag">{{/<span class="hljs-name"><span class="hljs-built_in">with</span>}}</span></span><span class="xml">
</span><span class="hljs-template-tag">{{#<span class="hljs-name"><span class="hljs-built_in">with</span></span> as <span class="hljs-keyword">as</span> | as |}}</span><span class="xml"> </span><span class="hljs-template-tag">{{/<span class="hljs-name"><span class="hljs-built_in">with</span></span>}}</span><span class="xml">
...
</span><span class="hljs-template-tag">{{#<span class="hljs-name"><span class="hljs-built_in">with</span></span> (<span class="hljs-name"><span class="hljs-built_in">lookup</span></span> 'abc') <span class="hljs-keyword">as</span> | as |}}</span><span class="xml"> </span><span class="hljs-template-tag">{{/<span class="hljs-name"><span class="hljs-built_in">with</span>}}</span></span><span class="xml">
</span><span class="hljs-template-tag">{{#<span class="hljs-name"><span class="hljs-built_in">with</span></span> (<span class="hljs-name"><span class="hljs-built_in">lookup</span></span> 'abc') <span class="hljs-keyword">as</span> | as |}}</span><span class="xml"> </span><span class="hljs-template-tag">{{/<span class="hljs-name"><span class="hljs-built_in">with</span></span>}}</span><span class="xml">
...
Done
</span>
2 changes: 1 addition & 1 deletion test/markup/handlebars/block-with-param.expect.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<span class="xml">Simple block with parameters:

</span><span class="hljs-template-tag">{{#<span class="hljs-name">blockHelper</span> param1 param2}}</span><span class="xml">block content</span><span class="hljs-template-tag">{{/<span class="hljs-name">blockHelper}}</span></span><span class="xml">
</span><span class="hljs-template-tag">{{#<span class="hljs-name">blockHelper</span> param1 param2}}</span><span class="xml">block content</span><span class="hljs-template-tag">{{/<span class="hljs-name">blockHelper</span>}}</span><span class="xml">
...
Done
</span>
2 changes: 1 addition & 1 deletion test/markup/handlebars/block.expect.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<span class="xml">Simple Block:

</span><span class="hljs-template-tag">{{#<span class="hljs-name">block</span>}}</span><span class="xml">block content</span><span class="hljs-template-tag">{{/<span class="hljs-name">block}}</span></span><span class="xml">
</span><span class="hljs-template-tag">{{#<span class="hljs-name">block</span>}}</span><span class="xml">block content</span><span class="hljs-template-tag">{{/<span class="hljs-name">block</span>}}</span><span class="xml">
...
Done
</span>
8 changes: 4 additions & 4 deletions test/markup/handlebars/built-ins.expect.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<span class="xml">Built-in helpers:

</span><span class="hljs-template-tag">{{#<span class="hljs-name"><span class="hljs-built_in">if</span></span> test}}</span><span class="xml">yes</span><span class="hljs-template-tag">{{/<span class="hljs-name"><span class="hljs-built_in">if</span>}}</span></span><span class="xml">
</span><span class="hljs-template-tag">{{#<span class="hljs-name"><span class="hljs-built_in">if</span></span> test}}</span><span class="xml">yes</span><span class="hljs-template-tag">{{/<span class="hljs-name"><span class="hljs-built_in">if</span></span>}}</span><span class="xml">
...
</span><span class="hljs-template-tag">{{#<span class="hljs-name"><span class="hljs-built_in">unless</span></span> test}}</span><span class="xml">no</span><span class="hljs-template-tag">{{/<span class="hljs-name"><span class="hljs-built_in">unless</span>}}</span></span><span class="xml">
</span><span class="hljs-template-tag">{{#<span class="hljs-name"><span class="hljs-built_in">unless</span></span> test}}</span><span class="xml">no</span><span class="hljs-template-tag">{{/<span class="hljs-name"><span class="hljs-built_in">unless</span></span>}}</span><span class="xml">
...
</span><span class="hljs-template-tag">{{#<span class="hljs-name"><span class="hljs-built_in">with</span></span> test}}</span><span class="xml">abc</span><span class="hljs-template-tag">{{/<span class="hljs-name"><span class="hljs-built_in">with</span>}}</span></span><span class="xml">
</span><span class="hljs-template-tag">{{#<span class="hljs-name"><span class="hljs-built_in">with</span></span> test}}</span><span class="xml">abc</span><span class="hljs-template-tag">{{/<span class="hljs-name"><span class="hljs-built_in">with</span></span>}}</span><span class="xml">
...
</span><span class="hljs-template-tag">{{#<span class="hljs-name"><span class="hljs-built_in">each</span></span> test}}</span><span class="xml">abc</span><span class="hljs-template-tag">{{/<span class="hljs-name"><span class="hljs-built_in">each</span>}}</span></span><span class="xml">
</span><span class="hljs-template-tag">{{#<span class="hljs-name"><span class="hljs-built_in">each</span></span> test}}</span><span class="xml">abc</span><span class="hljs-template-tag">{{/<span class="hljs-name"><span class="hljs-built_in">each</span></span>}}</span><span class="xml">
...
</span><span class="hljs-template-variable">{{<span class="hljs-name"><span class="hljs-built_in">lookup</span></span> abc}}</span><span class="xml">
...
Expand Down
8 changes: 8 additions & 0 deletions test/markup/handlebars/complex-combinations.expect.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<span class="xml">Complex combinations:

</span><span class="hljs-template-tag">{{#<span class="hljs-name">with.in.path</span> (<span class="hljs-name">helper</span> abc abc=abc cde=(<span class="hljs-name">helper</span> xzx=xzx))}}</span><span class="xml"> </span><span class="hljs-template-tag">{{/<span class="hljs-name">with.in.path</span>}}</span><span class="xml">
...
</span><span class="hljs-template-tag">{{#<span class="hljs-name">with.in.path</span> (<span class="hljs-name">helper</span> abc abc='ab}}' (<span class="hljs-name"><span class="hljs-built_in">log</span></span> x)))}}</span><span class="xml"> </span><span class="hljs-template-tag">{{/<span class="hljs-name">with.in.path</span>}}</span><span class="xml">
...
Done
</span>
7 changes: 7 additions & 0 deletions test/markup/handlebars/complex-combinations.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Complex combinations:

{{#with.in.path (helper abc abc=abc cde=(helper xzx=xzx))}} {{/with.in.path}}
...
{{#with.in.path (helper abc abc='ab}}' (log x)))}} {{/with.in.path}}
...
Done
8 changes: 4 additions & 4 deletions test/markup/handlebars/raw-block.expect.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<span class="xml">Raw blocks:

</span><span class="hljs-template-tag">{{{{#<span class="hljs-name">raw</span>}}}}</span><span class="xml"> {{verbatim}} content </span><span class="hljs-template-tag">{{{{/<span class="hljs-name">raw}}}}</span></span><span class="xml"> </span><span class="hljs-template-variable">{{<span class="hljs-name">var</span>}}</span><span class="xml">
</span><span class="hljs-template-tag">{{{{#<span class="hljs-name">raw</span>}}}}</span><span class="xml"> {{verbatim}} content </span><span class="hljs-template-tag">{{{{/<span class="hljs-name">raw</span>}}}}</span><span class="xml"> </span><span class="hljs-template-variable">{{<span class="hljs-name">var</span>}}</span><span class="xml">
...
</span><span class="hljs-template-tag">{{{{#<span class="hljs-name">raw</span> param}}}}</span><span class="xml"> {{verbatim}} content </span><span class="hljs-template-tag">{{{{/<span class="hljs-name">raw}}}}</span></span><span class="xml"> </span><span class="hljs-template-variable">{{<span class="hljs-name">var</span>}}</span><span class="xml">
</span><span class="hljs-template-tag">{{{{#<span class="hljs-name">raw</span> param}}}}</span><span class="xml"> {{verbatim}} content </span><span class="hljs-template-tag">{{{{/<span class="hljs-name">raw</span>}}}}</span><span class="xml"> </span><span class="hljs-template-variable">{{<span class="hljs-name">var</span>}}</span><span class="xml">
...
</span><span class="hljs-template-tag">{{{{#<span class="hljs-name">raw</span> key=value}}}}</span><span class="xml"> {{verbatim}} content </span><span class="hljs-template-tag">{{{{/<span class="hljs-name">raw}}}}</span></span><span class="xml"> </span><span class="hljs-template-variable">{{<span class="hljs-name">var</span>}}</span><span class="xml">
</span><span class="hljs-template-tag">{{{{#<span class="hljs-name">raw</span> key=value}}}}</span><span class="xml"> {{verbatim}} content </span><span class="hljs-template-tag">{{{{/<span class="hljs-name">raw</span>}}}}</span><span class="xml"> </span><span class="hljs-template-variable">{{<span class="hljs-name">var</span>}}</span><span class="xml">
...
</span><span class="hljs-template-tag">{{{{#<span class="hljs-name">raw</span> (<span class="hljs-name">helper</span> param) key=value}}}}</span><span class="xml"> {{verbatim}} content </span><span class="hljs-template-tag">{{{{/<span class="hljs-name">raw}}}}</span></span><span class="xml"> </span><span class="hljs-template-variable">{{<span class="hljs-name">var</span>}}</span><span class="xml">
</span><span class="hljs-template-tag">{{{{#<span class="hljs-name">raw</span> (<span class="hljs-name">helper</span> param) key=value}}}}</span><span class="xml"> {{verbatim}} content </span><span class="hljs-template-tag">{{{{/<span class="hljs-name">raw</span>}}}}</span><span class="xml"> </span><span class="hljs-template-variable">{{<span class="hljs-name">var</span>}}</span><span class="xml">
...
Done
</span>

0 comments on commit c33ae2f

Please sign in to comment.