diff --git a/src/formatitko/command.py b/src/formatitko/command.py index 5eb602c..943c304 100644 --- a/src/formatitko/command.py +++ b/src/formatitko/command.py @@ -27,3 +27,8 @@ class BlockCommand(Div, Command): return Div(Para(*content)) pass +class CodeCommand(BlockCommand): + test: str + def __init__(self, *args, **kwargs): + self.text = args[0] + super().__init__(**kwargs) diff --git a/src/formatitko/elements.py b/src/formatitko/elements.py index f39543d..17840f4 100644 --- a/src/formatitko/elements.py +++ b/src/formatitko/elements.py @@ -1,7 +1,7 @@ from panflute import Quoted -from .command import Command, InlineCommand, BlockCommand +from .command import Command, InlineCommand, BlockCommand, CodeCommand from .context import Group, BlockGroup, InlineGroup from .whitespace import Whitespace, NBSP diff --git a/src/formatitko/transform_processor.py b/src/formatitko/transform_processor.py index caf0d32..bd98656 100644 --- a/src/formatitko/transform_processor.py +++ b/src/formatitko/transform_processor.py @@ -17,7 +17,7 @@ from .context import Group, InlineGroup, BlockGroup from .util import nullify, import_md from .context import Context, CommandCallable 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 ELCl = Union[Element, ListContainer, list[Union[Element, ListContainer]]] @@ -104,7 +104,8 @@ class TransformProcessor: FQuoted: self.transform_FQuoted, 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=""): @@ -507,6 +508,8 @@ class TransformProcessor: return nullify(e) 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 # 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: return self.transform_Command(e) + def transform_CodeCommand(self, e: CodeCommand) -> Div: + return self.transform_Command(e) + def transform_Whitespace(self, e: Whitespace) -> Whitespace: if bavlna(e, self.context): return NBSP() diff --git a/test/test.md b/test/test.md index c9ed067..5c62835 100644 --- a/test/test.md +++ b/test/test.md @@ -184,3 +184,14 @@ ii. wym bro 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 +``` + +