From 91bbbfc2b78a6539165b8770b21c0fe649dd4cbc Mon Sep 17 00:00:00 2001 From: Greenscreener Date: Sat, 4 Feb 2023 00:51:46 +0100 Subject: [PATCH] Added direct print of metadata. --- test.md | 3 ++- transform.py | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/test.md b/test.md index 8febf7a..931dcb3 100644 --- a/test.md +++ b/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) diff --git a/transform.py b/transform.py index 4478570..be158d6 100644 --- a/transform.py +++ b/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.