-
Notifications
You must be signed in to change notification settings - Fork 1
/
pyslate.py
101 lines (84 loc) · 2.9 KB
/
pyslate.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
import re
import os
import yaml
import codecs
import argparse
import jinja2
import misaka as m
import unidecode
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
from flask import escape
RE_NON_WORD = re.compile(r'\W+')
def slugify(s):
s = unidecode.unidecode(s).lower()
return RE_NON_WORD.sub('-', s)
# Create a custom renderer
class PySlateRenderer(m.HtmlRenderer, m.SmartyPants):
def header(self, header, n):
header = header.strip()
slug = slugify(header)
return "\n<h{n} id=\"{slug}\">{header}</h{n}>\n".format(**locals())
def block_code(self, text, lang):
if not lang:
return '\n<pre><code>%s</code></pre>\n' % \
escape(text.strip())
lexer = get_lexer_by_name(lang, stripall=True)
formatter = HtmlFormatter(cssclass="highlight %s" % lang)
return highlight(text, lexer, formatter)
# And use the renderer
renderer = PySlateRenderer()
markdown = m.Markdown(renderer,
extensions=m.EXT_FENCED_CODE |
m.EXT_NO_INTRA_EMPHASIS | m.EXT_TABLES)
def render():
if not os.path.exists('source/index.md'):
return 'No file index.md found!'
md = codecs.open('source/index.md', encoding='utf8').read()
md_datas = md.split('\n---\n')
if md_datas[0].startswith('---'):
md_datas[0] = md_datas[0][4:]
metadata = yaml.load(md_datas[0])
content = markdown.render(md_datas[1])
if metadata.get('includes', []):
content += '\n'.join([
markdown.render(
codecs.open(
'source/includes/_%s.md' % include, encoding='utf8'
).read()
)
for include in metadata['includes']
])
template = jinja2.Template(open('layouts/layout.html').read())
return template.render(
{
'metadata': metadata,
'content': content
})
def run_server(host='localhost', port=9000):
from flask import Flask
app = Flask(__name__, static_folder='build/static')
@app.route('/')
def index():
return render()
app.run(host, port, debug=True)
def build():
with codecs.open('build/index.html', 'w', encoding='utf8') as outfile:
outfile.write(render())
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='PySlate', add_help=False)
parser.add_argument('action', default='run')
parser.add_argument(
"-p", "--port", dest="port",
help="port of server (default:%(default)s)",
type=int, default=9000)
parser.add_argument(
"-h", "--host", dest="host",
help="domain or IP of server (default:%(default)s)",
type=str, default='0.0.0.0')
cmd_args = parser.parse_args()
if cmd_args.action == 'build':
build()
else:
run_server(cmd_args.host, cmd_args.port)