diff --git a/builtin_commands.py b/builtin_commands.py new file mode 100644 index 0000000..685039c --- /dev/null +++ b/builtin_commands.py @@ -0,0 +1,5 @@ +from panflute import Element + +class commands: + def woah(e: Element) -> Element: + return e diff --git a/command.py b/command.py new file mode 100644 index 0000000..9cd3312 --- /dev/null +++ b/command.py @@ -0,0 +1,18 @@ +from panflute import Div,Span +from util import * +from typing import Callable + +class Command: + pass + +class InlineCommand(Span, Command): + def call(self, f: Callable) -> Span: + r = f(self) + return replaceEl(self, Span(*r.content, identifier=r.identifier, classes=r.classes, attributes=r.attributes)) + pass + +class MultilineCommand(Div, Command): + def call(self, f: Callable) -> Div: + r = f(self) + return replaceEl(self, Div(*r.content, identifier=r.identifier, classes=r.classes, attributes=r.attributes)) + pass diff --git a/formatitko.py b/formatitko.py index 6f45bae..07125f4 100644 --- a/formatitko.py +++ b/formatitko.py @@ -2,26 +2,52 @@ # Import local files from whitespace import * +from command import * +from util import * +from builtin_commands import commands from panflute import * +import re from mj_show import show -ifs = ["dog"] +flags = ["dog"] def transform(e: Element): """Transform the AST, making format-agnostic changes.""" if isinstance(e, Whitespace) and bavlna(e): - e.parent.content[e.index] = NBSP() + e = replaceEl(e, NBSP()) if hasattr(e, "attributes"): + # `if` attribute. Only show this element if flag is set. if "if" in e.attributes: - if not e.attributes["if"] in ifs: - del e.parent.content[e.index] + if not e.attributes["if"] in flags: + deleteEl(e) + return + # `ifn` attribute. Only show this element if flag is NOT set + if "ifn" in e.attributes: + if e.attributes["ifn"] in flags: + deleteEl(e) + return + + # `c` attribute. Execute a command with the name saved in this attribute. + if (isinstance(e, Div) or isinstance(e, Span)) and "c" in e.attributes: + if isinstance(e, Div): + e = replaceEl(e, MultilineCommand(*e.content, identifier=e.identifier, classes=e.classes, attributes=e.attributes)) + else: + e = replaceEl(e, InlineCommand(*e.content, identifier=e.identifier, classes=e.classes, attributes=e.attributes)) + + # Handle special command shorthand [!commandname]{} + if isinstance(e, Span) and len(e.content) == 1 and isinstance(e.content[0], Str) and re.match(r"^!\w+$", e.content[0].text): + e = replaceEl(e, InlineCommand(identifier=e.identifier, classes=e.classes, attributes={**e.attributes, "c": e.content[0].text[1:]})) if hasattr(e, "content"): for c in e.content: transform(c) + + # All commands on the inside execute first while being transformed + if isinstance(e, Command): + pass # TODO: Dodělat tohle doc = load() @@ -30,6 +56,5 @@ print(show(doc)) transform(doc) print("---------------------") print(show(doc)) -print(vars(doc)) diff --git a/test.md b/test.md index 66698e5..de6056c 100644 --- a/test.md +++ b/test.md @@ -14,6 +14,8 @@ This should be seen by all. [This too!]{if=cat} +[An inline command with contents and **bold** and another [!command]{} inside!]{c=bro} + [!woah]{a=b} diff --git a/util.py b/util.py new file mode 100644 index 0000000..3a05f67 --- /dev/null +++ b/util.py @@ -0,0 +1,7 @@ +from panflute import Element + +def replaceEl(e: Element, r: Element) -> Element: + e.parent.content[e.index] = r + return r +def deleteEl(e: Element): + del e.parent.content[e.index]