1
0
Fork 0
mirror of https://codeberg.org/Reuh/feather.git synced 2025-10-27 10:09:32 +00:00

feat: rename published/updated_formatted to published/updated in templates

This commit is contained in:
Étienne Fildadut 2025-10-11 18:40:52 +02:00
parent 70b930a820
commit 174486ced8
4 changed files with 21 additions and 32 deletions

View file

@ -23,7 +23,7 @@ See read articles in the trash can.
Or ask feather to keep read articles:
`server.only_sync_unread_articles = false`
`html.filename_template = "{% if unread %}☐{% else %}☑{% endif %} [{{ feed_title }}]\t{{ title }} ({{ published_formatted }}).html"`
`html.filename_template = "{% if unread %}☐{% else %}☑{% endif %} [{{ feed_title }}]\t{{ title }} ({{ published }}).html"`
### Updating with the server

View file

@ -128,7 +128,7 @@ class GReaderArticle(Article):
self.feed_url = item_content.origin.html_url
self.article_url = item_content.canonical[0].href
self.compute_fields()
self.compute_json_path()
class TTRSession(ClientSession):
@ -227,4 +227,4 @@ class TTRArticle(Article):
self.language = article.lang
self.image_url = article.flavor_image
self.compute_fields()
self.compute_json_path()

View file

@ -41,10 +41,8 @@ reader = "reader"
# - id: article id (int | str)
# - unread: if the article is unread (bool) - only makes sense if server.only_sync_unread_articles is false
# - title: article title (str)
# - published: article publication time (timestamp) (int)
# - published_formatted: article publication time (text) (str)
# - updated: article update time (timestamp) (int)
# - updated_formatted: article publication time (text) (str)
# - published: article publication date and time (str)
# - updated: article publication date and time (str)
# - author: article author (str)
# - summary: article summary (HTML) (str)
# - content: article content (HTML) (str)
@ -70,7 +68,7 @@ article_template = '''
<style>a{color:palevioletred; text-decoration:none;}</style>
<article style="max-width:60rem; margin:auto; text-align:justify;">
<p style="display:flex; flex-direction:row; justify-content:space-between;">
<span>{{ published_formatted }}</span>
<span>{{ published }}</span>
<span>
{% if feed_url %}
<a href="{{ feed_url }}">
@ -99,7 +97,7 @@ article_template = '''
# Filename template for generated HTML files.
# The available fields are the same as for template.
# Can be set through the environment variable HTML_FILENAME_TEMPLATE.
filename_template = "[{{ feed_title }}]\t{{ title }} ({{ published_formatted }}).html"
filename_template = "[{{ feed_title }}]\t{{ title }} ({{ published }}).html"
# Category directory name template for generated HTML files.
# Fields availables:
# - id: category id (str | int)

View file

@ -81,9 +81,6 @@ class Article(ABC):
# no default value
id: ArticleId # article unique id
category: Category # feed category
# no default value, computed by compute_fields
published_formatted: str # article publication time (text)
updated_formatted: str # article publication time (text)
# no default value, computed on save_html
html_path: str = None # html path, relative to the html_root directory (None will force it to be recomputed on next save_html)
# with default value
@ -132,14 +129,11 @@ class Article(ABC):
return html_path.relative_to(config.html_root)
def _get_template_dict(self) -> dict:
config = self.config
template_fields = (
"id",
"unread",
"title",
"published",
"published_formatted",
"updated",
"updated_formatted",
"author",
"summary",
"content",
@ -153,29 +147,27 @@ class Article(ABC):
"image_url",
)
d = {field: getattr(self, field) for field in template_fields}
d["published"] = format_datetime(config, self.updated)
d["updated"] = format_datetime(config, self.published)
d["category"] = self.category.asdict()
return d
def compute_fields(self):
config = self.config
self.updated_formatted = format_datetime(config, self.updated)
self.published_formatted = format_datetime(config, self.published)
def compute_json_path(self):
self.json_path = (
config.json_root
self.config.json_root
/ f"{sha256(str(self.id).encode('utf-8')).hexdigest()}.json"
)
self.html_path = None
def write_json(self):
def write_json(self, recompute_path=False):
"""Write the JSON file associated with this article. Error if it already exists."""
if recompute_path:
self.compute_json_path()
stored_fields = (
"id",
"unread",
"title",
"published",
"published_formatted",
"updated",
"updated_formatted",
"author",
"summary",
"content",
@ -210,13 +202,13 @@ class Article(ABC):
html_path = self.config.html_root / self.html_path
return html_path.exists()
def write_html(self):
def write_html(self, recompute_path=False):
"""Write the HTML file associated with this article. Error if it already exists.
Note: this may recompute html_path, which is saved into the JSON - so make sure to save the JSON file _after_ the HTML file."""
# Write HTML file for a JSON object
config = self.config
if self.html_path is None:
if self.html_path is None or recompute_path:
self.html_path = str(self._get_html_path())
html_path = config.html_root / self.html_path
@ -238,10 +230,10 @@ class Article(ABC):
if not ignore_deleted or html_path.exists():
html_path.unlink()
def write(self):
def write(self, recompute_paths=False):
"""Write all the files associated with this article to disk."""
self.write_html()
self.write_json()
self.write_html(recompute_path=recompute_paths)
self.write_json(recompute_path=recompute_paths)
def delete(self):
"""Delete all the files associated with this article."""
@ -251,8 +243,7 @@ class Article(ABC):
def regenerate(self):
"""Delete and rewrite all the files associated with this article using to the latest configuration."""
self.delete() # paths might change so we preemptively remove the old file
self.compute_fields() # recompute formatted datetime & paths from the current configuration
self.write() # rewrite JSON & HTML
self.write(recompute_paths=True) # rewrite JSON & HTML
def was_updated(self, old_article: Article) -> bool:
"""Returns true if the article is different from a previous version in a way that would require regeneration"""