diff options
author | Zhiming Wang <zmwangx@gmail.com> | 2015-07-23 10:29:05 -0700 |
---|---|---|
committer | Zhiming Wang <zmwangx@gmail.com> | 2015-07-23 10:31:58 -0700 |
commit | 44cf2a3c16e2dbe68f6c78bcc3a6bbe4c88eeb91 (patch) | |
tree | e3524b6c3338d01f4ad24af274e6edfc4d200a91 | |
parent | 740b9301747a5a401cd9c3a6f2a80b3d510dd5f9 (diff) | |
download | my_new_personal_website-44cf2a3c16e2dbe68f6c78bcc3a6bbe4c88eeb91.tar.xz my_new_personal_website-44cf2a3c16e2dbe68f6c78bcc3a6bbe4c88eeb91.zip |
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.
Diffstat (limited to '')
-rwxr-xr-x | pyblog | 24 |
1 files changed, 16 insertions, 8 deletions
@@ -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() |