Commit 3702db82 authored by berumuron's avatar berumuron
Browse files

Extract a function to write articles and pages

parent 05c9ee86
......@@ -100,26 +100,16 @@ def main(environment):
# STEP 2: we write all the pages and articles
for page in pages:
output_filepath = os.path.join(output_path, f"{page.slug()}.html")
# Create dir structure for the file
utils.mkdirs_for_file(output_filepath)
# And write the file
with open(output_filepath, "w") as output_file:
output_file.write(page.render())
utils.write_content(page, output_path)
print(f"Written page: {page.url()}")
# Sort the articles by publication date
# We want to sort the articles by publication date first
articles.sort(key=lambda article: article.date(), reverse=True)
for serie_articles in articles_by_series.values():
serie_articles.sort(key=lambda article: article.date(), reverse=True)
# And write them in the ./site folder
for article in articles:
output_filepath = os.path.join(output_path, f"{article.slug()}.html")
with open(output_filepath, "w") as output_file:
output_file.write(article.render())
utils.write_content(article, output_path)
print(f"Written article: {article.url()}")
# Write the page with the list of articles if blog template exists
......
import os
import markdown
import datetime
import uuid
import pytz
import boop.boopsy as boopsy
class Article:
def __init__(self, slug, article_content, configuration={}, serie=None):
......@@ -31,12 +28,6 @@ class Article:
self.meta["ARTICLE_CONTENT"] = content
self.meta["ARTICLE_SERIE"] = serie
# And we initialize a template for the article
article_template_filepath = os.path.join(
"templates", f"{self.meta['ARTICLE_TEMPLATE']}.html"
)
self.template = boopsy.Template(article_template_filepath)
def render(self):
return self.template.render(self.meta)
......@@ -88,3 +79,6 @@ class Article:
def serie(self):
return self.meta["ARTICLE_SERIE"]
def template(self):
return self.meta["ARTICLE_TEMPLATE"]
import os
import uuid
import re
import yaml
import boop.boopsy as boopsy
class Page:
def __init__(self, slug, page_content, configuration={}):
......@@ -20,20 +17,6 @@ class Page:
self.meta[article_key] = value
self.meta["PAGE_CONTENT"] = page_content
# And we initialize a template for the page
page_template_filepath = os.path.join(
"templates", f"{self.meta['PAGE_TEMPLATE']}.html"
)
self.template = None
if os.path.exists(page_template_filepath):
self.template = boopsy.Template(page_template_filepath)
def render(self):
if self.template is None:
return self.content()
else:
return self.template.render(self.meta)
def slug(self):
return self.meta["PAGE_SLUG"]
......@@ -50,6 +33,9 @@ class Page:
def content(self):
return self.meta["PAGE_CONTENT"]
def template(self):
return self.meta["PAGE_TEMPLATE"]
YAML_FRONT_MATTER_REGEX = re.compile("(---\n.*\n)(---\n)(.*)", re.M | re.S)
......
import os
import shutil
import boop.boopsy as boopsy
from boop.article import Article
from boop.page import Page
......@@ -66,6 +67,26 @@ def build_page_from_filepath(filepath, configuration, basename=None, slug=None):
return Page(final_slug, page_file.read(), configuration=configuration)
def write_content(content, output_path):
"""Write content (Article or Page) under output_path.
"""
output_filepath = os.path.join(output_path, f"{content.slug()}.html")
template_filepath = os.path.join(
os.curdir, "templates", f"{content.template()}.html"
)
# Initialize the template defined within content
if os.path.exists(template_filepath):
template = boopsy.Template(template_filepath)
rendered_content = template.render(content.meta)
else:
rendered_content = content.content()
mkdirs_for_file(output_filepath)
with open(output_filepath, "w") as output_file:
output_file.write(rendered_content)
def list_dirs(path):
"""List all the directories within a path.
"""
......
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