2020-01-19 22:13:18 -07:00
|
|
|
"""Markdown processing utility functions."""
|
|
|
|
|
2020-02-03 02:35:11 -07:00
|
|
|
# Project
|
2020-04-14 10:24:20 -07:00
|
|
|
from hyperglass.log import log
|
2020-01-17 02:50:57 -07:00
|
|
|
|
|
|
|
|
2020-04-13 01:04:28 -07:00
|
|
|
def _get_file(path_obj):
|
2020-01-17 02:50:57 -07:00
|
|
|
"""Read a file.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
path_obj {Path} -- Path to file.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
{str} -- File contents
|
|
|
|
"""
|
2020-04-13 01:04:28 -07:00
|
|
|
with path_obj.open("r") as raw_file:
|
|
|
|
return raw_file.read()
|
2020-01-17 02:50:57 -07:00
|
|
|
|
|
|
|
|
2020-04-13 01:04:28 -07:00
|
|
|
def format_markdown(content, params):
|
2020-01-17 02:50:57 -07:00
|
|
|
"""Format content with config parameters.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
content {str} -- Unformatted content
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
{str} -- Formatted content
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
fmt = content.format(**params)
|
|
|
|
except KeyError:
|
|
|
|
fmt = content
|
|
|
|
return fmt
|
|
|
|
|
|
|
|
|
2020-04-13 01:04:28 -07:00
|
|
|
def get_markdown(config_path, default, params):
|
2020-01-17 02:50:57 -07:00
|
|
|
"""Get markdown file if specified, or use default.
|
|
|
|
|
|
|
|
Format the content with config parameters.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
config_path {object} -- content config
|
|
|
|
default {str} -- default content
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
{str} -- Formatted content
|
|
|
|
"""
|
2020-03-22 22:09:03 -07:00
|
|
|
log.trace(f"Getting Markdown content for '{params['title']}'")
|
2020-01-17 02:50:57 -07:00
|
|
|
|
|
|
|
if config_path.enable and config_path.file is not None:
|
2020-04-13 01:04:28 -07:00
|
|
|
md = _get_file(config_path.file)
|
2020-01-17 02:50:57 -07:00
|
|
|
else:
|
|
|
|
md = default
|
|
|
|
|
2020-03-22 22:09:03 -07:00
|
|
|
log.trace(f"Unformatted Content for '{params['title']}':\n{md}")
|
2020-01-17 02:50:57 -07:00
|
|
|
|
2020-04-13 01:04:28 -07:00
|
|
|
md_fmt = format_markdown(md, params)
|
2020-01-17 02:50:57 -07:00
|
|
|
|
2020-03-22 22:09:03 -07:00
|
|
|
log.trace(f"Formatted Content for '{params['title']}':\n{md_fmt}")
|
2020-01-17 02:50:57 -07:00
|
|
|
|
|
|
|
return md_fmt
|