Pi66

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

← back to log

feat:add build modes (draft/release)

author: pixel
date: 2026-07-22 09:31
hash: 1b8d7d75c9f2f8000c32e13cff4dab14b3a01f51

Diffstat:

M

src/api.py
1

M

src/build.py
60 ++++++++++++++++++++++--------

M

src/config.py
30 ++++-----------

M

src/fileops.py
6 ++-

M

src/init_project.py
22 ++++++-----

M

src/main.py
11 ++++--

M

src/modules.py
49 ++++++++++++------------
  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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
diff --git a/src/api.py b/src/api.py
index ea920cd..a76eac0 100644
--- a/src/api.py
+++ b/src/api.py
@@ -24,6 +24,7 @@ class API:
         self.globals = GlobalStore()
         self.log = None
         self.config = None
+        self.mode = "draft"

 api = API()

diff --git a/src/build.py b/src/build.py
index 87afdef..004a822 100644
--- a/src/build.py
+++ b/src/build.py
@@ -190,7 +190,7 @@ def compile_file(md_file, html_dest, config=None, plugins=None, force: bool = Fa
     hook_call("on_page_built", md_file, html_dest)
     return result

-def walk_and_build(config, plugins, force_all=False):
+def walk_and_build(config, plugins, dest, force_all=False):
     md_path = config.tree.markdown
     hook_call("on_walk_start", config)
     files = []
@@ -201,16 +201,35 @@ def walk_and_build(config, plugins, force_all=False):
         hook_call("on_walk_file", md_file)
         md_relpath = path.relpath(md_file, md_path)
         if is_dotfile(md_relpath): continue
-        html_dest = path.join(config.tree.dest, md_relpath)
+        html_dest = path.join(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, force=force_all)
     hook_call("on_walk_end", config)

-def build_all(config, plugins):
+def build_all(config, plugins, dest):
     clear_all_hashes(config)
-    walk_and_build(config, plugins, force_all=True)
+    walk_and_build(config, plugins, dest, force_all=True)
+
+def validate_build(config, plugins):
+    errors = []
+    md_path = config.tree.markdown
+    for parent, _, filenames in walk(md_path):
+        for filename in filenames:
+            md_file = path.join(parent, filename)
+            if is_dotfile(path.relpath(md_file, md_path)):
+                continue
+            source = readlink(md_file) if path.islink(md_file) else md_file
+            try:
+                compile_page(read_md_content(source), config=config, plugins=plugins)
+            except Exception as e:
+                errors.append(f"{path.relpath(md_file, md_path)}: {e}")
+    if errors:
+        for e in errors:
+            warn(e)
+        raise Exception(f"Validation failed with {len(errors)} error(s)")
+    info("Validation passed.")

 def build_if_file(project_path, file):
     if project_path:
@@ -220,20 +239,27 @@ def build_if_file(project_path, file):
     else:
         compile_file(file[0], file[1])

-def build(building_type: str, project_path: str, file: list[str]):
+def build(mode, project_path, file, validate=False, force=False):
     try:
         if file:
-            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)
-            config = load_config()
-            plugins = load_plugins(config)
-            hook_call("on_build_start", config)
-            walk_and_build(config, plugins)
-            hook_call("on_build_end", config)
-
-
+            return build_if_file(project_path, file)
+
+        project_path = project_path or getcwd()
+        find_project_from_path(project_path)
+        chdir(project_path)
+        config = load_config()
+        plugins = load_plugins(config)
+        dest = config.tree.draft_dest if mode == "draft" else config.tree.release_dest
+
+        if validate:
+            validate_build(config, plugins)
+        if mode == "release":
+            import shutil
+            if path.exists(dest):
+                shutil.rmtree(dest)
+
+        hook_call("on_build_start", config)
+        walk_and_build(config, plugins, dest, force_all=force)
+        hook_call("on_build_end", config)
     except Exception as e:
         fatal(e, f"Build failed: {e}")
diff --git a/src/config.py b/src/config.py
index 63ee3be..c5233e9 100644
--- a/src/config.py
+++ b/src/config.py
@@ -22,30 +22,14 @@ def load_project_config(config) -> Project:
     ) 

 def load_tree_config(config) -> Tree:
-    markdown  = config["tree"].get("markdown",  "src/md")
-    static    = config["tree"].get("static",    "src/static")
-    templates = config["tree"].get("templates", "src/templates")
-    dest      = config["tree"].get("dest",      "src/dest")
-    plugins   = config["tree"].get("plugins",   "src/plugins.py")
+    t = config["tree"]
+    markdown, static, templates = t["markdown"], t["static"], t["templates"]
+    draft_dest, release_dest, plugins = t["draft_dest"], t["release_dest"], t["plugins"]
+    for name, p in [("markdown", markdown), ("static", static), ("templates", templates), ("plugins", plugins)]:
+        if not exists(p):
+            raise FileNotFoundError(f"{name} directory does not exist: {p}")
+    return Tree(markdown=markdown, static=static, templates=templates, draft_dest=draft_dest, release_dest=release_dest, plugins=plugins)

