Commit f0b2c9e814bfaeaba175d915a138c8c36c0b982f

Tan-Vinh Nguyen 2018-03-04T15:43:39

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

diff --git a/components.js b/components.js
index 4472716..20ec389 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 0000000..0c23178
--- /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 0000000..1422054
--- /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 0000000..46951c3
--- /dev/null
+++ b/examples/prism-liquid.html
@@ -0,0 +1,78 @@
+<h1>Liquid</h1>
+<p>To use this language, use the class "language-liquid".</p>
+
+<h2>Comments</h2>
+<pre><code>{% comment %}This is a comment{% endcomment %}</code></pre>
+
+<h2>Control Flow</h2>
+
+Liquid provides multiple control flow statements.
+
+<h3>if</h3>
+<pre><code>
+{% if customer.name == 'kevin' %}
+  Hey Kevin!
+{% elsif customer.name == 'anonymous' %}
+  Hey Anonymous!
+{% else %}
+  Hi Stranger!
+{% endif %}
+</code></pre>
+
+<h3>unless</h3>
+
+The opposite of <code>if</code> – executes a block of code only if a certain condition is not met.
+
+<pre><code>
+{% unless product.title == 'Awesome Shoes' %}
+These shoes are not awesome.
+{% endunless %}
+</code></pre>
+
+<h3>case</h3>
+
+Creates a switch statement to compare a variable with different values. <code>case</code> initializes the switch statement, and <code>when</code> compares its values.
+
+<pre><code>
+{% 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 %}
+</code></pre>
+
+<h3>for</h3>
+
+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.
+
+<pre><code>
+{% for i in (1..10) %}
+  {% if i == 4 %}
+    {% break %}
+  {% elsif i == 6 %}
+    {% continue %}
+  {% else %}
+    {{ i }}
+  {% endif %}
+{% endfor %}
+</code></pre>
+
+<h3>range</h3>
+
+<pre><code>
+{% for i in (3..5) %}
+  {{ i }}
+{% endfor %}
+
+{% assign num = 4 %}
+{% for i in (1..num) %}
+  {{ i }}
+{% endfor %}
+</code></pre>
diff --git a/tests/languages/liquid/function_feature.test b/tests/languages/liquid/function_feature.test
new file mode 100644
index 0000000..4689ef6
--- /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 0000000..84ffabd
--- /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 0000000..b88f082
--- /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 0000000..87f4906
--- /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