mirror of
https://github.com/rjNemo/vf-site
synced 2026-06-12 12:06:39 +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 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.
|
You can use template inheritance but not yet data injection.
|
||||||
### Configuration
|
### Configuration
|
||||||
|
|
||||||
|
|
@ -15,6 +15,7 @@ The configuration file ([config.json](./config.json)) is mandatory and should re
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"name": "VillaFleurie",
|
"name": "VillaFleurie",
|
||||||
|
"templatesFolder": "templates",
|
||||||
"templates": [
|
"templates": [
|
||||||
"index.html",
|
"index.html",
|
||||||
"t2-corail.html",
|
"t2-corail.html",
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ from typing import TypedDict
|
||||||
|
|
||||||
class Config(TypedDict):
|
class Config(TypedDict):
|
||||||
name: str
|
name: str
|
||||||
|
templatesFolder: str
|
||||||
templates: list[str]
|
templates: list[str]
|
||||||
staticFiles: list[str]
|
staticFiles: list[str]
|
||||||
outDir: str
|
outDir: str
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,19 @@
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from typing import Protocol
|
||||||
|
|
||||||
from jinja2 import Environment, FileSystemLoader, select_autoescape
|
from jinja2 import Environment, FileSystemLoader, select_autoescape
|
||||||
|
|
||||||
engine = Environment(loader=FileSystemLoader("templates"), autoescape=select_autoescape())
|
engine = Environment(loader=FileSystemLoader("templates"), autoescape=select_autoescape())
|
||||||
|
|
||||||
|
|
||||||
def render(template: str, context: dict | None = None) -> str:
|
class Renderer(Protocol):
|
||||||
return engine.get_template(template).render(context or {})
|
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 loguru import logger
|
||||||
|
|
||||||
from lib.config import load
|
from lib.config import load
|
||||||
from lib.engine import render
|
from lib.engine import FileSystemRenderer
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
@ -18,6 +18,7 @@ def main():
|
||||||
|
|
||||||
data = {}
|
data = {}
|
||||||
destination_path = config["outDir"]
|
destination_path = config["outDir"]
|
||||||
|
fs = FileSystemRenderer(config.get("templatesFolder"))
|
||||||
|
|
||||||
logger.info(f"🏁 Start building {config['name']}")
|
logger.info(f"🏁 Start building {config['name']}")
|
||||||
|
|
||||||
|
|
@ -28,11 +29,12 @@ def main():
|
||||||
for template in config["templates"]:
|
for template in config["templates"]:
|
||||||
logger.info(f"📃Render '{template}'")
|
logger.info(f"📃Render '{template}'")
|
||||||
with open(path.join(destination_path, template), "w") as f:
|
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")
|
logger.info("⏩ Start copying staticfiles to build")
|
||||||
for folder in config["staticFiles"]:
|
for folder in config["staticFiles"]:
|
||||||
shutil.copytree(folder, f"{config['outDir']}/{folder}")
|
shutil.copytree(folder, f"{config['outDir']}/{folder}")
|
||||||
|
|
||||||
logger.info("🎉 Done…")
|
logger.info("🎉 Done…")
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue