34 lines
1,006 B
Python
34 lines
1,006 B
Python
from panflute import Span, Div, Element, Plain, Para
|
|
|
|
class InlineError(Exception):
|
|
pass
|
|
|
|
class Command:
|
|
pass
|
|
|
|
# This distinction is needed because while transforming the tree, inline
|
|
# elements cannot be replaced with block ones
|
|
class InlineCommand(Span, Command):
|
|
def replaceSelf(self, *content: list[Element]) -> Span:
|
|
try:
|
|
return Span(*content)
|
|
except TypeError:
|
|
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}")
|
|
pass
|
|
|
|
class BlockCommand(Div, Command):
|
|
def replaceSelf(self, *content: list[Element]) -> Div:
|
|
try:
|
|
return Div(*content)
|
|
except TypeError:
|
|
return Div(Para(*content))
|
|
pass
|
|
|
|
class CodeCommand(BlockCommand):
|
|
test: str
|
|
def __init__(self, *args, **kwargs):
|
|
self.text = args[0]
|
|
super().__init__(**kwargs)
|