Pi66

merodi
A markdown-based static site generator.
git clone https://git.pi66.xyz/merodi

← back to log

refactor: use settings.CONFIG instead of passing config through call chain

author: pi66
date: 2026-07-04 23:03
hash: 476db8701d31338ed53ddd10f2750ce9b73c410a

Diffstat:

M

src/build.py
46 +++++++++++++++++++-----------

M

src/watcher.py
16 ++++++----

M

src/webviewer.py
27 +++++++++---------
  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
diff --git a/src/build.py b/src/build.py
index 45efff4..9c300e5 100644
--- a/src/build.py
+++ b/src/build.py
@@ -1,6 +1,7 @@
-import re
+import re, sys
 import importlib.util
 import latex2mathml.converter
+from . import settings

 from jinja2 import Environment, FileSystemLoader
 from markdown import markdown
@@ -8,7 +9,7 @@ from markdown.extensions.attr_list import AttrListTreeprocessor
 from os import chdir, getcwd, makedirs, path, walk
 from os.path import abspath, dirname

-from .config import find_project_from_path, load_extras_config, load_tree_config
+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
@@ -71,14 +72,21 @@ 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">&#x00028;</mo><mo>&#x0002E;</mo><mo>&#x0002B;</mo><mo>&#x0003F;</mo><mo stretchy="false">&#x00029;</mo><mi>\</mi></mrow></math>', inline, md_content)
     return md_content

-def jinja_handler(file, html_content, config=None):
+def jinja_handler(file, html_content):
     try:
-        env = Environment( loader=FileSystemLoader(config.templates)) if config else Environment()
+        tree = settings.CONFIG.tree if settings.CONFIG else None
+        env = (
+            Environment(loader=FileSystemLoader(tree.templates))
+            if tree is not None
+            else Environment()
+        )
         template = env.from_string(html_content)
+        return (
+            template.render(**load_plugins(tree.plugins))
+            if tree is not None
+            else template.render()
+        )

-        if config:
-            return template.render(**load_plugins(config.plugins))
-        return template.render()
     except Exception as e:
         return html_fatal(e, f"Template error in {file}")

@@ -98,11 +106,15 @@ 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, tree_config=None, extras_config=None):
+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)}...")
     md_content = read_file(md_file)
     math_rendered = render_math(md_content)
