Přidány CodeCommands, CodeBlocky nyní mají také atribut c a umí se zavolat jako Command. Resolves #30

This commit is contained in:
Jan Černohorský 2023-08-20 15:17:58 +02:00
parent 96fabf6a14
commit adcfa98fd2
4 changed files with 25 additions and 3 deletions

View file

@ -27,3 +27,8 @@ class BlockCommand(Div, Command):
return Div(Para(*content)) return Div(Para(*content))
pass pass
class CodeCommand(BlockCommand):
test: str
def __init__(self, *args, **kwargs):
self.text = args[0]
super().__init__(**kwargs)

View file

@ -1,7 +1,7 @@
from panflute import Quoted from panflute import Quoted
from .command import Command, InlineCommand, BlockCommand from .command import Command, InlineCommand, BlockCommand, CodeCommand
from .context import Group, BlockGroup, InlineGroup from .context import Group, BlockGroup, InlineGroup
from .whitespace import Whitespace, NBSP from .whitespace import Whitespace, NBSP

View file

@ -17,7 +17,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, Command from .command import BlockCommand, InlineCommand, CodeCommand, Command
from .command_util import handle_command_define, parse_command from .command_util import handle_command_define, parse_command
ELCl = Union[Element, ListContainer, list[Union[Element, ListContainer]]] ELCl = Union[Element, ListContainer, list[Union[Element, ListContainer]]]
@ -104,7 +104,8 @@ class TransformProcessor:
FQuoted: self.transform_FQuoted, FQuoted: self.transform_FQuoted,
InlineCommand: self.transform_InlineCommand, InlineCommand: self.transform_InlineCommand,
BlockCommand: self.transform_BlockCommand BlockCommand: self.transform_BlockCommand,
CodeCommand: self.transform_CodeCommand
} }
def add_command_module(self, module: Union[dict[str, CommandCallable], ModuleType], module_name: str=""): def add_command_module(self, module: Union[dict[str, CommandCallable], ModuleType], module_name: str=""):
@ -507,6 +508,8 @@ class TransformProcessor:
return nullify(e) return nullify(e)
return handle_command_define(e, self.context) return handle_command_define(e, self.context)
if "c" in e.attributes:
return self.transform(CodeCommand(e.text, identifier=e.identifier, classes=e.classes, attributes=e.attributes))
# Pass down metadata 'highlight' and 'highlight_style' as attribute to CodeBlocks # Pass down metadata 'highlight' and 'highlight_style' as attribute to CodeBlocks
# OG now has Context so this is not needed per se, but I'm keeping this here for the handling of attribute > context > default value # OG now has Context so this is not needed per se, but I'm keeping this here for the handling of attribute > context > default value
@ -529,6 +532,9 @@ class TransformProcessor:
def transform_BlockCommand(self, e: BlockCommand) -> Div: def transform_BlockCommand(self, e: BlockCommand) -> Div:
return self.transform_Command(e) return self.transform_Command(e)
def transform_CodeCommand(self, e: CodeCommand) -> Div:
return self.transform_Command(e)
def transform_Whitespace(self, e: Whitespace) -> Whitespace: def transform_Whitespace(self, e: Whitespace) -> Whitespace:
if bavlna(e, self.context): if bavlna(e, self.context):
return NBSP() return NBSP()

View file

@ -184,3 +184,14 @@ ii. wym bro
1 1 1 1 1 1 1 1
------- ------ ---------- ------- ------- ------ ---------- -------
```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))]
```
```bash {c=bash}
cat /etc/hostname
```