-    if not exists(markdown):
-        raise FileNotFoundError(f"markdown directory does not exist: {markdown}")
-    if not exists(static):
-        raise FileNotFoundError(f"static directory does not exist: {static}")
-    if not exists(templates):
-        raise FileNotFoundError(f"templates directory does not exist: {templates}")
-    if not exists(plugins):
-        raise FileNotFoundError(f"plugins file does not exist: {plugins}")
-    if not exists(dest):
-        makedirs(dest)
-
-    return Tree(
-        markdown  = markdown,
-        static    = static,
-        templates = templates,
-        dest      = dest,
-        plugins   = plugins,
-    )

 def load_webview_config(config) -> Webview:
     return Webview(
diff --git a/src/fileops.py b/src/fileops.py
index 396870f..0cad009 100644
--- a/src/fileops.py
+++ b/src/fileops.py
@@ -19,7 +19,8 @@ def resolve_tree_paths(project_dir: str, tree: Tree) -> Tree:
         markdown=path.join(project_dir, tree.markdown),
         static=path.join(project_dir, tree.static),
         templates=path.join(project_dir, tree.templates),
-        dest=path.join(project_dir, tree.dest),
+        draft_dest=path.join(project_dir, tree.draft_dest),
+        release_dest=path.join(project_dir, tree.release_dest),
         plugins=path.join(project_dir, tree.plugins),
     )

@@ -28,5 +29,6 @@ def create_tree_dirs (tree:Tree) -> None:
     makedirs(tree.markdown , exist_ok=True)
     makedirs(tree.static   , exist_ok=True)
     makedirs(tree.templates, exist_ok=True)
-    makedirs(tree.dest     , exist_ok=True)
+    makedirs(tree.draft_dest     , exist_ok=True)
+    makedirs(tree.release_dest   , exist_ok=True)
     makedirs(tree.plugins  , exist_ok=True)
diff --git a/src/init_project.py b/src/init_project.py
index 62f3ba9..2ac7973 100644
--- a/src/init_project.py
+++ b/src/init_project.py
@@ -15,11 +15,12 @@ def init_config_struct(project_name:str) -> Config:
             description = "Add your description here",
         ),
         tree = Tree(
-            markdown  = "src/md",
-            static    = "src/static",
-            templates = "src/templates",
-            dest      = "src/dest",
-            plugins   = "src/plugins",
+            markdown     = "src/md",
+            static       = "src/static",
+            templates    = "src/templates",
+            draft_dest   = "build/draft",
+            release_dest = "build/release",
+            plugins      = "src/plugins",
         ),
         webview = Webview(
             host        = "localhost",
@@ -43,11 +44,12 @@ version     = "{config.project.version}"
 description = "{config.project.description}"

 [tree]
-markdown  = "{config.tree.markdown}"
-static    = "{config.tree.static}"
-templates = "{config.tree.templates}"
-dest      = "{config.tree.dest}"
-plugins   = "{config.tree.plugins}"
+markdown     = "{config.tree.markdown}"
+static       = "{config.tree.static}"
+templates    = "{config.tree.templates}"
+draft_dest   = "{config.tree.draft_dest}"
+release_dest = "{config.tree.release_dest}"
+plugins      = "{config.tree.plugins}"

 [webview]
 host        = "{config.webview.host}"
diff --git a/src/main.py b/src/main.py
index 73b78a1..36f110c 100755
--- a/src/main.py
+++ b/src/main.py
@@ -27,8 +27,9 @@ def main():
     webview_parser.add_argument("path", nargs="?")

     build_parser = sub_parser.add_parser("build", parents=[common])
-    build_parser.add_argument("--release", action="store_true")
-    build_parser.add_argument("--debug", action="store_true")
+    build_mode = build_parser.add_mutually_exclusive_group()
+    build_mode.add_argument("--draft", action="store_true", default=True)
+    build_mode.add_argument("--release", action="store_true")
     build_parser.add_argument("path", nargs="?")
     build_parser.add_argument("--file", nargs=2, metavar=("SRC", "DEST"))

@@ -53,8 +54,11 @@ def main():
         init(project_path = args.path)

     elif args.command == "build":
+        from .api import api
+        mode = "release" if args.release else "draft"
+        api.mode = mode
         from .build import build
-        build(building_type = "release" if args.release else "debug", project_path = args.path, file=args.file)
+        build(mode = mode, project_path = args.path, file=args.file)

     elif args.command == "webview":
         from .webviewer import run
@@ -65,4 +69,3 @@ def main():
         run_watcher(args.path)

     atexit.register(lambda: hook_call("on_end"))
-
diff --git a/src/modules.py b/src/modules.py
index 9e8b61c..8267fc1 100644
--- a/src/modules.py
+++ b/src/modules.py
@@ -1,39 +1,40 @@
 from dataclasses import dataclass

-@dataclass()
+@dataclass
 class Project:
-    name        : str
-    version     : str
-    description : str
+    name: str
+    version: str
+    description: str

-@dataclass()
+@dataclass
 class Tree:
-    markdown  : str
-    static    : str
-    templates : str
-    dest      : str
-    plugins   : str
+    markdown: str
+    static: str
+    templates: str
+    draft_dest: str
+    release_dest: str
+    plugins: str

-@dataclass()
+@dataclass
 class Webview:
-    host        : str
-    port        : int
-    dev_tools   : str
-    html_path   : str
-    static_path : str
+    host: str
+    port: int
+    dev_tools: str
+    html_path: str
+    static_path: str

 @dataclass
 class Extras:
-    highlight :str 
+    highlight: str

 @dataclass
 class Cache:
-    hash :str 
+    hash: str

-@dataclass()
+@dataclass
 class Config:
-    project:Project
-    tree:Tree
-    webview:Webview
-    extras:Extras
-    cache:Cache
+    project: Project
+    tree: Tree
+    webview: Webview
+    extras: Extras
+    cache: Cache