template.awk 771 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. lastc = c
  10. end = "\n"
  11. if(c == "%")
  12. end = ""
  13. else if(c == "(")
  14. printf "echo -n "
  15. else if(c ~ "[})]") {
  16. end = "'\n"
  17. printf "echo -n '"
  18. }
  19. }
  20. BEGIN {
  21. lastc = "{"
  22. trans("}")
  23. }
  24. END {
  25. print end
  26. }
  27. /^%/ && $0 !~ /^%[{()}%]/ && lastc !~ /[({]/ {
  28. trans("%")
  29. print substr($0, 2)
  30. next
  31. }
  32. {
  33. if(lastc == "%")
  34. trans("}")
  35. n = split($0, a, "%")
  36. pr(a[1])
  37. for(i=2; i<=n; i++) {
  38. c = substr(a[i], 1, 1)
  39. rest = substr(a[i], 2)
  40. if((lastc !~ "[({]" && c ~ "[({]") ||
  41. (lastc == "{" && c == "}") ||
  42. (lastc == "(" && c == ")"))
  43. trans(c)
  44. else if(c == "%")
  45. pr("%")
  46. else
  47. pr("%" c)
  48. pr(rest)
  49. }
  50. pr("\n")
  51. }