From de5fc304be7bacafec3658192b3eb6f2126272df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20H=C3=A4drich?= <11225821+shaedrich@users.noreply.github.com> Date: Thu, 6 Jun 2024 02:33:15 +0200 Subject: [PATCH] dedent_subsections logic --- lib/superfences_ponylang/__init__.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/superfences_ponylang/__init__.py b/lib/superfences_ponylang/__init__.py index e12f92d5..4d79b4b1 100644 --- a/lib/superfences_ponylang/__init__.py +++ b/lib/superfences_ponylang/__init__.py @@ -13,6 +13,7 @@ from mkdocs.utils.templates import TemplateContext import os +import re def format(source, language, css_class, options, md, classes=None, id_value='', attrs=None, **kwargs): if "snippet" in attrs: #options @@ -36,8 +37,23 @@ def format(source, language, css_class, options, md, classes=None, id_value='', elif i == int(lineNum): lines.append(line) #source = str(lines) + + if 'dedent_subsections' in attrs and attrs.get('dedent_subsections'): + p = re.compile('^\s+') + indents = [] + for line in lines: + m = p.match(line) + if m is None: + indents.append(0) + else: + indents.append(m.span()[1]) + indent = min(indents) + if indent > 0: + for i, line in enumerate(lines): + lines[i] = line[indent:None] + source = '\n'.join(lines) - source = str(base.Config.user_configs.__dict__) + str(TemplateContext) + str(c) + str(options) + str(attrs) + str(classes) + str(kwargs) + #source = str(base.Config.user_configs.__dict__) + str(TemplateContext) + str(c) + str(options) + str(attrs) + str(classes) + str(kwargs) else: with open(os.getcwd() + "/code-samples/" + snippetPath, 'r') as f: source = f.read()