/
functions.py
53 lines (48 loc) · 1.84 KB
/
functions.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
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# For code highlight
import re
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.styles import get_style_by_name
from pygments.formatters import HtmlFormatter
def html_parser(value):
value = re.sub('(?is)(<script[^>]*>.*?</script[^>]*>|<link[^>/]*/?>)', '', value).strip() # cleaning
if value:
formatter = HtmlFormatter(nowrap=True, lineseparator='<br/>', style=get_style_by_name('native'))
def render_raw(match):
lexer = get_lexer_by_name(match.groups()[0])
return '<code class="highlight">%s</code>' % highlight(match.groups()[1], lexer, formatter)
value = re.sub('(?is)<code[^>]*language="(\w+)"[^>]*>(.*?)</code[^>]*>', render_raw, value)
return value
def int2bin(num):
res = []
while num:
res.append(num % 2)
num = num / 2
return res
def binary_date(date):
res = '<div class="day-line">'
for i in int2bin(date.day):
res += '<span class="bit-%d"></span>' % i
res += '</div><div class="month-line">'
for i in int2bin(date.month):
res += '<span class="bit-%d"></span>' % i
res += '</div><div class="year-line">'
for i in int2bin(date.year % 100):
res += '<span class="bit-%d"></span>' % i
res += '</div>'
return res
def formatted_date(date):
res = u"""
<span class="date-day">%d</span>
<span class="date-month">%s</span>
<span class="date-year">%d</span>
<span class="date-time">%s</span>
""" % (
date.day,
[u'Jan', u'Feb', u'Mar', u'Apr', u'May', u'Jun', u'Jul', u'Aug', u'Sep', u'Oct', u'Nov', u'Dec'][date.month - 1],
date.year,
date.time().strftime('%H:%M')
)
return res