-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshowmark.py
126 lines (105 loc) · 3.19 KB
/
showmark.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
import webview
import argparse
import markdown
from mdx_linkify.mdx_linkify import LinkifyExtension
from pygments.formatters import HtmlFormatter
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import os
import yaml
DIR = os.path.dirname(os.path.realpath(__file__))
CONFIG_PATH = os.path.join(DIR, "showmark.yml")
try:
with open(CONFIG_PATH) as f:
SETTINGS = yaml.safe_load(f)
except FileNotFoundError:
SETTINGS = dict(
style="style.css",
markdown_extensions=["fenced_code", "mdx_truly_sane_lists", "codehilite"],
window=dict(
text_select=True,
),
)
with open(CONFIG_PATH, "w") as f:
yaml.dump(SETTINGS, f)
def open_in_default_browser(attrs, new=False):
attrs[(None, "target")] = "_blank"
return attrs
class ChangeHandler(FileSystemEventHandler):
def __init__(self, display):
super().__init__()
self.display = display
def on_modified(self, event):
display.update_html()
class MarkdownDisplay:
def __init__(self):
self.extensions = [
"mdx_linkify"
]
self.extensions += SETTINGS["markdown_extensions"]
if os.path.isabs(SETTINGS["style"]):
self.csspath = SETTINGS["style"]
else:
self.csspath = os.path.join(DIR, SETTINGS["style"])
self.window = None
self.path = None
def get_css(self):
css = HtmlFormatter(style=SETTINGS["pygmentize_style"]).get_style_defs()
with open(self.csspath) as f:
css += f.read()
return css
def set_css(self):
css = self.get_css()
self.window.load_css(css)
def get_html(self, path):
try:
with open(path) as f:
html = markdown.markdown(f.read(), extensions=self.extensions)
except FileNotFoundError:
html = f"<p>The file {path} does not exist.</p>"
return html
def export(self, inpath, outpath):
html = f"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<style>
{self.get_css()}
</style>
<body>
{self.get_html(inpath)}
</body>
</html>
"""
with open(outpath, "w") as f:
f.write(html)
def display(self, path):
self.path = path
html = self.get_html(path)
self.window = webview.create_window(
os.path.basename(path),
html=html,
**SETTINGS["window"]
)
# watch the file for changes
event_handler = ChangeHandler(self)
observer = Observer()
observer.schedule(event_handler, path=path)
observer.start()
webview.start(self.set_css, debug=SETTINGS.get("debug", False))
def update_html(self):
html = self.get_html(self.path)
self.window.load_html(html)
self.set_css()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("input", help="Markdown file")
parser.add_argument("--export", metavar="output", help="export to html")
args = parser.parse_args()
display = MarkdownDisplay()
if args.export:
display.export(args.input, args.export)
else:
display.display(args.input)