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:
Yax 2026-01-10 16:03:08 +01:00
parent 199911aaae
commit 3b7484fbe6
9 changed files with 94 additions and 44 deletions

7
.env Normal file
View 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
View 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
View 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

View file

@ -3,9 +3,13 @@
# declare phony targets
.PHONY: build test typecheck
# run locally
# run locally (uses --local-stacosys if stacosys is running on port 8100)
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
# run type checks

View file

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

View file

@ -2,19 +2,15 @@
"""Make static website/blog with Python."""
import argparse
import datetime
import json
import locale
import os
import re
import shutil
import sys
import time
import unicodedata
from email import utils
from pathlib import Path
from dotenv import load_dotenv
import requests
import mistune
from pygments import highlight
@ -701,28 +697,36 @@ def generate_sitemap(posts, params):
)
def get_params(param_file):
"""Load site parameters from JSON file with defaults.
def get_params(env_file=None):
"""Load site parameters from .env files.
Args:
param_file: Path to JSON parameters file
env_file: Optional .env file to load and override .env values
Returns:
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 = {
"title": "Blog",
"subtitle": "Lorem Ipsum",
"author": "Admin",
"site_url": "http://localhost:8000",
"title": os.getenv("TITLE", "Blog"),
"subtitle": os.getenv("SUBTITLE", "Lorem Ipsum"),
"author": os.getenv("AUTHOR", "Admin"),
"site_url": os.getenv("SITE_URL", "http://localhost:8000"),
"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
@ -733,14 +737,14 @@ def rebuild_site_directory():
shutil.copytree("static", "_site")
def main(param_file):
def main(env_file=None):
"""Main entry point for static site generation.
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.
rebuild_site_directory()
@ -769,8 +773,10 @@ def main(param_file):
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Makesite')
parser.add_argument('--params', dest='param_file', type=str,
default="params.json", help='Custom param file')
args = parser.parse_args()
main(args.param_file)
# Determine which env file to use
env_file = None
if "--local-stacosys" in sys.argv:
env_file = ".env.local-stacosys"
elif "--local" in sys.argv:
env_file = ".env.local"
main(env_file)

View 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"
}

View file

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

View file

@ -10,6 +10,7 @@ requires-python = ">=3.12.9"
dependencies = [
"mistune>=3.0.2",
"pygments>=2.18.0",
"python-dotenv>=1.0.0",
"requests>=2.32.3",
]