mako/index.mk

158 lines
4.5 KiB
Makefile
Raw Normal View History

2023-09-14 13:39:45 -04:00
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-#
# index.mk
#
#> Index Gemini site content
#>
#> This Makefile builds content indicies for a Gemini site.
#>
#
# Allowing posts to be m4 files introduces complexity without clear benefit.
# Thus we are
2023-09-14 13:39:45 -04:00
#
# Strategy:
# 1. Mark all *.gmi files as posts.
# 2. Limit macros to:
# a. Section index
# b. Tagged index
# c. Header
# d. Footer
# 4. Create index entries for each *.gmi file.
# 5. Build the full list of tags.
# 6. Generate the post as `cat footer.gmi post.gmi footer.gmi`.
# 7. Build the tagged index files from the list of tags.
# 8. Build index.gmi from tag list.
#
# The key challenge is step 7. We do not know how to build this until we
# completed step 5. Two possible approaches:
# 1. Create a macro
# 2. Recurse into another Makefile.
#
# © 2023 Andrew Stryker <axs@sdf.org>
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-#
#-----------------------------------------------------------------------------#
#
# Configuration
#
#-----------------------------------------------------------------------------#
# Record the name of this Makefile
self ::= $(lastword ${MAKEFILE_LIST})
# Load environment variables if needed--not the case when this is called from
# the main Makefile
ifndef ENV_LOADED
# Assume the base directory is two levels up if not defined
MAKO_DIR ?= ../..
include ${MAKO_DIR}/environment.mk
endif
# Define using the same definition as in the main Makefile
build_date_msg ?= This page was built on $$(date).
# Define special files
index_template ::= index.gmi.m4
index ::= ${STG_SECTION}/index.gmi
2023-09-14 13:39:45 -04:00
header_template ::= header.gmi.m4
header ::= ${WRK_SECTION}/header.gmi
2023-09-14 13:39:45 -04:00
footer_template ::= footer.gmi.m4
footer ::= ${WRK_SECTION}/footer.gmi
2023-09-14 13:39:45 -04:00
2023-09-14 14:29:45 -04:00
# Capture all posts as Gemtext files that begin with an ISO formatted date
posts_gmi ::= $(shell ls *.gmi | grep "[[:digit:]]\{4\}\(-[[:digit:]]\{2\}\)\{2\}")
posts ::= $(addprefix ${STG_SECTION}/, ${posts_gmi})
entries ::= $(addprefix ${WRK_SECTION}/, ${posts_gmi:.gmi=.lnk})
2023-09-14 13:39:45 -04:00
TAG_LIST ::= ${WRK_SECTION}/tag-list
export TAG_LIST
2023-09-14 13:39:45 -04:00
# Create list of potential dependencies of *.gmi.m4 templates
template_depends ::= $(notdir $(filter-out %.gmi.m4 _% %~, $(wildcard *)))
2023-09-14 13:39:45 -04:00
#-----------------------------------------------------------------------------#
#
# User interface
#
#> This makefile supports the following targets:
#>
#-----------------------------------------------------------------------------#
.PHONY: default build clean show help create
# define the default target explicitly
default: create
create: #> Create a new post (default)
@if [ -z $${EDITOR} ]; then \
python3 create-post.py; \
else \
python3 create-post.py --edit; \
fi
2023-09-20 12:06:49 -04:00
build: ${index} #> Build the site (default)
2023-09-14 13:39:45 -04:00
@echo "✓ Completed processing ${content_section}"
@echo
show: #> Show enironment variables with values
@echo staging area: ${STAGING}
@echo workspace: ${WORKSPACE}
@echo post_index: ${post_index}
@echo ${build_date_msg}
@echo targets: ${post_targets}
@echo header: ${header}
@echo footer: ${footer}
@echo tag list: ${TAG_LIST}
2023-09-14 13:39:45 -04:00
clean: #> Delete generated files
@rm -rf ${STAGING} ${WORKSPACE}
@echo "\t✓ Deleted intermediate files"
@echo "\t✓ Deleted all posts in ${STAGING}"
help: #> Display this help message
@awk -f ${AWKHELP} ${self}
2023-09-14 13:39:45 -04:00
#-----------------------------------------------------------------------------#
#
# File system interface
#
#-----------------------------------------------------------------------------#
${WRK_SECTION} ${STG_SECTION}: %:
2023-09-14 13:39:45 -04:00
@mkdir -p $@
@echo "\t✓ Created space: $@"
${header} ${footer}: ${WRK_SECTION}/%: % ${WRK_SECTION} ${template_depends}
2023-09-14 14:29:45 -04:00
@m4 --include=${MAKO_DIR} $< > $@
@echo "\t✓ Created: $@"
${posts}: ${STG_SECTION}/%: % ${header} ${footer} ${STG_SECTION}
@cat ${header} $< ${footer} > $@
2023-09-14 14:29:45 -04:00
@echo "${build_date_msg}" >> $@
@echo "\t✓ Created $@"
${entries}: ${WRK_SECTION}/%.lnk: ${MAKO_DIR}/create-index-entry.awk %.gmi ${WRK_SECTION}
2023-09-14 14:29:45 -04:00
@awk -f $(notdir $^) > $@
2023-09-14 13:39:45 -04:00
@echo "\t✓ Created: $@"
${TAG_LIST}: ${entries}
2023-09-14 14:29:45 -04:00
@cat $^ | sort --unique | \
sed -e '/^---/ d; s/^\([a-zA-Z0-09]\+\).*/=> \1.gmi \1/' > $@
@echo "\t✓ Created the tags list"
${index}: ${index_template} ${header} ${footer} ${posts} ${TAG_LIST}
@${MAKE} -f ${TAG_MAKE} build
2023-09-14 14:29:45 -04:00
@m4 --include=.. \
--define=TAGS=${tags_list} \
--define=POSTS=${post_entry} \
--define=HEADER=${header} \
--define=FOOTER=${footer} \
$< > $@
@echo "✓ Created: $@"
2023-09-14 13:39:45 -04:00
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-#