-
Notifications
You must be signed in to change notification settings - Fork 4
/
lib.py
36 lines (32 loc) · 965 Bytes
/
lib.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
#!/usr/bin/env python2
# coding=utf-8
import hashlib
import markdown2
import datetime
def gravatar(email, size="80", default="identicon"):
if not email: return None
'''生成GravatarURL'''
email = email.encode('utf-8')
email = hashlib.md5(email).hexdigest()
url = "http://1.gravatar.com/avatar/%s?s=%s&d=%s&r=G" % (
email,size,default)
return url
def escape(raw):
'''HTML转义'''
escape_dict = {
"<": "<",
">": ">",
"\"": """,
"\'": "'",
"&": "&",
}
for key in escape_dict:
raw = raw.replace(key, escape_dict[key])
return raw
def markdown_to_html(raw):
"""将markdown格式文本转换为HTML"""
html = markdown2.Markdown().convert(raw)
return html
def strtime(time, time_format="%y-%m-%d %H:%M"):
'''时间格式化,strftime的重命名,但是带默认参数'''
return datetime.datetime.strftime(time, time_format)