Commit 487dba0c authored by Marien Fressinaud's avatar Marien Fressinaud

Refactor page loading with a Page class

parent 5c796e4c
......@@ -251,7 +251,8 @@ $ cat site/foo/index.html
<html><body>My page</body></html>
```
Please note this template **also applies to the index page**.
Please note since the index page is considered as a "normal" page, this
template **also applies to the index page**.
## Tests
......
......@@ -114,6 +114,33 @@ class Article:
return article_update.astimezone(tz=self.timezone())
class Page:
def __init__(self, slug, page_content, configuration={}):
# We initialize the page meta variables which will be accessible in the
# template
self.meta = {}
self.meta.update(configuration)
self.meta["PAGE_SLUG"] = slug
self.meta["PAGE_CONTENT"] = page_content
# And we initialize a template for the page
page_template_filepath = os.path.join("templates", "page.html")
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"]
def content(self):
return self.meta["PAGE_CONTENT"]
def dir_tree(path):
"""List all the tree directory under the given path.
"""
......@@ -174,52 +201,42 @@ def main(environment):
if configuration["SITE_URL"] == "filesystem":
configuration["SITE_URL"] = f"file://{os.path.abspath(output_path)}/"
# Check that index file exists and copy it under the ./site folder
# Check that index file exists and load a Page from it
index_filepath = os.path.join(os.curdir, "index.html")
if not os.path.exists(index_filepath):
raise ProgramError("./index.html file does not exist")
index_output_filepath = os.path.join(output_path, "index.html")
page_template_filepath = os.path.join("templates", "page.html")
if os.path.exists(page_template_filepath):
template = boopsy.Template(page_template_filepath)
meta = {}
meta.update(configuration)
with open(index_filepath, "r") as index_file:
meta["PAGE_CONTENT"] = index_file.read()
with open(index_output_filepath, "w") as output_file:
output_file.write(template.render(meta))
else:
shutil.copyfile(index_filepath, index_output_filepath)
print(f"Written page: file://{os.path.abspath(index_output_filepath)}")
# Check that pages directory exists on the filesystem
pages = []
with open(index_filepath, "r") as index_file:
page = Page("index", index_file.read(), configuration=configuration)
pages.append(page)
# Check that pages directory exists and load Pages from it
pages_path = os.path.join(os.curdir, "pages")
if os.path.isdir(pages_path):
for filename in dir_tree(pages_path):
_, ext = os.path.splitext(filename)
slug, ext = os.path.splitext(filename)
if ext != ".html":
# Don't consider files which are not HTML
continue
# We read the page file and initialize a Page from it
page_filepath = os.path.join(pages_path, filename)
output_filepath = os.path.join(output_path, filename)
# Create dir structure for the file
mkdirs_for_file(output_filepath)
# And copy it (either using page template or simple copy)
if os.path.exists(page_template_filepath):
template = boopsy.Template(page_template_filepath)
meta = {}
meta.update(configuration)
with open(page_filepath, "r") as page_file:
meta["PAGE_CONTENT"] = page_file.read()
with open(output_filepath, "w") as output_file:
output_file.write(template.render(meta))
else:
shutil.copyfile(page_filepath, output_filepath)
print(f"Written page: file://{os.path.abspath(output_filepath)}")
with open(page_filepath, "r") as page_file:
page = Page(slug, page_file.read(), configuration=configuration)
pages.append(page)
# And write them in the ./site folder
for page in pages:
output_filepath = os.path.join(output_path, f"{page.slug()}.html")
# Create dir structure for the file
mkdirs_for_file(output_filepath)
# And write the file
with open(output_filepath, "w") as output_file:
output_file.write(page.render())
print(f"Written article: file://{os.path.abspath(output_filepath)}")
# Check that a ./static folder exists and copy its content under the ./site
# folder
......
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