2020-01-19 22:13:18 -07:00
|
|
|
"""Markdown processing utility functions."""
|
|
|
|
|
2021-09-25 21:36:08 -07:00
|
|
|
# Standard Library
|
|
|
|
import typing as t
|
|
|
|
from pathlib import Path
|
2020-01-17 02:50:57 -07:00
|
|
|
|
2021-09-25 21:36:08 -07:00
|
|
|
if t.TYPE_CHECKING:
|
|
|
|
# Project
|
|
|
|
from hyperglass.models import HyperglassModel
|
2020-01-17 02:50:57 -07:00
|
|
|
|
|
|
|
|
2021-09-25 21:36:08 -07:00
|
|
|
def get_markdown(config: "HyperglassModel", default: str, params: t.Dict[str, t.Any]) -> str:
|
|
|
|
"""Get markdown file if specified, or use default."""
|
2020-01-17 02:50:57 -07:00
|
|
|
|
2021-09-25 21:36:08 -07:00
|
|
|
if config.enable and config.file is not None:
|
|
|
|
# with config_path.file
|
|
|
|
if hasattr(config, "file") and isinstance(config.file, Path):
|
|
|
|
with config.file.open("r") as config_file:
|
|
|
|
md = config_file.read()
|
2020-01-17 02:50:57 -07:00
|
|
|
else:
|
|
|
|
md = default
|
|
|
|
|
2021-09-25 21:36:08 -07:00
|
|
|
try:
|
|
|
|
return md.format(**params)
|
|
|
|
except KeyError:
|
|
|
|
return md
|