Ajoute le flux atom

This commit is contained in:
2025-07-17 18:12:51 +02:00
parent c1b730d396
commit 4c49d4e96a
4 changed files with 71 additions and 25 deletions

View File

@@ -1,10 +1,11 @@
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
from rss import RssFeed
class Blog:
@@ -30,7 +31,7 @@ class Blog:
self._copy_css()
self._build_all_pages(page_template)
self._build_index(index_template)
self._build_rss()
self._build_atom()
def _load_pages(self, path: Path):
"""Charge tous les fichiers .md dans le dossier inbox"""
@@ -46,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)
@@ -58,18 +59,49 @@ class Blog:
with open(f"{self.conf.outbox}/index.html", "w+") as html_file:
html_file.write(html_content)
def _build_rss(self):
def _build_atom(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')
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"""

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(),
)