aboutsummaryrefslogtreecommitdiff
path: root/bin/template.awk
blob: f5435065494d08480e419215b091093637e82d40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/awk -f
#taken from werc [http://werc.cat-v.org/]
function pr(str) {
	if(lastc !~ "[{(]")
		gsub(/'/, "''", str)
	printf "%s", str
}
function trans(c) {
	printf "%s", end

	lastc = c
	end = "\n"
	if(c == "%")
		end = ""
	else if(c == "(")
		printf "echo -n "
	else if(c ~ "[})]") {
		end = "'\n"
		printf "echo -n '"
	}
}

BEGIN {
	lastc = "{"
	trans("}")
}
END {
	print end
}

/^%/ && $0 !~ /^%[{()}%]/ && lastc !~ /[({]/ {
	trans("%")
	print substr($0, 2)
	next
}
{
	if(lastc == "%")
		trans("}")
	n = split($0, a, "%")
	pr(a[1])
	for(i=2; i<=n; i++) {
		c = substr(a[i], 1, 1)
		rest = substr(a[i], 2)

		if((lastc !~ "[({]" && c ~ "[({]") ||
		   (lastc == "{" && c == "}") ||
		   (lastc == "(" && c == ")"))
			trans(c)
		else if(c == "%")
			pr("%")
		else
			pr("%" c)
		pr(rest)
	}
	pr("\n")
}