From f0b2c9e814bfaeaba175d915a138c8c36c0b982f Mon Sep 17 00:00:00 2001 From: Tan-Vinh Nguyen Date: Sun, 4 Mar 2018 15:43:39 +0100 Subject: [PATCH] Basic Syntax Highlighting for Liquid (#1326) * Basic Syntax Highlighting for Liquid [Liquid](http://shopify.github.io/liquid/), a template language by shopify It is used in Jekyll and add some basic highlighting * fix typo * Corrections and Improvements - move liquid above livescript - regnerate languages for show language plugin, after fixing typo - keywords - tablerow and endtablerow are keywords - add all keywords in test - add funtcions test * Minor Corrections comply to whitespace settings, use tabs instead of spaces for indentation --- components.js | 4 + components/prism-liquid.js | 12 +++ components/prism-liquid.min.js | 1 + examples/prism-liquid.html | 78 ++++++++++++++++++++ tests/languages/liquid/function_feature.test | 39 ++++++++++ tests/languages/liquid/keyword_feature.test | 29 ++++++++ tests/languages/liquid/number_feature.test | 27 +++++++ tests/languages/liquid/operator_feature.test | 33 +++++++++ 8 files changed, 223 insertions(+) create mode 100644 components/prism-liquid.js create mode 100644 components/prism-liquid.min.js create mode 100644 examples/prism-liquid.html create mode 100644 tests/languages/liquid/function_feature.test create mode 100644 tests/languages/liquid/keyword_feature.test create mode 100644 tests/languages/liquid/number_feature.test create mode 100644 tests/languages/liquid/operator_feature.test diff --git a/components.js b/components.js index 4472716092..20ec389e35 100644 --- a/components.js +++ b/components.js @@ -350,6 +350,10 @@ var components = { "require": "css", "owner": "Golmote" }, + "liquid": { + "title": "Liquid", + "owner": "cinhtau" + }, "livescript": { "title": "LiveScript", "owner": "Golmote" diff --git a/components/prism-liquid.js b/components/prism-liquid.js new file mode 100644 index 0000000000..0c23178b8a --- /dev/null +++ b/components/prism-liquid.js @@ -0,0 +1,12 @@ +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-f]*\.?[\da-fp\-]+\b|\b\d*\.?\d+(?:e[+-]?\d+)?[df]?\b/i, + 'operator': { + pattern: /(^|[^.])(?:\+[+=]?|-[-=]?|!=?|<>?>?=?|==?|&[&=]?|\|[|=]?|\*=?|\/=?|%=?|\^=?|[?:~])/m, + 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 + } +}; diff --git a/components/prism-liquid.min.js b/components/prism-liquid.min.js new file mode 100644 index 0000000000..142205453a --- /dev/null +++ b/components/prism-liquid.min.js @@ -0,0 +1 @@ +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-f]*\.?[\da-fp\-]+\b|\b\d*\.?\d+(?:e[+-]?\d+)?[df]?\b/i,operator:{pattern:/(^|[^.])(?:\+[+=]?|-[-=]?|!=?|<>?>?=?|==?|&[&=]?|\|[|=]?|\*=?|\/=?|%=?|\^=?|[?:~])/m,lookbehind:!0},"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:!0}}; \ No newline at end of file diff --git a/examples/prism-liquid.html b/examples/prism-liquid.html new file mode 100644 index 0000000000..46951c384b --- /dev/null +++ b/examples/prism-liquid.html @@ -0,0 +1,78 @@ +

Liquid

+

To use this language, use the class "language-liquid".

+ +

Comments

+
{% comment %}This is a comment{% endcomment %}
+ +

Control Flow

+ +Liquid provides multiple control flow statements. + +

if

+

+{% if customer.name == 'kevin' %}
+  Hey Kevin!
+{% elsif customer.name == 'anonymous' %}
+  Hey Anonymous!
+{% else %}
+  Hi Stranger!
+{% endif %}
+
+ +

unless

+ +The opposite of if – executes a block of code only if a certain condition is not met. + +

+{% unless product.title == 'Awesome Shoes' %}
+These shoes are not awesome.
+{% endunless %}
+
+ +

case

+ +Creates a switch statement to compare a variable with different values. case initializes the switch statement, and when compares its values. + +

+{% assign handle = 'cake' %}
+{% case handle %}
+  {% when 'cake' %}
+    This is a cake
+  {% when 'cookie' %}
+    This is a cookie
+  {% else %}
+    This is not a cake nor a cookie
+{% endcase %}
+
+ +

for

+ +Repeatedly executes a block of code. + +break = Causes the loop to stop iterating when it encounters the break tag. +continue = Causes the loop to skip the current iteration when it encounters the continue tag. + +

+{% for i in (1..10) %}
+  {% if i == 4 %}
+    {% break %}
+  {% elsif i == 6 %}
+    {% continue %}
+  {% else %}
+    {{ i }}
+  {% endif %}
+{% endfor %}
+
+ +

range

+ +

+{% for i in (3..5) %}
+  {{ i }}
+{% endfor %}
+
+{% assign num = 4 %}
+{% for i in (1..num) %}
+  {{ i }}
+{% endfor %}
+
diff --git a/tests/languages/liquid/function_feature.test b/tests/languages/liquid/function_feature.test new file mode 100644 index 0000000000..4689ef6a49 --- /dev/null +++ b/tests/languages/liquid/function_feature.test @@ -0,0 +1,39 @@ +abs append at_least at_most +capitalize ceil cols compact concat cycle +date decrement default divided_by downcase +escape escape_once +first floor +include increment +join +last lstrip +map minus modulo +newline_to_br +paginate plus prepend +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 + +---------------------------------------------------- + +[ + ["function", "abs"], ["function", "append"], ["function", "at_least"], ["function", "at_most"], + ["function", "capitalize"], ["function", "ceil"], ["function", "cols"], ["function", "compact"], ["function", "concat"], ["function", "cycle"], + ["function", "date"], ["function", "decrement"], ["function", "default"], ["function", "divided_by"], ["function", "downcase"], + ["function", "escape"], ["function", "escape_once"], + ["function", "first"], ["function", "floor"], + ["function", "include"], ["function", "increment"], + ["function", "join"], + ["function", "last"], ["function", "lstrip"], + ["function", "map"], ["function", "minus"], ["function", "modulo"], + ["function", "newline_to_br"], + ["function", "paginate"], ["function", "plus"], ["function", "prepend"], + ["function", "remove"], ["function", "remove_first"], ["function", "replace"], ["function", "replace_first"], ["function", "reverse"], ["function", "round"], ["function", "rstrip"], + ["function", "size"], ["function", "slice"], ["function", "sort"], ["function", "sort_natural"], ["function", "split"], ["function", "strip"], ["function", "strip_html"], ["function", "strip_newlines"], + ["function", "times"], ["function", "truncate"], ["function", "truncatewords"], + ["function", "uniq"], ["function", "upcase"], ["function", "url_decode"], ["function", "url_encode"] +] + +---------------------------------------------------- + +Checks for functions. Also checks for unicode characters in identifiers. diff --git a/tests/languages/liquid/keyword_feature.test b/tests/languages/liquid/keyword_feature.test new file mode 100644 index 0000000000..84ffabd96a --- /dev/null +++ b/tests/languages/liquid/keyword_feature.test @@ -0,0 +1,29 @@ +comment endcomment +if else elsif endif +unless endunless +for endfor in break +case endcase when +assign continue +limit offset range reversed +raw endraw +capture endcapture +tablerow endtablerow + +---------------------------------------------------- + +[ + ["keyword", "comment"], ["keyword", "endcomment"], + ["keyword", "if"], ["keyword", "else"], ["keyword", "elsif"], ["keyword", "endif"], + ["keyword", "unless"], ["keyword", "endunless"], + ["keyword", "for"], ["keyword", "endfor"], ["keyword", "in"], ["keyword", "break"], + ["keyword", "case"], ["keyword", "endcase"], ["keyword", "when"], + ["keyword", "assign"], ["keyword", "continue"], + ["keyword", "limit"], ["keyword", "offset"], ["keyword", "range"], ["keyword", "reversed"], + ["keyword", "raw"], ["keyword", "endraw"], + ["keyword", "capture"], ["keyword", "endcapture"], + ["keyword", "tablerow"], ["keyword", "endtablerow"] +] + +---------------------------------------------------- + +Checks for all keywords. \ No newline at end of file diff --git a/tests/languages/liquid/number_feature.test b/tests/languages/liquid/number_feature.test new file mode 100644 index 0000000000..b88f082c19 --- /dev/null +++ b/tests/languages/liquid/number_feature.test @@ -0,0 +1,27 @@ +0b11110000 +0xBadFace +0x1.8p1 +0xa.fp-2 +42 +42d +1.2e3f +0.1E-4f +0.2e+1f + +---------------------------------------------------- + +[ + ["number", "0b11110000"], + ["number", "0xBadFace"], + ["number", "0x1.8p1"], + ["number", "0xa.fp-2"], + ["number", "42"], + ["number", "42d"], + ["number", "1.2e3f"], + ["number", "0.1E-4f"], + ["number", "0.2e+1f"] +] + +---------------------------------------------------- + +Checks for binary, hexadecimal and decimal numbers. \ No newline at end of file diff --git a/tests/languages/liquid/operator_feature.test b/tests/languages/liquid/operator_feature.test new file mode 100644 index 0000000000..87f4906b6d --- /dev/null +++ b/tests/languages/liquid/operator_feature.test @@ -0,0 +1,33 @@ ++ ++ += +- -- -= +! != +< << <= <<= +> >> >>> >= >>= >>>= += == +& && &= +| || |= +? : ~ +* *= +/ /= +% %= + +---------------------------------------------------- + +[ + ["operator", "+"], ["operator", "++"], ["operator", "+="], + ["operator", "-"], ["operator", "--"], ["operator", "-="], + ["operator", "!"], ["operator", "!="], + ["operator", "<"], ["operator", "<<"], ["operator", "<="], ["operator", "<<="], + ["operator", ">"], ["operator", ">>"], ["operator", ">>>"], ["operator", ">="], ["operator", ">>="], ["operator", ">>>="], + ["operator", "="], ["operator", "=="], + ["operator", "&"], ["operator", "&&"], ["operator", "&="], + ["operator", "|"], ["operator", "||"], ["operator", "|="], + ["operator", "?"], ["operator", ":"], ["operator", "~"], + ["operator", "*"], ["operator", "*="], + ["operator", "/"], ["operator", "/="], + ["operator", "%"], ["operator", "%="] +] + +---------------------------------------------------- + +Checks for all operators. \ No newline at end of file