remove os.chdir usage, rely on absolute and relative paths more
os.chdir was getting confusing and hurting the log output, and potentially the cause of a couple bugs left to fix, so this removes it, but it means we need to pass around the pages/ absolute path into the markdown parser, because it relies on knowing both the absolute path now (to open files), and also the path relative to the pages dir in order to know where to stop reading parent files/how to generate proper URL-like references to other files. probably this should be refactored at some point to inherit the pages/ path from the SSG somehow, rather than passing it through a bunch of methods, but this seems to work for now fixes #22 Signed-off-by: Brian S. Stephan <bss@incorporeal.org>
This commit is contained in:
@@ -44,18 +44,21 @@ def instance_resource_path_to_request_path(path):
|
||||
return '/' + re.sub(r'.md$', '', re.sub(r'index.md$', '', path))
|
||||
|
||||
|
||||
def parse_md(path: str):
|
||||
def parse_md(path: str, pages_root: str):
|
||||
"""Given a file to parse, return file content and other derived data along with the md object.
|
||||
|
||||
Args:
|
||||
path: the path to the file to render
|
||||
pages_root: the absolute path to the pages/ dir, which the path should be within. necessary for
|
||||
proper resolution of resolving parent pages (which needs to know when to stop)
|
||||
"""
|
||||
try:
|
||||
logger.debug("opening path '%s'", path)
|
||||
with open(path, 'r') as input_file:
|
||||
absolute_path = os.path.join(pages_root, path)
|
||||
logger.debug("opening path '%s'", absolute_path)
|
||||
with open(absolute_path, 'r') as input_file:
|
||||
mtime = datetime.datetime.fromtimestamp(os.path.getmtime(input_file.name), tz=datetime.timezone.utc)
|
||||
entry = input_file.read()
|
||||
logger.debug("path '%s' read", path)
|
||||
logger.debug("path '%s' read", absolute_path)
|
||||
md = init_md()
|
||||
content = Markup(md.convert(entry)) # nosec B704
|
||||
except (OSError, FileNotFoundError):
|
||||
@@ -67,17 +70,25 @@ def parse_md(path: str):
|
||||
|
||||
logger.debug("file metadata: %s", md.Meta)
|
||||
|
||||
page_name = get_meta_str(md, 'title') if md.Meta.get('title') else instance_resource_path_to_request_path(path)
|
||||
rel_path = os.path.relpath(path, pages_root)
|
||||
page_name = get_meta_str(md, 'title') if md.Meta.get('title') else instance_resource_path_to_request_path(rel_path)
|
||||
page_title = f'{page_name} - {Config.TITLE_SUFFIX}' if page_name else Config.TITLE_SUFFIX
|
||||
logger.debug("title (potentially derived): %s", page_title)
|
||||
|
||||
return content, md, page_name, page_title, mtime
|
||||
|
||||
|
||||
def handle_markdown_file_path(path: str) -> str:
|
||||
"""Given a location on disk, attempt to open it and render the markdown within."""
|
||||
content, md, page_name, page_title, mtime = parse_md(path)
|
||||
parent_navs = generate_parent_navs(path)
|
||||
def handle_markdown_file_path(path: str, pages_root: str) -> str:
|
||||
"""Given a location on disk, attempt to open it and render the markdown within.
|
||||
|
||||
Args:
|
||||
path: the path to the file to parse and produce metadata for
|
||||
pages_root: the absolute path to the pages/ dir, which the path should be within. necessary for
|
||||
proper resolution of resolving parent pages (which needs to know when to stop)
|
||||
"""
|
||||
content, md, page_name, page_title, mtime = parse_md(path, pages_root)
|
||||
relative_path = os.path.relpath(path, pages_root)
|
||||
parent_navs = generate_parent_navs(relative_path, pages_root)
|
||||
extra_footer = get_meta_str(md, 'footer') if md.Meta.get('footer') else None
|
||||
template_name = get_meta_str(md, 'template') if md.Meta.get('template') else 'base.html'
|
||||
|
||||
@@ -92,14 +103,21 @@ def handle_markdown_file_path(path: str) -> str:
|
||||
description=get_meta_str(md, 'description'),
|
||||
image=Config.BASE_HOST + get_meta_str(md, 'image'),
|
||||
content=content,
|
||||
base_url=Config.BASE_HOST + instance_resource_path_to_request_path(path),
|
||||
base_url=Config.BASE_HOST + instance_resource_path_to_request_path(relative_path),
|
||||
navs=parent_navs,
|
||||
mtime=mtime.strftime('%Y-%m-%d %H:%M:%S %Z'),
|
||||
extra_footer=extra_footer)
|
||||
|
||||
|
||||
def generate_parent_navs(path):
|
||||
"""Create a series of paths/links to navigate up from the given resource path."""
|
||||
def generate_parent_navs(path, pages_root: str):
|
||||
"""Create a series of paths/links to navigate up from the given resource path.
|
||||
|
||||
Args:
|
||||
path: the path to parse and generate parent metadata nav links for
|
||||
pages_root: the absolute path to the pages/ dir, which the path should be within. path is relative,
|
||||
but opening parents requires the full path
|
||||
"""
|
||||
logger.debug("path to generate navs for: %s", path)
|
||||
if path == 'index.md':
|
||||
# bail and return the domain name as a terminal case
|
||||
return [(Config.DOMAIN_NAME, '/')]
|
||||
@@ -124,14 +142,14 @@ def generate_parent_navs(path):
|
||||
|
||||
# read the resource
|
||||
try:
|
||||
with open(path, 'r') as entry_file:
|
||||
with open(os.path.join(pages_root, path), 'r') as entry_file:
|
||||
entry = entry_file.read()
|
||||
_ = Markup(md.convert(entry)) # nosec B704
|
||||
page_name = (" ".join(md.Meta.get('title')) if md.Meta.get('title')
|
||||
else request_path_to_breadcrumb_display(request_path))
|
||||
return generate_parent_navs(parent_resource_path) + [(page_name, request_path)]
|
||||
return generate_parent_navs(parent_resource_path, pages_root) + [(page_name, request_path)]
|
||||
except FileNotFoundError:
|
||||
return generate_parent_navs(parent_resource_path) + [(request_path, request_path)]
|
||||
return generate_parent_navs(parent_resource_path, pages_root) + [(request_path, request_path)]
|
||||
|
||||
|
||||
def request_path_to_breadcrumb_display(path):
|
||||
|
||||
Reference in New Issue
Block a user