-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace JinjaMarkdownStep with JinjaExtendedMarkdownStep (#72)
- Loading branch information
Showing
3 changed files
with
79 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import sys | ||
import typing as t | ||
|
||
|
||
def simple_frontmatter_parser(content: str) -> dict: | ||
""" | ||
Read metadata from the front of a markdown-formatted text in a very simple | ||
YAML-like format, without value parsing. | ||
""" | ||
meta = {} | ||
lines = content.splitlines() | ||
|
||
for line in lines: | ||
if ':' not in line: | ||
break | ||
key, value = line.split(':', 1) | ||
if not key.isidentifier(): | ||
break | ||
meta[key.strip()] = value.strip() | ||
|
||
print(meta, '...') | ||
return meta | ||
|
||
|
||
def get_toml_frontmatter_parser(): | ||
if sys.version_info < (3, 11): | ||
import tomli as tomllib | ||
else: | ||
import tomllib | ||
return tomllib.loads | ||
|
||
|
||
def get_yaml_frontmatter_parser(): | ||
from ruamel.yaml import YAML | ||
return YAML(typ='safe').load | ||
|
||
|
||
FrontMatterParser = t.Callable[[str], dict] | ||
FrontMatterParserName = t.Literal['simple', 'toml', 'yaml'] | ||
|
||
FRONTMATTER_PARSER_FACTORIES: dict[FrontMatterParserName, t.Callable[[], FrontMatterParser]] = { | ||
'simple': lambda: simple_frontmatter_parser, | ||
'toml': get_toml_frontmatter_parser, | ||
'yaml': get_yaml_frontmatter_parser, | ||
} | ||
|
||
|
||
def get_frontmatter_parser(parser) -> FrontMatterParser: | ||
if callable(parser): | ||
return parser | ||
return FRONTMATTER_PARSER_FACTORIES[parser]() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters