mirror of
https://github.com/rjNemo/vf-site
synced 2026-06-12 12:06:39 +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
|
```json
|
||||||
{
|
{
|
||||||
"name": "VillaFleurie",
|
"name": "VillaFleurie",
|
||||||
"templatesFolder": "templates",
|
"templatesDir": "templates",
|
||||||
"templates": [
|
"templates": [
|
||||||
"index.html",
|
"index.html",
|
||||||
"t2-corail.html",
|
"t2-corail.html",
|
||||||
|
|
@ -23,7 +23,7 @@ The configuration file ([config.json](./config.json)) is mandatory and should re
|
||||||
"contact.html",
|
"contact.html",
|
||||||
"reservation.html"
|
"reservation.html"
|
||||||
],
|
],
|
||||||
"staticFiles": [],
|
"staticFilesDir": [],
|
||||||
"outDir": "dist"
|
"outDir": "dist"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@
|
||||||
"contact.html",
|
"contact.html",
|
||||||
"reservation.html"
|
"reservation.html"
|
||||||
],
|
],
|
||||||
"staticFiles": [
|
"staticFilesDir": [
|
||||||
"images",
|
"images",
|
||||||
"js",
|
"js",
|
||||||
"css",
|
"css",
|
||||||
|
|
|
||||||
|
|
@ -4,9 +4,9 @@ from typing import TypedDict
|
||||||
|
|
||||||
class Config(TypedDict):
|
class Config(TypedDict):
|
||||||
name: str
|
name: str
|
||||||
templatesFolder: str
|
templatesDir: str
|
||||||
templates: list[str]
|
templates: list[str]
|
||||||
staticFiles: list[str]
|
staticFilesDir: list[str]
|
||||||
outDir: str
|
outDir: str
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,16 @@
|
||||||
from dataclasses import dataclass
|
|
||||||
from typing import Protocol
|
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())
|
|
||||||
|
|
||||||
|
|
||||||
class Renderer(Protocol):
|
class Renderer(Protocol):
|
||||||
def render(self, template: str, context: dict | None = None) -> str:
|
def render(self, template: str, context: dict | None = None) -> str:
|
||||||
...
|
...
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class FileSystemRenderer(Renderer):
|
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:
|
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 os
|
||||||
import shutil
|
import shutil
|
||||||
import sys
|
import sys
|
||||||
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 FileSystemRenderer
|
from lib.engine import FileSystemRenderer
|
||||||
|
|
||||||
|
NAME = "name"
|
||||||
|
TEMPLATES = "templates"
|
||||||
|
TEMPLATES_DIR = "templatesDir"
|
||||||
|
STATIC_FILES_DIR = "staticFilesDir"
|
||||||
|
OUT_DIR = "outDir"
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
try:
|
try:
|
||||||
|
|
@ -17,23 +22,23 @@ def main():
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
data = {}
|
data = {}
|
||||||
destination_path = config["outDir"]
|
destination_path = config[OUT_DIR]
|
||||||
fs = FileSystemRenderer(config.get("templatesFolder"))
|
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):
|
if os.path.exists(destination_path) and os.path.isdir(destination_path):
|
||||||
shutil.rmtree(destination_path)
|
shutil.rmtree(destination_path)
|
||||||
os.mkdir(destination_path)
|
os.mkdir(destination_path)
|
||||||
|
|
||||||
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(os.path.join(destination_path, template), "w") as f:
|
||||||
f.write(fs.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[STATIC_FILES_DIR]:
|
||||||
shutil.copytree(folder, f"{config['outDir']}/{folder}")
|
shutil.copytree(folder, os.path.join(config[OUT_DIR], folder))
|
||||||
|
|
||||||
logger.info("🎉 Done…")
|
logger.info("🎉 Done…")
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue