TP is now passed to commands. Output from the TP is not transformed automatically and has to be done manually from the commands.
This commit is contained in:
parent
84a4f6acb7
commit
caef60d472
6 changed files with 20 additions and 8 deletions
|
@ -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
|
||||
|
|
|
@ -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"]
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
|
||||
|
||||
|
|
|
@ -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):
|
||||
|
|
15
test/test.md
15
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
|
||||
<div>
|
||||
hahahahaah
|
||||
|
|
Loading…
Reference in a new issue