Skip to content

Instantly share code, notes, and snippets.

@dallaylaen
Created January 5, 2016 20:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dallaylaen/79ef8d5caa0c36c9f735 to your computer and use it in GitHub Desktop.
Save dallaylaen/79ef8d5caa0c36c9f735 to your computer and use it in GitHub Desktop.
#!/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