mirror of
https://github.com/rjNemo/vf-site
synced 2026-06-06 01:16:38 +00:00
add config for templatesFolder
This commit is contained in:
parent
04006bb736
commit
7761a3b7f0
4 changed files with 21 additions and 5 deletions
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ from typing import TypedDict
|
|||
|
||||
class Config(TypedDict):
|
||||
name: str
|
||||
templatesFolder: str
|
||||
templates: list[str]
|
||||
staticFiles: list[str]
|
||||
outDir: str
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
return engine.get_template(template).render(context or {})
|
||||
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 {})
|
||||
|
|
|
|||
|
|
@ -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…")
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue