141 lines
3.9 KiB
Python
141 lines
3.9 KiB
Python
import logging
|
|
import shutil
|
|
from pathlib import Path
|
|
import os
|
|
import markdown
|
|
from typing import Optional
|
|
|
|
from .config import SOURCE_DIRECTORY, DIST_DIRECTORY, STATIC_DIRECTORY
|
|
|
|
|
|
logger = logging.getLogger("stsg.build")
|
|
|
|
|
|
def copy_static(src, dst):
|
|
if not os.path.exists(src):
|
|
logger.warn("The static folder '%s' wasn't defined.", src)
|
|
return
|
|
|
|
logger.info("copying static files from '%s' to '%r'", src, dst)
|
|
|
|
os.makedirs(dst, exist_ok=True)
|
|
|
|
for root, dirs, files in os.walk(src):
|
|
if any(p.startswith(".") for p in Path(root).parts):
|
|
continue
|
|
|
|
# Compute relative path from the source root
|
|
rel_path = os.path.relpath(root, src)
|
|
dest_dir = os.path.join(dst, rel_path)
|
|
|
|
os.makedirs(dest_dir, exist_ok=True)
|
|
|
|
for file in files:
|
|
if file.startswith("."):
|
|
continue
|
|
|
|
src_file = os.path.join(root, file)
|
|
dest_file = os.path.join(dest_dir, file)
|
|
shutil.copy2(src_file, dest_file)
|
|
|
|
|
|
class Context:
|
|
def __init__(self, root: str = SOURCE_DIRECTORY):
|
|
self.file = None
|
|
|
|
current_root = Path(root)
|
|
while current_root.parts and self.file is None:
|
|
current_file = Path(current_root, "index.html")
|
|
if current_file.exists() and current_file.is_file:
|
|
self.file = current_file
|
|
|
|
current_root = current_root.parent
|
|
|
|
if self.file is None:
|
|
logger.error("couldn't find context for %s", root)
|
|
exit(1)
|
|
logger.info("%s found context %r", root, str(self.file))
|
|
|
|
def get_text(self, **placeholder_values: dict):
|
|
text = self.file.read_text()
|
|
|
|
for key, value in placeholder_values.items():
|
|
text = text.replace(f"<{key}/>", value)
|
|
text = text.replace(f"<{key} />", value)
|
|
|
|
return text
|
|
|
|
|
|
def convert_md(src: Path, dst: Path, context: Optional[Context] = None):
|
|
logger.info("converting %s", src)
|
|
|
|
html_content = markdown.markdown(src.read_text())
|
|
context = context or Context(str(src.parent))
|
|
full_page = context.get_text(content=html_content)
|
|
|
|
folder_dst = dst.parent / dst.name.replace(".md", "")
|
|
folder_dst.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
with Path(folder_dst, "index.html").open("w") as f:
|
|
f.write(full_page)
|
|
|
|
|
|
class CustomLanguageCode:
|
|
def __init__(self, file: Path):
|
|
self.file: Path = file
|
|
|
|
@property
|
|
def language_code(self) -> str:
|
|
return self.file.name.replace(".md", "")
|
|
|
|
@property
|
|
def relative_url(self) -> str:
|
|
return "/" + str(Path(self.file.parent, self.language_code))
|
|
|
|
def __repr__(self) -> str:
|
|
return f"{self.language_code}"
|
|
|
|
@property
|
|
def html_code(self) -> str:
|
|
return f'<ul><a href="{self.relative_url}"><bold>{self.language_code}</bold></a></ul>'
|
|
|
|
|
|
def walk_directory(root):
|
|
src_path = Path(SOURCE_DIRECTORY, root)
|
|
dst_path = Path(DIST_DIRECTORY, root)
|
|
|
|
context = Context(src_path)
|
|
language_codes_found = []
|
|
|
|
for current_full_path in src_path.iterdir():
|
|
current_name = Path(current_full_path).name
|
|
current_dst = Path(dst_path, current_name)
|
|
current_src = Path(src_path, current_name)
|
|
|
|
if current_name == "static":
|
|
copy_static(current_src, current_dst)
|
|
continue
|
|
|
|
if current_name.endswith(".md"):
|
|
convert_md(current_src, current_dst, context=context)
|
|
|
|
language_codes_found.append(CustomLanguageCode(Path(root, current_name)))
|
|
continue
|
|
|
|
if current_src.is_dir():
|
|
walk_directory(Path(root, current_full_path.name))
|
|
|
|
content = f"""
|
|
<li>
|
|
{''.join(l.html_code for l in language_codes_found)}
|
|
</li>
|
|
"""
|
|
with Path(dst_path, "index.html").open("w") as f:
|
|
f.write(context.get_text(content=content))
|
|
|
|
|
|
def build():
|
|
logger.info("building static page")
|
|
walk_directory("")
|