3 # This GNUmakefile searches the `sources/` directory for files with an extension of `.atom` or `.xml` and applies `transform.xslt` to them, outputting the result in one of the following locations :—
 
   5 # • For files with a location of `sources/index.xml` or `sources/index-*.xml`, the transformed file will be written to `public/%.html` (where `%` is the filename).
 
   7 # • For all other files with a location of `sources/*.xml` or `sources/*/*.xml`, the transformed file will be written to `public/%/index.html` (where `%` is the filename and subdirectory if applicable).
 
   8 # Any files in a corresponding sibling directory (i·e without the `.xml`) are copied over verbatim.
 
   9 # Only one level of subdirectory is supported.
 
  11 # • For files with a location of `sources/*.atom` or `sources/*/*.atom`, the transformed file will be written to `public/%.atom` (where `%` is the filename and subdirectory if applicable).
 
  13 # By default, running `make` will do this for all applicable source files.
 
  17 # © 2022–2023 Lady [@ Lady’s Computer]
 
  19 # This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
 
  20 # If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
 
  22 BASEIRI = http://example.com
 
  23 DATETIME = $(shell date -Iseconds)
 
  25 FOOTER_SUFFIX = -footer.xml
 
  26 HEADER_SUFFIX = -header.xml
 
  30 TEMPLATE = template.xml
 
  31 TRANSFORM = transform.xslt
 
  37 headers := $(wildcard *$(HEADER_SUFFIX))
 
  38 footers := $(wildcard *$(FOOTER_SUFFIX))
 
  39 override prerequisites := $(YIELD_DEPENDENCIES) $(TEMPLATE) $(TRANSFORM) $(headers) $(footers)
 
  41 override indexsources := $(wildcard $(SOURCE_DIR)/index$(SOURCE_EXT) $(SOURCE_DIR)/index-*$(SOURCE_EXT))
 
  42 override indices := $(patsubst $(SOURCE_DIR)/%$(SOURCE_EXT),$(OUTPUT_DIR)/%.html,$(indexsources))
 
  44 override pagesources := $(filter-out $(indexsources),$(wildcard $(SOURCE_DIR)/*$(SOURCE_EXT) $(SOURCE_DIR)/*/*$(SOURCE_EXT)))
 
  45 override pages := $(patsubst $(SOURCE_DIR)/%$(SOURCE_EXT),$(OUTPUT_DIR)/%/index.html,$(pagesources))
 
  47 override resourcesources := $(wildcard $(addsuffix /*,$(basename $(pagesources))))
 
  48 override resources := $(patsubst $(SOURCE_DIR)/%,$(OUTPUT_DIR)/%,$(resourcesources))
 
  50 override feedsources := $(filter-out $(resourcesources),$(wildcard $(SOURCE_DIR)/*$(FEED_EXT) $(SOURCE_DIR)/*/*$(FEED_EXT)))
 
  51 override feeds := $(patsubst $(SOURCE_DIR)/%$(FEED_EXT),$(OUTPUT_DIR)/%.atom,$(feedsources))
 
  53 override content := $(indices) $(pages)
 
  55 # This function does the following :—
 
  57 # • `$(YIELD)`s `$(1)`.
 
  59 # • Calls `$(XSLT)` using `$(TRANSFORM)` with the result, providing `$(BASEIRI)` and `$(DATETIME) as params and providing `$(2)` (minus the initial `$(OUTPUT_DIR)/`) as the param `OUTPUTPATH`.
 
  61 # • Removes any `xmlns` prefix declarations from output to `.html` files (with `sed`).
 
  63 # • Removes any doctype for root elements other than `html` (with `grep -v`).
 
  65 # • Saves the output to `$(2)`.
 
  66 override makexslt = $(YIELD) $(1)\
 
  67         | $(XSLT) --nonet --novalid $(XSLTOPTS) --stringparam BASEIRI "$(BASEIRI)" --stringparam DATETIME "$(DATETIME)" --stringparam OUTPUTPATH "$(patsubst $(OUTPUT_DIR)/%,/%,$(2))" $(TRANSFORM) -\
 
  68         $(if $(filter %.html,$(2)),| sed 's/ xmlns:[0-9A-Za-z_-]*="[^"]*"//g',)\
 
  69         | grep -v '^<!DOCTYPE \([^h]\|.[^t]\|..[^m]\|...[^l]\|....[^ >]\)'\
 
  72 all: $(content) $(resources) $(feeds);
 
  74 $(indices): $(OUTPUT_DIR)/%.html: $(SOURCE_DIR)/%$(SOURCE_EXT) $(prerequisites)
 
  75         @echo "Generating $@…"
 
  77         @$(call makexslt,$<,$@)
 
  79 $(pages): $(OUTPUT_DIR)/%/index.html: $(SOURCE_DIR)/%$(SOURCE_EXT) $(prerequisites)
 
  80         @echo "Generating $@…"
 
  82         @$(call makexslt,$<,$@)
 
  84 $(resources): $(OUTPUT_DIR)/%: $(SOURCE_DIR)/%
 
  85         @echo "Copying over $@…"
 
  89 $(feeds): $(OUTPUT_DIR)/%.atom: $(SOURCE_DIR)/%$(FEED_EXT) $(TRANSFORM)
 
  90         @echo "Generating $@…"
 
  92         @$(call makexslt,$<,$@)