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.
This commit is contained in:
parent
f69e8a7127
commit
c85fc8ce55
6 changed files with 52 additions and 5 deletions
16
context.py
16
context.py
|
@ -44,3 +44,19 @@ class Context:
|
||||||
return self.parent.get_metadata(key)
|
return self.parent.get_metadata(key)
|
||||||
else:
|
else:
|
||||||
return None
|
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__
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,7 @@ print(show(doc))
|
||||||
context = Context(doc)
|
context = Context(doc)
|
||||||
doc = doc.walk(transform, context)
|
doc = doc.walk(transform, context)
|
||||||
print("---------------------")
|
print("---------------------")
|
||||||
#print(show(doc))
|
print(show(doc))
|
||||||
print(convert_text(doc, input_format="panflute", output_format="markdown"))
|
print(convert_text(doc, input_format="panflute", output_format="markdown"))
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -18,6 +18,18 @@ println()
|
||||||
println(f"The subdocument's subtitle is \n\n## {ctx.get_metadata('subtitle')}")
|
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}
|
:::{if=cat}
|
||||||
This should be only shown to included cats.
|
This should be only shown to included cats.
|
||||||
:::
|
:::
|
||||||
|
|
7
test.md
7
test.md
|
@ -22,12 +22,19 @@ ctx.set_flag("cat", True)
|
||||||
|
|
||||||
``` {.python .run}
|
``` {.python .run}
|
||||||
println(f"The main document's title is '{ctx.get_metadata('title')}'")
|
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}
|
::::{if=cat}
|
||||||
This should only be shown to cats the second time
|
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.
|
This should be seen by all.
|
||||||
|
|
||||||
[!opendatatask]{}
|
[!opendatatask]{}
|
||||||
|
|
|
@ -9,8 +9,8 @@ from context import *
|
||||||
|
|
||||||
def transform(e: Element, c: Context) -> Element: # Returns next sibling element to transform
|
def transform(e: Element, c: Context) -> Element: # Returns next sibling element to transform
|
||||||
"""Transform the AST, making format-agnostic changes."""
|
"""Transform the AST, making format-agnostic changes."""
|
||||||
|
|
||||||
if isinstance(e, Whitespace) and bavlna(e):
|
if isinstance(e, Whitespace) and bavlna(e, c):
|
||||||
e = NBSP()
|
e = NBSP()
|
||||||
|
|
||||||
if hasattr(e, "attributes"):
|
if hasattr(e, "attributes"):
|
||||||
|
|
|
@ -1,12 +1,24 @@
|
||||||
from panflute import Space,SoftBreak
|
from panflute import Space,SoftBreak,Str
|
||||||
from typing import Union
|
from typing import Union
|
||||||
|
|
||||||
|
# Import local files
|
||||||
|
from context import Context
|
||||||
|
|
||||||
Whitespace = Union[Space,SoftBreak]
|
Whitespace = Union[Space,SoftBreak]
|
||||||
|
|
||||||
class NBSP(Space):
|
class NBSP(Space):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def bavlna(e: Whitespace):
|
def bavlna(e: Whitespace, c: Context) -> bool:
|
||||||
"""Determine if given piece of whitespace should be non-breakable."""
|
"""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
|
return False
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue