code / chum / chums

1#!/bin/sh
2trampoline=; define()(:) # -*- makefile -*-
3###########################################
4## C H U M S : lots of CHUMs at once ... ##
5## Make a website out of code repoes, ez ##
6## Quickstart: cd project; vi .chum ---> ##
7## [change variables given below].  see          ##
8## README for details #####################
9###########################################
10define trampoline
11if command -v gmake >/dev/null 2>&1
12then exec gmake -kr -f "$0" "$@"
13else exec make -kr -f "$0" "$@"
14fi
15endef
16####################### code starts here ##
17PROJECTS =
18OUTDIR = $(PWD)/chums.d
19VDUMPF = $(OUTDIR)/.chump.txt
20
21CHUM = $(HOME)/code/chum/chum
22SERVER = @printf 'Generated files are in %s/\n'
23
24REMOTE =
25ROOT =
26
27define TEMPLATE =
28<html><title>chums</title>
29{{CONTENT}}
30</html>
31endef
32
33### INTERNAL
34
35define _CHUMS
36BEGIN { FS = "\t"; }
37{ if (URLS[$$1]) next; URLS[$$1] = 1
38  thisline = sprintf("<dt><a href=\"%s\">%s</a></dt>\n<dd>%s</dd>\n",
39                     escape($$2), $$1, $$3)
40  OUTSTR = OUTSTR (OUTSTR?"\n":"") thisline
41}
42END { print(template_replace(template)) }
43
44function template_replace (str) {
45  for (ts in TMPLV) gsub("{{"ts"}}", TMPLV[ts], str)
46  gsub(/&/, "\\&", OUTSTR)
47  sub("{{CONTENT}}", OUTSTR, str)
48  return str
49}
50
51function escape(str, c, len, res) {
52    ord[" "] = 32; ord["!"] = 33; ord["\""] = 34; ord["#"] = 35; ord["$$"] = 36
53    ord["%"] = 37; ord["&"] = 38; ord["'"] = 39; ord["("] = 40; ord[")"] = 41
54    ord["*"] = 42; ord["+"] = 43; ord[","] = 44; ord["-"] = 45; ord["."] = 46
55    ord[":"] = 58; ord[";"] = 59; ord["<"] = 60; ord["="] = 61; ord[">"] = 62
56    ord["?"] = 63; ord["@"] = 64; ord["["] = 91; ord["\\"] = 92; ord["]"] = 93
57    ord["^"] = 94; ord["_"] = 95; ord["`"] = 96; ord["{"] = 123; ord["|"] = 124
58    ord["}"] = 125; ord["~"] = 126
59    len = length(str)
60    res = ""
61    for (i = 1; i <= len; i++) {
62	c = substr(str, i, 1);
63	if (c ~ /[0-9A-Za-z\/]/)
64	    res = res c
65	else
66	    res = res "%" sprintf("%02X", ord[c])
67    }
68    return res
69}
70endef
71
72-include .chums
73
74$(OUTDIR): export TEMPL:=-vtemplate=$(TEMPLATE)
75$(OUTDIR): export PROG:=$(_CHUMS)
76$(OUTDIR): $(PROJECTS)
77	for p in $(PROJECTS); do \
78	$(CHUM) VDUMPF="$(VDUMPF)" \
79		URL_ROOT="$(ROOT)/$$p/" \
80		OUTDIR="$(OUTDIR)/$$p" \
81		-C "$$p" build; done
82	awk "$$TEMPL" "$$PROG" "$(VDUMPF)" > $(OUTDIR)/index.html
83
84.PHONY: serve publish clean build
85
86build: $(OUTDIR)
87
88serve: $(OUTDIR)
89	$(SERVER) $(OUTDIR)
90
91publish: $(OUTDIR)
92	rsync -zaP $(OUTDIR)/ $(REMOTE)/
93
94clean:
95	rm -rf $(OUTDIR) $(VDUMPF)