Pi66

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

← back to log

refactor:update watcher and webviewer for build modes

author: pixel
date: 2026-07-22 09:37
hash: a9e0da70dfc9c2de8ac68d9bea128fa4fdbae3b2

Diffstat:

M

src/watcher.py
60 +++++++++++++-----------------

M

src/webviewer.py
2 -
  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
diff --git a/src/watcher.py b/src/watcher.py
index 140d87d..ecdf72e 100644
--- a/src/watcher.py
+++ b/src/watcher.py
@@ -4,7 +4,6 @@ from watchdog.events import FileSystemEventHandler
 import time

 from src.hooks import hook_call
-
 from .config import find_project_from_path, load_config
 from .build import compile_file, load_plugins, build_all
 from .log import warn
@@ -13,6 +12,10 @@ from .errors import fatal
 current_page = None
 _plugins = None

+def page_to_url(filesystem_path, config):
+    rel = path.relpath(filesystem_path, config.tree.markdown)
+    return path.splitext(rel)[0] + ".html"
+
 def is_template_or_plugin(file_path, config):
     return (file_path.startswith(config.tree.templates) or
             file_path.startswith(config.tree.plugins))
@@ -24,12 +27,9 @@ def single_file_reload(changed_path, config):
             current_page = changed_path
         if current_page is None:
             return None
-
-        md_relpath = path.relpath(current_page, config.tree.markdown)
-        html = path.splitext(md_relpath)[0] + ".html"
-        dest = path.join(config.tree.dest, html)
-        compile_status = compile_file(current_page, dest, config, _plugins)
-        if compile_status is None:
+        html = page_to_url(current_page, config)
+        dest = path.join(config.tree.draft_dest, html)
+        if compile_file(current_page, dest, config, _plugins) is None:
             return None
         return html
     except Exception as e:
@@ -39,8 +39,8 @@ def single_file_reload(changed_path, config):
 def full_reload(config, plugins):
     global current_page
     try:
-        build_all(config, plugins)
-        return current_page
+        build_all(config, plugins, config.tree.draft_dest)
+        return page_to_url(current_page, config) if current_page else None
     except Exception as e:
         hook_call("on_reload_error", e)
         warn(str(e))
@@ -54,30 +54,27 @@ def watch_files(config, reload_func=None):
         def on_modified(self, event):
             global _plugins
             try:
-                reload_path = event.src_path
+                p = event.src_path
                 if event.is_directory:
                     return
-                if reload_path.startswith(config.tree.static):
+                if p.startswith(config.tree.static):
                     if reload_func:
                         reload_func("")
                     return
-
-                if is_template_or_plugin(reload_path, config):
-                    if reload_path.startswith(config.tree.plugins):
+                if is_template_or_plugin(p, config):
+                    if p.startswith(config.tree.plugins):
                         _plugins = load_plugins(config)
-                        hook_call("on_plugin_changed", reload_path)
+                        hook_call("on_plugin_changed", p)
                     file = full_reload(config, _plugins)
                     if reload_func and file:
                         reload_func(file)
                     return
-
                 now = time.time()
-                last = last_reload.get(reload_path, 0)
-                if now - last < 0.3:
+                if now - last_reload.get(p, 0) < 0.3:
                     return
-                last_reload[reload_path] = now
-                hook_call("on_file_changed", reload_path, config)
-                file = single_file_reload(reload_path, config)
+                last_reload[p] = now
+                hook_call("on_file_changed", p, config)
+                file = single_file_reload(p, config)
                 if reload_func and file:
                     reload_func(file)
             except Exception as e:
@@ -86,30 +83,25 @@ def watch_files(config, reload_func=None):

     observer = Observer()
     handler = ReloadHandler()
-    for file in [config.tree.markdown, config.tree.templates, config.tree.plugins, config.tree.static]:
-        observer.schedule(
-            handler,
-            path=file,
-            recursive=True
-        )
+    for dir_path in [config.tree.markdown, config.tree.templates, config.tree.plugins, config.tree.static]:
+        observer.schedule(handler, path=dir_path, recursive=True)
     hook_call("on_watch_start", config)
     return observer

 def run_watcher(project_path):
     try:
-        project_path = project_path if project_path else getcwd()
+        project_path = project_path or getcwd()
         find_project_from_path(project_path)
         chdir(project_path)
         config = load_config()
-        observers = watch_files(config)
-        observers.start()
+        observer = watch_files(config)
+        observer.start()
         try:
             while True:
                 time.sleep(1)
         except KeyboardInterrupt:
-            observers.stop()
+            observer.stop()
             hook_call("on_watch_stop", config)
-        observers.join()
-
+        observer.join()
     except Exception as e:
-        fatal(e, f"Build failed: {e}")
+        fatal(e, f"Watch failed: {e}")
diff --git a/src/webviewer.py b/src/webviewer.py
index fe3cf4d..dc0be86 100644
--- a/src/webviewer.py
+++ b/src/webviewer.py
@@ -103,7 +103,7 @@ def run(project_path):
                 "static": config.webview.static_path,
             },
             "fs_path": {
-                "html":   config.tree.dest,
+                "html":   config.tree.draft_dest,
                 "static": config.tree.static,
             },
         }