From fe3bc526e524c052b9751f8bc86b1a97001e10b6 Mon Sep 17 00:00:00 2001 From: Jason Kurian Date: Thu, 15 Jul 2021 12:35:26 -0400 Subject: [PATCH] Liquid: Added `empty` keyword (#2997) --- components/prism-liquid.js | 6 +- components/prism-liquid.min.js | 2 +- tests/languages/liquid/empty_feature.test | 78 +++++++++++++++++++++++ 3 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 tests/languages/liquid/empty_feature.test diff --git a/components/prism-liquid.js b/components/prism-liquid.js index 487dcc5db6..b337c19561 100644 --- a/components/prism-liquid.js +++ b/components/prism-liquid.js @@ -32,7 +32,11 @@ Prism.languages.liquid = { // 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': /[.,\[\]()]/ + 'punctuation': /[.,\[\]()]/, + 'empty': { + pattern: /\bempty\b/, + alias: 'keyword' + }, }; Prism.hooks.add('before-tokenize', function (env) { diff --git a/components/prism-liquid.min.js b/components/prism-liquid.min.js index 9afc80ef06..f33aafeba5 100644 --- a/components/prism-liquid.min.js +++ b/components/prism-liquid.min.js @@ -1 +1 @@ -Prism.languages.liquid={comment:{pattern:/(^\{%\s*comment\s*%\})[\s\S]+(?=\{%\s*endcomment\s*%\}$)/,lookbehind:!0},delimiter:{pattern:/^\{(?:\{\{|[%\{])-?|-?(?:\}\}|[%\}])\}$/,alias:"punctuation"},string:{pattern:/"[^"]*"|'[^']*'/,greedy:!0},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:!0,alias:"filter"},{pattern:/(\.\s*)(?:first|last|size)/,lookbehind:!0}],boolean:/\b(?:true|false|nil)\b/,range:{pattern:/\.\./,alias:"operator"},number:/\b\d+(?:\.\d+)?\b/,operator:/[!=]=|<>|[<>]=?|[|?:=-]|\b(?:and|or|contains(?=\s))\b/,punctuation:/[.,\[\]()]/},Prism.hooks.add("before-tokenize",function(e){var r=!1;Prism.languages["markup-templating"].buildPlaceholders(e,"liquid",/\{%\s*comment\s*%\}[\s\S]*?\{%\s*endcomment\s*%\}|\{(?:%[\s\S]*?%|\{\{[\s\S]*?\}\}|\{[\s\S]*?\})\}/g,function(e){var n=/^\{%-?\s*(\w+)/.exec(e);if(n){var t=n[1];if("raw"===t&&!r)return r=!0;if("endraw"===t)return!(r=!1)}return!r})}),Prism.hooks.add("after-tokenize",function(e){Prism.languages["markup-templating"].tokenizePlaceholders(e,"liquid")}); \ No newline at end of file +Prism.languages.liquid={comment:{pattern:/(^\{%\s*comment\s*%\})[\s\S]+(?=\{%\s*endcomment\s*%\}$)/,lookbehind:!0},delimiter:{pattern:/^\{(?:\{\{|[%\{])-?|-?(?:\}\}|[%\}])\}$/,alias:"punctuation"},string:{pattern:/"[^"]*"|'[^']*'/,greedy:!0},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:!0,alias:"filter"},{pattern:/(\.\s*)(?:first|last|size)/,lookbehind:!0}],boolean:/\b(?:true|false|nil)\b/,range:{pattern:/\.\./,alias:"operator"},number:/\b\d+(?:\.\d+)?\b/,operator:/[!=]=|<>|[<>]=?|[|?:=-]|\b(?:and|or|contains(?=\s))\b/,punctuation:/[.,\[\]()]/,empty:{pattern:/\bempty\b/,alias:"keyword"}},Prism.hooks.add("before-tokenize",function(e){var r=!1;Prism.languages["markup-templating"].buildPlaceholders(e,"liquid",/\{%\s*comment\s*%\}[\s\S]*?\{%\s*endcomment\s*%\}|\{(?:%[\s\S]*?%|\{\{[\s\S]*?\}\}|\{[\s\S]*?\})\}/g,function(e){var n=/^\{%-?\s*(\w+)/.exec(e);if(n){var t=n[1];if("raw"===t&&!r)return r=!0;if("endraw"===t)return!(r=!1)}return!r})}),Prism.hooks.add("after-tokenize",function(e){Prism.languages["markup-templating"].tokenizePlaceholders(e,"liquid")}); \ No newline at end of file diff --git a/tests/languages/liquid/empty_feature.test b/tests/languages/liquid/empty_feature.test new file mode 100644 index 0000000000..bfc7fefaa3 --- /dev/null +++ b/tests/languages/liquid/empty_feature.test @@ -0,0 +1,78 @@ +{% unless pages == empty %} + +

{{ pages.frontpage.not_empty_title }}

+
{{ pages.frontpage.content }}
+{% endunless %} + +---------------------------------------------------- + +[ + ["liquid", [ + ["delimiter", "{%"], + ["keyword", "unless"], + " pages ", + ["operator", "=="], + ["empty", "empty"], + ["delimiter", "%}"] + ]], + + ["comment", ""], + + ["tag", [ + ["tag", [ + ["punctuation", "<"], + "h1" + ]], + ["punctuation", ">"] + ]], + ["liquid", [ + ["delimiter", "{{"], + " pages", + ["punctuation", "."], + "frontpage", + ["punctuation", "."], + "not_empty_title ", + ["delimiter", "}}"] + ]], + ["tag", [ + ["tag", [ + ["punctuation", ""] + ]], + + ["tag", [ + ["tag", [ + ["punctuation", "<"], + "div" + ]], + ["punctuation", ">"] + ]], + ["liquid", [ + ["delimiter", "{{"], + " pages", + ["punctuation", "."], + "frontpage", + ["punctuation", "."], + "content ", + ["delimiter", "}}"] + ]], + ["tag", [ + ["tag", [ + ["punctuation", ""] + ]], + + ["liquid", [ + ["delimiter", "{%"], + ["keyword", "endunless"], + ["delimiter", "%}"] + ]] +] + +---------------------------------------------------- + +Test for the 'empty' keyword / special thing.