From 4ca04d82027a9136fb9df9e5ccd1acbb47bb7a22 Mon Sep 17 00:00:00 2001 From: Greenscreener Date: Sun, 20 Nov 2022 00:20:41 +0100 Subject: [PATCH] Initial very WIP commit. --- .gitignore | 1 + formatitko.py | 35 +++++++++++++++++++++++++++++++++++ test.md | 19 +++++++++++++++++++ whitespace.py | 12 ++++++++++++ 4 files changed, 67 insertions(+) create mode 100644 .gitignore create mode 100644 formatitko.py create mode 100644 test.md create mode 100644 whitespace.py 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 +