Skip to content

Commit

Permalink
Rerender all template files if any config file is updated
Browse files Browse the repository at this point in the history
  • Loading branch information
ssorj committed Apr 23, 2024
1 parent 0a8d8cb commit 613146c
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion python/transom/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ def __init__(self, project_dir, verbose=False, quiet=False):
self._body_template = None
self._page_template = None

self._config_modified = False

self._files = list()
self._index_files = dict() # parent input dir => _File

Expand All @@ -90,6 +92,16 @@ def init(self):
except FileNotFoundError as e:
self.warning("Config file not found: {}", e)

def _get_config_modified(self, output_mtime):
for root, dirs, names in _os.walk(self.config_dir):
for name in {x for x in names if not self._ignored_file_regex.match(x)}:
mtime = _os.path.getmtime(_os.path.join(root, name))

if mtime > output_mtime:
return True

return False

def _init_files(self):
self._files.clear()
self._index_files.clear()
Expand Down Expand Up @@ -120,6 +132,10 @@ def _init_file(self, input_path):
def render(self, force=False):
self.notice("Rendering files from '{}' to '{}'", self.input_dir, self.output_dir)

if _os.path.exists(self.output_dir):
output_mtime = _os.path.getmtime(self.output_dir)
self._config_modified = self._get_config_modified(output_mtime)

self._init_files()

self.notice("Found {:,} input {}", len(self._files), _plural("file", len(self._files)))
Expand Down Expand Up @@ -360,6 +376,9 @@ def _process_input(self):
except KeyError:
self._body_template = self.site._body_template

def _is_modified(self):
return self.site._config_modified or super()._is_modified()

def _render_content(self):
if not hasattr(self, "_content"):
self._process_input()
Expand Down Expand Up @@ -485,7 +504,7 @@ def render_file(event):

def render_site(event):
self.site.init()
self.site.render(force=True)
self.site.render()

watcher.add_watch(self.site.input_dir, mask, render_file, rec=True, auto_add=True)
watcher.add_watch(self.site.config_dir, mask, render_site, rec=True, auto_add=True)
Expand Down

0 comments on commit 613146c

Please sign in to comment.