-
Notifications
You must be signed in to change notification settings - Fork 0
/
cartello.py
163 lines (150 loc) · 5.65 KB
/
cartello.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import mistune
import shutil
import os
import glob
class KanbanRenderer(mistune.Renderer):
closeHeader = False
openList = True
colors = ['is-warning', 'is-info', 'is-success', 'is-link', 'is-primary', 'is-danger']
headerCnt = 0
singleHeaderCheck = False
strippedNames = []
def header(self, text, level, raw=None):
html = ""
if level == 3:
if not self.closeHeader:
self.closeHeader = True
else:
html += "</div></div>"
html += """<div class="column">
<div class="message %s">
<header class="message-header">
<p class="message-header-title">
%s
</p>
</header>
<div class="message-content">
<div>
""" % (self.colors[self.headerCnt], text)
self.headerCnt += 1
elif level == 1:
if self.singleHeaderCheck:
raise Exception('Only a single h1 allowed. Specify multiple projects in different files.')
else:
self.singleHeaderCheck = True
boards = self.strippedNames
html += """
<div class="hero-head">
<div class="container">
<nav class="navbar is-transparent is-boxed">
<div class="navbar-menu">
<div class="navbar-end">
<p class="navbar-item">
%s
</p>""" % text
if len(boards) > 0:
html += """<div class="navbar-item has-dropdown is-hoverable">
<a class="navbar-link">
Other boards
</a>"""
for item in boards:
html += """
<div class="navbar-dropdown is-boxed">
<a class="navbar-item" href="/%s.html">
%s
</a>
</div>
</div>""" % (item, item.replace('_', ' '))
html += """</div></div></nav></div></div>
<div class="hero-body">
<div class="container">
<div class="columns is-multiline">
"""
self.headerCnt = 0
else:
raise Exception('Only header of level 1 and 3 are supported')
return html
def list(self, *args, **kwargs):
self.openList = True
return '</div></div>' + super().list(*args, **kwargs)
def codespan(self, tag, **kwargs):
color = ''
if '|' in tag:
cnt = tag.index('|')
color = tag[:cnt]
if 'is-' not in color[:3]:
color = 'is-' + color
tag = tag[cnt+1:]
if color not in self.colors:
raise Exception('Not a valid Bulma color tag')
html = '<span class="tag is-small %s">%s</span>' % (color, tag)
return html
def list_item(self, body, ordered=True):
html = ""
if self.openList:
html += '<div class="box">'
self.openList = False
else:
html += "<hr>"
if ': ' in body:
idx = body.index(': ')
title = body[:idx]
content = body[idx+1:]
html += """<div class="content">
<strong>%s</strong>
<br>
%s
</div>
""" %(title, content)
else:
html += """<div class="content">
<strong>%s</strong>
</div>
""" %(body)
return html
def process_markdown(fp, current, strippedNames):
cs = set(); cs.add(current)
renderer = KanbanRenderer(escape=False)
renderer.strippedNames = tuple(sorted(set(strippedNames) - cs)) # ugly
markdown = mistune.Markdown(renderer=renderer)
with open(fp, 'r') as f:
content = f.read()
body = markdown(content)
result = """<html lang="en">
<head>
<meta charset="utf-8">
<meta name="description" content="Cartello, markdown kanban board">
<title>Cartello</title>
<link rel="stylesheet" href="./bulma.min.css">
</head>
<body>
<section class="hero is-fullheight is-light is-bold">
""" + body + """
</div></div></div></div></div>
<div class="hero-foot">
<div class="section">
<div class="container">
<div class="content has-text-centered has-text-grey">
<p> Create kanban boards to projects from flat markdown files.</p>
<a href="https://github.com/framecca/cartello" class="is-click">
Fork it on Github.
</a>
</div>
</div>
</div>
</div>
</body></html>
"""
return result
if __name__ == '__main__':
outputDir = 'site'
files = tuple(sorted(glob.glob('boards/*.md')))
strippedNames = [''.join(fp.split('/')[1:])[:-3] for fp in files]
for f in glob.glob(outputDir + '/*'):
os.remove(f)
shutil.copyfile('./css/bulma.min.css', outputDir + '/bulma.min.css')
for fp, name in zip(files, strippedNames):
text = process_markdown(fp, name, strippedNames)
print("Processing '" + name + "'")
with open(outputDir + '/' + name +'.html', 'w') as out:
out.write(text)