From caef60d4726cd1813025d26c9ef2e09e20f59bd0 Mon Sep 17 00:00:00 2001 From: Greenscreener Date: Sun, 18 Feb 2024 00:27:13 +0100 Subject: [PATCH] TP is now passed to commands. Output from the TP is not transformed automatically and has to be done manually from the commands. --- src/formatitko/command_env.py | 1 + src/formatitko/command_util.py | 2 +- src/formatitko/context.py | 2 +- src/formatitko/output_generator.py | 2 +- src/formatitko/transform_processor.py | 6 +++--- test/test.md | 15 +++++++++++++-- 6 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/formatitko/command_env.py b/src/formatitko/command_env.py index 4775db4..a943d6f 100644 --- a/src/formatitko/command_env.py +++ b/src/formatitko/command_env.py @@ -5,4 +5,5 @@ from formatitko.util import parse_string from formatitko.context import Context from formatitko.command import Command +from .nop_processor import NOPProcessor from panflute import Element diff --git a/src/formatitko/command_util.py b/src/formatitko/command_util.py index f11a154..e4af09a 100644 --- a/src/formatitko/command_util.py +++ b/src/formatitko/command_util.py @@ -15,7 +15,7 @@ def parse_command(code: str) -> CommandCallable: indented_code_lines = [] for line in code_lines: indented_code_lines.append(("\t" if tabs else " ")+line) - code = "def command(element: Command, context: Context) -> list[Element]:\n"+"\n".join(indented_code_lines) + code = "def command(element: Command, context: Context, processor: NOPProcessor) -> list[Element]:\n"+"\n".join(indented_code_lines) env = {**command_env.__dict__} exec(code, env) return env["command"] diff --git a/src/formatitko/context.py b/src/formatitko/context.py index 31c1587..11f0b94 100644 --- a/src/formatitko/context.py +++ b/src/formatitko/context.py @@ -7,7 +7,7 @@ import warnings from .command import Command -CommandCallable = Callable[[Command, 'Context'], list[Element]] # This is here because of a wild circular import dependency between many functions and classes +CommandCallable = Callable[[Command, 'Context', 'NOPProcessor'], list[Element]] # This is here because of a wild circular import dependency between many functions and classes # This class is used to keep state while transforming the document using # transform.py. For the context to be available to the html and TeX generators, diff --git a/src/formatitko/output_generator.py b/src/formatitko/output_generator.py index 332bb25..daa30af 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 = 0 + sys.tracebacklimit = 2 raise self.__cause__ from None diff --git a/src/formatitko/transform_processor.py b/src/formatitko/transform_processor.py index 60cba5a..d3b02b4 100644 --- a/src/formatitko/transform_processor.py +++ b/src/formatitko/transform_processor.py @@ -229,7 +229,7 @@ class TransformProcessor(NOPProcessor): if "python" in e.classes and "run" in e.classes: if not self.context.trusted: return nullify(e) - command_output = parse_command(e.text)(BlockCommand(), self.context) + command_output = parse_command(e.text)(BlockCommand(), self.context, self) e = BlockCommand().replaceSelf(*([] if command_output is None else command_output)) return self.transform(e) @@ -252,9 +252,9 @@ class TransformProcessor(NOPProcessor): def transform_Command(self, e: Command) -> Union[Div, Span]: if not self.context.get_command(e.attributes["c"]): raise NameError(f"Command not defined '{e.attributes['c']}'.") - command_output = self.context.get_command(e.attributes["c"])(e, self.context) + command_output = self.context.get_command(e.attributes["c"])(e, self.context, self) e = e.replaceSelf(*([] if command_output is None else command_output)) - return self.transform(e) + return e def transform_Whitespace(self, e: Whitespace) -> Whitespace: if bavlna(e, self.context): diff --git a/test/test.md b/test/test.md index 87929b8..80be954 100644 --- a/test/test.md +++ b/test/test.md @@ -198,13 +198,24 @@ ii. wym bro ```python {define=bash} import subprocess c = subprocess.run(["bash", "-c", element.text], stdout=subprocess.PIPE, check=True, encoding="utf-8") -return [pf.Para(pf.Str(c.stdout))] +return [pf.CodeBlock(c.stdout)] ``` ```bash {c=bash} -cat /etc/hostname +cat /etc/os-release ``` +::: {.group lang=cs} +```python {.run} +return processor.transform([ + *parse_string("V "), + pf.Link(pf.Str("odevzdávátku"), url="https://ksp.mff.cuni.cz/z/odevzdavatko/"), + *parse_string(" si necháte vygenerovat vstupy a odevzdáte příslušné výstupy. Záleží jen na vás, jak výstupy vyrobíte.") + +]) +``` +::: + ```html
hahahahaah