+    highlight = "noclasses"
+    if settings.CONFIG:
+        highlight = settings.CONFIG.extras.highlight
     raw_html_content = markdown(
         math_rendered,
         extensions = [
@@ -121,18 +133,18 @@ def compile_md_to_html(md_file:str, html_dest:str, tree_config=None, extras_conf
         ],extension_configs={
             "pymdownx.highlight": {
                 "use_pygments": True,
-                "noclasses": extras_config.highlight != "noclasses",
+                "noclasses": highlight != "noclasses",
                 **(
                     {}
-                    if extras_config.highlight == "noclasses"
-                    else {"pygments_style": extras_config.highlight}
+                    if highlight == "noclasses"
+                    else {"pygments_style": highlight}
                 ),
             }
         }
     )
     escaped_html = escape_code_blocks(raw_html_content)
     filtered_html = html_filter(escaped_html)
-    html_content = jinja_handler(md_file, filtered_html, tree_config)
+    html_content = jinja_handler(md_file, filtered_html)
     save_html(html_content, html_dest)
     info(f"Done: {GRAY(html_dest)}...")

@@ -153,16 +165,16 @@ def build(building_type:str,project_path:str, file:list[str]):
             project_path = project_path if project_path else getcwd()
             find_project_from_path(project_path)
             chdir(project_path)
-            tree_config   = load_tree_config()
-            extras_config = load_extras_config()
-            md_path = tree_config.markdown
+            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(tree_config.dest, md_relpath).removesuffix(".md") + ".html"
-                    compile_md_to_html(md_file, html_dest, tree_config, extras_config)
+                    html_dest  = path.join(config.tree.dest, md_relpath).removesuffix(".md") + ".html"
+                    compile_md_to_html(md_file, html_dest)

     except Exception as e:
         fatal(e, f"Build failed: {e}")
diff --git a/src/watcher.py b/src/watcher.py
index 12f0b26..4dbadb2 100644
--- a/src/watcher.py
+++ b/src/watcher.py
@@ -3,7 +3,9 @@ from typing import Callable
 from watchdog.observers import Observer
 from watchdog.events import FileSystemEventHandler
 import time
-from .config import find_project_from_path, load_extras_config, load_tree_config 
+
+from src import settings
+from .config import find_project_from_path, load_config
 from .build import compile_md_to_html
 from .log import  warn
 from .errors import fatal
@@ -21,13 +23,14 @@ def reload(changed_path, tree_config, extras_config):
         md_relpath = path.relpath(current_md_file, tree_config.markdown)
         html = path.splitext(md_relpath)[0] + ".html"
         dest = path.join(tree_config.dest, html)
-        compile_md_to_html(current_md_file, dest, tree_config, extras_config)
+        compile_md_to_html(current_md_file, dest)
         return html
     except Exception as e:
         warn(str(e))

-def watch_files(tree_config, extra_config, reload_func:Callable | None=None):
+def watch_files(reload_func:Callable | None=None):
     last_reload = {}
+    config = settings.CONFIG

     class ReloadHandler(FileSystemEventHandler):
         def on_modified(self, event):
@@ -39,7 +42,7 @@ def watch_files(tree_config, extra_config, reload_func:Callable | None=None):
             if now - last < 0.3:
                 return
             last_reload[reload_path] = now
-            file = reload( reload_path, tree_config, extra_config)
+            file = reload( reload_path, config.tree , config.extras)
             if reload_func and file: reload_func(file)
     observer = Observer()
     handler = ReloadHandler()
@@ -56,9 +59,8 @@ def run_watcher(project_path):
         project_path = project_path if project_path else getcwd()
         find_project_from_path(project_path)
         chdir(project_path)
-        extra_config = load_extras_config()
-        tree_config  = load_tree_config()
-        observers = watch_files(tree_config, extra_config)
+        settings.CONFIG = load_config()
+        observers = watch_files()
         observers.start()
         try:
             while True:
diff --git a/src/webviewer.py b/src/webviewer.py
index 276587a..40e359b 100644
--- a/src/webviewer.py
+++ b/src/webviewer.py
@@ -1,9 +1,11 @@
-from os import getcwd, path
+from os import chdir, getcwd, path
 from http.server import HTTPServer, SimpleHTTPRequestHandler
 from threading import Thread
 import webview

-from .config import find_project_from_path, load_extras_config, load_tree_config, load_webview_config
+from src import settings
+
+from .config import find_project_from_path, load_config
 from .errors import fatal, html_fatal
 from .watcher import watch_files 
 from .log import info, warn
@@ -91,19 +93,19 @@ def run(project_path):
         global _ip_cache
         project_path = project_path if project_path else getcwd()
         find_project_from_path(project_path)
-        webview_config = load_webview_config()
-        host = webview_config.host
-        port = webview_config.port
-        tree_config    = load_tree_config()
-        extras_config = load_extras_config()
+        chdir(project_path)
+        settings.CONFIG = load_config()
+        config = settings.CONFIG
+        host = config.webview.host
+        port = config.webview.port
         routes = {
             "url_path" : {
-                "html":   webview_config.html_path,
-                "static": webview_config.static_path
+                "html":   config.webview.html_path,
+                "static": config.webview.static_path,
             },
             "fs_path"  : {
-                "html":   tree_config.dest,
-                "static": tree_config.static
+                "html":   config.tree.dest,
+                "static": config.tree.static,
             },
         }

@@ -118,11 +120,10 @@ def run(project_path):

         window = webview.create_window("Merodi", url=f"http://{_ip_cache[0]}:{port}/")
         observer = watch_files(
-                tree_config, extras_config,
                 lambda path: reload_webview(window, path, _ip_cache[0], port),
                 )
         observer.start()
-        webview.start(debug=webview_config.dev_tools in ["true",1])
+        webview.start(debug=config.webview.dev_tools in ["true",1])


     except KeyboardInterrupt: