-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
66 lines (48 loc) · 2.02 KB
/
run.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
import os
from jinja2 import Environment
import markdown
import mdx_smartypants
from staticjinja import Renderer
from optparse import OptionParser
def markdown_filter(source):
"""
Markdown filter for Jinja2.
:param source: Source in markdown format
:return: Markdown rendered HTML.
"""
return markdown.markdown(source, extensions=[mdx_smartypants.makeExtension({"entities": "named"})])
def datetimeformat(value, format='%H:%M / %d-%m-%Y'):
return value.strftime(format)
if __name__ == "__main__":
parser = OptionParser()
parser.add_option("-s", "--dont-stop", dest="dont_stop",
help="Don't stop for reloading", action='store_true', default=False)
# The default watcher does not work. This is a custom one which does.
parser.add_option("-w", "--customwatch", dest="customwatch", help="Use a custom watcher", action="store_true",
default=False)
(options, args) = parser.parse_args()
def filter_func(filepath):
filename = os.path.basename(filepath)
if filename.startswith(('.', '_')):
return False
if not filename.endswith('.html'):
return False
return True
def dont_render(*args, **kwargs):
print "Rendering..."
pass
if options.customwatch:
options.dont_stop = True
renderer = Renderer(outpath='./output', rules=[('.*.tmp', dont_render), ('.*.swx', dont_render), ('.*.swp', dont_render)])
renderer._env.filters["markdown"] = markdown_filter
renderer._env.filters['datetimeformat'] = datetimeformat
renderer.filter_func = filter_func
renderer.run(debug=True, use_reloader=not options.dont_stop)
if options.customwatch:
import easywatch
def handler(file, handler):
print "Detected changes: rendering again."
renderer.run(debug=True, use_reloader=False)
print "Render finished. Watching for changes..."
print "Watching out for changes..."
easywatch.watch("./templates", handler)