MARKDOWN = "cmark"

################################################################################
# Files to render
MD_FILES := $(shell find . -name "*.md")
DOT_FILES := $(shell find . -name "*.dot")
DOT_2X_FILES = $(subst .dot,@2x.png,$(DOT_FILES))
IMG_2X_FILES := $(shell find . -name "*@2x.*") $(DOT_2X_FILES)

################################################################################
# Output files
HTML_FILES := $(MD_FILES:.md=.html)
IMG_FILES := $(subst @2x,,$(IMG_2X_FILES))
ATOM := atom.xml

all: $(HTML_FILES) $(IMG_FILES) $(DOT_2X_FILES) $(ATOM) index.html

################################################################################
# HTML templates
HTML_TEMPLATES := $(shell find templates -name "*.html")
PROJECT_TEMPLATE = $(shell cat templates/project.html)
BLOG_TEMPLATE = $(shell cat templates/blog.html)
HTML_HEADER = templates/header.html
HTML_FOOTER = templates/footer.html

PROJECT_REGEX := {{PROJECT \(.*\);\(.*\);\(.*\);.*\([0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}\)}}
BLOG_REGEX := {{BLOG \(.*\);\(.*\);.*\([0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}\)}}
FEED_REGEX := \([0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}\);\(.*\);\(.*\);\(.*\)

################################################################################
# Atom feed templates
ATOM_HEADER = templates/header.xml
ATOM_TEMPLATES := $(shell find templates -name "*.xml")
ATOM_TEMPLATE = $(shell cat templates/item.xml)
ATOM_NOW := $(shell date '+%FT%TZ')

################################################################################
# Page rendering
%.html: PAGE_TITLE = $(shell sed -n 's/^##* //gp' $<| grep -v blog | head -n 1 | sed 's/\&/\\\&/g' | sed "s/'/'\\\''/g" | sed 's/:/ -/g')
%.html: HEADER_TITLE = $(shell sed -n 's/^#[^#]//gp' $< | head -n 1 | sed 's/\&/\\\&/g' | sed "s/'/'\\\''/g")
%.html: %.md $(HTML_TEMPLATES) Makefile
	@echo ".md -> .html  [$@]"
	@# Insert title into page header
	@sed 's:{{PAGE_TITLE}}:$(PAGE_TITLE):g' $(HTML_HEADER) | sed 's:{{HEADER_TITLE}}:${HEADER_TITLE}:g' > $@
	@# Preprocess markdown file, applying templates
	@sed "s:$(PROJECT_REGEX):${PROJECT_TEMPLATE}:g" $< > $<.1
	@sed "s:$(BLOG_REGEX):${BLOG_TEMPLATE}:g" $<.1 > $<.2
	@# Render .md file to .html, skipping the first line
	@# (which is assumed to be the title)
	@tail -n +2 $<.2 | $(MARKDOWN) --unsafe >> $@
	@rm $<.1 $<.2
	@cat $(HTML_FOOTER) >> $@

index.html: home.html templates/footer.html
	@echo "$< -> $@"
	@cat $< > $@
	@cat $(HTML_FOOTER) >> $@

################################################################################
# Image rendering
%.png: %@2x.png
	@echo "@2x.png -> .png  [$@]"
	@convert $< -resize 50% $@
%.jpg: %@2x.jpg
	@echo "@2x.jpg -> .jpg  [$@]"
	@convert $< -resize 50% $@
%.gif: %@2x.gif
	@echo "@2x.gif -> .gif  [$@]"
	@convert $< -coalesce -resize 50% -layers optimize $@
%@2x.png: %.dot
	@echo ".dot -> @2x.png  [$@]"
	@dot -Tpng $< > $@ 2>/dev/null

################################################################################
# Feed generation
$(ATOM): projects/index.md blog/index.md $(ATOM_HEADER) $(ATOM_TEMPLATES) Makefile
	@echo "Building $@"
	@# Combine project and blog posts into a temporary file
	@sed -n "s:${PROJECT_REGEX}:\4;projects/\1;\2;\3:gp" projects/index.md > $@.tmp
	@sed -n "s:${BLOG_REGEX}:\3;blog/\3-\2; \1;:gp" blog/index.md >> $@.tmp

	@# Build the atom.xml feed
	@sed "s/{{NOW}}/$(ATOM_NOW)/g" $(ATOM_HEADER) > $@
	@sort -r $@.tmp | sed -n "s:${FEED_REGEX}:${ATOM_TEMPLATE}:gp" \
	                | sed "s/\&/\&amp;/g" \
	                | sed 's|<code>\(.*\)</code>|\1|' >> $@
	@echo "</feed>" >> $@
	@rm $@.tmp

################################################################################

serve:
	python3 -m http.server
