From 89bf34ed46edb0b41a0dbc626032cac5f87461b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mart=C3=ADnez=20Portela?= Date: Sat, 27 Aug 2022 13:33:47 +0200 Subject: [PATCH] Remove common indentation of code blocks. --- scripts/generate.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/scripts/generate.py b/scripts/generate.py index 944c57a..01aef10 100644 --- a/scripts/generate.py +++ b/scripts/generate.py @@ -288,7 +288,20 @@ def render_list_item(element, acc): def render_code_block(element, acc): acc.append('
')
-    acc.append(html.escape(element.lines))
+    content = html.escape(element.lines)
+
+    # Remove indentation common to all lines
+    base_indentation = min([
+        len(l) - len(l.lstrip(' '))
+        for l in content.split('\n')
+        if len(l.strip()) > 0
+    ])
+    content_lines = [
+        l[base_indentation:]
+        for l in content.split('\n')
+    ]
+
+    acc.append('\n'.join(content_lines))
     acc.append('
') def render_results_block(element, acc):