#!/usr/bin/python3 """ =========================================================================================== genhtml.py - static HTML inserts Version: 2.7, March 2022 (see VERSIONS ahead) License: provided freely but with no warranties of any kind Attribution: © M. Lutz (learning-python.com), 2015-2022 ------------------------------------------------------------------------------------------ SYNOPSIS ------------------------------------------------------------------------------------------ genhtml builds HTML files by performing key-based text insertions on all the files in a folder (dir) at site-build time. Given an HTML templates dir and an HTML inserts dir, it generates final HTML files by applying text replacements to the templates, where replacement keys and text correspond to insert file names and contents. The text which replaces keys in the generated files can be any textual component: HTML, CSS, JavaScript, and so on. For static insert content (common text that can change over time, but need not be generated on each new page request), the net result provides a basic webpage macro utility, and 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 or Apache - which require a server to view pages - cpp - which may not be present, plus makefiles - which must be manually coded This script adds a local admin step, as it must be run on every HTML content change (e.g., from a publishing script), but there is no direct HTML include. ------------------------------------------------------------------------------------------ COMMAND-LINE USAGE ------------------------------------------------------------------------------------------ Given the SOURCEDIR, TARGETDIR, and INSERTDIR settings in this script's code: % python3 genhtml.py (or run via icon or shortcut click) 1) Copy all changed non-HTML files in SOURCEDIR to TARGETDIR (if any) 2) Regenerate all HTML files whose SOURCEDIR template or any INSERTDIR insert file it uses has changed since the HTML's latest generation, replacing all insert-file references with insert-file text, and storing the expanded HTML files in TARGETDIR % python3 genhtml.py [filename]+ Same, but apply to just one or more SOURCEDIR files, listed without dir name ------------------------------------------------------------------------------------------ USAGE PATTERNS ------------------------------------------------------------------------------------------ As shipped, SOURCEDIR, INSERTDIR, and TARGETDIR all are in the script's current working directory (a.k.a. '.', from where the script is run); change as desired. 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: A) 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. B) 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 flexible, 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 any and all '$XXX$' in HTML template with the content of file INSERTDIR/XXX.txt Save the 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.txt (a