]> git.phdru.name Git - phdru.name/phdru.name.git/commitdiff
Feat(remove_old_html.py): Remove old HTML in the blog
authorOleg Broytman <phd@phdru.name>
Fri, 12 Jun 2020 11:57:06 +0000 (14:57 +0300)
committerOleg Broytman <phd@phdru.name>
Fri, 12 Jun 2020 12:00:38 +0000 (15:00 +0300)
They will be regenerated with the corrected back/forth links.

TODO
reindex_blog_ru
remove_old_html.py [new file with mode: 0755]

diff --git a/TODO b/TODO
index 219689cdce1dbdcec54663defdac98c3e527a887..a7a08bb76a4856709c4634145fde7b440d4fd64a 100644 (file)
--- a/TODO
+++ b/TODO
@@ -1,9 +1,3 @@
-Исправить в блоге ссылки вперёд.
-
-Сама-то ссылка в коде есть, но надо перегенерировать HTML
-предыдущей страницы.
-
-
 Проставить на всех страницах ссылки на валидаторы
 http://validator.w3.org/check/referer и
 http://jigsaw.w3.org/css-validator/check/referer
index a02f1906874a4ccfca474fea9bbe584947341aae..1109cdca9bd1ce5bfc0e8271af3c924cfc71443a 100755 (executable)
@@ -5,5 +5,8 @@ umask 022
 LC_TIME=ru_RU.KOI8-R
 export LC_TIME
 
+cd "`dirname \"$0\"`" &&
+./remove_old_html.py &&
+
 cd ../htdocs/phdru.name &&
 exec ../../phdru.name/reindex_blog.py
diff --git a/remove_old_html.py b/remove_old_html.py
new file mode 100755 (executable)
index 0000000..904ff9e
--- /dev/null
@@ -0,0 +1,80 @@
+#! /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)