60 lines
1.9 KiB
Python
60 lines
1.9 KiB
Python
import glob
|
|
from pathlib import Path
|
|
from blog.page import Page
|
|
from blog.config import Config
|
|
from jinja2 import Environment, FileSystemLoader, Template
|
|
|
|
|
|
class Blog:
|
|
|
|
def __init__(self, conf: Config):
|
|
""" """
|
|
self.conf = conf
|
|
self.pages = dict()
|
|
|
|
def load_pages(self):
|
|
"""Charge tous les fichiers .md dans le dossier inbox"""
|
|
files_list = glob.glob(f"{self.conf.inbox}/*.md")
|
|
|
|
self.pages = dict()
|
|
for file in files_list:
|
|
self.pages[Path(file).stem] = Page(Path(file))
|
|
|
|
def make(self):
|
|
"""Convertit les pages en un site html"""
|
|
if not self.pages:
|
|
self.load_pages()
|
|
|
|
env = Environment(loader=FileSystemLoader(self.conf.theme))
|
|
|
|
page_template = env.get_template("page.html.j2")
|
|
index_template = env.get_template("index.html.j2")
|
|
|
|
self._build_all_pages(page_template)
|
|
self._build_index(index_template)
|
|
|
|
def _build_all_pages(self, template: Template):
|
|
"""Convertit les pages markdown dans conf.inbox en html dans conf.outbox"""
|
|
|
|
outbox_path = Path(f"{self.conf.outbox}/pages")
|
|
if not outbox_path.is_dir():
|
|
outbox_path.mkdir()
|
|
|
|
for filename in self.pages:
|
|
html_content = self.pages[filename].html(template)
|
|
with open(f"{self.conf.outbox}/pages/{filename}.html", "w+") as html_file:
|
|
html_file.write(html_content)
|
|
|
|
def _build_index(self, template: Template):
|
|
"""Crée l'index du blog."""
|
|
html_content = template.render(
|
|
pages=self.pages, title=self.conf.title, presentation=self.conf.presentation
|
|
)
|
|
with open(f"{self.conf.outbox}/index.html", "w+") as html_file:
|
|
html_file.write(html_content)
|
|
|
|
# def _copycss_files(src_dir: str, dest_dir: str) -> list:
|
|
# list_css = glob.glob(f'{src_dir}/css/*.css')
|
|
# for css_file in list_css:
|
|
# shutil.copy(css_file, dest_dir)
|