Commit 5c796e4c authored by Marien Fressinaud's avatar Marien Fressinaud

Use page template if it exists

The page template is used to factorize website layout between pages.
The index page is considered as a normal page.
parent 3f235329
......@@ -241,6 +241,18 @@ $ 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 this template **also applies to the index page**.
## Tests
There are some tests (i.e. doctests) that can be run to check that everything
......
......@@ -179,7 +179,18 @@ def main(environment):
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)
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
......@@ -197,8 +208,17 @@ def main(environment):
# Create dir structure for the file
mkdirs_for_file(output_filepath)
# And copy it
shutil.copyfile(page_filepath, 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)}")
# Check that a ./static folder exists and copy its content under the ./site
......
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