form.awk 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # $1 = type
  2. # $2 = form variable name
  3. # $3 = form variable value
  4. # $4 = (radio button) value of button
  5. # $5 = string to append
  6. # $6 = additional attributes
  7. BEGIN {
  8. FS="|"
  9. }
  10. # trim leading whitespaces
  11. {
  12. gsub(/^[ \t]+/,"",$1)
  13. }
  14. $1 ~ /^onchange/ {
  15. onchange = $2
  16. }
  17. ($1 != "") && ($1 !~ /^option/) && (select_open == 1) {
  18. select_open = 0
  19. printf "</select>"
  20. }
  21. $1 ~ /^start_form/ {
  22. if ($3 != "") field_opts=" id=\"" $3 "\""
  23. else field_opts=""
  24. if ($4 == "hidden") field_opts = field_opts " style=\"display: none\""
  25. start_form($2, field_opts);
  26. print "<table width=\"100%\" summary=\"Settings\">"
  27. form_help = ""
  28. form_help_link = ""
  29. }
  30. $1 ~ /^field/ {
  31. if (field_open == 1) print "</td></tr>"
  32. if ($3 != "") field_opts=" id=\"" $3 "\""
  33. else field_opts=""
  34. if ($4 == "hidden") field_opts = field_opts " style=\"display: none\""
  35. print "<tr" field_opts ">"
  36. if ($2 != "") print "<td width=\"50%\">" $2 "</td><td width=\"50%\">"
  37. else print "<td colspan=\"2\">"
  38. field_open=1
  39. }
  40. $1 ~ /^checkbox/ {
  41. if ($3==$4) opts="checked=\"checked\" "
  42. else opts=""
  43. if (onchange != "") opts = opts " onClick=\"" onchange "()\" onChange=\"" onchange "()\""
  44. print "<input id=\"" $2 "_" $4 "\" type=\"checkbox\" name=\"" $2 "\" value=\"" $4 "\" " opts " />"
  45. }
  46. $1 ~ /^radio/ {
  47. if ($3==$4) opts="checked=\"checked\" "
  48. else opts=""
  49. if (onchange != "") opts = opts " onClick=\"" onchange "()\" onChange=\"" onchange "()\""
  50. print "<input id=\"" $2 "_" $4 "\" type=\"radio\" name=\"" $2 "\" value=\"" $4 "\" " opts " />"
  51. }
  52. $1 ~ /^select/ {
  53. opts = ""
  54. if (onchange != "") opts = opts " onClick=\"" onchange "()\" onChange=\"" onchange "()\""
  55. print "<select id=\"" $2 "\" name=\"" $2 "\"" opts ">"
  56. select_id = $2
  57. select_open = 1
  58. select_default = $3
  59. }
  60. ($1 ~ /^option/) && (select_open == 1) {
  61. if ($2 == select_default) option_selected=" selected=\"selected\""
  62. else option_selected=""
  63. if ($3 != "") option_title = $3
  64. else option_title = $2
  65. print "<option id=\"" select_id "_" $2 "\"" option_selected " value=\"" $2 "\">" option_title "&nbsp;&nbsp;</option>"
  66. }
  67. ($1 ~ /^listedit/) {
  68. n = split($4 " ", items, " ")
  69. for (i = 1; i <= n; i++) {
  70. if (items[i] != "") print "<tr><td width=\"50%\">" items[i] "</td><td>&nbsp;<a href=\"" $3 $2 "remove=" items[i] "\">@TR<<Remove>></a></td></tr>"
  71. }
  72. print "<tr><td width=\"100%\" colspan=\"2\"><input type=\"text\" name=\"" $2 "add\" value=\"" $5 "\" /><input type=\"submit\" name=\"" $2 "submit\" value=\"@TR<<Add>>\" /></td></tr>"
  73. }
  74. $1 ~ /^caption/ { print "<b>" $2 "</b>" }
  75. $1 ~ /^string/ { print $2 }
  76. $1 ~ /^text/ { print "<input id=\"" $2 "\" type=\"text\" name=\"" $2 "\" value=\"" $3 "\" />" $4 }
  77. $1 ~ /^password/ { print "<input id=\"" $2 "\" type=\"password\" name=\"" $2 "\" value=\"" $3 "\" />" $4 }
  78. $1 ~ /^upload/ { print "<input id=\"" $2 "\" type=\"file\" name=\"" $2 "\"/>" }
  79. $1 ~ /^submit/ { print "<input type=\"submit\" name=\"" $2 "\" value=\"" $3 "\" />" }
  80. $1 ~ /^helpitem/ { form_help = form_help "<dt>@TR<<" $2 ">>: </dt>" }
  81. $1 ~ /^helptext/ { form_help = form_help "<dd>@TR<<" $2 ">> </dd>" }
  82. $1 ~ /^helplink/ { form_help_link = "<div class=\"more-help\"><a href=\"" $2 "\">@TR<<more...>></a></div>" }
  83. ($1 ~ /^checkbox/) || ($1 ~ /^radio/) {
  84. print $5
  85. }
  86. $1 ~ /^end_form/ {
  87. if (field_open == 1) print "</td></tr>"
  88. field_open = 0
  89. print "</table>"
  90. end_form(form_help, form_help_link);
  91. form_help = ""
  92. form_help_link = ""
  93. }