From 0d4a7b43ec1889b6f59781790a8b8850417cc8ab Mon Sep 17 00:00:00 2001 From: Ruidy Date: Sun, 28 May 2023 12:18:05 +0200 Subject: [PATCH] code refactor --- Makefile | 4 ++-- Pipfile | 1 + Pipfile.lock | 10 +++++++++- README.md | 10 ++++++++-- config.json | 8 ++++++++ dist/contact.html | 2 +- dist/index.html | 2 +- dist/t2-corail.html | 2 +- dist/t3-azur.html | 2 +- lib/__init__.py | 0 lib/config.py | 7 +++++++ lib/engine.py | 10 ++++++++++ lib/main.py | 19 +++++++++++++++++++ main.py | 22 ---------------------- templates/layout.html | 2 +- 15 files changed, 69 insertions(+), 32 deletions(-) create mode 100644 config.json create mode 100644 lib/__init__.py create mode 100644 lib/config.py create mode 100644 lib/engine.py create mode 100644 lib/main.py delete mode 100644 main.py diff --git a/Makefile b/Makefile index a53dddb..09a787a 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ build: - pipenv run python main.py + @pipenv run python -m lib.main run: build - cd dist && pipenv run python -m http.server + @cd dist && pipenv run python -m http.server diff --git a/Pipfile b/Pipfile index e093f26..00450c6 100644 --- a/Pipfile +++ b/Pipfile @@ -5,6 +5,7 @@ name = "pypi" [packages] jinja2 = "*" +loguru = "*" [dev-packages] black = {extras = ["d"], version = "*"} diff --git a/Pipfile.lock b/Pipfile.lock index 8186c11..d81d590 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "38d153c877b45a305481c3020014557d5caaabdaf51551f5be73963080006fcd" + "sha256": "b822c04b3fa63fe6c790438fd4e4506758278651094042a9bf6422eb1d11584e" }, "pipfile-spec": 6, "requires": { @@ -24,6 +24,14 @@ "index": "pypi", "version": "==3.1.2" }, + "loguru": { + "hashes": [ + "sha256:1612053ced6ae84d7959dd7d5e431a0532642237ec21f7fd83ac73fe539e03e1", + "sha256:b93aa30099fa6860d4727f1b81f8718e965bb96253fa190fab2077aaad6d15d3" + ], + "index": "pypi", + "version": "==0.7.0" + }, "markupsafe": { "hashes": [ "sha256:0576fe974b40a400449768941d5d0858cc624e3249dfd1e0c33674e5c7ca7aed", diff --git a/README.md b/README.md index 876a323..94486f0 100644 --- a/README.md +++ b/README.md @@ -7,8 +7,8 @@ - [X] Create a base template for the header and footer - [X] Build index page - [X] Build room pages - - [x] T2 - - [x] T3 + - [x] T2 + - [x] T3 - [X] Build contact pages - [X] Use netlify form for the contact form - [ ] Deploy to VillaFleurie's domain @@ -18,8 +18,14 @@ - [ ] Automate the file search - [ ] Extract data out of the template - [ ] Create a template for the rooms +- [ ] Build script before commit ## Excluded * The article page * The language switcher + +## Built with + +- [Jinja](https://jinja.palletsprojects.com/en/3.1.x/) - fast, expressive, extensible templating + engine \ No newline at end of file diff --git a/config.json b/config.json new file mode 100644 index 0000000..09a1c76 --- /dev/null +++ b/config.json @@ -0,0 +1,8 @@ +{ + "templates": [ + "index.html", + "t2-corail.html", + "t3-azur.html", + "contact.html" + ] +} \ No newline at end of file diff --git a/dist/contact.html b/dist/contact.html index 2ac7934..c47ff2e 100644 --- a/dist/contact.html +++ b/dist/contact.html @@ -33,7 +33,7 @@ - + diff --git a/dist/index.html b/dist/index.html index 796a683..b3abfab 100644 --- a/dist/index.html +++ b/dist/index.html @@ -33,7 +33,7 @@ - + diff --git a/dist/t2-corail.html b/dist/t2-corail.html index ca2d102..a1b09ae 100644 --- a/dist/t2-corail.html +++ b/dist/t2-corail.html @@ -33,7 +33,7 @@ - + diff --git a/dist/t3-azur.html b/dist/t3-azur.html index 9211395..2442029 100644 --- a/dist/t3-azur.html +++ b/dist/t3-azur.html @@ -33,7 +33,7 @@ - + diff --git a/lib/__init__.py b/lib/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/lib/config.py b/lib/config.py new file mode 100644 index 0000000..827fc4f --- /dev/null +++ b/lib/config.py @@ -0,0 +1,7 @@ +import json + + +def load_templates(): + with open("config.json", "r") as f: + res = json.loads(f.read()) + return res["templates"] diff --git a/lib/engine.py b/lib/engine.py new file mode 100644 index 0000000..941c125 --- /dev/null +++ b/lib/engine.py @@ -0,0 +1,10 @@ +from jinja2 import Environment, FileSystemLoader, select_autoescape + +engine = Environment( + loader=FileSystemLoader("templates"), + autoescape=select_autoescape(), +) + + +def render(template: str) -> str: + return engine.get_template(template).render() diff --git a/lib/main.py b/lib/main.py new file mode 100644 index 0000000..0c39dd1 --- /dev/null +++ b/lib/main.py @@ -0,0 +1,19 @@ +from os import path + +from loguru import logger + +from lib.config import load_templates +from lib.engine import render + + +def main(): + logger.info("🏁 Start building site") + for template in load_templates(): + logger.info(f"📃Render '{template}'") + with open(path.join("dist", template), "w") as f: + f.write(render(template)) + logger.info("🎉 Done…") + + +if __name__ == "__main__": + main() diff --git a/main.py b/main.py deleted file mode 100644 index bcf789d..0000000 --- a/main.py +++ /dev/null @@ -1,22 +0,0 @@ -from os import path - -from jinja2 import Environment, FileSystemLoader, select_autoescape - -engine = Environment( - loader=FileSystemLoader("templates"), - autoescape=select_autoescape(), -) - - -def render(template: str) -> str: - return engine.get_template(template).render() - - -# TODO: from a config -templates = ["index.html", "t2-corail.html", "t3-azur.html", "contact.html"] - - -for template in templates: - print(f"render '{template}'") - with open(path.join("dist", template), "w") as f: - f.write(render(template)) diff --git a/templates/layout.html b/templates/layout.html index a0de994..46193bb 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -33,7 +33,7 @@ - +