Support headers, footers and static content
This commit is contained in:
parent
2524601616
commit
6913498941
1 changed files with 28 additions and 4 deletions
|
@ -1,5 +1,6 @@
|
|||
from pathlib import Path
|
||||
import os
|
||||
import shutil
|
||||
|
||||
from markdown_it import MarkdownIt
|
||||
|
||||
|
@ -7,21 +8,44 @@ class SiteGenerator():
|
|||
|
||||
def __init__(self, source_dir: Path,
|
||||
build_dir: Path):
|
||||
self.source_dir = source_dir
|
||||
self.build_dir = build_dir
|
||||
self.source_dir = source_dir.resolve()
|
||||
self.build_dir = build_dir.resolve()
|
||||
self.parser = MarkdownIt()
|
||||
|
||||
def run(self):
|
||||
|
||||
header_src = ""
|
||||
footer_src = ""
|
||||
|
||||
header_path = self.source_dir / "templates/header.html"
|
||||
footer_path = self.source_dir / "templates/footer.html"
|
||||
if header_path.exists():
|
||||
with open(header_path, 'r', encoding='utf-8') as f:
|
||||
header_src = f.read()
|
||||
|
||||
if footer_path.exists():
|
||||
with open(footer_path, 'r', encoding='utf-8') as f:
|
||||
footer_src = f.read()
|
||||
|
||||
static_dir = self.source_dir / "static"
|
||||
build_static_dir = self.build_dir / "static"
|
||||
if static_dir.exists():
|
||||
|
||||
if build_static_dir.exists():
|
||||
shutil.rmtree(build_static_dir)
|
||||
|
||||
shutil.copytree(static_dir, self.build_dir / "static")
|
||||
|
||||
for md_file in self.source_dir.glob('**/*.md'):
|
||||
with open(md_file, 'r', encoding='utf-8') as f:
|
||||
md_src = f.read()
|
||||
|
||||
html_path = self.build_dir / (str(md_file.stem) + ".html")
|
||||
md_dir = md_file.parent.relative_to(self.source_dir)
|
||||
html_path = self.build_dir / md_dir / (str(md_file.stem) + ".html")
|
||||
os.makedirs(html_path.parent, exist_ok=True)
|
||||
|
||||
html_src = self.parser.render(md_src)
|
||||
with open(html_path, 'w', encoding='utf-8') as f:
|
||||
f.write(html_src)
|
||||
f.write(header_src + html_src + footer_src)
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue