From 44cf2a3c16e2dbe68f6c78bcc3a6bbe4c88eeb91 Mon Sep 17 00:00:00 2001
From: Zhiming Wang <zmwangx@gmail.com>
Date: Thu, 23 Jul 2015 10:29:05 -0700
Subject: pyblog: make HTML postprocessing extensible

Previously there's only one postprocessing function `number_code_lines`,
which directly reads an HTML file, and after processing, writes
back. Now the reading and writing is handled by a dedicated dispatcher
`postprocess_html_file`, which can call multiple postprocessors that
operates on a soup object.
---
 pyblog | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)

(limited to 'pyblog')

diff --git a/pyblog b/pyblog
index 605813ed..af3214a6 100755
--- a/pyblog
+++ b/pyblog
@@ -605,16 +605,23 @@ def _pre_tag_insert_line_numbers(soup, pre_tag):
         pre_tag.code.append(ln_tag)
 
 
-def number_code_lines(htmlfilepath):
+def number_code_lines(soup):
     """Insert line numbers to preformatted code blocks."""
+    for pre_tag in soup.find_all("pre"):
+        if ((pre_tag.code is None or "class" not in pre_tag.attrs or
+             not "sourceCode" in pre_tag["class"])):
+            # not really a block of source code
+            continue
+        _pre_tag_insert_line_numbers(soup, pre_tag)
+
+
+def postprocess_html_file(htmlfilepath):
+    """Perform a series of postprocessing to an HTML file."""
     with open(htmlfilepath, "r+", encoding="utf-8") as htmlfileobj:
         soup = bs4.BeautifulSoup(htmlfileobj.read(), "lxml")
-        for pre_tag in soup.find_all("pre"):
-            if ((pre_tag.code is None or "class" not in pre_tag.attrs or
-                 not "sourceCode" in pre_tag["class"])):
-                # not really a block of source code
-                continue
-            _pre_tag_insert_line_numbers(soup, pre_tag)
+
+        # a series of postprocessing (extensible)
+        number_code_lines(soup)
 
         # write back
         htmlfileobj.seek(0)
@@ -722,7 +729,8 @@ def generate_blog(fresh=False, report_total_errors=True):
                         failed_builds += 1
                         sys.stderr.write("error: failed to generate %s" %
                                          relpath)
-                    number_code_lines(dstpath)
+                    # postprocess generated HTML file
+                    postprocess_html_file(dstpath)
 
     if anything_modified:
         generate_index_and_feed()
-- 
cgit v1.2.1