--- /dev/null
+#! /usr/bin/env python
+
+__author__ = "Oleg Broytman <phd@phdru.name>"
+__copyright__ = "Copyright (C) 2020 PhiloSoft Design"
+
+import os
+from blog_db import blog_root, load_blog
+blog = load_blog()
+
+# Run a loop over the blog posts:
+# for every post note its previous and next posts
+# and compare timestamps of previous and next HTML
+# with the timestamp of the current template;
+# if HTMLs are older - remove them,
+# they will be regenerated with the corrected back/forth links.
+
+
+def fullpath(key, post):
+ return os.path.join(*(blog_root,) + key + (post[0],))
+
+
+def html(tmpl):
+ return tmpl[:-len('tmpl')] + 'html'
+
+
+prev_key = prev_blog_post = current_key = current_blog_post = \
+ next_key = next_blog_post = None
+for key in sorted(blog):
+ for blog_post in sorted(blog[key]):
+ if current_blog_post:
+ prev_key = current_key
+ prev_blog_post = current_blog_post
+ if next_blog_post:
+ current_key = next_key
+ current_blog_post = next_blog_post
+ next_key = key
+ next_blog_post = blog_post
+
+ if current_key:
+ current_tmpl = fullpath(current_key, current_blog_post)
+ try:
+ current_mtime = os.path.getmtime(current_tmpl)
+ except OSError:
+ current_mtime = None
+
+ if prev_key:
+ prev_html = html(fullpath(prev_key, prev_blog_post))
+ try:
+ prev_mtime = os.path.getmtime(prev_html)
+ except OSError:
+ pass
+ else:
+ if current_mtime is not None and (prev_mtime < current_mtime):
+ os.remove(prev_html)
+ if current_key and next_key:
+ next_html = html(fullpath(next_key, next_blog_post))
+ try:
+ next_mtime = os.path.getmtime(next_html)
+ except OSError:
+ pass
+ else:
+ if current_mtime is not None and (next_mtime < current_mtime):
+ os.remove(next_html)
+
+current_tmpl = fullpath(key, blog_post)
+try:
+ current_mtime = os.path.getmtime(current_tmpl)
+except OSError:
+ pass
+else:
+ current_mtime = None
+
+prev_html = html(fullpath(current_key, current_blog_post))
+try:
+ prev_mtime = os.path.getmtime(prev_html)
+except OSError:
+ pass
+else:
+ if current_mtime is not None and (prev_mtime < current_mtime):
+ os.remove(prev_html)