#!/usr/bin/env python3 # 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 flags = ["dog"] def transform(e: Element): """Transform the AST, making format-agnostic changes.""" if isinstance(e, Whitespace) and bavlna(e): 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 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() print(show(doc)) transform(doc) print("---------------------") print(show(doc))