9
\$\begingroup\$

Take a following character table:

-------------------------------------------------------
|       |1233456 | abc    | xyz    |        |         |
|-------|--------|--------|--------|--------|---------|
|       |abcdefgh| 1234567|        | 12345  |         |
|   abc |xyzabcqw|        |        |        |         |
|       |zzzz    |        |        |        |         |
|-------|--------|--------|--------|--------|---------|

It can have a variable number of columns or rows. Column width and row height can vary. Cells can be empty or have text, but the row height is optimized to wrap the text. So, second row has three lines because second cell content in second row is three lines high.

Input is ASCII text as shown in example. Cells can not contain | or - characters. Cell content should be escaped, but no need to worry about rowspan and colspan, since the table structure is always simple (every row same number of columns and every column same number of rows).

Output should be well-formed xhtml. Html table should reflect the table structure, content and line breaks. So, resulting html based on the sample input above is the following:

<table>
<tr>
    <td></td>
    <td>1233456</td>
    <td>abc</td>
    <td>xyz</td>
    <td></td>
    <td></td>
</tr>
<tr>
    <td>abc</td>
    <td>abcdefgh<br/>xyzabcqw<br/>zzzz</td>
    <td>1234567</td>
    <td></td>
    <td>12345</td>
    <td></td>
</tr>
</table>

Shortest code which takes this table and turns it into an xhtml table wins.

\$\endgroup\$
13
  • 1
    \$\begingroup\$ Please add some specification to your challenge. What are allowed input formats? Are all columns of even size (as your example indicates)? Why are some contents padded by spaces/empty lines? What features do we need to reflect also in the html table? Column widths? Padding? Text wrapping? Do we need to output well-formed html? \$\endgroup\$
    – Howard
    Jun 11, 2013 at 4:07
  • \$\begingroup\$ @Howard hope the last edit will help. \$\endgroup\$
    – Dan
    Jun 11, 2013 at 4:53
  • 1
    \$\begingroup\$ Are colspan and rowspan support required? Are the cell contents expected to be escaped, or do we need to escape them? \$\endgroup\$ Jun 11, 2013 at 6:59
  • \$\begingroup\$ Do you you require the output to be XHTML-compliant (every tag is closed or self-closing), as the output example suggests, or HTML 4.0 Transitional (most closing tags are not required) will suffice? \$\endgroup\$ Jun 11, 2013 at 7:09
  • \$\begingroup\$ @PeterTaylor I edited the text to respond your question, in short: no need for row and colspan, but should escape content. \$\endgroup\$
    – Dan
    Jun 11, 2013 at 14:39

3 Answers 3

2
\$\begingroup\$

K, 186

Haven't put any effort into golfing this.

