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:
2025-03-21 10:35:56 -05:00
parent e75d5c48d2
commit 8c75947088
7 changed files with 91 additions and 67 deletions

View File

@@ -86,7 +86,6 @@ class StaticSiteGenerator(object):
convert_markdown: whether or not to convert Markdown files (or simply copy them)
"""
cprint(f"copying files from '{source_dir}' to '{dest_dir}'", 'green')
os.chdir(source_dir)
for base_dir, subdirs, files in os.walk(source_dir):
logger.debug("starting to build against %s || %s || %s", base_dir, subdirs, files)
# remove the absolute path of the directory from the base_dir
@@ -113,9 +112,12 @@ class StaticSiteGenerator(object):
dest_dir: the output directory to place the subdir in
"""
dst = os.path.join(dest_dir, base_dir, subdir)
if os.path.islink(os.path.join(base_dir, subdir)):
absolute_dir = os.path.join(source_dir, base_dir, subdir)
logger.debug("checking if %s is a symlink or not", absolute_dir)
if os.path.islink(absolute_dir):
logger.debug("symlink; raw destination is %s", os.path.realpath(absolute_dir))
# keep the link relative to the output directory
src = self.symlink_to_relative_dest(source_dir, os.path.join(base_dir, subdir))
src = self.symlink_to_relative_dest(source_dir, absolute_dir)
print(f"creating directory symlink '{dst}' -> '{src}'")
os.symlink(src, dst, target_is_directory=True)
else:
@@ -137,9 +139,12 @@ class StaticSiteGenerator(object):
dest_dir: the output directory to place the subdir in
"""
dst = os.path.join(dest_dir, base_dir, file_)
if os.path.islink(os.path.join(base_dir, file_)):
absolute_file = os.path.join(source_dir, base_dir, file_)
logger.debug("checking if %s is a symlink or not", absolute_file)
if os.path.islink(absolute_file):
logger.debug("symlink; raw destination is %s", os.path.realpath(absolute_file))
# keep the link relative to the output directory
src = self.symlink_to_relative_dest(source_dir, os.path.join(base_dir, file_))
src = self.symlink_to_relative_dest(source_dir, absolute_file)
print(f"creating symlink '{dst}' -> '{src}'")
os.symlink(src, dst, target_is_directory=False)
if src.endswith('.md') and convert_markdown:
@@ -151,7 +156,7 @@ class StaticSiteGenerator(object):
os.symlink(second_src, second_dst, target_is_directory=False)
else:
src = os.path.join(base_dir, file_)
src = os.path.join(source_dir, base_dir, file_)
print(f"copying file '{src}' -> '{dst}'")
shutil.copy2(src, dst)
@@ -160,7 +165,7 @@ class StaticSiteGenerator(object):
rendered_file = dst.removesuffix('.md') + '.html'
print(f"rendering file '{src}' -> '{rendered_file}'")
try:
content = handle_markdown_file_path(src)
content = handle_markdown_file_path(src, source_dir)
except UnicodeDecodeError:
# perhaps this isn't a markdown file at all for some reason; we
# copied it above so stick with tha