]> Devi Nivas Git - chevron.git/commitdiff
initial commit
authornoah morrison <noah@morrison.ph>
Thu, 30 Oct 2014 02:43:15 +0000 (22:43 -0400)
committernoah morrison <noah@morrison.ph>
Thu, 30 Oct 2014 02:43:15 +0000 (22:43 -0400)
LICENSE [new file with mode: 0644]
README.md [new file with mode: 0644]
chevron.py [new file with mode: 0755]
test.mustache [new file with mode: 0644]

diff --git a/LICENSE b/LICENSE
new file mode 100644 (file)
index 0000000..8138aa8
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,22 @@
+The MIT License (MIT)
+
+Copyright (c) 2014 Noah Morrison
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+
diff --git a/README.md b/README.md
new file mode 100644 (file)
index 0000000..8dfb475
--- /dev/null
+++ b/README.md
@@ -0,0 +1,3 @@
+A python3 implementation of the [mustache templating language](http://mustache.github.io).
+
+Currently it only tokenizes the file, and doesn't have any nice way of changing the target file.
diff --git a/chevron.py b/chevron.py
new file mode 100755 (executable)
index 0000000..35a9901
--- /dev/null
@@ -0,0 +1,78 @@
+#!/usr/bin/python
+
+
+def tokenize(template):
+    class EOF(Exception):
+        pass
+
+    def get(amount=1):
+        return template[current:current + amount]
+
+    def look(ahead=0, amount=1):
+        idx = current + ahead
+        if idx > len(template):
+            raise EOF()
+
+        return template[idx:idx + amount]
+
+    tag_types = {
+        '!': 'comment',
+        '#': 'section',
+        '^': 'inverted section',
+        '/': 'end',
+        '>': 'partial',
+        '=': 'set delimiter?',
+        '{': 'no escape?',
+        '&': 'no escape'
+    }
+
+    current = 0
+    l_del = '{{'
+    r_del = '}}'
+    while current < len(template):
+        try:
+            size = 0
+            while look(size, 2) != l_del:
+                size += 1
+        except EOF:
+            yield ('literal', get(size))
+            return
+
+        if size != 0:
+            yield ('literal', get(size))
+
+        current += size + 2
+
+        tag_type = tag_types.get(get(), 'variable')
+        if tag_type != 'variable':
+            current += 1
+
+        size = 1
+        while look(size, 2) != r_del:
+            size += 1
+
+        tag_key = get(size).strip()
+        current += size + 2
+
+        if tag_type == 'no escape?':
+            if look(-2, 3) == '}}}':
+                tag_type = 'no escape'
+                current += 1
+
+        elif tag_type == 'set delimiter?':
+            if look(-3) == '=':
+                l_del, r_del = tag_key[:-1].split(' ')
+                continue
+
+        yield (tag_type, tag_key)
+
+    return
+
+
+if __name__ == '__main__':
+    with open('test.mustache', 'r') as f:
+        template = f.read()
+
+    tokens = tokenize(template)
+    for token in tokens:
+        print(token)
diff --git a/test.mustache b/test.mustache
new file mode 100644 (file)
index 0000000..597a2e6
--- /dev/null
@@ -0,0 +1,28 @@
+{{!
+    mustache comment!
+}}
+
+Hello, {{ thing }}!
+
+{{{<b>NO ESCAPE!</b>}}}
+{{&<i>You understand?</i>}}
+
+{{#show}}
+Show is true!
+{{/show}}
+
+{{^show}}
+Show if false!
+{{/show}}
+
+{{> partial}}
+
+{{=<< >>=}}
+
+<<delimiter>>
+
+<<={{ }}=>>
+
+{{delimiter
+
+That's all folks!