static site generator part 3
Signed-off-by: Brian S. Stephan <bss@incorporeal.org>
This commit is contained in:
parent
1ace0e1427
commit
02c548880e
@ -37,7 +37,6 @@ def build():
|
|||||||
|
|
||||||
output_dir = os.path.abspath(args.output_dir)
|
output_dir = os.path.abspath(args.output_dir)
|
||||||
instance_dir = os.path.abspath(args.instance_dir)
|
instance_dir = os.path.abspath(args.instance_dir)
|
||||||
pages_dir = os.path.join(instance_dir, 'pages')
|
|
||||||
|
|
||||||
# initialize configuration with the path to the instance
|
# initialize configuration with the path to the instance
|
||||||
init_instance(instance_dir)
|
init_instance(instance_dir)
|
||||||
@ -47,50 +46,22 @@ def build():
|
|||||||
cprint(f"creating temporary directory '{tmp_output_dir}' for writing", 'green')
|
cprint(f"creating temporary directory '{tmp_output_dir}' for writing", 'green')
|
||||||
|
|
||||||
# CORE CONTENT
|
# CORE CONTENT
|
||||||
# render and/or copy into the output dir after changing into the instance dir (to simplify paths)
|
pages_dir = os.path.join(instance_dir, 'pages')
|
||||||
os.chdir(pages_dir)
|
copy_to_destination(pages_dir, tmp_output_dir)
|
||||||
for base_dir, subdirs, files in os.walk(pages_dir):
|
|
||||||
# remove the absolute path of the pages directory from the base_dir
|
|
||||||
base_dir = os.path.relpath(base_dir, pages_dir)
|
|
||||||
# create subdirs seen here for subsequent depth
|
|
||||||
for subdir in subdirs:
|
|
||||||
dst = os.path.join(tmp_output_dir, base_dir, subdir)
|
|
||||||
if os.path.islink(os.path.join(base_dir, subdir)):
|
|
||||||
# keep the link relative to the output directory
|
|
||||||
src = symlink_to_relative_dest(pages_dir, os.path.join(base_dir, subdir))
|
|
||||||
print(f"creating directory symlink '{dst}' -> '{src}'")
|
|
||||||
os.symlink(src, dst, target_is_directory=True)
|
|
||||||
else:
|
|
||||||
print(f"creating directory '{dst}'")
|
|
||||||
os.mkdir(dst)
|
|
||||||
|
|
||||||
# process and copy files
|
# STATIC DIR
|
||||||
for file_ in files:
|
program_static_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static')
|
||||||
dst = os.path.join(tmp_output_dir, base_dir, file_)
|
static_output_dir = os.path.join(tmp_output_dir, 'static')
|
||||||
if os.path.islink(os.path.join(base_dir, file_)):
|
try:
|
||||||
# keep the link relative to the output directory
|
os.mkdir(static_output_dir)
|
||||||
src = symlink_to_relative_dest(pages_dir, os.path.join(base_dir, file_))
|
except FileExistsError:
|
||||||
print(f"creating symlink '{dst}' -> '{src}'")
|
# already exists
|
||||||
os.symlink(src, dst, target_is_directory=False)
|
pass
|
||||||
else:
|
copy_to_destination(program_static_dir, static_output_dir, convert_markdown=False)
|
||||||
src = os.path.join(base_dir, file_)
|
|
||||||
print(f"copying file '{src}' -> '{dst}'")
|
|
||||||
shutil.copy2(src, dst)
|
|
||||||
|
|
||||||
# render markdown as HTML
|
# INSTANCE STATIC DIR
|
||||||
if src.endswith('.md'):
|
custom_static_dir = os.path.join(instance_dir, 'custom-static')
|
||||||
rendered_file = dst.removesuffix('.md') + '.html'
|
copy_to_destination(custom_static_dir, static_output_dir, convert_markdown=False)
|
||||||
try:
|
|
||||||
content = handle_markdown_file_path(src)
|
|
||||||
except UnicodeDecodeError:
|
|
||||||
# perhaps this isn't a markdown file at all for some reason; we
|
|
||||||
# copied it above so stick with tha
|
|
||||||
cprint(f"{src} has invalid bytes! skipping", 'yellow')
|
|
||||||
continue
|
|
||||||
with open(rendered_file, 'w') as dst_file:
|
|
||||||
dst_file.write(content)
|
|
||||||
|
|
||||||
# TODO: STATIC DIR
|
|
||||||
|
|
||||||
# move temporary dir to the destination
|
# move temporary dir to the destination
|
||||||
old_output_dir = f'{output_dir}-old-{os.path.basename(tmp_output_dir)}'
|
old_output_dir = f'{output_dir}-old-{os.path.basename(tmp_output_dir)}'
|
||||||
@ -105,6 +76,62 @@ def build():
|
|||||||
# TODO: unlink old dir above? arg flag?
|
# TODO: unlink old dir above? arg flag?
|
||||||
|
|
||||||
|
|
||||||
|
def copy_to_destination(source_dir: str, dest_dir: str, convert_markdown: bool = True) -> None:
|
||||||
|
"""Walk the source directory and copy and/or convert its contents into the destination.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
source_dir: the directory to copy into the destination
|
||||||
|
dest_dir: the directory to place copied/converted files into
|
||||||
|
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):
|
||||||
|
# remove the absolute path of the pages directory from the base_dir
|
||||||
|
base_dir = os.path.relpath(base_dir, source_dir)
|
||||||
|
# create subdirs seen here for subsequent depth
|
||||||
|
for subdir in subdirs:
|
||||||
|
dst = os.path.join(dest_dir, base_dir, subdir)
|
||||||
|
if os.path.islink(os.path.join(base_dir, subdir)):
|
||||||
|
# keep the link relative to the output directory
|
||||||
|
src = symlink_to_relative_dest(source_dir, os.path.join(base_dir, subdir))
|
||||||
|
print(f"creating directory symlink '{dst}' -> '{src}'")
|
||||||
|
os.symlink(src, dst, target_is_directory=True)
|
||||||
|
else:
|
||||||
|
print(f"creating directory '{dst}'")
|
||||||
|
try:
|
||||||
|
os.mkdir(dst)
|
||||||
|
except FileExistsError:
|
||||||
|
# already exists
|
||||||
|
pass
|
||||||
|
|
||||||
|
# process and copy files
|
||||||
|
for file_ in files:
|
||||||
|
dst = os.path.join(dest_dir, base_dir, file_)
|
||||||
|
if os.path.islink(os.path.join(base_dir, file_)):
|
||||||
|
# keep the link relative to the output directory
|
||||||
|
src = symlink_to_relative_dest(source_dir, os.path.join(base_dir, file_))
|
||||||
|
print(f"creating symlink '{dst}' -> '{src}'")
|
||||||
|
os.symlink(src, dst, target_is_directory=False)
|
||||||
|
else:
|
||||||
|
src = os.path.join(base_dir, file_)
|
||||||
|
print(f"copying file '{src}' -> '{dst}'")
|
||||||
|
shutil.copy2(src, dst)
|
||||||
|
|
||||||
|
# render markdown as HTML
|
||||||
|
if src.endswith('.md') and convert_markdown:
|
||||||
|
rendered_file = dst.removesuffix('.md') + '.html'
|
||||||
|
try:
|
||||||
|
content = handle_markdown_file_path(src)
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
# perhaps this isn't a markdown file at all for some reason; we
|
||||||
|
# copied it above so stick with tha
|
||||||
|
cprint(f"{src} has invalid bytes! skipping", 'yellow')
|
||||||
|
continue
|
||||||
|
with open(rendered_file, 'w') as dst_file:
|
||||||
|
dst_file.write(content)
|
||||||
|
|
||||||
|
|
||||||
def symlink_to_relative_dest(base_dir: str, source: str) -> str:
|
def symlink_to_relative_dest(base_dir: str, source: str) -> str:
|
||||||
"""Given a symlink, make sure it points to something inside the instance and provide its real destination.
|
"""Given a symlink, make sure it points to something inside the instance and provide its real destination.
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user