Compare commits

...

4 Commits

4 changed files with 55 additions and 24 deletions

View File

@ -1,7 +1,8 @@
[project] [project]
name = "stsg" name = "stsg"
dependencies = [ dependencies = [
"watchdog~=6.0.0" "watchdog~=6.0.0",
"markdown~=3.3.6"
] ]
dynamic = [] dynamic = []
authors = [] authors = []

View File

@ -1 +1,6 @@
# foo
Lore ipsum dolor sit amend
- bar
- foobar

View File

@ -4,7 +4,7 @@
<meta charset="utf-8" /> <meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" /> <meta name="viewport" content="width=device-width, initial-scale=1" />
<title>STSG</title> <title>STSG</title>
<link rel="stylesheet" href="static/bulma.min.css" /> <link rel="stylesheet" href="/static/bulma.min.css" />
</head> </head>
<body> <body>
<!-- Header (Navbar) --> <!-- Header (Navbar) -->
@ -22,7 +22,7 @@
<!-- Main Content --> <!-- Main Content -->
<section class="section"> <section class="section">
<div class="container"> <div class="content">
<content /> <content />
</div> </div>
</section> </section>

View File

@ -2,6 +2,8 @@ import logging
import shutil import shutil
from pathlib import Path from pathlib import Path
import os import os
import markdown
from typing import Optional
from .config import SOURCE_DIRECTORY, DIST_DIRECTORY, STATIC_DIRECTORY from .config import SOURCE_DIRECTORY, DIST_DIRECTORY, STATIC_DIRECTORY
@ -9,10 +11,7 @@ from .config import SOURCE_DIRECTORY, DIST_DIRECTORY, STATIC_DIRECTORY
logger = logging.getLogger("stsg.build") logger = logging.getLogger("stsg.build")
def copy_static(): def copy_static(src, dst):
src = str(Path(SOURCE_DIRECTORY, STATIC_DIRECTORY))
dst = str(Path(DIST_DIRECTORY, STATIC_DIRECTORY))
if not os.path.exists(src): if not os.path.exists(src):
logger.warn("The static folder '%s' wasn't defined.", src) logger.warn("The static folder '%s' wasn't defined.", src)
return return
@ -42,7 +41,7 @@ def copy_static():
class Context: class Context:
def __init__(self, root: str = SOURCE_DIRECTORY): def __init__(self, root: str = SOURCE_DIRECTORY):
self.file = Path(root, "index.html") self.file = None
current_root = Path(root) current_root = Path(root)
while current_root.parts and self.file is None: while current_root.parts and self.file is None:
@ -57,7 +56,7 @@ class Context:
exit(1) exit(1)
logger.info("%s found context %r", root, str(self.file)) logger.info("%s found context %r", root, str(self.file))
def get_text(self, placeholder_values: dict): def get_text(self, **placeholder_values: dict):
text = self.file.read_text() text = self.file.read_text()
for key, value in placeholder_values.items(): for key, value in placeholder_values.items():
@ -67,18 +66,44 @@ class Context:
return text return text
def convert_md(src: Path, dst: Path, context: Optional[Context] = None):
html_content = markdown.markdown(src.read_text())
context = 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)
def walk_directory(root):
src_path = Path(SOURCE_DIRECTORY, root)
dst_path = Path(DIST_DIRECTORY, root)
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)
language_codes_found.append(current_name.replace(".md", ""))
continue
if current_src.is_dir():
walk_directory(Path(root, current_full_path.name))
print(language_codes_found)
def build(): def build():
logger.info("building static page") logger.info("building static page")
copy_static() walk_directory("")
context = Context()
# debug
t = context.get_text({
"content": """
<h1 class="title">Hello World</h1>
<p class="subtitle">My first website with <strong>Bulma</strong>!</p>
"""
})
with Path(DIST_DIRECTORY, "index.html").open("w") as f:
f.write(t)