85 lines
2.9 KiB
Python
85 lines
2.9 KiB
Python
import glob
|
|
import shutil
|
|
from pathlib import Path
|
|
from blog.page import Page
|
|
from blog.config import Config
|
|
from jinja2 import Environment, FileSystemLoader, Template
|
|
from rss import RssFeed
|
|
|
|
class Blog:
|
|
|
|
def __init__(self, conf: Config):
|
|
""" """
|
|
self.conf = conf
|
|
self.pages = dict()
|
|
|
|
def make(self, draft: bool = False):
|
|
"""Convertit les pages en un site html"""
|
|
|
|
self.pages = dict()
|
|
self._load_pages(self.conf.inbox)
|
|
|
|
if draft:
|
|
self._load_pages(self.conf.draft)
|
|
|
|
env = Environment(loader=FileSystemLoader(self.conf.theme))
|
|
|
|
page_template = env.get_template("page.html.j2")
|
|
index_template = env.get_template("index.html.j2")
|
|
|
|
self._copy_css()
|
|
self._build_all_pages(page_template)
|
|
self._build_index(index_template)
|
|
self._build_rss()
|
|
|
|
def _load_pages(self, path: Path):
|
|
"""Charge tous les fichiers .md dans le dossier inbox"""
|
|
files_list = glob.glob(f"{path}/*.md")
|
|
for file in files_list:
|
|
self.pages[Path(file).stem] = Page(Path(file))
|
|
|
|
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 _build_rss(self):
|
|
""" """
|
|
feed = RssFeed(language="fr-fr", title=self.conf.title, link=self.conf.url, description=self.conf.presentation )
|
|
for page in self.pages:
|
|
feed.add_item(
|
|
title = self.pages[page].meta["title"],
|
|
link = Path(self.conf.url) / 'pages' / (page + '.html'),
|
|
description = "Ma description a moi !",
|
|
pubdate = self.pages[page].meta["date"]
|
|
)
|
|
with open(Path(self.conf.outbox) / "flux.rss", 'w+') as rss_file:
|
|
feed.write(rss_file, 'utf-8')
|
|
|
|
def _copy_css(self):
|
|
"""Copie les fichiers CSS du theme vers l'export"""
|
|
css_path = Path(self.conf.theme) / "css"
|
|
dest_path = Path(self.conf.outbox) / "css"
|
|
|
|
if not dest_path.exists():
|
|
dest_path.mkdir()
|
|
|
|
for css_file in css_path.glob("*.css"):
|
|
dest_file = dest_path / css_file.name
|
|
shutil.copy(css_file, dest_file)
|