From: noah morrison Date: Wed, 5 Nov 2014 02:30:36 +0000 (-0500) Subject: Removed cgi import X-Git-Url: https://git.devinivas.org/?a=commitdiff_plain;h=44d86498c405d0226cdb8e092bb9f2140960474a;p=chevron.git Removed cgi import 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. --- diff --git a/data.json b/data.json index b68ba41..c6a05b4 100644 --- a/data.json +++ b/data.json @@ -2,6 +2,7 @@ "thing": "world", "html_escaped": "no escape!", "html_escaped2": "ok", + "html_escaped3": " this is escaped though ", "show": true, "delimiter": { "1": "do delimiters work?", diff --git a/entei.py b/entei.py index 14ce32a..ae11452 100755 --- 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;', + '&': '&', + '<': '<', + '>': '>', + } + + 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: diff --git a/test.mustache b/test.mustache index 095ca64..dd2cdf0 100644 --- a/test.mustache +++ b/test.mustache @@ -6,6 +6,7 @@ Hello, {{ thing }}! {{{html_escaped}}} {{&html_escaped2}} +{{html_escaped3}} {{#show}} Show is true!