Commit 81ff584a authored by Marien Fressinaud's avatar Marien Fressinaud

add: Accept Yaml front in HTML pages

parent 6f6d63c9
......@@ -254,6 +254,51 @@ $ cat site/foo/index.html
Please note since the index page is considered as a "normal" page, this
template **also applies to the index page**.
You can define Yaml variables in the pages that will be used in the template
then. The variables are uppercased and prepended by `PAGE_`. For instance, in a
`pages/my-page.html` file:
```html
---
title: My page
---
<div>
This is the content of my page
</div>
```
And in the `templates/page.html` file:
```html
<html>
<head>
<title>{{ PAGE_TITLE }}</title>
</head>
<body>
<h1>{{ PAGE_TITLE }}</h1>
{{ PAGE_CONTENT }}
</body>
</html>
```
This will generate the `site/my_page.html` file:
```html
<html>
<head>
<title>My page</title>
</head>
<body>
<h1>My page</h1>
<div>
This is the content of my page
</div>
</body>
</html>
```
## Tests
There are some tests (i.e. doctests) that can be run to check that everything
......
......@@ -8,6 +8,7 @@ import locale
import yaml
import datetime
import uuid
import re
from enum import Enum
import markdown
......@@ -121,6 +122,10 @@ class Page:
self.meta = {}
self.meta.update(configuration)
self.meta["PAGE_SLUG"] = slug
yaml_conf, page_content = extract_yaml_configuration(page_content)
for key, value in yaml_conf.items():
article_key = f"PAGE_{key.upper()}"
self.meta[article_key] = value
self.meta["PAGE_CONTENT"] = page_content
# And we initialize a template for the page
......@@ -141,6 +146,18 @@ class Page:
return self.meta["PAGE_CONTENT"]
YAML_FRONT_MATTER_REGEX = re.compile("(---\n.*\n)(---\n)(.*)", re.M | re.S)
def extract_yaml_configuration(content):
match = YAML_FRONT_MATTER_REGEX.match(content)
if match:
yaml_conf = yaml.load(match.group(1))
return yaml_conf, match.group(3)
else:
return {}, content
def dir_tree(path):
"""List all the tree directory under the given 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