Browse Source

A lot of things, ifn, WIP commands.

pull/28/head
Jan Černohorský 2 years ago
parent
commit
f04ce0693b
  1. 5
      builtin_commands.py
  2. 18
      command.py
  3. 35
      formatitko.py
  4. 2
      test.md
  5. 7
      util.py

5
builtin_commands.py

@ -0,0 +1,5 @@
from panflute import Element
class commands:
def woah(e: Element) -> Element:
return e

18
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

35
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))

2
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} <!-- A special command! WOW -->

7
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]
Loading…
Cancel
Save