-
Notifications
You must be signed in to change notification settings - Fork 4
/
sigvehtml.py
126 lines (91 loc) · 3.17 KB
/
sigvehtml.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import sys
import re
def convert(lines):
# remove \n
lines = [line[:-1] for line in lines]
boxes = [[] for i in range(len(lines))]
for y, line in enumerate(lines):
if '#' in line:
for x, letter in enumerate(line):
if letter == '#':
boxes[y].append(x)
boxes = filter(lambda x: x[1], enumerate(boxes))
output = []
for i in range(0, len(boxes), 2):
start = boxes[i]
end = boxes[i + 1]
output.append(row())
for j in range(0, len(start[1]), 2):
topleft = start[0], start[1][j]
topright = start[0], start[1][j + 1]
bottomleft = end[0], end[1][j]
bottomright = end[0], end[1][j + 1]
width = (topright[1] - topleft[1] + 2) / 6
output.append(div(width))
# do we think it is a picture?
picture = True
for line in lines[topleft[0] + 1:bottomleft[0]]:
line = line[topleft[1] + 1:topright[1]]
if 'the' in line or 'Download' in line:
picture = False
if picture:
image = educated_guess_at_image()
output.append('<img style=width:100%%;margin-bottom:40px;display:block src="%s" />' % image)
else:
output.append('<p>\n')
for _line in lines[topleft[0] + 1:bottomleft[0]]:
line = _line[topleft[1] + 1:topright[1]]
if not line.strip():
output.append('</p>\n<p>\n')
else:
if '*' in line:
idx = line.find('*')
text = line[idx+2:].strip()
# yes, we cheat and drop the ul tags
output.append('<li><a href="%s">%s</a></li>' % (re.sub(' ','-', text.lower()), text))
else:
output.append(line)
output.append('</p>\n')
output.append(div_end())
output.append(row_end())
return header() + ''.join(output) + footer()
def row():
return '<div class="row">\n'
def div(width):
return '<div class="col-xs-%s">\n' % str(width)
def div_end():
return '</div>'
def row_end():
return div_end()
def header():
return """
<!DOCTYPE html>
<html>
<head>
<title>Sigvehtml</title>
<meta charset="utf8" />
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">
</head>
<body>
<header>
<h1>Sigvehtml</h1>
</header>
<div class="container">
"""
def footer():
return """
</div>
</body>
</html>
"""
def educated_guess_at_image():
return 'http://pensivetoaster.com/wp-content/uploads/2013/05/elephant-banner.jpg'
def main():
with open(sys.argv[1], 'r') as f:
lines = [line for line in f]
output = convert(lines)
print output
if __name__ == "__main__":
main()