commit 4ca04d82027a9136fb9df9e5ccd1acbb47bb7a22 Author: Greenscreener Date: Sun Nov 20 00:20:41 2022 +0100 Initial very WIP commit. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..eeb8a6e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +**/__pycache__ diff --git a/formatitko.py b/formatitko.py new file mode 100644 index 0000000..e4b276f --- /dev/null +++ b/formatitko.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python3 + +# Import local files +from whitespace import * + +from panflute import * + + +ifs = ["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() + + if hasattr(e, "attributes"): + if "if" in e.attributes: + if not e.attributes["if"] in ifs: + del e.parent.content[e.index] + + if hasattr(e, "content"): + for c in e.content: + transform(c) + + +doc = load() + +print(stringify(doc)) +transform(doc) +print("---------------------") +print(stringify(doc)) +print(vars(doc)) + + diff --git a/test.md b/test.md new file mode 100644 index 0000000..66698e5 --- /dev/null +++ b/test.md @@ -0,0 +1,19 @@ +# Hello world! + +This is an *example* **yay**! + +```python3 +def what(): + return no way +``` +:::{if=cat} +This should only be shown to cats +::: + +This should be seen by all. + +[This too!]{if=cat} + +[!woah]{a=b} + + diff --git a/whitespace.py b/whitespace.py new file mode 100644 index 0000000..a1da497 --- /dev/null +++ b/whitespace.py @@ -0,0 +1,12 @@ +from panflute import Space,SoftBreak +from typing import Union + +Whitespace = Union[Space,SoftBreak] + +class NBSP(Space): + pass + +def bavlna(e: Whitespace): + """Determine if given piece of whitespace should be non-breakable.""" + return False +