htmlqqz.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/bin/sh
  2. #
  3. # Usage: sh htmlqqz.sh file
  4. #
  5. # Extracts and converts quick quizzes in a proto-HTML document file.htmlx.
  6. # Commands, all of which must be on a line by themselves:
  7. #
  8. # "<p>@@QQ@@": Start of a quick quiz.
  9. # "<p>@@QQA@@": Start of a quick-quiz answer.
  10. # "<p>@@QQE@@": End of a quick-quiz answer, and thus of the quick quiz.
  11. # "<p>@@QQAL@@": Place to put quick-quiz answer list.
  12. #
  13. # Places the result in file.html.
  14. #
  15. # This program is free software; you can redistribute it and/or modify
  16. # it under the terms of the GNU General Public License as published by
  17. # the Free Software Foundation; either version 2 of the License, or
  18. # (at your option) any later version.
  19. #
  20. # This program is distributed in the hope that it will be useful,
  21. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. # GNU General Public License for more details.
  24. #
  25. # You should have received a copy of the GNU General Public License
  26. # along with this program; if not, you can access it online at
  27. # http://www.gnu.org/licenses/gpl-2.0.html.
  28. #
  29. # Copyright (c) 2013 Paul E. McKenney, IBM Corporation.
  30. fn=$1
  31. if test ! -r $fn.htmlx
  32. then
  33. echo "Error: $fn.htmlx unreadable."
  34. exit 1
  35. fi
  36. echo "<!-- DO NOT HAND EDIT. -->" > $fn.html
  37. echo "<!-- Instead, edit $fn.htmlx and run 'sh htmlqqz.sh $fn' -->" >> $fn.html
  38. awk < $fn.htmlx >> $fn.html '
  39. state == "" && $1 != "<p>@@QQ@@" && $1 != "<p>@@QQAL@@" {
  40. print $0;
  41. if ($0 ~ /^<p>@@QQ/)
  42. print "Bad Quick Quiz command: " NR " (expected <p>@@QQ@@ or <p>@@QQAL@@)." > "/dev/stderr"
  43. next;
  44. }
  45. state == "" && $1 == "<p>@@QQ@@" {
  46. qqn++;
  47. qqlineno = NR;
  48. haveqq = 1;
  49. state = "qq";
  50. print "<p><a name=\"Quick Quiz " qqn "\"><b>Quick Quiz " qqn "</b>:</a>"
  51. next;
  52. }
  53. state == "qq" && $1 != "<p>@@QQA@@" {
  54. qq[qqn] = qq[qqn] $0 "\n";
  55. print $0
  56. if ($0 ~ /^<p>@@QQ/)
  57. print "Bad Quick Quiz command: " NR ". (expected <p>@@QQA@@)" > "/dev/stderr"
  58. next;
  59. }
  60. state == "qq" && $1 == "<p>@@QQA@@" {
  61. state = "qqa";
  62. print "<br><a href=\"#qq" qqn "answer\">Answer</a>"
  63. next;
  64. }
  65. state == "qqa" && $1 != "<p>@@QQE@@" {
  66. qqa[qqn] = qqa[qqn] $0 "\n";
  67. if ($0 ~ /^<p>@@QQ/)
  68. print "Bad Quick Quiz command: " NR " (expected <p>@@QQE@@)." > "/dev/stderr"
  69. next;
  70. }
  71. state == "qqa" && $1 == "<p>@@QQE@@" {
  72. state = "";
  73. next;
  74. }
  75. state == "" && $1 == "<p>@@QQAL@@" {
  76. haveqq = "";
  77. print "<h3><a name=\"Answers to Quick Quizzes\">"
  78. print "Answers to Quick Quizzes</a></h3>"
  79. print "";
  80. for (i = 1; i <= qqn; i++) {
  81. print "<a name=\"qq" i "answer\"></a>"
  82. print "<p><b>Quick Quiz " i "</b>:"
  83. print qq[i];
  84. print "";
  85. print "</p><p><b>Answer</b>:"
  86. print qqa[i];
  87. print "";
  88. print "</p><p><a href=\"#Quick%20Quiz%20" i "\"><b>Back to Quick Quiz " i "</b>.</a>"
  89. print "";
  90. }
  91. next;
  92. }
  93. END {
  94. if (state != "")
  95. print "Unterminated Quick Quiz: " qqlineno "." > "/dev/stderr"
  96. else if (haveqq)
  97. print "Missing \"<p>@@QQAL@@\", no Quick Quiz." > "/dev/stderr"
  98. }'