Commit 6f6d63c9 authored by Marien Fressinaud's avatar Marien Fressinaud

add: Add Boopsy support for all the pages

parents 61b3d16d 487dba0c
......@@ -241,6 +241,19 @@ $ tree
└── index.html
```
You can also define a generic page template in the `templates/page.html` file.
The file's content is accessible through the `PAGE_CONTENT` variable.
```console
$ echo '<html><body>{{ PAGE_CONTENT }}</body></html>' > templates/page.html
$ echo 'My page' > pages/foo/index.html
$ cat site/foo/index.html
<html><body>My page</body></html>
```
Please note since the index page is considered as a "normal" page, this
template **also applies to the index page**.
## Tests
There are some tests (i.e. doctests) that can be run to check that everything
......
......@@ -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.
"""
......@@ -144,45 +171,6 @@ def main(environment):
os.remove(output_path)
os.mkdir(output_path)
# Check that index file exists and copy it under the ./site folder
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")
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_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)
if ext != ".html":
# Don't consider files which are not HTML
continue
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
shutil.copyfile(page_filepath, output_filepath)
print(f"Written page: file://{os.path.abspath(output_filepath)}")
# 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
# Load the configuration (it can be overidden by a configuration.yml file)
configuration = {
"SITE_URL": "filesystem",
......@@ -213,6 +201,55 @@ def main(environment):
if configuration["SITE_URL"] == "filesystem":
configuration["SITE_URL"] = f"file://{os.path.abspath(output_path)}/"
# 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")
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):
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)
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
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):
......
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