aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZhiming Wang <zmwangx@gmail.com>2015-07-23 10:40:55 -0700
committerZhiming Wang <zmwangx@gmail.com>2015-07-23 10:40:55 -0700
commit18d5bcebcf459db56688c352f2eb8ad9b4e35480 (patch)
treed152e03feae676c1fcc3c345fab71bba0d4eb22e
parent44cf2a3c16e2dbe68f6c78bcc3a6bbe4c88eeb91 (diff)
downloadmy_new_personal_website-18d5bcebcf459db56688c352f2eb8ad9b4e35480.tar.xz
my_new_personal_website-18d5bcebcf459db56688c352f2eb8ad9b4e35480.zip
pyblog: convert each <img> tag in <article> to a link to its original
-rwxr-xr-xpyblog11
1 files changed, 11 insertions, 0 deletions
diff --git a/pyblog b/pyblog
index af3214a6..aac4bd9d 100755
--- a/pyblog
+++ b/pyblog
@@ -615,6 +615,16 @@ def number_code_lines(soup):
_pre_tag_insert_line_numbers(soup, pre_tag)
+def link_img_tags(soup):
+ """Convert each <img> tag in <article> to a link to its original."""
+ if not soup.article:
+ return
+ for img_tag in soup.article.find_all("img"):
+ a_tag = soup.new_tag("a", href=img_tag["src"], target="_blank")
+ a_tag.insert(0, copy.copy(img_tag))
+ img_tag.replace_with(a_tag)
+
+
def postprocess_html_file(htmlfilepath):
"""Perform a series of postprocessing to an HTML file."""
with open(htmlfilepath, "r+", encoding="utf-8") as htmlfileobj:
@@ -622,6 +632,7 @@ def postprocess_html_file(htmlfilepath):
# a series of postprocessing (extensible)
number_code_lines(soup)
+ link_img_tags(soup)
# write back
htmlfileobj.seek(0)