diff --git a/misc/gen_blog_post_html.py b/misc/gen_blog_post_html.py index 00e167e4a3a25..06794a895b38b 100644 --- a/misc/gen_blog_post_html.py +++ b/misc/gen_blog_post_html.py @@ -17,6 +17,7 @@ import os import re import sys +import textwrap def format_lists(h: str) -> str: @@ -44,16 +45,23 @@ def format_code(h: str) -> str: while i < len(a): if a[i].startswith(" ") or a[i].startswith("```"): indent = a[i].startswith(" ") + language: str = "" if not indent: + language = a[i][3:] i += 1 - r.append("
")
+            if language:
+                r.append(f'
')
+            else:
+                r.append("
")
             while i < len(a) and (
                 (indent and a[i].startswith("    ")) or (not indent and not a[i].startswith("```"))
             ):
                 # Undo > and <
                 line = a[i].replace(">", ">").replace("<", "<")
-                if not indent:
-                    line = "    " + line
+                if indent:
+                    # Undo this extra level of indentation so it looks nice with
+                    # syntax highlighting CSS.
+                    line = line[4:]
                 r.append(html.escape(line))
                 i += 1
             r.append("
") @@ -64,7 +72,7 @@ def format_code(h: str) -> str: i += 1 formatted = "\n".join(r) # remove empty first line for code blocks - return re.sub(r"\n", r"", formatted) + return re.sub(r"]*)>\n", r"", formatted) def convert(src: str) -> str: @@ -131,8 +139,16 @@ def convert(src: str) -> str: h, ) - # Add missing top-level HTML tags - h = '\n\n\n' + h + "\n" + # Add top-level HTML tags and headers for syntax highlighting css/js + h = f""" + + + + + +{h} + +""" return h