diff --git a/.env b/.env new file mode 100644 index 0000000..0559a6b --- /dev/null +++ b/.env @@ -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 diff --git a/.env.local b/.env.local new file mode 100644 index 0000000..33546b8 --- /dev/null +++ b/.env.local @@ -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 diff --git a/.env.local-stacosys b/.env.local-stacosys new file mode 100644 index 0000000..df29a15 --- /dev/null +++ b/.env.local-stacosys @@ -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 diff --git a/Makefile b/Makefile index 99068b9..205412b 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/README.md b/README.md index 37796d4..69d0252 100755 --- a/README.md +++ b/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) +## 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 +``` + diff --git a/makesite.py b/makesite.py index 469b90b..63f90c8 100755 --- a/makesite.py +++ b/makesite.py @@ -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) diff --git a/params-local.json b/params-local.json deleted file mode 100644 index cbc8291..0000000 --- a/params-local.json +++ /dev/null @@ -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" -} diff --git a/params.json b/params.json deleted file mode 100644 index a59d1f8..0000000 --- a/params.json +++ /dev/null @@ -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" -} diff --git a/pyproject.toml b/pyproject.toml index 8c14c0a..d36c24c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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", ]