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,7 +1,11 @@
theme: './theme/default' author_name: 'Firstname Name'
inbox: './inbox' author_mail: 'name@example.com'
outbox: './outbox'
draft: './draft' 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' title: 'Blog title'
url: 'https://blog.example.com'
outbox: './outbox'
presentation: 'Blog presentation.' presentation: 'Blog presentation.'

View File

@@ -1,10 +1,11 @@
import glob import glob
import shutil import shutil
import datetime
import textwrap
from pathlib import Path from pathlib import Path
from blog.page import Page from blog.page import Page
from blog.config import Config from blog.config import Config
from jinja2 import Environment, FileSystemLoader, Template from jinja2 import Environment, FileSystemLoader, Template
from rss import RssFeed
class Blog: class Blog:
@@ -30,7 +31,7 @@ class Blog:
self._copy_css() self._copy_css()
self._build_all_pages(page_template) self._build_all_pages(page_template)
self._build_index(index_template) self._build_index(index_template)
self._build_rss() self._build_atom()
def _load_pages(self, path: Path): def _load_pages(self, path: Path):
"""Charge tous les fichiers .md dans le dossier inbox""" """Charge tous les fichiers .md dans le dossier inbox"""
@@ -46,7 +47,7 @@ class Blog:
outbox_path.mkdir() outbox_path.mkdir()
for filename in self.pages: 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: with open(f"{self.conf.outbox}/pages/{filename}.html", "w+") as html_file:
html_file.write(html_content) html_file.write(html_content)
@@ -58,18 +59,49 @@ class Blog:
with open(f"{self.conf.outbox}/index.html", "w+") as html_file: with open(f"{self.conf.outbox}/index.html", "w+") as html_file:
html_file.write(html_content) 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 ) updated = datetime.date(1970, 1, 1)
for page in self.pages:
feed.add_item( articles = ""
title = self.pages[page].meta["title"], for filename in self.pages:
link = Path(self.conf.url) / 'pages' / (page + '.html'), date = self.pages[filename].date
description = "Ma description a moi !", if updated < date:
pubdate = self.pages[page].meta["date"] updated = date
) articles += textwrap.indent(
with open(Path(self.conf.outbox) / "flux.rss", 'w+') as rss_file: textwrap.dedent(f"""\
feed.write(rss_file, 'utf-8') <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): def _copy_css(self):
"""Copie les fichiers CSS du theme vers l'export""" """Copie les fichiers CSS du theme vers l'export"""

View File

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

View File

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