From c85fc8ce55d3be6d9146dc846d709fdffc2dae2a Mon Sep 17 00:00:00 2001 From: Greenscreener Date: Wed, 1 Feb 2023 01:32:36 +0100 Subject: [PATCH] Added some functionality to bavlna, metadata write Also includes an hour and a half of debugging this issue: https://github.com/sergiocorreia/panflute/pull/224 just to find out it was already fixed in the version that I looked at the source code of but not in the version installed on my system. --- context.py | 16 ++++++++++++++++ formatitko.py | 2 +- test-partial.md | 12 ++++++++++++ test.md | 7 +++++++ transform.py | 4 ++-- whitespace.py | 16 ++++++++++++++-- 6 files changed, 52 insertions(+), 5 deletions(-) diff --git a/context.py b/context.py index 9817fdd..bb8cebb 100644 --- a/context.py +++ b/context.py @@ -44,3 +44,19 @@ class Context: return self.parent.get_metadata(key) else: return None + + def set_metadata(self, key, value): + meta = self.doc.metadata + key = key.split(".") + for k in key[:-1]: + meta = meta[k] + meta[key[-1]] = value + + def unset_metadata(self, key): + meta = self.doc.metadata + key = key.split(".") + for k in key[:-1]: + meta = meta[k] + print(type(meta)) + del meta.content[key[-1]] # A hack because MetaMap doesn't have a __delitem__ + diff --git a/formatitko.py b/formatitko.py index b997257..373791c 100755 --- a/formatitko.py +++ b/formatitko.py @@ -18,7 +18,7 @@ print(show(doc)) context = Context(doc) doc = doc.walk(transform, context) print("---------------------") -#print(show(doc)) +print(show(doc)) print(convert_text(doc, input_format="panflute", output_format="markdown")) diff --git a/test-partial.md b/test-partial.md index 0ab00a2..8e021f8 100644 --- a/test-partial.md +++ b/test-partial.md @@ -18,6 +18,18 @@ println() println(f"The subdocument's subtitle is \n\n## {ctx.get_metadata('subtitle')}") ``` +``` {.python .run} +ctx.set_metadata("language", "cs") +``` + +Tak toto je v prádelně pánové! + +``` {.python .run} +ctx.unset_metadata("language") +``` + +I am a duck. + :::{if=cat} This should be only shown to included cats. ::: diff --git a/test.md b/test.md index a0f901a..8f68efb 100644 --- a/test.md +++ b/test.md @@ -22,12 +22,19 @@ ctx.set_flag("cat", True) ``` {.python .run} println(f"The main document's title is '{ctx.get_metadata('title')}'") +ctx.set_metadata("a", {}) +ctx.set_metadata("a.b", {}) +ctx.set_metadata("a.b.c", "Bruh **bruh** bruh") ``` ::::{if=cat} This should only be shown to cats the second time :::: +A non-breakable space bro + +A lot of spaces + This should be seen by all. [!opendatatask]{} diff --git a/transform.py b/transform.py index 7c85e9b..c88fe1c 100644 --- a/transform.py +++ b/transform.py @@ -9,8 +9,8 @@ from context import * def transform(e: Element, c: Context) -> Element: # Returns next sibling element to transform """Transform the AST, making format-agnostic changes.""" - - if isinstance(e, Whitespace) and bavlna(e): + + if isinstance(e, Whitespace) and bavlna(e, c): e = NBSP() if hasattr(e, "attributes"): diff --git a/whitespace.py b/whitespace.py index a1da497..bf27d26 100644 --- a/whitespace.py +++ b/whitespace.py @@ -1,12 +1,24 @@ -from panflute import Space,SoftBreak +from panflute import Space,SoftBreak,Str from typing import Union +# Import local files +from context import Context + Whitespace = Union[Space,SoftBreak] class NBSP(Space): pass -def bavlna(e: Whitespace): +def bavlna(e: Whitespace, c: Context) -> bool: """Determine if given piece of whitespace should be non-breakable.""" + + if c.get_metadata("language") == "cs": + + # TODO: Add more clever stuff + if isinstance(e.prev, Str) and isinstance(e.next, Str) and e.prev.text.lower() in ['k', 's', 'v', 'z', 'o', 'u', 'a', 'i']: + return True + + + return False