use file based routing

This commit is contained in:
Ruidy 2023-06-18 23:05:25 +02:00
parent c7f5429266
commit 255714eca2
9 changed files with 44 additions and 27 deletions

View file

@ -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) [![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. 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. 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 ### Configuration
@ -17,13 +28,6 @@ The configuration file ([config.json](./config.json)) is mandatory and should re
{ {
"name": "VillaFleurie", "name": "VillaFleurie",
"templatesDir": "templates", "templatesDir": "templates",
"templates": [
"index.html",
"t2-corail.html",
"t3-azur.html",
"contact.html",
"reservation.html"
],
"staticFilesDir": [], "staticFilesDir": [],
"outDir": "dist" "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 - [x] Pick real reviews from AirBnB and Booking
- [ ] Optimize images - [ ] Optimize images
- [ ] Automate the file search - [ ] 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 'all' key for data available in all templates
- [ ] Create a template for the rooms - [ ] Create a template for the rooms
- [ ] Build script before commit - [ ] Build script before commit

View file

@ -1,12 +1,5 @@
{ {
"name": "VillaFleurie", "name": "VillaFleurie",
"templates": [
"index.html",
"t2-corail.html",
"t3-azur.html",
"contact.html",
"reservation.html"
],
"staticFilesDir": [ "staticFilesDir": [
"images", "images",
"js", "js",

View file

@ -11,7 +11,6 @@ OUT_DIR = "outDir"
@dataclass(frozen=True) @dataclass(frozen=True)
class Config: class Config:
name: str name: str
templates: list[str]
static_files_dir: list[str] static_files_dir: list[str]
out_dir: str out_dir: str
templates_dir: str templates_dir: str
@ -22,7 +21,6 @@ def load() -> Config:
raw_config = json.loads(f.read()) raw_config = json.loads(f.read())
return Config( return Config(
name=raw_config[NAME], name=raw_config[NAME],
templates=raw_config[TEMPLATES],
static_files_dir=raw_config[STATIC_FILES_DIR], static_files_dir=raw_config[STATIC_FILES_DIR],
out_dir=raw_config.setdefault(OUT_DIR, "dist"), out_dir=raw_config.setdefault(OUT_DIR, "dist"),
templates_dir=raw_config.setdefault(TEMPLATES_DIR, "templates"), templates_dir=raw_config.setdefault(TEMPLATES_DIR, "templates"),

View file

@ -1,6 +1,7 @@
import os import os
import shutil import shutil
import sys import sys
import tomllib
from loguru import logger 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") logger.error("The configuration file 'config.json' was not found. Please verify it exists at the root level")
sys.exit() sys.exit()
data = {}
destination_path = config.out_dir destination_path = config.out_dir
fs = FileSystemRenderer(config.templates_dir) fs = FileSystemRenderer(config.templates_dir)
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): clean_dist(destination_path)
shutil.rmtree(destination_path)
os.mkdir(destination_path)
for template in config.templates: pages = [page for page in os.scandir("./pages") if page.is_file()]
logger.info(f"📃Render '{template}'") for page in pages:
with open(os.path.join(destination_path, template), "w") as f: with open(page, "rb") as f:
f.write(fs.render(template, data.get(template))) 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") logger.info("⏩ Start copying staticfiles to build")
for folder in config.static_files_dir: for folder in config.static_files_dir:
@ -37,5 +38,11 @@ def main():
logger.info("🎉 Done…") 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__": if __name__ == "__main__":
main() main()

3
pages/contact.toml Normal file
View file

@ -0,0 +1,3 @@
name = "contact"
title = "Contact | VillaFleurie - Locations de vacances au Gosier en Guadeloupe"
template = "contact.html"

3
pages/index.toml Normal file
View file

@ -0,0 +1,3 @@
name = "index"
title = "VillaFleurie | Locations de vacances au Gosier en Guadeloupe"
template = "index.html"

3
pages/reservation.toml Normal file
View file

@ -0,0 +1,3 @@
name = "reservation"
title = "Reservation | VillaFleurie - Locations de vacances au Gosier en Guadeloupe"
template = "reservation.html"

3
pages/t2-corail.toml Normal file
View file

@ -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"

3
pages/t3-azur.toml Normal file
View file

@ -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"