Compare commits

...

3 Commits

Author SHA1 Message Date
4c49d4e96a Ajoute le flux atom 2025-07-17 18:12:51 +02:00
c1b730d396 Merge branch 'main' into rss 2025-07-13 19:49:03 +02:00
Florestan Bredow
1e30909e52 WIP ajoute generation flux rss 2025-06-06 08:41:37 +02:00
4 changed files with 73 additions and 13 deletions

View File

@@ -1,7 +1,11 @@
theme: './theme/default'
inbox: './inbox'
outbox: './outbox'
author_name: 'Firstname Name'
author_mail: 'name@example.com'
draft: './draft'
url: 'https://blog.example.com'
# python -m uuid -u uuid4
id: 'a06cd9a6-3a48-479b-bf7f-40ddcdde7982'
inbox: './inbox'
theme: './theme/default'
title: 'Blog title'
url: 'https://blog.example.com'
outbox: './outbox'
presentation: 'Blog presentation.'

View File

@@ -1,11 +1,12 @@
import glob
import shutil
import datetime
import textwrap
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):
@@ -30,6 +31,7 @@ class Blog:
self._copy_css()
self._build_all_pages(page_template)
self._build_index(index_template)
self._build_atom()
def _load_pages(self, path: Path):
"""Charge tous les fichiers .md dans le dossier inbox"""
@@ -45,7 +47,7 @@ class Blog:
outbox_path.mkdir()
for filename in self.pages:
html_content = self.pages[filename].html(template)
html_content = self.pages[filename].html_template(template)
with open(f"{self.conf.outbox}/pages/{filename}.html", "w+") as html_file:
html_file.write(html_content)
@@ -57,6 +59,50 @@ class Blog:
with open(f"{self.conf.outbox}/index.html", "w+") as html_file:
html_file.write(html_content)
def _build_atom(self):
""" """
updated = datetime.date(1970, 1, 1)
articles = ""
for filename in self.pages:
date = self.pages[filename].date
if updated < date:
updated = date
articles += textwrap.indent(
textwrap.dedent(f"""\
<entry>
<title>{self.pages[filename].title}</title>
<link href="{self.conf.url}/pages/{filename}.html"/>
<updated>{date}T00:00:00Z</updated>
<id>urn:uuid:{self.pages[filename].id}</id>
<content type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
{self.pages[filename].html()}
</div>
</content>
</entry>
"""),
" ")
header = textwrap.dedent(f""" <?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>{self.conf.title}</title>
<subtitle>{self.conf.presentation}</subtitle>
<link href="{self.conf.url}/atom.xml" rel="self"/>
<link href="{self.conf.url}/index.html"/>
<updated>{updated}T00:00:00Z</updated>
<author>
<name>{self.conf.author_name}</name>
<email>{self.conf.author_mail}</email>
</author>
<id>urn:uuid:{self.conf.id}</id>
""")
footer = "</feed>"
with open(Path(self.conf.outbox) / "atom.xml", 'w+') as rss_file:
rss_file.write(header + articles + footer)
def _copy_css(self):
"""Copie les fichiers CSS du theme vers l'export"""
css_path = Path(self.conf.theme) / "css"

View File

@@ -13,6 +13,9 @@ class Config:
"presentation",
"url",
"draft",
"author_name",
"author_mail",
"id"
}
def __init__(self, config_file: Path):

View File

@@ -1,6 +1,7 @@
import re
import os
import yaml
import uuid
import jinja2
import textwrap
import markdown
@@ -14,12 +15,13 @@ def new_page(title: str, path: Path) -> Path:
today = date.today().strftime("%Y-%m-%d")
filename = path + '/' + re.sub("[^a-zA-Z0-9-]", "_", title) + ".md"
id = uuid.uuid4()
content = textwrap.dedent(
f"""\
---
date: {today}
title: {title}
id: {id}
category:
tags:
-
@@ -55,7 +57,7 @@ class Page:
if "date" not in self.meta or type(self.meta["date"]) is not date:
self.meta["date"] = date(1970, 1, 1)
def __getattr__(self, name):
def __getattr__(self, name: str):
if name not in self.meta:
raise AttributeError(f"L'attribut '{name}' n'existe pas.")
@@ -80,14 +82,19 @@ class Page:
meta += line + "\n"
return meta, markdown
def html(self, template: jinja2.Template) -> str:
def html(self) -> str:
"""Convertit le markdown en html"""
return markdown.markdown(
self.md_content, extensions=["codehilite", "fenced_code"]
)
def html_template(self, template: jinja2.Template) -> str:
"""Convertit la page en html a partir d'un template jinja2"""
title = self.meta["title"] or "Sans titre"
date = self.meta["date"] or None
return template.render(
title=title,
date=date,
content=markdown.markdown(
self.md_content, extensions=["codehilite", "fenced_code"]
),
content=self.html(),
)