aboutsummaryrefslogtreecommitdiff
path: root/pyblog
diff options
context:
space:
mode:
authorZhiming Wang <zmwangx@gmail.com>2015-05-14 20:57:01 -0700
committerZhiming Wang <zmwangx@gmail.com>2015-05-14 20:57:01 -0700
commit862e27c9e40be18fb411193631828b101791943c (patch)
tree6a6bee787c2a8b9342cf055515642f31b4252578 /pyblog
parentb6d0d727c5be9a5180fb9baf9565fc1768c0ddfe (diff)
downloadmy_new_personal_website-862e27c9e40be18fb411193631828b101791943c.tar.xz
my_new_personal_website-862e27c9e40be18fb411193631828b101791943c.zip
add source code line numbers
The way I handle line numbers and the pre block in general is inspired by the MDN wiki. See, for instance, https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript a screenshot is here: https://i.imgur.com/982TBDc.png Also tweaked other styles, e.g., changed the primary monospace font to Consolas, and slightly tweaked a few old posts.
Diffstat (limited to 'pyblog')
-rwxr-xr-xpyblog36
1 files changed, 36 insertions, 0 deletions
diff --git a/pyblog b/pyblog
index 42694cba..add06650 100755
--- a/pyblog
+++ b/pyblog
@@ -54,6 +54,7 @@ ATOM = os.path.join(BUILDDIR, "atom.xml")
INDEXHTML = os.path.join(BUILDDIR, "index.html")
FEED_MAX_ENTRIES = 20
+CODE_LINE_HEIGHT = 18
####################### END OF GENERATOR CONFIGURATIONS ########################
@@ -409,6 +410,38 @@ def generate_index_and_feed():
generate_sitemap(feed)
+def _pre_tag_insert_line_numbers(soup, pre_tag):
+ """Insert line numbers to a pre tag."""
+ num_lines = len(pre_tag.text.split("\n"))
+ for line_number in range(1, num_lines + 1):
+ # line number divs will look like:
+ # <span class="line-number" data-line="1" style="top: 0px"><!----></span>
+ # <span class="line-number" data-line="2" style="top: 18px"><!----></span>
+ ln_tag = soup.new_tag("span")
+ ln_tag["class"] = "line-number"
+ ln_tag["data-line"] = line_number
+ ln_tag["style"] = "top: %dpx" % ((line_number - 1) * CODE_LINE_HEIGHT)
+ ln_tag.append(soup.new_string("", bs4.Comment))
+ pre_tag.code.append(ln_tag)
+
+
+def number_code_lines(htmlfilepath):
+ """Insert line numbers to preformatted code blocks."""
+ with open(htmlfilepath, "r+", encoding="utf-8") as htmlfileobj:
+ soup = bs4.BeautifulSoup(htmlfileobj.read())
+ 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)
+
+ # write back
+ htmlfileobj.seek(0)
+ htmlfileobj.write(str(soup))
+ htmlfileobj.truncate()
+
+
def generate_blog(fresh=False, report_total_errors=True):
"""Generate the blog in BUILDDIR.
@@ -503,8 +536,11 @@ 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)
+
if anything_modified:
generate_index_and_feed()
+ sys.stderr.write("done\n")
if report_total_errors:
sys.stderr.write("build finished with %d errors\n" % failed_builds)