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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233 | diff --git a/src/build.py b/src/build.py
index ad59d24..2f17d3d 100644
--- a/src/build.py
+++ b/src/build.py
@@ -3,18 +3,16 @@ import importlib.util
import latex2mathml.converter
from src.hash import handle_hash_sync
-from . import settings
from jinja2 import Environment, FileSystemLoader
from markdown import markdown
from markdown.extensions.attr_list import AttrListTreeprocessor
from os import chdir, getcwd, makedirs, path, walk, readlink
-from os.path import abspath, dirname
from .config import find_project_from_path, load_config
from .errors import fatal, html_fatal
-from .fileops import read_file, write_file
-from .log import GRAY, info
+from .fileops import is_dotfile, read_file, write_file
+from .log import GRAY, die, info, warn
# patch [ ] instead of { }
AttrListTreeprocessor.BASE_RE = r'\[\:?[ ]*([^\]\n ][^\n]*)[ ]*\]'
@@ -27,17 +25,24 @@ replace_filters = [
("%}</p>", "%}" ),
]
-def load_plugins(path):
- module_dir = dirname(abspath(path))
+def load_plugins(config):
+ module_dir = path.abspath(config.tree.plugins)
sys.path.insert(0, module_dir)
try:
+ if not path.exists(path.join(module_dir, "main.py") ):
+ die("can not find `main.py` on plugins directory")
for name in list(sys.modules):
mod_file = getattr(sys.modules[name], "__file__", "") or ""
if mod_file.startswith(module_dir):
del sys.modules[name]
- spec = importlib.util.spec_from_file_location("plugins", path)
+ spec = importlib.util.spec_from_file_location("plugins", path.join(module_dir, "main.py") )
+ if spec is None or spec.loader is None:
+ raise RuntimeError("Failed to create plugin module spec.")
module = importlib.util.module_from_spec(spec)
- module.CONFIG = settings.CONFIG
+ module.CONFIG = config
+ module.compile_page = compile_page
+ module.info = info
+ module.GRAY = GRAY
spec.loader.exec_module(module)
return {
@@ -47,7 +52,7 @@ def load_plugins(path):
}
except Exception as e:
- raise RuntimeError(f"Failed loading plugin {path}: {e}") from e
+ raise RuntimeError(f"Failed loading plugin {config.tree.plugins}: {e}") from e
finally:
sys.path.pop(0)
@@ -73,9 +78,9 @@ def render_math(md_content: str) -> str:
md_content = re.sub(r'\<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mo stretchy="false">(</mo><mo>.</mo><mo>+</mo><mo>?</mo><mo stretchy="false">)</mo><mi>\</mi></mrow></math>', inline, md_content)
return md_content
-def jinja_handler(file, html_content):
+def jinja_handler(config, html_content, plugins=None):
try:
- tree = settings.CONFIG.tree if settings.CONFIG else None
+ tree = config.tree if config else None
env = (
Environment(loader=FileSystemLoader(tree.templates))
if tree is not None
@@ -83,13 +88,13 @@ def jinja_handler(file, html_content):
)
template = env.from_string(html_content)
return (
- template.render(**load_plugins(tree.plugins))
- if tree is not None
+ template.render(**plugins)
+ if plugins is not None
else template.render()
)
except Exception as e:
- return html_fatal(e, f"Template error in {file}")
+ return html_fatal(e, f"Template error")
def escape_code_blocks(html_content: str) -> str:
""" Neutralize '{' inside rendered <pre>/<code> blocks so Jinja never
@@ -108,21 +113,26 @@ def escape_code_blocks(html_content: str) -> str:
return html_content
def save_html(html_content:str, html_dest:str):
- makedirs( dirname( abspath(html_dest)), exist_ok=True )
+ makedirs(path.dirname(html_dest), exist_ok=True )
write_file( html_dest, html_content)
-def compile_md_to_html(md_file:str, html_dest:str ):
- """Convert a Markdown file to HTML, applying filters and Jinja2 processing, and save to dest."""
-
- info(f"Building {GRAY(md_file)}...")
- hash = handle_hash_sync(md_file)
- if hash is None:
- return None
- md_content = read_file(md_file)
- math_rendered = render_math(md_content)
+def process_highlighting(config):
highlight = "noclasses"
- if settings.CONFIG:
- highlight = settings.CONFIG.extras.highlight
+ if config:
+ highlight = config.extras.highlight
+ return {
+ "use_pygments": True,
+ "noclasses": highlight != "noclasses",
+ **(
+ {}
+ if highlight == "noclasses"
+ else {"pygments_style": highlight}
+ ),
+ }
+
+def md_to_html(config, md_content:str):
+ """Convert a Markdown file to HTML, applying filters and Jinja2 processing, and save to dest."""
+ math_rendered = render_math(md_content)
raw_html_content = markdown(
math_rendered,
extensions = [
@@ -139,52 +149,67 @@ def compile_md_to_html(md_file:str, html_dest:str ):
"pymdownx.tabbed",
"pymdownx.tilde",
],extension_configs={
- "pymdownx.highlight": {
- "use_pygments": True,
- "noclasses": highlight != "noclasses",
- **(
- {}
- if highlight == "noclasses"
- else {"pygments_style": highlight}
- ),
- }
+ "pymdownx.highlight": process_highlighting(config)
}
)
escaped_html = escape_code_blocks(raw_html_content)
filtered_html = html_filter(escaped_html)
- html_content = jinja_handler(md_file, filtered_html)
- save_html(html_content, html_dest)
- info(f"Done: {GRAY(html_dest)}...")
- return True
+ return filtered_html
+
+
+def compile_page(md_content:str, html_dest:str | None=None, config=None, plugins=None):
+ html_raw = md_to_html(config, md_content)
+ html_content = jinja_handler(config, html_raw, plugins)
+ if html_dest is None:
+ return html_content
+ else:
+ save_html(html_content, html_dest)
+ return True
+
+def compile_file(md_file, html_dest, config=None, plugins=None, force:bool = False):
+ md_content = read_file(md_file)
+ if config and not force:
+ hash = handle_hash_sync(config, md_file)
+ if hash is None and path.exists(html_dest):
+ info(f"Skipping {GRAY(md_file)}...")
+ return None
+
+ info(f"Building {GRAY(md_file)}...")
+ return compile_page(md_content, html_dest, config, plugins)
+
+def walk_and_build(config, plugins):
+ md_path = config.tree.markdown
+ for parent, _, files in walk(md_path):
+ for filename in files:
+ md_file = path.join(parent, filename)
+ md_relpath = path.relpath(md_file,md_path)
+ if is_dotfile(md_relpath): continue
+ html_dest = path.join(config.tree.dest, md_relpath)
+ if html_dest.endswith(".md"):
+ html_dest = html_dest.removesuffix(".md") + ".html"
+ source_file = readlink(md_file) if path.islink(md_file) else md_file
+ compile_file(source_file, html_dest, config, plugins)
+
+def build_if_file(project_path, file):
+ if project_path:
+ raise ValueError("Please specify either a project path or a file path, not both.")
+ elif len(file) != 2:
+ raise ValueError("A file path must include exactly a source and a destination.")
+ else:
+ compile_file(file[0], file[1])
def build(building_type:str,project_path:str, file:list[str]):
try:
if file:
- if project_path:
- raise ValueError("Please specify either a project path or a file path, not both.")
-
- elif len(file) != 2:
- raise ValueError("A file path must include exactly a source and a destination.")
-
- else:
- compile_md_to_html(file[0], file[1].removesuffix(".md") + ".html")
- return
-
+ build_if_file(project_path, file)
else:
project_path = project_path if project_path else getcwd()
find_project_from_path(project_path)
chdir(project_path)
- settings.CONFIG = load_config()
- config = settings.CONFIG
- md_path = config.tree.markdown
-
- for parent, _, files in walk(md_path):
- for filename in files:
- md_file = path.join(parent, filename)
- md_relpath = path.relpath(md_file,md_path)
- html_dest = path.join(config.tree.dest, md_relpath).removesuffix(".md") + ".html"
- source_file = readlink(md_file) if path.islink(md_file) else md_file
- compile_md_to_html(source_file, html_dest)
+ config = load_config()
+ plugins = load_plugins(config)
+ walk_and_build(config, plugins)
+
except Exception as e:
fatal(e, f"Build failed: {e}")
|