J00K5

                      1. Постигнутый фонарь путешествия
                      2. Руки выпрямляют воду
                      3. Воздушный дисплей математика
                      4. Последствия изгиба головы
                      5. Фальстарт теней

                      Pekka Pohjola, 
                      The Mathematician's Air Display, 1977

# ПИШЕМ ПРОСТОЙ ГЕНЕРАТОР ТЕСТОВ НА AWK

test.txt:

   -----------------
  |Вопрос           |
  |  ( ) Ноль       |
  |  ( ) Один       |
  |  ( ) Два        |
  |Вопрос           |
  |  ( ) Ноль       |
  |  ( ) Один       |
  |  ( ) Два        |
  |  ( ) Три        |
  |  ( ) Четыре     |
  |Вопрос           |
  |  ( ) Ноль       |
  |  ( ) Один       |
  |  ( ) Два        |
   -----------------
   
conv.awk

  /* NOTE awk script itself and processed text in UTF-8 WITHOUT BOM */

  BEGIN {
    qcounter = 0;
    acounter = 0;
    allowed  = 0;
    
    printf("%s","\xef\xbb\xbf");
    print "<!DOCTYPE HTML>";
    print "<html>";
    print "<head>";
    print "  <title>BuRadioJS</title>";
    print "  ";
    print "  <script type=\"text/javascript\">";
    print "  function listQA() {";
    print "    var optionQA = document.querySelectorAll(\"input\");";
    print "    var s = \"\";";
    print "    for (j=0; j<optionQA.length; j++) ";
    print "      if (document.getElementById( optionQA[j].id).checked == true)";
    print "        s += optionQA[j].id + \",\";";
    print "    alert( s);";
    print "  }";
    print "  </script>";
    print "  ";
    print "</head>";
    print "<body>";
  }

  match($0, /Вопрос/ ) { 
    qcounter++;
    if (allowed==1) { 
      print "</tbody>";
      print "</table>";
    }
    print "<div id=\"Q" qcounter "\"><b>Вопрос " qcounter ":</b></div>"; 
    print "<table>";
    print "<tbody>";
    acounter = 0; 
    allowed = 1;
  }

  match($0, /^  \( \) / ) {
    print "<tr>";
    print "  <td>";
    print "    <input type=\"radio\" id=\"Q" qcounter "A" acounter "\" name=\"option" qcounter "\">";
    print "  </td>";
    print "  <td>";
    printf("  <div>%s</div>\n", substr($0, 7));
    print "  </td>";
    print "</tr>";
    acounter++;
  }

  END {
    print "</tbody>";
    print "</table>";
    print "<button id=\"b1\" onclick=\"listQA()\">Что выбрано?</button>"; 
    print "</body>";
    print "</html>";
  }

Применение:

  $ cat buradiojs.txt | awk -f conv.awk
  
Output:

  ﻿<!DOCTYPE HTML>
  <html>
  <head>
    <title>BuRadioJS</title>

    <script type="text/javascript">
    function listQA() {
      var optionQA = document.querySelectorAll("input");
      var s = "";
      for (j=0; j<optionQA.length; j++)
        if (document.getElementById( optionQA[j].id).checked == true)
          s += optionQA[j].id + ",";
      alert( s);
    }
    </script>
  </head>
  <body>
  <div id="Q1"><b>Вопрос 1:</b></div>
  <table>
  <tbody>
  <tr>
    <td>
      <input type="radio" id="Q1A0" name="option1">
    </td>
    <td>
    <div>Ноль</div>
    </td>
  </tr>
  <tr>
    <td>
      <input type="radio" id="Q1A1" name="option1">
    </td>
    <td>
    <div>Один</div>
    </td>
  </tr>
  <tr>
    <td>
      <input type="radio" id="Q1A2" name="option1">
    </td>
    <td>
    <div>Два</div>
    </td>
  </tr>
  </tbody>
  </table>
  <div id="Q2"><b>Вопрос 2:</b></div>
  <table>
  <tbody>
  <tr>
    <td>
      <input type="radio" id="Q2A0" name="option2">
    </td>
    <td>
    <div>Ноль</div>
    </td>
  </tr>
  <tr>
    <td>
      <input type="radio" id="Q2A1" name="option2">
    </td>
    <td>
    <div>Один</div>
    </td>
  </tr>
  <tr>
    <td>
      <input type="radio" id="Q2A2" name="option2">
    </td>
    <td>
    <div>Два</div>
    </td>
  </tr>
  <tr>
    <td>
      <input type="radio" id="Q2A3" name="option2">
    </td>
    <td>
    <div>Три</div>
    </td>
  </tr>
  <tr>
    <td>
      <input type="radio" id="Q2A4" name="option2">
    </td>
    <td>
    <div>Четыре</div>
    </td>
  </tr>
  </tbody>
  </table>
  <div id="Q3"><b>Вопрос 3:</b></div>
  <table>
  <tbody>
  <tr>
    <td>
      <input type="radio" id="Q3A0" name="option3">
    </td>
    <td>
    <div>Ноль</div>
    </td>
  </tr>
  <tr>
    <td>
      <input type="radio" id="Q3A1" name="option3">
    </td>
    <td>
    <div>Один</div>
    </td>
  </tr>
  <tr>
    <td>
      <input type="radio" id="Q3A2" name="option3">
    </td>
    <td>
    <div>Два</div>
    </td>
  </tr>
  </tbody>
  </table>
  <button id="b1" onclick="listQA()">Что выбрано?</button>
  </body>
  </html>
