forked from kangfuzi/tornado
-
Notifications
You must be signed in to change notification settings - Fork 0
/
escape.py
54 lines (35 loc) · 1.07 KB
/
escape.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python
# fileencoding=utf-8
import json
from HTMLParser import HTMLParser
from bson.objectid import ObjectId
import tornado.escape
type_encoders = [
(ObjectId, str),
]
def objectid_encoder(obj):
for encoder in type_encoders:
if isinstance(obj, encoder[0]):
return encoder[1](obj)
raise TypeError("Unknown value '%s' of type %s" % (
obj, type(obj)))
def json_encode(value, ensure_ascii=False, indent=None):
# adapted from tornado.escape.json_encode
return json.dumps(
value, default=objectid_encoder,
ensure_ascii=ensure_ascii,
indent=indent).replace("</", "<\\/")
json_decode = tornado.escape.json_decode
# http://stackoverflow.com/questions/753052/strip-html-from-strings-in-python
class _MLStripper(HTMLParser):
def __init__(self):
self.reset()
self.fed = []
def handle_data(self, d):
self.fed.append(d)
def get_data(self):
return ''.join(self.fed)
def strip_html_tags(html):
s = _MLStripper()
s.feed(html)
return s.get_data()