Permalink
Browse files

Initial commit of liquid-mode and tests

  • Loading branch information...
1 parent 0eb04d5 commit 0937eca67d76a87e4c9fb62131ff3b0997caca9c @rehanift committed Sep 21, 2010
Showing with 160 additions and 0 deletions.
  1. +44 −0 liquid.el
  2. +26 −0 tests/assignment.liquid
  3. +1 −0 tests/comment.liquid
  4. +9 −0 tests/cycle.liquid
  5. +23 −0 tests/for.liquid
  6. +57 −0 tests/ifelse.liquid
View
@@ -0,0 +1,44 @@
+;;; NOTES:
+;;; - two syntax matching types:
+;;; 1) "regex" . font-lock-name
+;;; 2) "regex" (capture-group-id font-lock-name)
+;;; - variable name regex: \\(?:\\w\\|\\.\\|_\\)+
+;;;
+;;; TODO:
+;;; - strings can be in single quotes
+;;; - detect string vs. object value types
+;;; - detect boolean operators (and|or)
+;;; - add groups for cycle tags
+;;; - for:
+;;; - add limit:n, offset:n
+;;; - add reversed
+;;; - add support for ranges
+
+(setq liquidKeywords
+ '(
+ ;;; core stuff
+ ("{%\\|%}\\|{{\\|}}" . font-lock-comment-face) ;;; liquid tag delimiters
+ ("{%\s*\\(assign\\|capture\\|endcapture\\|for\\|endfor\\|if\\|endif\\|comment\\|endcomment\\|else\\|elsif\\|unless\\|endunless\\|case\\|when\\|endcase\\|cycle\\)" (1 font-lock-keyword-face)) ;;; liquid construct tags
+ ("forloop" . font-lock-keyword-face)
+ ("forloop.\\(length\\|index0\\|index\\|rindex0\\|rindex\\|first\\|last\\)" (1 font-lock-variable-name-face))
+ ("{%\s*\\(?:assign\\|capture\\|for\\|if\\|unless\\|case\\|when\\)\s+\\(\\(?:\\w\\|\\.\\|_\\)+\\)" (1 font-lock-variable-name-face)) ;;; variable after assign|capture|for|if
+
+ ("{{\s*\\(\\(?:\\w\\|\\.\\)+\\)" (1 font-lock-variable-name-face)) ;;; variable/object being outputted
+
+ ;;; filter stuff (hack, only supports 2 chained filters)
+ ("\s+|\s+" . font-lock-comment-face) ;;; liquid tag delimiters
+ ("{{\s*\\(?:\\w\\|\\.\\)+\s+|\s+\\(\\w+\\)" (1 font-lock-type-face)) ;;; variable after assign|capture|for|if
+ ("{{\s*\\(?:\\w\\|\\.\\)+\s+|\s+\\w+\s+|\s+\\(\\w+\\)" (1 font-lock-type-face)) ;;; variable after assign|capture|for|if
+
+ ;;; if/else stuff
+ ("{%\s*\\(?:if\\|unless\\)\s+\\(?:\\w\\|\\.\\)+\s+\\(contains\\|>\\|<\\|==\\|!=\\)" (1 font-lock-keyword-face)) ;;; liquid operators
+
+ ;;; for loop stuff
+ ("{%\s*for\s+\\w+\s+\\(in\\)" (1 font-lock-keyword-face)) ;;; the 'in' in "for temp in collection"
+ ("{%\s*for\s+\\w+\s+in\s+\\(\\(?:\\w\\|\\.\\|_\\)+\\)" (1 font-lock-variable-name-face)) ;;; the 'collection' in "for temp in collection"
+ )
+ )
+(define-derived-mode liquid-mode html-mode
+ (setq font-lock-defaults '(liquidKeywords))
+ (setq mode-name "liquid mode")
+ )
View
@@ -0,0 +1,26 @@
+{% assign name = 'freestyle' %}
+
+{% for t in collections.tags %}{% if t == name %}
+ <p>Freestyle!</p>
+{% endif %}{% endfor %}
+
+{% assign freestyle = false %}
+
+{% for t in collections.tags %}{% if t == 'freestyle' %}
+ {% assign freestyle = true %}
+{% endif %}{% endfor %}
+
+{% if freestyle %}
+ <p>Freestyle!</p>
+{% endif %}
+
+{% capture attribute_name %}{{ item.title | handleize }}-{{ i }}-color{% endcapture %}
+
+ <label for="{{ attribute_name }}">Color:</label>
+ <select name="attributes[{{ attribute_name }}]" id="{{ attribute_name }}">
+ <option value="red">Red</option>
+ <option value="green">Green</option>
+ <option value="blue">Blue</option>
+ </select>
+
+{{ foo | barize | bazize }}
View
@@ -0,0 +1 @@
+We made 1 million dollars {% comment %} in losses {% endcomment %} this year
View
@@ -0,0 +1,9 @@
+{% cycle 'one', 'two', 'three' %}
+{% cycle 'one', 'two', 'three' %}
+{% cycle 'one', 'two', 'three' %}
+{% cycle 'one', 'two', 'three' %}
+
+{% cycle 'group 1': 'one', 'two', 'three' %}
+{% cycle 'group 1': 'one', 'two', 'three' %}
+{% cycle 'group 2': 'one', 'two', 'three' %}
+{% cycle 'group 2': 'one', 'two', 'three' %}
View
@@ -0,0 +1,23 @@
+{% for item in array %}
+ {{ forloop.length }}
+ {{ forloop.index }}
+ {{ forloop.index0 }}
+ {{ forloop.rindex }}
+ {{ forloop.rindex0 }}
+ {{ forloop.first }}
+ {{ forloop.last }}
+{% endfor %}
+
+# array = [1,2,3,4,5,6]
+{% for item in array limit:2 offset:2 %}
+ {{ item }}
+{% endfor %}
+# results in 3,4
+
+{% for item in collection reversed %} {{item}} {% endfor %}
+
+# if item.quantity is 4...
+{% for i in (1..item.quantity) %}
+ {{ i }}
+{% endfor %}
+# results in 1,2,3,4
View
@@ -0,0 +1,57 @@
+{% if user %}
+ Hello {{ user.name }}
+{% endif %}
+
+{% if user.name == 'tobi' %}
+ Hello tobi
+{% elsif user.name == 'bob' %}
+ Hello bob
+{% endif %}
+
+{% if user.name == 'tobi' or user.name == 'bob' %}
+ Hello tobi or bob
+{% endif %}
+
+{% if user.name == 'bob' and user.age > 45 %}
+ Hello old bob
+{% endif %}
+
+{% if user.name != 'tobi' %}
+ Hello non-tobi
+{% endif %}
+
+# Same as above
+{% unless user.name == 'tobi' %}
+ Hello non-tobi
+{% endunless %}
+
+# Check if the user has a credit card
+{% if user.creditcard != null %}
+ poor sob
+{% endif %}
+
+# Same as above
+{% if user.creditcard %}
+ poor sob
+{% endif %}
+
+# Check for an empty array
+{% if user.payments == empty %}
+ you never paid !
+{% endif %}
+
+{% if user.age > 18 %}
+ Login here
+{% else %}
+ Sorry, you are too young
+{% endif %}
+
+# array = 1,2,3
+{% if array contains 2 %}
+ array inxcludes 2
+{% endif %}
+
+# string = 'hello world'
+{% if string contains "hello" %}
+ string includes 'hello'
+{% endif %}

0 comments on commit 0937eca

Please sign in to comment.