-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmd.py
More file actions
84 lines (72 loc) · 3.14 KB
/
md.py
File metadata and controls
84 lines (72 loc) · 3.14 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
78
79
80
81
82
83
84
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# I would not recommend that you use this script, it's a horrible hack.
# Don't come running to me if this breaks your site and eats your cat.
import os
from os.path import join
from os import walk
import time
import logging
import mistune
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler, FileModifiedEvent, FileCreatedEvent, FileDeletedEvent
mypath = "md"
def process_headers(string):
split = string.split("~#~\n")
if len(split) == 1:
return {}
props = dict(item.split("=") for item in split[0].split("\n"))
for x in props:
props[x] = props[x].replace("\"", "")
props["TEXT"] = split[1]
logging.info("Properties : {props}".format(props=props))
return props
def process_file(file):
if file[len(file) - 3:len(file)] != ".md":
return
with open("template.html", 'r') as r:
template = r.read()
name = file.replace("md" + os.path.sep, "pages" + os.path.sep)
htmlname = name.replace(".md", ".html")
ospath = os.path.split(join(os.getcwd(), htmlname))[0]
if not os.path.exists(ospath):
os.makedirs(ospath)
with open(file, 'r') as r:
raw = r.read()
props = process_headers(raw)
if "TITLE" not in props:
logging.info("New file without header detected, not processing")
return
mark = mistune.markdown(props["TEXT"])
with open(htmlname, 'w') as w:
logging.info("Writing to {file}".format(file=htmlname))
w.write(template.replace("{TITLE}", props["TITLE"]).replace("{BODY}", mark))
class Handler(FileSystemEventHandler):
def on_any_event(self, event):
mdpath = "." + os.path.sep + "md" + os.path.sep
if (type(event) is FileModifiedEvent or type(event) is FileCreatedEvent) and event.src_path.startswith(mdpath):
logging.info("File created or changed! Path: {path}".format(path=event.src_path))
process_file(event.src_path)
elif (type(event) is FileDeletedEvent) and event.src_path.startswith(mdpath):
logging.info("File deleted! Path: {path}".format(path=event.src_path))
if os.path.exists(event.src_path):
os.remove(event.src_path.replace(os.path.sep + "md" + os.path.sep, os.path.sep + "pages" + os.path.sep))
elif (type(event) is FileModifiedEvent) and len(event.src_path) >= len("template.html") and event.src_path[len(
event.src_path) - len("template.html"):] == "template.html":
logging.info("Template changed, reprocessing all files.")
for (dirpath, dirnames, filenames) in walk(mypath):
for x in filenames:
process_file(dirpath + os.path.sep + x)
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
observer = Observer()
observer.schedule(Handler(), ".", recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()