Skip to content

Commit

Permalink
Liquid: Added Markup support, missing tokens, and other improvements (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment committed Jun 15, 2021
1 parent abab910 commit ac1d12f
Show file tree
Hide file tree
Showing 16 changed files with 921 additions and 110 deletions.
2 changes: 1 addition & 1 deletion components.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions components.json
Expand Up @@ -732,6 +732,7 @@
},
"liquid": {
"title": "Liquid",
"require": "markup-templating",
"owner": "cinhtau"
},
"lisp": {
Expand Down
65 changes: 57 additions & 8 deletions components/prism-liquid.js
@@ -1,12 +1,61 @@
Prism.languages.liquid = {
'keyword': /\b(?:comment|endcomment|if|elsif|else|endif|unless|endunless|for|endfor|case|endcase|when|in|break|assign|continue|limit|offset|range|reversed|raw|endraw|capture|endcapture|tablerow|endtablerow)\b/,
'number': /\b0b[01]+\b|\b0x(?:\.[\da-fp-]+|[\da-f]+(?:\.[\da-fp-]+)?)\b|(?:\b\d+(?:\.\d*)?|\B\.\d+)(?:e[+-]?\d+)?[df]?/i,
'operator': {
pattern: /(^|[^.])(?:\+[+=]?|-[-=]?|!=?|<<?=?|>>?>?=?|==?|&[&=]?|\|[|=]?|\*=?|\/=?|%=?|\^=?|[?:~])/m,
'comment': {
pattern: /(^\{%\s*comment\s*%\})[\s\S]+(?=\{%\s*endcomment\s*%\}$)/,
lookbehind: true
},
'function': {
pattern: /(^|[\s;|&])(?:append|prepend|capitalize|cycle|cols|increment|decrement|abs|at_least|at_most|ceil|compact|concat|date|default|divided_by|downcase|escape|escape_once|first|floor|join|last|lstrip|map|minus|modulo|newline_to_br|plus|remove|remove_first|replace|replace_first|reverse|round|rstrip|size|slice|sort|sort_natural|split|strip|strip_html|strip_newlines|times|truncate|truncatewords|uniq|upcase|url_decode|url_encode|include|paginate)(?=$|[\s;|&])/,
lookbehind: true
}
'delimiter': {
pattern: /^\{(?:\{\{|[%\{])-?|-?(?:\}\}|[%\}])\}$/,
alias: 'punctuation'
},
'string': {
pattern: /"[^"]*"|'[^']*'/,
greedy: true
},
'keyword': /\b(?:as|assign|break|continue|cycle|decrement|echo|else|elsif|(?:end)?(?:capture|case|comment|for|form|if|paginate|style|raw|tablerow|unless)|in|include|increment|limit|liquid|offset|range|render|reversed|section|when|with)\b/,
'function': [
{
pattern: /(\|\s*)\w+/,
lookbehind: true,
alias: 'filter'
},
{
// array functions
pattern: /(\.\s*)(?:first|last|size)/,
lookbehind: true
}
],
'boolean': /\b(?:true|false|nil)\b/,
'range': {
pattern: /\.\./,
alias: 'operator'
},
// https://github.com/Shopify/liquid/blob/698f5e0d967423e013f6169d9111bd969bd78337/lib/liquid/lexer.rb#L21
'number': /\b\d+(?:\.\d+)?\b/,
'operator': /[!=]=|<>|[<>]=?|[|?:=-]|\b(?:and|or|contains(?=\s))\b/,
'punctuation': /[.,\[\]()]/
};

Prism.hooks.add('before-tokenize', function (env) {
var liquidPattern = /\{%\s*comment\s*%\}[\s\S]*?\{%\s*endcomment\s*%\}|\{(?:%[\s\S]*?%|\{\{[\s\S]*?\}\}|\{[\s\S]*?\})\}/g;
var insideRaw = false;

Prism.languages['markup-templating'].buildPlaceholders(env, 'liquid', liquidPattern, function (match) {
var tagMatch = /^\{%-?\s*(\w+)/.exec(match);
if (tagMatch) {
var tag = tagMatch[1];
if (tag === 'raw' && !insideRaw) {
insideRaw = true;
return true;
} else if (tag === 'endraw') {
insideRaw = false;
return true;
}
}

return !insideRaw;
});
});

Prism.hooks.add('after-tokenize', function (env) {
Prism.languages['markup-templating'].tokenizePlaceholders(env, 'liquid');
});
2 changes: 1 addition & 1 deletion components/prism-liquid.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions plugins/autoloader/prism-autoloader.js
Expand Up @@ -88,6 +88,7 @@
],
"less": "css",
"lilypond": "scheme",
"liquid": "markup-templating",
"markdown": "markup",
"markup-templating": "markup",
"mongodb": "javascript",
Expand Down
2 changes: 1 addition & 1 deletion plugins/autoloader/prism-autoloader.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions tests/languages/liquid/boolean_feature.test
@@ -0,0 +1,26 @@
{% if true and false and false or true %}

{% nil %}

----------------------------------------------------

[
["liquid", [
["delimiter", "{%"],
["keyword", "if"],
["boolean", "true"],
["operator", "and"],
["boolean", "false"],
["operator", "and"],
["boolean", "false"],
["operator", "or"],
["boolean", "true"],
["delimiter", "%}"]
]],

["liquid", [
["delimiter", "{%"],
["boolean", "nil"],
["delimiter", "%}"]
]]
]
61 changes: 61 additions & 0 deletions tests/languages/liquid/comment_feature.test
@@ -0,0 +1,61 @@
My name is Wilson Abercrombie{% comment %}, esquire{% endcomment %}.

{% assign verb = "turned" %}
{% comment %}
{% assign verb = "converted" %}
{% endcomment %}
Anything you put between {% comment %} and {% endcomment %} tags
is {{ verb }} into a comment.

----------------------------------------------------

[
"My name is Wilson Abercrombie",
["liquid", [
["delimiter", "{%"],
["keyword", "comment"],
["delimiter", "%}"],
["comment", ", esquire"],
["delimiter", "{%"],
["keyword", "endcomment"],
["delimiter", "%}"]
]],
".\r\n\r\n",

["liquid", [
["delimiter", "{%"],
["keyword", "assign"],
" verb ",
["operator", "="],
["string", "\"turned\""],
["delimiter", "%}"]
]],

["liquid", [
["delimiter", "{%"],
["keyword", "comment"],
["delimiter", "%}"],
["comment", "\r\n{% assign verb = \"converted\" %}\r\n"],
["delimiter", "{%"],
["keyword", "endcomment"],
["delimiter", "%}"]
]],

"\r\nAnything you put between ",
["liquid", [
["delimiter", "{%"],
["keyword", "comment"],
["delimiter", "%}"],
["comment", " and "],
["delimiter", "{%"],
["keyword", "endcomment"],
["delimiter", "%}"]
]],
" tags\r\nis ",
["liquid", [
["delimiter", "{{"],
" verb ",
["delimiter", "}}"]
]],
" into a comment."
]

0 comments on commit ac1d12f

Please sign in to comment.