Replace JSON config with .env files
- Add python-dotenv dependency for environment variable loading - Replace params.json and params-local.json with .env files - Add --local and --local-stacosys flags to makesite.py - Update Makefile to auto-detect stacosys availability - Document configuration and usage in README.md
This commit is contained in:
parent
199911aaae
commit
3b7484fbe6
9 changed files with 94 additions and 44 deletions
7
.env
Normal file
7
.env
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
# Site configuration (production)
|
||||||
|
TITLE=Le blog du Yax
|
||||||
|
SUBTITLE=GNU, Linux, BSD et autres libertés
|
||||||
|
AUTHOR=Yax
|
||||||
|
SITE_URL=https://blogduyax.madyanne.fr
|
||||||
|
STACOSYS_URL=http://stacosys:8100/api
|
||||||
|
EXTERNAL_CHECK=./check_git.sh
|
||||||
7
.env.local
Normal file
7
.env.local
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
# Site configuration (local development)
|
||||||
|
TITLE=Le blog du Yax
|
||||||
|
SUBTITLE=GNU, Linux, BSD et autres libertés
|
||||||
|
AUTHOR=Yax
|
||||||
|
SITE_URL=http://127.0.0.1:8000
|
||||||
|
STACOSYS_URL=
|
||||||
|
EXTERNAL_CHECK=./check_git.sh
|
||||||
7
.env.local-stacosys
Normal file
7
.env.local-stacosys
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
# Site configuration (local development with local Stacosys)
|
||||||
|
TITLE=Le blog du Yax
|
||||||
|
SUBTITLE=GNU, Linux, BSD et autres libertés
|
||||||
|
AUTHOR=Yax
|
||||||
|
SITE_URL=http://127.0.0.1:8000
|
||||||
|
STACOSYS_URL=http://127.0.0.1:8100/api
|
||||||
|
EXTERNAL_CHECK=./check_git.sh
|
||||||
8
Makefile
8
Makefile
|
|
@ -3,9 +3,13 @@
|
||||||
# declare phony targets
|
# declare phony targets
|
||||||
.PHONY: build test typecheck
|
.PHONY: build test typecheck
|
||||||
|
|
||||||
# run locally
|
# run locally (uses --local-stacosys if stacosys is running on port 8100)
|
||||||
site:
|
site:
|
||||||
uv run python makesite.py --params params-local.json
|
@if curl -s --connect-timeout 1 http://127.0.0.1:8100/api > /dev/null 2>&1; then \
|
||||||
|
uv run python makesite.py --local-stacosys; \
|
||||||
|
else \
|
||||||
|
uv run python makesite.py --local; \
|
||||||
|
fi
|
||||||
cd _site && python -m SimpleHTTPServer 2> /dev/null || python3 -m http.server
|
cd _site && python -m SimpleHTTPServer 2> /dev/null || python3 -m http.server
|
||||||
|
|
||||||
# run type checks
|
# run type checks
|
||||||
|
|
|
||||||
35
README.md
35
README.md
|
|
@ -7,3 +7,38 @@ This static blog generator code is under MIT license.
|
||||||
|
|
||||||
"Blog du Yax" content is under [CC-BY-NC-SA](https://creativecommons.org/licenses/by-nc-sa/3.0)
|
"Blog du Yax" content is under [CC-BY-NC-SA](https://creativecommons.org/licenses/by-nc-sa/3.0)
|
||||||
|
|
||||||
|
## Configuration
|
||||||
|
|
||||||
|
Configuration is managed through `.env` files:
|
||||||
|
|
||||||
|
| File | Description |
|
||||||
|
|------|-------------|
|
||||||
|
| `.env` | Production configuration |
|
||||||
|
| `.env.local` | Local development (no Stacosys) |
|
||||||
|
| `.env.local-stacosys` | Local development with local Stacosys |
|
||||||
|
|
||||||
|
Environment variables:
|
||||||
|
|
||||||
|
- `TITLE` - Site title
|
||||||
|
- `SUBTITLE` - Site subtitle
|
||||||
|
- `AUTHOR` - Author name
|
||||||
|
- `SITE_URL` - Base URL for the site
|
||||||
|
- `STACOSYS_URL` - Stacosys API endpoint (optional)
|
||||||
|
- `EXTERNAL_CHECK` - Script for git change detection
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Production build (uses .env)
|
||||||
|
uv run python makesite.py
|
||||||
|
|
||||||
|
# Local development without Stacosys (uses .env.local)
|
||||||
|
uv run python makesite.py --local
|
||||||
|
|
||||||
|
# Local development with local Stacosys (uses .env.local-stacosys)
|
||||||
|
uv run python makesite.py --local-stacosys
|
||||||
|
|
||||||
|
# Or use make for local development
|
||||||
|
make site
|
||||||
|
```
|
||||||
|
|
||||||
|
|
|
||||||
56
makesite.py
56
makesite.py
|
|
@ -2,19 +2,15 @@
|
||||||
|
|
||||||
"""Make static website/blog with Python."""
|
"""Make static website/blog with Python."""
|
||||||
|
|
||||||
import argparse
|
|
||||||
import datetime
|
import datetime
|
||||||
import json
|
|
||||||
import locale
|
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import shutil
|
import shutil
|
||||||
import sys
|
import sys
|
||||||
import time
|
|
||||||
import unicodedata
|
import unicodedata
|
||||||
from email import utils
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
from dotenv import load_dotenv
|
||||||
import requests
|
import requests
|
||||||
import mistune
|
import mistune
|
||||||
from pygments import highlight
|
from pygments import highlight
|
||||||
|
|
@ -701,28 +697,36 @@ def generate_sitemap(posts, params):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def get_params(param_file):
|
def get_params(env_file=None):
|
||||||
"""Load site parameters from JSON file with defaults.
|
"""Load site parameters from .env files.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
param_file: Path to JSON parameters file
|
env_file: Optional .env file to load and override .env values
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
dict: Site parameters with defaults and loaded values
|
dict: Site parameters with defaults and loaded values
|
||||||
"""
|
"""
|
||||||
# Default parameters.
|
# Load .env file first
|
||||||
|
load_dotenv(".env")
|
||||||
|
|
||||||
|
# Load override file if specified
|
||||||
|
if env_file:
|
||||||
|
load_dotenv(env_file, override=True)
|
||||||
|
log("use params from " + env_file)
|
||||||
|
else:
|
||||||
|
log("use params from .env")
|
||||||
|
|
||||||
|
# Build params from environment variables
|
||||||
params = {
|
params = {
|
||||||
"title": "Blog",
|
"title": os.getenv("TITLE", "Blog"),
|
||||||
"subtitle": "Lorem Ipsum",
|
"subtitle": os.getenv("SUBTITLE", "Lorem Ipsum"),
|
||||||
"author": "Admin",
|
"author": os.getenv("AUTHOR", "Admin"),
|
||||||
"site_url": "http://localhost:8000",
|
"site_url": os.getenv("SITE_URL", "http://localhost:8000"),
|
||||||
"current_year": datetime.datetime.now().year,
|
"current_year": datetime.datetime.now().year,
|
||||||
"stacosys_url": "",
|
"stacosys_url": os.getenv("STACOSYS_URL", ""),
|
||||||
|
"external_check": os.getenv("EXTERNAL_CHECK", ""),
|
||||||
}
|
}
|
||||||
|
|
||||||
log("use params from " + param_file)
|
|
||||||
if os.path.isfile(param_file):
|
|
||||||
params.update(json.loads(fread(param_file)))
|
|
||||||
return params
|
return params
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -733,14 +737,14 @@ def rebuild_site_directory():
|
||||||
shutil.copytree("static", "_site")
|
shutil.copytree("static", "_site")
|
||||||
|
|
||||||
|
|
||||||
def main(param_file):
|
def main(env_file=None):
|
||||||
"""Main entry point for static site generation.
|
"""Main entry point for static site generation.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
param_file: Path to JSON parameters file
|
env_file: Optional .env file to override .env values
|
||||||
"""
|
"""
|
||||||
|
|
||||||
params = get_params(param_file)
|
params = get_params(env_file)
|
||||||
|
|
||||||
# Create a new _site directory from scratch.
|
# Create a new _site directory from scratch.
|
||||||
rebuild_site_directory()
|
rebuild_site_directory()
|
||||||
|
|
@ -769,8 +773,10 @@ def main(param_file):
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
parser = argparse.ArgumentParser(description='Makesite')
|
# Determine which env file to use
|
||||||
parser.add_argument('--params', dest='param_file', type=str,
|
env_file = None
|
||||||
default="params.json", help='Custom param file')
|
if "--local-stacosys" in sys.argv:
|
||||||
args = parser.parse_args()
|
env_file = ".env.local-stacosys"
|
||||||
main(args.param_file)
|
elif "--local" in sys.argv:
|
||||||
|
env_file = ".env.local"
|
||||||
|
main(env_file)
|
||||||
|
|
|
||||||
|
|
@ -1,9 +0,0 @@
|
||||||
{
|
|
||||||
"title": "Le blog du Yax",
|
|
||||||
"subtitle": "GNU, Linux, BSD et autres libertés",
|
|
||||||
"author": "Yax",
|
|
||||||
"site_url": "http://127.0.0.1:8000",
|
|
||||||
"stacosys_url": "http://127.0.0.1:8100/api",
|
|
||||||
"stacosys_url": "",
|
|
||||||
"external_check": "./check_git.sh"
|
|
||||||
}
|
|
||||||
|
|
@ -1,8 +0,0 @@
|
||||||
{
|
|
||||||
"title": "Le blog du Yax",
|
|
||||||
"subtitle": "GNU, Linux, BSD et autres libertés",
|
|
||||||
"author": "Yax",
|
|
||||||
"site_url": "https://blogduyax.madyanne.fr",
|
|
||||||
"stacosys_url": "http://stacosys:8100/api",
|
|
||||||
"external_check": "./check_git.sh"
|
|
||||||
}
|
|
||||||
|
|
@ -10,6 +10,7 @@ requires-python = ">=3.12.9"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"mistune>=3.0.2",
|
"mistune>=3.0.2",
|
||||||
"pygments>=2.18.0",
|
"pygments>=2.18.0",
|
||||||
|
"python-dotenv>=1.0.0",
|
||||||
"requests>=2.32.3",
|
"requests>=2.32.3",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue