You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

43 lines
1.6 KiB

from panflute import Element, Block, Inline, Null, Str, Doc, convert_text, Para, Plain, Span, Space
import re
from typing import Union
# It sometimes happens that an element contains a single paragraph or even a
# single plaintext line. It can be sometimes useful to extract this single
# paragraph, which is inline.
def inlinify(e: Element) -> Union[list[Element], None]:
if len(e.content) == 1 and (isinstance(e.content[0], Para) or isinstance(e.content[0], Plain)):
return e.content[0].content
# In transform, inline elements cannot be replaced with Block ones and also
# cannot be removed from the tree entirely, because that would mess up the
# iteration process through the tree. We replace them with null elements
# instead which never make it to the output.
def nullify(e: Element) -> Union[Str, Null]:
if isinstance(e, Inline):
return Str("")
else:
return Null()
def parse_string(s: str) -> list[Union[Str, Space]]:
words = s.split(" ")
output = []
first_word, *words = words
if first_word != "":
output.append(Str(first_word))
for word in words:
output.append(Space())
if word != "":
output.append(Str(word))
return output
# A helper function to import markdown using panflute (which calls pandoc). If
# we ever want to disable or enable some of panflute's markdown extensions,
# this is the place to do it.
def import_md(s: str, standalone: bool=True) -> Union[Doc, list[Element]]:
return convert_text(s, standalone=standalone, input_format="markdown-definition_lists-latex_macros", extra_args=["--strip-comments"])
def import_md_list(s: str) -> list[Element]:
return import_md(s, standalone=False)