spec2html.awk (1300B)
1 #!/usr/bin/awk -f 2 3 BEGIN { 4 FS="%[>^] " 5 printf("%s\n", "<!DOCTYPE html>") 6 printf("%s\n", "<html><head><style type=\"text/css\">") 7 printf("\t%s\n", "table.code_doc { border-collapse: collapse; border: 0px; }") 8 printf("\t%s\n", "table.code_doc code { white-space: pre; }") 9 printf("\t%s\n", "table.code_doc tr { vertical-align: top; }") 10 printf("\t%s\n", "table.code_doc td:hover { background-color: #3331; }") 11 printf("%s\n", "</style></head><body><table class=\"code_doc\">") 12 last_comm = 1 13 } 14 15 /%> / || /^$/{ 16 last_comm = NR 17 } 18 19 { 20 code[NR] = $1 21 if(last_comm in comm) { 22 if(length($2)) { 23 comm[last_comm] = comm[last_comm] "\n" $2 24 } 25 } else { 26 comm[last_comm] = $2 27 } 28 span[last_comm] = span[last_comm] + 1 29 } 30 31 function entity_escape(s) { 32 gsub(/&/, "\\&", s) 33 gsub(/</, "\\<", s) 34 gsub(/>/, "\\>", s) 35 return s 36 } 37 38 function line(n){ 39 if(span[n]) { 40 printf("<tr><td>%d</td><td><code>%s</code></td><td rowspan=\"%d\">%s</td></tr>\n"\ 41 , n \ 42 , entity_escape(code[n]) \ 43 , span[n] \ 44 , entity_escape(comm[n]) \ 45 ) 46 } else { 47 printf("<tr><td>%d</td><td><code>%s</code></td></tr>\n"\ 48 , n \ 49 , entity_escape(code[n]) \ 50 ) 51 } 52 } 53 54 END { 55 for(n=1; (n in code); n++) { 56 line(n) 57 } 58 printf("%s\n", "</table></body></html>"); 59 }