swerc

anselm's simpler werc fork
git clone git://git.suckless.org/swerc
Log | Files | Refs | README

template.awk (771B)


      1 #!/usr/bin/awk -f
      2 function pr(str) {
      3 	if(lastc !~ "[{(]")
      4 		gsub(/'/, "''", str)
      5 	printf "%s", str
      6 }
      7 function trans(c) {
      8 	printf "%s", end
      9 
     10 	lastc = c
     11 	end = "\n"
     12 	if(c == "%")
     13 		end = ""
     14 	else if(c == "(")
     15 		printf "echo -n "
     16 	else if(c ~ "[})]") {
     17 		end = "'\n"
     18 		printf "echo -n '"
     19 	}
     20 }
     21 
     22 BEGIN {
     23 	lastc = "{"
     24 	trans("}")
     25 }
     26 END {
     27 	print end
     28 }
     29 
     30 /^%/ && $0 !~ /^%[{()}%]/ && lastc !~ /[({]/ {
     31 	trans("%")
     32 	print substr($0, 2)
     33 	next
     34 }
     35 {
     36 	if(lastc == "%")
     37 		trans("}")
     38 	n = split($0, a, "%")
     39 	pr(a[1])
     40 	for(i=2; i<=n; i++) {
     41 		c = substr(a[i], 1, 1)
     42 		rest = substr(a[i], 2)
     43 
     44 		if((lastc !~ "[({]" && c ~ "[({]") ||
     45 		   (lastc == "{" && c == "}") ||
     46 		   (lastc == "(" && c == ")"))
     47 			trans(c)
     48 		else if(c == "%")
     49 			pr("%")
     50 		else
     51 			pr("%" c)
     52 		pr(rest)
     53 	}
     54 	pr("\n")
     55 }