1
0
mirror of https://github.com/checktheroads/hyperglass synced 2024-05-11 05:55:08 +00:00

66 lines
1.4 KiB
Python
Raw Normal View History

2020-01-19 22:13:18 -07:00
"""Markdown processing utility functions."""
2020-01-17 02:50:57 -07:00
# Third Party Imports
from aiofile import AIOFile
# Project Imports
from hyperglass.util import log
async def _get_file(path_obj):
"""Read a file.
Arguments:
path_obj {Path} -- Path to file.
Returns:
{str} -- File contents
"""
async with AIOFile(path_obj, "r") as raw_file:
file = await raw_file.read()
return file
async def format_markdown(content, params):
"""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
async def get_markdown(config_path, default, params):
"""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
"""
log.debug(f"Getting Markdown content for '{params['title']}'")
if config_path.enable and config_path.file is not None:
md = await _get_file(config_path.file)
else:
md = default
log.debug(f"Unformatted Content for '{params['title']}':\n{md}")
md_fmt = await format_markdown(md, params)
log.debug(f"Formatted Content for '{params['title']}':\n{md_fmt}")
return md_fmt