Browse Source

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.
pull/28/head
Jan Černohorský 2 years ago
parent
commit
c85fc8ce55
  1. 16
      context.py
  2. 2
      formatitko.py
  3. 12
      test-partial.md
  4. 7
      test.md
  5. 4
      transform.py
  6. 16
      whitespace.py

16
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__

2
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"))

12
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.
:::

7
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]{}

4
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"):

16
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

Loading…
Cancel
Save