]> Devi Nivas Git - chevron.git/commitdiff
Removed cgi import
authornoah morrison <noah@morrison.ph>
Wed, 5 Nov 2014 02:30:36 +0000 (21:30 -0500)
committernoah morrison <noah@morrison.ph>
Wed, 5 Nov 2014 02:30:36 +0000 (21:30 -0500)
Made my own html escape function to replace the cgi import.

Also updated the test.mustache and data.json files to have an
escaped string get printed.

data.json
entei.py
test.mustache

index b68ba41824294f76b68ca5099b1a3b68832fed18..c6a05b43c0c812e3058c8b1771296e56a338cac9 100644 (file)
--- a/data.json
+++ b/data.json
@@ -2,6 +2,7 @@
   "thing": "world",
   "html_escaped": "<b>no escape!</b>",
   "html_escaped2": "<i>ok</i>",
+  "html_escaped3": "<i> this is escaped though </i>",
   "show": true,
   "delimiter": {
     "1": "do delimiters work?",
index 14ce32a3b3ea12c3e62c976361802d1f7e3a4a33..ae11452f8538270758c765a6b8ee330f55aae5f0 100755 (executable)
--- a/entei.py
+++ b/entei.py
@@ -4,7 +4,6 @@ import json
 
 from sys import argv
 from io import StringIO
-from cgi import escape as html_escape
 
 
 def tokenize(template):
@@ -146,6 +145,23 @@ def render(template, data, partials_path='.', partials_ext='mustache'):
     Returns:
     A string containing the rendered template.
     """
+
+    def html_escape(string):
+        html_codes = {
+            '"': '$quot;',
+            '&': '&amp;',
+            '<': '&lt;',
+            '>': '&gt;',
+        }
+
+        def escape_char(char):
+            return html_codes.get(char, char)
+
+        try:
+            return ''.join(map(escape_char, string))
+        except TypeError:
+            return ''
+
     def get_key(key):
         for scope in scopes:
             try:
index 095ca644ddb98560f4c26f597d61782d04383808..dd2cdf03cf07f5e9695defb6ee63c3ddc15556d1 100644 (file)
@@ -6,6 +6,7 @@ Hello, {{ thing }}!
 
 {{{html_escaped}}}
 {{&html_escaped2}}
+{{html_escaped3}}
 
 {{#show}}
 Show is true!