changelog shortlog tags branches changeset files revisions annotate raw help

Mercurial > hg > werc / bin/template.awk

changeset 674: 92f7463dac1a
parent: fb460a671d19
author: stanley.lieber@gmail.com
date: Thu, 10 Aug 2017 21:43:54 -0400
permissions: -rwxr-xr-x
description: bin/contrib/rc-httpd/handlers/dir-index: use urlencode(1), not urlencode.awk (for now?)
1 #!/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 }