From 255714eca24fc1e82695643b05d72cb0a5d6907f Mon Sep 17 00:00:00 2001 From: Ruidy Date: Sun, 18 Jun 2023 23:05:25 +0200 Subject: [PATCH] use file based routing --- README.md | 24 ++++++++++++++---------- config.json | 7 ------- lib/config.py | 2 -- lib/main.py | 23 +++++++++++++++-------- pages/contact.toml | 3 +++ pages/index.toml | 3 +++ pages/reservation.toml | 3 +++ pages/t2-corail.toml | 3 +++ pages/t3-azur.toml | 3 +++ 9 files changed, 44 insertions(+), 27 deletions(-) create mode 100644 pages/contact.toml create mode 100644 pages/index.toml create mode 100644 pages/reservation.toml create mode 100644 pages/t2-corail.toml create mode 100644 pages/t3-azur.toml diff --git a/README.md b/README.md index 1cfe229..41e6a9d 100644 --- a/README.md +++ b/README.md @@ -2,12 +2,23 @@ [![Netlify Status](https://api.netlify.com/api/v1/badges/aa5c29ee-eced-46dd-ad53-1e0822001364/deploy-status)](https://app.netlify.com/sites/villafleurie-site/deploys) -## Static Site Generator +## How to use + +You can build the site using the built-in static site generator included. + +To create a page, create a `toml` file in the `pages` directory. It should contain at least the following fields: + +```toml +name = "index" +template = "index.html" +``` + +You can add other fields they will become available in the template. The entry point is located in the [main file](./lib/main.py). It should not be modified. 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. ### Configuration @@ -17,13 +28,6 @@ The configuration file ([config.json](./config.json)) is mandatory and should re { "name": "VillaFleurie", "templatesDir": "templates", - "templates": [ - "index.html", - "t2-corail.html", - "t3-azur.html", - "contact.html", - "reservation.html" - ], "staticFilesDir": [], "outDir": "dist" } @@ -64,7 +68,7 @@ You can then deploy the site on any platform supporting static sites (Netlify, - [x] Pick real reviews from AirBnB and Booking - [ ] Optimize images - [ ] Automate the file search -- [ ] Extract data out of the template +- [x] Extract data out of the template - [ ] Create a 'all' key for data available in all templates - [ ] Create a template for the rooms - [ ] Build script before commit diff --git a/config.json b/config.json index 176f6ee..1d98306 100644 --- a/config.json +++ b/config.json @@ -1,12 +1,5 @@ { "name": "VillaFleurie", - "templates": [ - "index.html", - "t2-corail.html", - "t3-azur.html", - "contact.html", - "reservation.html" - ], "staticFilesDir": [ "images", "js", diff --git a/lib/config.py b/lib/config.py index 7b97423..01639f8 100644 --- a/lib/config.py +++ b/lib/config.py @@ -11,7 +11,6 @@ OUT_DIR = "outDir" @dataclass(frozen=True) class Config: name: str - templates: list[str] static_files_dir: list[str] out_dir: str templates_dir: str @@ -22,7 +21,6 @@ def load() -> Config: raw_config = json.loads(f.read()) return Config( name=raw_config[NAME], - templates=raw_config[TEMPLATES], static_files_dir=raw_config[STATIC_FILES_DIR], out_dir=raw_config.setdefault(OUT_DIR, "dist"), templates_dir=raw_config.setdefault(TEMPLATES_DIR, "templates"), diff --git a/lib/main.py b/lib/main.py index 544091e..4aa201d 100644 --- a/lib/main.py +++ b/lib/main.py @@ -1,6 +1,7 @@ import os import shutil import sys +import tomllib from loguru import logger @@ -15,20 +16,20 @@ def main(): logger.error("The configuration file 'config.json' was not found. Please verify it exists at the root level") sys.exit() - data = {} destination_path = config.out_dir fs = FileSystemRenderer(config.templates_dir) 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) + clean_dist(destination_path) - for template in config.templates: - logger.info(f"📃Render '{template}'") - with open(os.path.join(destination_path, template), "w") as f: - f.write(fs.render(template, data.get(template))) + pages = [page for page in os.scandir("./pages") if page.is_file()] + for page in pages: + with open(page, "rb") as f: + data = tomllib.load(f) + logger.info(f"📃Render '{page.name}'") + with open(os.path.join(destination_path, f'{data["name"]}.html'), "w") as fd: + fd.write(fs.render(data["template"], data)) logger.info("⏩ Start copying staticfiles to build") for folder in config.static_files_dir: @@ -37,5 +38,11 @@ def main(): logger.info("🎉 Done…") +def clean_dist(destination_path: str): + if os.path.exists(destination_path) and os.path.isdir(destination_path): + shutil.rmtree(destination_path) + os.mkdir(destination_path) + + if __name__ == "__main__": main() diff --git a/pages/contact.toml b/pages/contact.toml new file mode 100644 index 0000000..6c0c179 --- /dev/null +++ b/pages/contact.toml @@ -0,0 +1,3 @@ +name = "contact" +title = "Contact | VillaFleurie - Locations de vacances au Gosier en Guadeloupe" +template = "contact.html" diff --git a/pages/index.toml b/pages/index.toml new file mode 100644 index 0000000..680b90b --- /dev/null +++ b/pages/index.toml @@ -0,0 +1,3 @@ +name = "index" +title = "VillaFleurie | Locations de vacances au Gosier en Guadeloupe" +template = "index.html" diff --git a/pages/reservation.toml b/pages/reservation.toml new file mode 100644 index 0000000..3a305be --- /dev/null +++ b/pages/reservation.toml @@ -0,0 +1,3 @@ +name = "reservation" +title = "Reservation | VillaFleurie - Locations de vacances au Gosier en Guadeloupe" +template = "reservation.html" diff --git a/pages/t2-corail.toml b/pages/t2-corail.toml new file mode 100644 index 0000000..8225f30 --- /dev/null +++ b/pages/t2-corail.toml @@ -0,0 +1,3 @@ +name = "t2-corail" +title = "T2 Corail, petit nid douillet | VillaFleurie - Locations de vacances au Gosier en Guadeloupe" +template = "t2-corail.html" diff --git a/pages/t3-azur.toml b/pages/t3-azur.toml new file mode 100644 index 0000000..cea8728 --- /dev/null +++ b/pages/t3-azur.toml @@ -0,0 +1,3 @@ +name = "t3-azur" +title = "T3 Azur, besoin d'un lieu confortable | VillaFleurie - Locations de vacances au Gosier en Guadeloupe" +template = "t3-azur.html"