Commit 796ba09e authored by berumuron's avatar berumuron
Browse files

Make articles aware of their serie belonging

parent 89ddf839
......@@ -42,7 +42,7 @@ class ProgramError(Exception):
class Article:
def __init__(self, slug, article_content, configuration={}):
def __init__(self, slug, article_content, configuration={}, serie=None):
# We convert the file's content into HTML
md = markdown.Markdown(extensions=["meta", "fenced_code", "attr_list"])
content = md.convert(article_content)
......@@ -62,6 +62,7 @@ class Article:
else:
self.meta[article_key] = values[0]
self.meta["ARTICLE_CONTENT"] = content
self.meta["ARTICLE_SERIE"] = serie
# And we initialize a template for the article
article_template_filepath = os.path.join(
......@@ -118,6 +119,9 @@ class Article:
)
return article_update.astimezone(tz=self.timezone())
def serie(self):
return self.meta["ARTICLE_SERIE"]
class Page:
def __init__(self, slug, page_content, configuration={}):
......@@ -150,6 +154,9 @@ class Page:
def slug(self):
return self.meta["PAGE_SLUG"]
def title(self):
return self.meta.get("PAGE_TITLE", "A page")
def url(self):
site_url = self.meta.get("SITE_URL", "")
return f"{site_url}{self.slug()}.html"
......@@ -170,6 +177,24 @@ def extract_yaml_configuration(content):
return {}, content
def list_dirs(path):
"""List all the directories within a path.
"""
for filename in os.listdir(path):
full_path = os.path.join(path, filename)
if os.path.isdir(full_path):
yield filename
def list_files(path):
"""List all the files within a path.
"""
for filename in os.listdir(path):
full_path = os.path.join(path, filename)
if os.path.isfile(full_path):
yield filename
def dir_tree(path):
"""List all the tree directory under the given path.
"""
......@@ -273,27 +298,55 @@ def main(environment):
# Check that articles directory exists and load Articles from it
articles_path = os.path.join(os.curdir, "articles")
if os.path.isdir(articles_path):
for filename in dir_tree(articles_path):
filename_no_ext, ext = os.path.splitext(filename)
slug = os.path.basename(filename_no_ext)
if slug == "serie" and ext == ".html":
# We have a serie Page! Its slug is the directory name
slug = f"serie/{os.path.dirname(filename)}"
serie_filepath = os.path.join(articles_path, filename)
# We iterate on the directories of articles dir. If they contain
# "serie.html" file, it means they contain a serie. Otherwise, we list
# articles as usual.
for dirname in list_dirs(articles_path):
dirpath = os.path.join(articles_path, dirname)
serie_filepath = os.path.join(dirpath, "serie.html")
serie = None
if os.path.exists(serie_filepath):
# We have a serie Page! Each article of the directory must be
# considered part of the serie.
slug = f"serie/{dirname}"
with open(serie_filepath, "r") as serie_file:
serie = Page(slug, serie_file.read(), configuration=configuration)
pages.append(serie)
elif ext == ".md":
for filename in list_files(dirpath):
filename_no_ext, ext = os.path.splitext(filename)
slug = os.path.basename(filename_no_ext)
if ext != ".md":
continue
# We read the article file and initialize an Article from it
article_filepath = os.path.join(articles_path, filename)
article_filepath = os.path.join(dirpath, filename)
with open(article_filepath, "r") as article_file:
article = Article(
slug, article_file.read(), configuration=configuration
slug,
article_file.read(),
configuration=configuration,
serie=serie,
)
articles.append(article)
for filename in list_files(articles_path):
filename_no_ext, ext = os.path.splitext(filename)
slug = os.path.basename(filename_no_ext)
if ext != ".md":
continue
# We read the article file and initialize an Article from it
article_filepath = os.path.join(articles_path, filename)
with open(article_filepath, "r") as article_file:
article = Article(
slug, article_file.read(), configuration=configuration
)
articles.append(article)
# STEP 2: we write all the pages and articles
for page in pages:
output_filepath = os.path.join(output_path, f"{page.slug()}.html")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment