Created
January 5, 2016 20:00
-
-
Save dallaylaen/79ef8d5caa0c36c9f735 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import cgi | |
class safe: | |
def __init__(this, text): | |
this.content = text | |
def __str__(this): | |
return safe.current_method(this) | |
def as_is(this): | |
return this.content | |
def as_html(this): | |
return cgi.escape(this.content, 1) | |
@staticmethod | |
def use(new_way): | |
if (new_way == "old"): | |
safe.current_method = safe.stack.pop() | |
else: | |
safe.stack.append( safe.current_method ) | |
safe.current_method = safe.table[ new_way ] | |
safe.stack = [] | |
safe.current_method = safe.as_is | |
safe.table = { "html" : safe.as_html, "text" : safe.as_is } | |
x = safe("<b>foo</b>") | |
print "x1=", x | |
safe.use("html") | |
print "x2=", x | |
safe.use("old") | |
print "x3=", x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment