diff options
author | Zhiming Wang <zmwangx@gmail.com> | 2015-07-23 10:40:55 -0700 |
---|---|---|
committer | Zhiming Wang <zmwangx@gmail.com> | 2015-07-23 10:40:55 -0700 |
commit | 18d5bcebcf459db56688c352f2eb8ad9b4e35480 (patch) | |
tree | d152e03feae676c1fcc3c345fab71bba0d4eb22e /pyblog | |
parent | 44cf2a3c16e2dbe68f6c78bcc3a6bbe4c88eeb91 (diff) | |
download | my_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
Diffstat (limited to '')
-rwxr-xr-x | pyblog | 11 |
1 files changed, 11 insertions, 0 deletions
@@ -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) |