#!/usr/bin/python3 """ =========================================================================================== genhtml.py - static HTLM inserts Author and copyright: M. Lutz, 2015 License: provided freely but with no warranties of any kind. VERSION 2, November 17, 2015: smarter dependency checking Don't regenerate an HTML file for a changed insert file, unless the HTML's template actually USES the insert file, or an insert file that inserts it. Also refactor as functions - at ~250 lines, top-level script code becomes too scattered to read (and at ~1K lines, class structure is nearly required). SYNOPSIS Simple static HTML inserts: given an HTML templates dir and an HTML inserts dir, generate final HTML files by applying text replacements to the templates, where replacement keys and text correspond to insert file names and contents. For static insert content, this script is an alternative to: - Mass file edits on every common-item change - which can be painfully tedious - Client-side includes via embedded JavaScript - which not all visitors may run - Server-side includes via PHP - which makes pages unviewable without a server This script adds a local admin step, as it must be run on every HTML content change, but there is no HTML include; doesn't work on all browsers. COMMAND-LINE USAGE % gensite.py (or run via icon click) => Copy all changed non-HTML files in SOURCEDIR to TARGETDIR (if any) => Regenerate all HTML files whose SOURCEDIR template or any inserts it uses have changed since the HTML's last generation, per dependency tests ahead % gensite.py [file.htm]+ Same, but apply to just one or more SOURCEDIR files, listed without dir name USAGE PATTERNS To use this script to maintain a site's files: 1) Change HTML template files in SOURCEDIR, and/or insert files in INSERTSDIR. 2) Run this script to regenerate all changed HTML files in TARGETDIR as needed. 3) Upload newly-generated files (or all) from the TARGETDIR to the web server. Do not change HTML files in TARGETDIR: they may be overwritten by generations! There are two ways to structure a site's files: 1) Keep both HTML templates and all other site files in SOURCEDIR. In this mode, changed non-HTML files are copied to TARGETDIR when HTML files are regenerated. 2) Use SOURCEDIR for HTML template files only, keep other web site files in TARGETDIR. This mode avoids copying other non-HTML files to TARGETDIR on HTML regenerations. Either way, TARGETDIR is always the complete web site, for viewing and uploads. TEXT REPLACEMENTS Text replacements are variable, and derived from insert folder content: - Replacement keys are '$XXX$' for all INSERTDIR/XXX.txt filenames. - Replacement values are the contents of the INSERTDIR/XXX.txt files. - Algorithm: For each changed '*.htm' and '*.html' (caseless) HTML template in SOURCEDIR: For each XXX in INSERTDIR/XXX.txt: Replace all '$XXX$' in HTML template with the content of file XXX.txt Save result in TARGETDIR Other changed non-HTML files (if any) are copied to TARGETDIR verbatim. To automate changing DATES in both HTML files and insert files, the script also replaces special non-file '$_DATE*$' keys: e.g., '$_DATELONG$' => 'November 6, 2015'. See the script's code ahead for the full set of date keys available. Example key=>file replacements (with possible nested inserts, described ahead): Coded in $STYLE$ => INSERTDIR/STYLE.html (a