{-1',/(,"<",g;,/{,/(,"<tr>";x;,"</tr>")}'{("<td>",/:*:'x),\:"</td>"}'{{$[1<+/~""~/:x;,"<br/>"/:x;x@&~""~/:x]}'+x}'-1_'/:1_'/:f@&~()~/:f:$`$"|"\:''1_'(&"-"in/:x)_x;,"</",g:"table>");}

.

k)t
"-------------------------------------------------------"
"|       |1233456 | abc    | xyz    |        |         |"
"|-------|--------|--------|--------|--------|---------|"
"|       |abcdefgh| 1234567|        | 12345  |         |"
"|   abc |xyzabcqw|        |        |        |         |"
"|       |zzzz    |        |        |        |         |"
"|-------|--------|--------|--------|--------|---------|"

k){-1',/(,"<",g;,/{,/(,"<tr>";x;,"</tr>")}'{("<td>",/:*:'x),\:"</td>"}'{{$[1<+/~""~/:x;,"<br/>"/:x;x@&~""~/:x]}'+x}'-1_'/:1_'/:f@&~()~/:f:$`$"|"\:''1_'(&"-"in/:x)_x;,"</",g:"table>");} t
<table>
<tr>
<td></td>
<td>1233456</td>
<td>abc</td>
<td>xyz</td>
<td></td>
<td></td>
</tr>
<tr>
<td>abc</td>
<td>abcdefgh<br/>xyzabcqw<br/>zzzz</td>
<td>1234567</td>
<td></td>
<td>12345</td>
<td></td>
</tr>
</table>
\$\endgroup\$
2
\$\begingroup\$

Ruby: 233 characters

BEGIN{puts"<table>";f=[]};if/^[-|]+$/ then if f!=[] then puts"<tr>\n#{f[1..-2].map{|i|"    <td>#{i.join"<br/>"}</td>"}*$/}\n</tr>\n";f=[]end else$F.each_index{|i|f[i]||=[];f[i]<<CGI.escapeHTML($F[i])if$F[i]>""}end;END{puts"</table>"}

Sample run:

bash-4.2$ ruby -naF' *\| *' -r cgi -e 'BEGIN{puts"<table>";f=[]};if/^[-|]+$/ then if f!=[] then puts"<tr>\n#{f[1..-2].map{|i|"    <td>#{i.join"<br/>"}</td>"}*$/}\n</tr>\n";f=[]end else$F.each_index{|i|f[i]||=[];f[i]<<CGI.escapeHTML($F[i])if$F[i]>""}end;END{puts"</table>"}' table.txt
<table>
<tr>
    <td></td>
    <td>1233456</td>
    <td>abc</td>
    <td>xyz</td>
    <td></td>
    <td></td>
</tr>
<tr>
    <td>abc</td>
    <td>abcdefgh<br/>xyzabcqw<br/>zzzz</td>
    <td>1234567</td>
    <td></td>
    <td>12345</td>
    <td></td>
</tr>
</table>

(Out of contest CW, due to the shameless amount of used command line options.)

\$\endgroup\$
5
  • \$\begingroup\$ It's better to wait until the spec is fairly complete before answering, because otherwise there's a risk that either you'll have to rewrite to accommodate clarifications or that you'll bias the clarifications towards your implementation (which might mean biasing them away from the more interesting problem). \$\endgroup\$ Jun 11, 2013 at 10:16
  • \$\begingroup\$ Use of command line options is fine, but they must be included into the code length. \$\endgroup\$ Jun 11, 2013 at 18:42
  • \$\begingroup\$ @JanDvorak, I know that -p counts 1 in case of perl and ruby. But never found a concise set of rules for them. Do you know about such rule collection? \$\endgroup\$
    – manatwork
    Jun 11, 2013 at 18:52
  • \$\begingroup\$ @manatwork Try ruby -h to get you started. \$\endgroup\$
    – steenslag
    Jun 12, 2013 at 21:49
  • \$\begingroup\$ @steenslag, you misunderstood the question. I know the interpreter's command line options. I not know how their use should be counted in code-golf challenges' results. \$\endgroup\$
    – manatwork
    Jun 13, 2013 at 7:35
0
\$\begingroup\$

GNU Awk: 277 characters

BEGIN{split("38&60<62>34\"39'",e,/\W/,c)
FS=" *\\| *"
print"<table>"}/^[-|]+$/&&n{print"<tr>"
for(i=2;i<n;i++)print"    <td>"f[i]"</td>"
print"</tr>"
delete f
next}{for(i=1;i<6;i++)gsub(c[i],"\\&#"e[i]";")
for(i=2;i<n=NF;i++)$i&&f[i]=f[i](f[i]?"<br>":"")$i}END{print"</table>"}

Note that the above code requires gawk version 4.0 or never, because

  • delete for an entire array is GNU extension
  • split()'s 4th parameters was implemented in version 4.0

Without HTML escaping: 201 characters

As there is not specified how HTML escaping should happen (whether the use of built-in or third party functions is allowed, the set of characters to escape, whether using entity names or character codes matters), I believe this challenge was neater without the escaping:

BEGIN{FS=" *\\| *"
print"<table>"}/^[-|]+$/&&n{print"<tr>"
for(i=2;i<n;i++)print"    <td>"f[i]"</td>"
print"</tr>"
delete f
next}{for(i=2;i<n=NF;i++)$i&&f[i]=f[i](f[i]?"<br>":"")$i}END{print"</table>"}

Sample run:

bash-4.1$ awk -f table.awk table.txt
<table>
<tr>
    <td></td>
    <td>1233456</td>
    <td>abc</td>
    <td>xyz</td>
    <td></td>
    <td></td>
</tr>
<tr>
    <td>abc</td>
    <td>abcdefgh<br>xyzabcqw<br>zzzz</td>
    <td>1234567</td>
    <td></td>
    <td>12345</td>
    <td></td>
</tr>
</table>
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.