From e2f2c4f5f05d9d3694bb863c6195f52a8802a0c4 Mon Sep 17 00:00:00 2001 From: Greenscreener Date: Tue, 20 Feb 2024 20:09:58 +0100 Subject: [PATCH] =?UTF-8?q?Je=C5=A1t=C4=9B=20v=C3=ADce=20magie,=20kter?= =?UTF-8?q?=C3=A1=20se=20sna=C5=BE=C3=AD=20zachra=C5=88ovat=20blokov=C3=A9?= =?UTF-8?q?=20v=C3=BDstupy=20z=20p=C5=99=C3=ADkaz=C5=AF,=20kter=C3=A9=20by?= =?UTF-8?q?ly=20zavol=C3=A1ny=20jako=20Span.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/formatitko/command.py | 2 +- src/formatitko/output_generator.py | 2 +- src/formatitko/transform_processor.py | 27 ++++++++++++++++++++++++++- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/formatitko/command.py b/src/formatitko/command.py index 943c304..88686a0 100644 --- a/src/formatitko/command.py +++ b/src/formatitko/command.py @@ -16,7 +16,7 @@ class InlineCommand(Span, Command): if len(content) == 1 and (isinstance(content[0], Para) or isinstance(content[0], Plain)): return Span(*content[0].content) else: - raise InlineError(f"The command {self.attributes['c']} returned multiple Paragraphs and must be executed using `::: {{c={self.attributes['c']}}}\\n:::`.\n\n{content}") + return Div(*content) pass class BlockCommand(Div, Command): diff --git a/src/formatitko/output_generator.py b/src/formatitko/output_generator.py index daa30af..332bb25 100644 --- a/src/formatitko/output_generator.py +++ b/src/formatitko/output_generator.py @@ -49,7 +49,7 @@ class FormatitkoRecursiveError(Exception): eprint('on line: "' + stringify(line).strip() + '"', end="") eprint() eprint("in element: " + str(self.elements[0]).replace("\n", "\\n")) - sys.tracebacklimit = 2 + sys.tracebacklimit = 0 raise self.__cause__ from None diff --git a/src/formatitko/transform_processor.py b/src/formatitko/transform_processor.py index d3b02b4..d327663 100644 --- a/src/formatitko/transform_processor.py +++ b/src/formatitko/transform_processor.py @@ -18,7 +18,7 @@ from .context import Group, InlineGroup, BlockGroup from .util import nullify, import_md from .context import Context, CommandCallable from .whitespace import Whitespace, bavlna -from .command import BlockCommand, InlineCommand, CodeCommand, Command +from .command import BlockCommand, InlineCommand, CodeCommand, Command, InlineError from .command_util import handle_command_define, parse_command from .nop_processor import NOPProcessor, ELCl, DoubleDocError @@ -56,6 +56,18 @@ class TransformProcessor(NOPProcessor): return nullify(e) return e + def transform_ListContainer(self, e: ListContainer) -> ListContainer: + try: + return super().transform_ListContainer(e) + except TypeError as err: + names = [] + for el in e: + if hasattr(el, "attributes") and "c" in el.attributes: + names.append(el.attributes["c"]) + if len(names) > 0: + raise InlineError(f"The command{'s' if len(names) > 1 else ''} {names[0] if len(names) == 1 else names} was called in an Inline way but returned Block content. Put it in a paragraph alone or execute it as a Div using: \n::: {{c={names[0] if len(names) == 1 else ''}}}\n:::") + else: + raise err def transform_Doc(self, e: Doc) -> Doc: if self.context is not None: @@ -96,6 +108,19 @@ class TransformProcessor(NOPProcessor): else: return BlockGroup(*content, context=new_context) + def transform_Para(self, e: Para) -> Union[Para, Div]: + if len(e.content) == 1 and isinstance(e.content[0], Span): + # If the span turns out to be a command, it might return a Div. We should then replace ourselves with the Div + span = e.content[0] + span = self.transform(span) + if isinstance(span, Div): + return span + else: + e.content[0] = span + return super().transform_Para(e) + else: + return super().transform_Para(e) + def transform_Div(self, e: Div) -> Union[Div, Group, Null, RawBlock]: e.content = self.transform(e.content)