Skip to content

Instantly share code, notes, and snippets.

@fclad
Created September 19, 2019 14:18
Show Gist options
  • Save fclad/8a9efd3f87fa2dd4ff3875f1e635074b to your computer and use it in GitHub Desktop.
Save fclad/8a9efd3f87fa2dd4ff3875f1e635074b to your computer and use it in GitHub Desktop.
draft generation and snapshots
KDRFC = ${HOME}/.gem/ruby/2.3.0/bin/kramdown-rfc2629
XML2RFC = xml2rfc
RM = rm -f
SRC = $(wildcard *.md)
.PHONY: snapshot cleanall
all: $(SRC:.md=.xml) $(SRC:.md=.txt) $(SRC:.md=.html)
define fix-xml
sed -i "" -e ':a' -e 'N' -e '$$!ba' -e 's/\(<postal>\n\)\( *\)\(<country>\)/\1\2<street><\/street>\'$$'\n\\2\\3/g' -e 's/\(Email: [^<]*\)/\1<vspace blankLines=\"1\" \/>/g' $*.xml
endef
define take-snapshot
if [ ! -e "snapshots" ]; then mkdir "snapshots"; res="1a"; else if [ ! -d "snapshots" ]; then echo "snapshots is not a directory"; exit 1; fi; a=(`ls -r "snapshots" | egrep '$(basename $<)-rev[1-9][0-9]*[a-z].txt' | head -1`); if [ -z "$$a" ]; then res="1a"; else b=$${a##*-rev}; c=$${b%%.*}; n=$${c:0:1}; l=$${c:1:1}; if [ $$l = "z" ]; then n=$$((n+1)); fi; la=`printf "%d" "'$$l"`; l=`printf "\x$$(printf %x $$((97+(la-96)%26)))"`; res="$$n$$l"; fi; fi; cp $< snapshots/$(basename $<)-rev$${res}.txt;
endef
%.xml: %.md
@$(KDRFC) $< > $*.xml
@$(fix-xml)
%.txt: %.xml
@$(XML2RFC) --text $<
%.html: %.xml
@$(XML2RFC) --html $<
snapshot: $(SRC:.md=.txt)
@$(take-snapshot)
cleanall:
@$(RM) $(SRC:.md=.xml) $(SRC:.md=.txt) $(SRC:.md=.html)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment