Commit 72f05132 authored by berumuron's avatar berumuron
Browse files

Reorder steps to load and write content

We'll soon introduce the series in the code itself. They will be defined
by a `serie.html` file in an article folder. So pages might appear while
we are loading articles. The consequence is that we must write pages
after loading the articles.

Also, the static files are loaded at the end so there are never erased.
parent 20af2ee3
......@@ -242,12 +242,15 @@ def main(environment):
configuration_path = os.path.join(os.curdir, "configuration.yml")
configuration = load_configuration(configuration_path, output_path)
# Check that index file exists and load a Page from it
# STEP 1: we load all the Articles and Pages
pages = []
articles = []
# Check that index file exists
index_filepath = os.path.join(os.curdir, "index.html")
if not os.path.exists(index_filepath):
raise ProgramError("./index.html file does not exist")
pages = []
with open(index_filepath, "r") as index_file:
page = Page("index", index_file.read(), configuration=configuration)
pages.append(page)
......@@ -267,7 +270,26 @@ def main(environment):
page = Page(slug, page_file.read(), configuration=configuration)
pages.append(page)
# And write them in the ./site folder
# 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)
if ext != ".md":
# Don't consider files which are not Markdown
continue
slug = os.path.basename(filename_no_ext)
# 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")
......@@ -279,38 +301,6 @@ def main(environment):
output_file.write(page.render())
print(f"Written page: {page.url()}")
# Check that a ./static folder exists and copy its content under the ./site
# folder
static_dirpath = os.path.join(os.curdir, "static")
if os.path.isdir(static_dirpath):
distutils.dir_util.copy_tree(static_dirpath, output_path)
# Check that articles directory exists on the filesystem
articles_path = os.path.join(os.curdir, "articles")
if not os.path.isdir(articles_path):
# If not, just stop here
return
# List files from ./articles
articles = []
for filename in dir_tree(articles_path):
filename_no_ext, ext = os.path.splitext(filename)
if ext != ".md":
# Don't consider files which are not Markdown
continue
slug = os.path.basename(filename_no_ext)
# 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)
# If there is no articles, stop there
if len(articles) == 0:
return
# Sort the articles by publication date
articles.sort(key=lambda article: article.date(), reverse=True)
......@@ -333,6 +323,7 @@ def main(environment):
output_file.write(blog_template.render(context))
print(f"Written page: {configuration['SITE_URL']}blog.html")
# STEP 3: we write also the Atom feed.
# Just create some variables so it's easier to manipulate after
site_url = configuration["SITE_URL"]
site_uuid = configuration["SITE_UUID"]
......@@ -377,7 +368,13 @@ def main(environment):
os.mkdir(feed_dirpath)
feed_filepath = os.path.join(feed_dirpath, "all.atom.xml")
feed.write(feed_filepath)
print(f"Written feed: {configuration['SITE_URL']}feeds/all.atom.xml")
print(f"Written feed: {site_url}feeds/all.atom.xml")
# STEP 4: and we finish by copying the static folder.
static_dirpath = os.path.join(os.curdir, "static")
if os.path.isdir(static_dirpath):
distutils.dir_util.copy_tree(static_dirpath, output_path)
print(f"Static files copied")
if __name__ == "__main__":
......
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