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
127 | diff --git a/src/build.py b/src/build.py
index 03b1716..4a7edf1 100644
--- a/src/build.py
+++ b/src/build.py
@@ -8,7 +8,7 @@ from markdown.extensions.attr_list import AttrListTreeprocessor
from os import getcwd, makedirs, path, walk
from os.path import abspath, dirname
-from .config import find_project_from_path, load_tree_config
+from .config import find_project_from_path, load_extras_config, load_tree_config
from .errors import fatal, html_fatal
from .fileops import read_file, write_file
from .log import GRAY, info
@@ -73,7 +73,7 @@ def save_html(html_content:str, html_dest:str):
makedirs( dirname( abspath(html_dest)), exist_ok=True )
write_file( html_dest, html_content)
-def compile_md_to_html(md_file:str, html_dest:str, config =None):
+def compile_md_to_html(md_file:str, html_dest:str, tree_config=None, extras_config=None):
"""Convert a Markdown file to HTML, applying filters and Jinja2 processing, and save to dest."""
info(f"Building {GRAY(md_file)}...")
md_content = read_file(md_file)
@@ -94,10 +94,20 @@ def compile_md_to_html(md_file:str, html_dest:str, config =None):
"pymdownx.details",
"pymdownx.tabbed",
"pymdownx.critic",
- ]
+ ],extension_configs={
+ "pymdownx.highlight": {
+ "use_pygments": True,
+ "noclasses": extras_config.highlight != "noclasses",
+ **(
+ {}
+ if extras_config.highlight == "noclasses"
+ else {"pygments_style": extras_config.highlight}
+ ),
+ }
+ }
)
filtered_html = html_filter(raw_html_content)
- html_content = jinja_handler(md_file, filtered_html, config)
+ html_content = jinja_handler(md_file, filtered_html, tree_config)
save_html(html_content, html_dest)
info(f"Done: {GRAY(html_dest)}...")
@@ -117,7 +127,8 @@ def build(building_type:str,project_path:str, file:list[str]):
else:
project_path = project_path if project_path else getcwd()
find_project_from_path(project_path)
- tree_config = load_tree_config(project_path)
+ tree_config = load_tree_config(project_path)
+ extras_config = load_extras_config(project_path)
md_path = tree_config.markdown
for parent, _, files in walk(md_path):
@@ -125,7 +136,7 @@ def build(building_type:str,project_path:str, file:list[str]):
md_file = path.join(parent, filename)
md_relpath = path.relpath(md_file,md_path)
html_dest = path.join(tree_config.dest, md_relpath).removesuffix(".md") + ".html"
- compile_md_to_html(md_file, html_dest, tree_config)
+ compile_md_to_html(md_file, html_dest, tree_config, extras_config)
except Exception as e:
fatal(e, f"Build failed: {e}")
diff --git a/src/webviewer.py b/src/webviewer.py
index 2961db8..6337da7 100644
--- a/src/webviewer.py
+++ b/src/webviewer.py
@@ -6,7 +6,7 @@ from watchdog.events import FileSystemEventHandler
import webview
import time
-from .config import find_project_from_path, load_tree_config, load_webview_config
+from .config import find_project_from_path, load_extras_config, load_tree_config, load_webview_config
from .errors import fatal, html_fatal
from .build import compile_md_to_html
from .log import info
@@ -58,7 +58,7 @@ def http_server(host, port, routes):
server = HTTPServer((host, port), Handler)
return server
-def reload(window, file, tree_config, webview_config):
+def reload(window, file, tree_config, webview_config, extras_config):
global current_url
try:
md_relpath = path.relpath(file, tree_config.markdown)
@@ -66,14 +66,14 @@ def reload(window, file, tree_config, webview_config):
if dest.endswith(".md"):
dest = dest.removesuffix(".md") + ".html"
current_url = path.relpath(dest, tree_config.dest)
- compile_md_to_html(file, dest, tree_config)
+ compile_md_to_html(file, dest, tree_config, extras_config)
info(f"Reloading: {current_url}")
if current_url:
window.load_url(f"http://{webview_config.host}:{webview_config.port}/{current_url}")
except Exception as e:
window.load_url(f"data:text/html,{html_fatal(e, f'Failed to reload {file}')}")
-def watch_files(window, tree_config, webview_config):
+def watch_files(window, tree_config, webview_config, extras_config):
last_reload = {}
class ReloadHandler(FileSystemEventHandler):
def on_modified(self, event):
@@ -88,7 +88,7 @@ def watch_files(window, tree_config, webview_config):
if now - last < 0.3:
return
last_reload[reload_path] = now
- reload(window, reload_path, tree_config, webview_config)
+ reload(window, reload_path, tree_config, webview_config, extras_config)
observer = Observer()
@@ -109,6 +109,7 @@ def run(project_path):
host = webview_config.host
port = webview_config.port
tree_config = load_tree_config(project_path)
+ extras_config = load_extras_config(project_path)
routes = {
"url_path" : {
"html": webview_config.html_path,
@@ -124,7 +125,7 @@ def run(project_path):
Thread(target=server.serve_forever, daemon=True).start()
window = webview.create_window("Merodi", url=f"http://{host}:{port}/")
- watch_files(window, tree_config, webview_config )
+ watch_files(window, tree_config, webview_config, extras_config)
webview.start()
|