Browse Source

Added direct print of metadata.

pull/28/head
Jan Černohorský 1 year ago
parent
commit
91bbbfc2b7
  1. 3
      test.md
  2. 15
      transform.py

3
test.md

@ -1,6 +1,7 @@
---
title: 'Wooooo a title'
subtitle: 'A subtitle'
are_we_there_yet: False
---
[#test-import.md]{}
@ -36,7 +37,7 @@ def bruh(no):
This should only be shown to cats the second time
::::
# [$are_we_there_yet]{}
![This is a figure, go figure...](/this/image/does/not/exist.jpg)

15
transform.py

@ -75,7 +75,7 @@ def transform(e: Element, c: Context) -> Element: # Returns next sibling element
## Shorthands
if isinstance(e, Span) and len(e.content) == 1 and isinstance(e.content[0], Str):
## Handle special command shorthand [!commandname]{}
if re.match(r"^![\w.]+$", e.content[0].text):
if re.match(r"^![\w]+$", e.content[0].text):
e = InlineCommand(identifier=e.identifier, classes=e.classes, attributes={**e.attributes, "c": e.content[0].text[1:]})
## Handle import [#path/file.md]{}
@ -86,6 +86,19 @@ def transform(e: Element, c: Context) -> Element: # Returns next sibling element
importedDoc.walk(transform, c)
return nullify(e)
## Handle metadata print [$something.something]{}
elif re.match(r"^\$[\w.]+$", e.content[0].text):
val = c.get_metadata(e.content[0].text[1:], False)
if isinstance(val, MetaInlines):
e = Span(*val.content)
e = e.walk(transform, c)
elif isinstance(val, MetaString):
e = Span(Str(val.string))
elif isinstance(val, MetaBool):
e = Span(Str(str(val.boolean)))
else:
raise ValueError(f"Cannot print value of metadatum {e.content[0].text[1:]}")
## Execute commands
# panflute's walk transforms the children first, then the root element, so
# the content of the element the command receives is already transformed.

Loading…
Cancel
Save