mirror of
https://github.com/rjNemo/vf-site
synced 2026-06-06 01:16:38 +00:00
use consistent naming convention for config keys
set default in main instead of inside the renderer fix: renderer template folder setup
This commit is contained in:
parent
7761a3b7f0
commit
a5136d102b
5 changed files with 21 additions and 19 deletions
|
|
@ -15,7 +15,7 @@ The configuration file ([config.json](./config.json)) is mandatory and should re
|
|||
```json
|
||||
{
|
||||
"name": "VillaFleurie",
|
||||
"templatesFolder": "templates",
|
||||
"templatesDir": "templates",
|
||||
"templates": [
|
||||
"index.html",
|
||||
"t2-corail.html",
|
||||
|
|
@ -23,7 +23,7 @@ The configuration file ([config.json](./config.json)) is mandatory and should re
|
|||
"contact.html",
|
||||
"reservation.html"
|
||||
],
|
||||
"staticFiles": [],
|
||||
"staticFilesDir": [],
|
||||
"outDir": "dist"
|
||||
}
|
||||
```
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@
|
|||
"contact.html",
|
||||
"reservation.html"
|
||||
],
|
||||
"staticFiles": [
|
||||
"staticFilesDir": [
|
||||
"images",
|
||||
"js",
|
||||
"css",
|
||||
|
|
|
|||
|
|
@ -4,9 +4,9 @@ from typing import TypedDict
|
|||
|
||||
class Config(TypedDict):
|
||||
name: str
|
||||
templatesFolder: str
|
||||
templatesDir: str
|
||||
templates: list[str]
|
||||
staticFiles: list[str]
|
||||
staticFilesDir: list[str]
|
||||
outDir: str
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,19 +1,16 @@
|
|||
from dataclasses import dataclass
|
||||
from typing import Protocol
|
||||
|
||||
from jinja2 import Environment, FileSystemLoader, select_autoescape
|
||||
|
||||
engine = Environment(loader=FileSystemLoader("templates"), autoescape=select_autoescape())
|
||||
|
||||
|
||||
class Renderer(Protocol):
|
||||
def render(self, template: str, context: dict | None = None) -> str:
|
||||
...
|
||||
|
||||
|
||||
@dataclass
|
||||
class FileSystemRenderer(Renderer):
|
||||
path: str = "templates"
|
||||
def __init__(self, path: str):
|
||||
self.engine = Environment(loader=FileSystemLoader(path), autoescape=select_autoescape())
|
||||
|
||||
def render(self, template: str, context: dict | None = None) -> str:
|
||||
return engine.get_template(template).render(context or {})
|
||||
return self.engine.get_template(template).render(context or {})
|
||||
|
|
|
|||
21
lib/main.py
21
lib/main.py
|
|
@ -1,13 +1,18 @@
|
|||
import os
|
||||
import shutil
|
||||
import sys
|
||||
from os import path
|
||||
|
||||
from loguru import logger
|
||||
|
||||
from lib.config import load
|
||||
from lib.engine import FileSystemRenderer
|
||||
|
||||
NAME = "name"
|
||||
TEMPLATES = "templates"
|
||||
TEMPLATES_DIR = "templatesDir"
|
||||
STATIC_FILES_DIR = "staticFilesDir"
|
||||
OUT_DIR = "outDir"
|
||||
|
||||
|
||||
def main():
|
||||
try:
|
||||
|
|
@ -17,23 +22,23 @@ def main():
|
|||
sys.exit()
|
||||
|
||||
data = {}
|
||||
destination_path = config["outDir"]
|
||||
fs = FileSystemRenderer(config.get("templatesFolder"))
|
||||
destination_path = config[OUT_DIR]
|
||||
fs = FileSystemRenderer(config.setdefault(TEMPLATES_DIR, TEMPLATES))
|
||||
|
||||
logger.info(f"🏁 Start building {config['name']}")
|
||||
logger.info(f"🏁 Start building {config[NAME]}")
|
||||
|
||||
if os.path.exists(destination_path) and os.path.isdir(destination_path):
|
||||
shutil.rmtree(destination_path)
|
||||
os.mkdir(destination_path)
|
||||
|
||||
for template in config["templates"]:
|
||||
for template in config[TEMPLATES]:
|
||||
logger.info(f"📃Render '{template}'")
|
||||
with open(path.join(destination_path, template), "w") as f:
|
||||
with open(os.path.join(destination_path, template), "w") as f:
|
||||
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}")
|
||||
for folder in config[STATIC_FILES_DIR]:
|
||||
shutil.copytree(folder, os.path.join(config[OUT_DIR], folder))
|
||||
|
||||
logger.info("🎉 Done…")
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue