Skip to content

Commit

Permalink
Basic Syntax Highlighting for Liquid (#1326)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
cinhtau authored and Golmote committed Mar 4, 2018
1 parent 109bd6f commit f0b2c9e
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 0 deletions.
4 changes: 4 additions & 0 deletions components.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,10 @@ var components = {
"require": "css",
"owner": "Golmote"
},
"liquid": {
"title": "Liquid",
"owner": "cinhtau"
},
"livescript": {
"title": "LiveScript",
"owner": "Golmote"
Expand Down
12 changes: 12 additions & 0 deletions components/prism-liquid.js
Original file line number Diff line number Diff line change
@@ -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
}
};
1 change: 1 addition & 0 deletions components/prism-liquid.min.js

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

78 changes: 78 additions & 0 deletions examples/prism-liquid.html
Original file line number Diff line number Diff line change
@@ -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>
39 changes: 39 additions & 0 deletions tests/languages/liquid/function_feature.test
Original file line number Diff line number Diff line change
@@ -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.
29 changes: 29 additions & 0 deletions tests/languages/liquid/keyword_feature.test
Original file line number Diff line number Diff line change
@@ -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.
27 changes: 27 additions & 0 deletions tests/languages/liquid/number_feature.test
Original file line number Diff line number Diff line change
@@ -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.
33 changes: 33 additions & 0 deletions tests/languages/liquid/operator_feature.test
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit f0b2c9e

Please sign in to comment.