Browse Source

Ještě více magie, která se snaží zachraňovat blokové výstupy z příkazů, které byly zavolány jako Span.

master
Jan Černohorský 9 months ago
parent
commit
e2f2c4f5f0
  1. 2
      src/formatitko/command.py
  2. 2
      src/formatitko/output_generator.py
  3. 27
      src/formatitko/transform_processor.py

2
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)): if len(content) == 1 and (isinstance(content[0], Para) or isinstance(content[0], Plain)):
return Span(*content[0].content) return Span(*content[0].content)
else: 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 pass
class BlockCommand(Div, Command): class BlockCommand(Div, Command):

2
src/formatitko/output_generator.py

@ -49,7 +49,7 @@ class FormatitkoRecursiveError(Exception):
eprint('on line: "' + stringify(line).strip() + '"', end="") eprint('on line: "' + stringify(line).strip() + '"', end="")
eprint() eprint()
eprint("in element: " + str(self.elements[0]).replace("\n", "\\n")) eprint("in element: " + str(self.elements[0]).replace("\n", "\\n"))
sys.tracebacklimit = 2 sys.tracebacklimit = 0
raise self.__cause__ from None raise self.__cause__ from None

27
src/formatitko/transform_processor.py

@ -18,7 +18,7 @@ from .context import Group, InlineGroup, BlockGroup
from .util import nullify, import_md from .util import nullify, import_md
from .context import Context, CommandCallable from .context import Context, CommandCallable
from .whitespace import Whitespace, bavlna 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 .command_util import handle_command_define, parse_command
from .nop_processor import NOPProcessor, ELCl, DoubleDocError from .nop_processor import NOPProcessor, ELCl, DoubleDocError
@ -56,6 +56,18 @@ class TransformProcessor(NOPProcessor):
return nullify(e) return nullify(e)
return 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 '<command_name>'}}}\n:::")
else:
raise err
def transform_Doc(self, e: Doc) -> Doc: def transform_Doc(self, e: Doc) -> Doc:
if self.context is not None: if self.context is not None:
@ -96,6 +108,19 @@ class TransformProcessor(NOPProcessor):
else: else:
return BlockGroup(*content, context=new_context) 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]: def transform_Div(self, e: Div) -> Union[Div, Group, Null, RawBlock]:
e.content = self.transform(e.content) e.content = self.transform(e.content)

Loading…
Cancel
Save