A first models with articles and site

This commit is contained in:
Fred Pauchet 2022-12-29 21:34:02 +01:00
parent bd2bc1d551
commit 725c738056
1 changed files with 56 additions and 0 deletions

56
jack/models.py Normal file
View File

@ -0,0 +1,56 @@
from datetime import datetime
import logging
import os
import markdown
class Article:
"""An article is composed by a content, associated to facultative tags and a category
"""
def __init__(self, file_content: str, file_name: str, categories: list):
self.content = file_content
self.file_name = file_name
self.file_path = file_path
self.published_date = datetime.strptime(filename[0:10], "%Y-%m-%d")
self.categories = categories
md = markdown.Markdown(extensions=["meta", "fenced_code"])
self.html = md.convert(self.content)
self.meta = md.Meta
self.title = self.meta.get("title")
self.description = self.meta.get("description")
class Site:
"""A site contains articles, categories and all related data.
"""
def __init__(self, root_directory: str):
self.root_directory = root_directory
self.articles = []
self.categories = {}
self.tags = {}
def add(self, folder: str, article_filepath: str):
"""Add a new article to the current site
Args:
article (Article)
"""
try:
article = Article(self.root_directory, folder, article_filepath)
logging.info("Append article {}".format(article))
self.articles.append(article)
category = self.categories.setdefault(article.category, [])
category.append(article)
for tag in article.tags:
tag_key = self.tags.setdefault(tag, [])
tag_key.append(article)
except ValueError as value_error:
logging.warn("Article does not support {}".format(value_error))