forked from BonsaiDen/JavaScript-Garden
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake
More file actions
executable file
·78 lines (55 loc) · 2.07 KB
/
make
File metadata and controls
executable file
·78 lines (55 loc) · 2.07 KB
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
#!/usr/bin/python
import markdown
import shutil
import os
def create_index():
articles = []
page = {}
doc = create_navigation('doc/index')
# Page & Navigation
page['title'] = doc['title']
page['navigation'] = doc['navigation']
page['description'] = 'Guide to JavaScript\'s Quirks and Flaws'
# Intro
intro = to_content('doc/%s' % doc['articles'][0])
page['intro'] = create_article(doc['articles'][0], intro[0], intro[2])
# Articles
for a in doc['articles'][1:]:
content = to_content('doc/%s' % a)
articles.append(create_article(a, content[0], content[2], top = doc['articles'][0]))
page['articles'] = '\n\n'.join(articles)
# Create HTML
template = open('html/template.html').read()
with open('html/index.html', 'wb') as f:
f.write(template.format(**page).encode('utf-8'))
def create_navigation(file):
content = to_content(file)
articles = []
links = content[1].split('\n')
for l in links:
articles.append(l.split('#')[1].strip(')'))
return {
'articles': articles,
'title': content[0],
'navigation': content[2]
}
def create_article(link, title, html, top = None):
if top is not None:
title = to_markdown('## %s [^](#%s)' % (title, top)).replace('<h2>', '<h2 id="%s">' % link)
else:
title = to_markdown('# %s' % title).replace('<h1>', '<h1 id="%s">' % link)
return '<article><section><header>%s</header>\n%s</article>' % (title, html)
def to_content(file):
md = open('%s.md' % file).read()
title = md.split('\n')[0]
content = md[len(title):].strip()
html = to_markdown(content)
html = html.replace('<blockquote>', '<aside>').replace('</blockquote>', '</aside>')
html = '</section><section><h3>'.join(html.split('<h3>')) + '</section>'
html = html.replace('<h3>', '<header><h3>').replace('</h3>', '</h3></header>')
title = title.strip('#').strip()
return (title, content, html)
def to_markdown(text):
return markdown.markdown(text, ['abbr'])
if __name__ == '__main__':
create_index()