add config for templatesFolder

This commit is contained in:
Ruidy 2023-06-04 11:27:06 +02:00
parent 04006bb736
commit 7761a3b7f0
4 changed files with 21 additions and 5 deletions

View file

@ -6,7 +6,7 @@
The entry point is located in the [main file](./lib/main.py). It should not be modified.
The templates files must be located in the [templates](./templates) directory.
By default, the templates files are located in the [templates](./templates) directory.
You can use template inheritance but not yet data injection.
### Configuration
@ -15,6 +15,7 @@ The configuration file ([config.json](./config.json)) is mandatory and should re
```json
{
"name": "VillaFleurie",
"templatesFolder": "templates",
"templates": [
"index.html",
"t2-corail.html",

View file

@ -4,6 +4,7 @@ from typing import TypedDict
class Config(TypedDict):
name: str
templatesFolder: str
templates: list[str]
staticFiles: list[str]
outDir: str

View file

@ -1,7 +1,19 @@
from dataclasses import dataclass
from typing import Protocol
from jinja2 import Environment, FileSystemLoader, select_autoescape
engine = Environment(loader=FileSystemLoader("templates"), autoescape=select_autoescape())
def render(template: str, context: dict | None = None) -> str:
class Renderer(Protocol):
def render(self, template: str, context: dict | None = None) -> str:
...
@dataclass
class FileSystemRenderer(Renderer):
path: str = "templates"
def render(self, template: str, context: dict | None = None) -> str:
return engine.get_template(template).render(context or {})

View file

@ -6,7 +6,7 @@ from os import path
from loguru import logger
from lib.config import load
from lib.engine import render
from lib.engine import FileSystemRenderer
def main():
@ -18,6 +18,7 @@ def main():
data = {}
destination_path = config["outDir"]
fs = FileSystemRenderer(config.get("templatesFolder"))
logger.info(f"🏁 Start building {config['name']}")
@ -28,11 +29,12 @@ def main():
for template in config["templates"]:
logger.info(f"📃Render '{template}'")
with open(path.join(destination_path, template), "w") as f:
f.write(render(template, data.get(template)))
f.write(fs.render(template, data.get(template)))
logger.info("⏩ Start copying staticfiles to build")
for folder in config["staticFiles"]:
shutil.copytree(folder, f"{config['outDir']}/{folder}")
logger.info("🎉 Done…")