code refactor

This commit is contained in:
Ruidy 2023-05-28 12:18:05 +02:00
parent e89d204dd0
commit 0d4a7b43ec
15 changed files with 69 additions and 32 deletions

View file

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

View file

@ -5,6 +5,7 @@ name = "pypi"
[packages]
jinja2 = "*"
loguru = "*"
[dev-packages]
black = {extras = ["d"], version = "*"}

10
Pipfile.lock generated
View file

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

View file

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

8
config.json Normal file
View file

@ -0,0 +1,8 @@
{
"templates": [
"index.html",
"t2-corail.html",
"t3-azur.html",
"contact.html"
]
}

2
dist/contact.html vendored
View file

@ -33,7 +33,7 @@
<link href="../css/styles.css" rel="stylesheet">
<!-- Favicon -->
<link rel="icon" href="../images/favicon.png">
<link rel="icon" href="../images/logo.png">
</head>
<body data-spy="scroll" data-target=".fixed-top">

2
dist/index.html vendored
View file

@ -33,7 +33,7 @@
<link href="../css/styles.css" rel="stylesheet">
<!-- Favicon -->
<link rel="icon" href="../images/favicon.png">
<link rel="icon" href="../images/logo.png">
</head>
<body data-spy="scroll" data-target=".fixed-top">

2
dist/t2-corail.html vendored
View file

@ -33,7 +33,7 @@
<link href="../css/styles.css" rel="stylesheet">
<!-- Favicon -->
<link rel="icon" href="../images/favicon.png">
<link rel="icon" href="../images/logo.png">
</head>
<body data-spy="scroll" data-target=".fixed-top">

2
dist/t3-azur.html vendored
View file

@ -33,7 +33,7 @@
<link href="../css/styles.css" rel="stylesheet">
<!-- Favicon -->
<link rel="icon" href="../images/favicon.png">
<link rel="icon" href="../images/logo.png">
</head>
<body data-spy="scroll" data-target=".fixed-top">

0
lib/__init__.py Normal file
View file

7
lib/config.py Normal file
View file

@ -0,0 +1,7 @@
import json
def load_templates():
with open("config.json", "r") as f:
res = json.loads(f.read())
return res["templates"]

10
lib/engine.py Normal file
View file

@ -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()

19
lib/main.py Normal file
View file

@ -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()

22
main.py
View file

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

View file

@ -33,7 +33,7 @@
<link href="../css/styles.css" rel="stylesheet">
<!-- Favicon -->
<link rel="icon" href="../images/favicon.png">
<link rel="icon" href="../images/logo.png">
</head>
<body data-spy="scroll" data-target=".fixed-top">