7
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.
1Please 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? – Howard – 2013-06-11T04:07:32.437
@Howard hope the last edit will help. – Dan – 2013-06-11T04:53:47.883
1Are colspan and rowspan support required? Are the cell contents expected to be escaped, or do we need to escape them? – Peter Taylor – 2013-06-11T06:59:45.540
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? – John Dvorak – 2013-06-11T07:09:42.840
@PeterTaylor I edited the text to respond your question, in short: no need for row and colspan, but should escape content. – Dan – 2013-06-11T14:39:13.517
@JanDvorak should be valid xhtml. – Dan – 2013-06-11T14:39:35.330
You say XHTML, but then your "HTML table" link points to the HTML 4.0 spec. Which one is it, then? – John Dvorak – 2013-06-11T16:27:59.203
@JanDvorak xhtml. Just wondering: what is the difference between the two except that in xhtml you have to close the tags properly? – Dan – 2013-06-11T16:42:28.930
@Dan XHTML is valid XML while HTML is valid SGML. Apart from the need to close every tag, the differences include a different doctype and, more importantly, the handling of character data: In XHTML, you need to enclose data like scripts in
<![CDATA[...]]>
or similar (I don't remember the exact syntax) or XML-escape them, whereas in HTML literal handling is assumed. This makes dual-XHTML/HTML scripts a little unwieldy (//<![CDATA[
...//]]>
). Another difference is that XML supports namespaces whereas HTML merely supports colons in tag names. XHTML is practically dead now (thankfully). – John Dvorak – 2013-06-11T16:54:19.583Maybe some special characters in the sample input would help to exemplify the escaping part of the requirement. – manatwork – 2013-06-11T17:48:28.030
@manatwork I assume "valid XML that displays the input characters" – John Dvorak – 2013-06-11T18:03:19.390
@JanDvorak, I understand that. But I see tmartin used the sample input for testing his code, just as I did. So I wish we have a better official sample, including as much catchy items as possible. – manatwork – 2013-06-11T18:09:08.413
It's my first question, so I appreciate contributions to the question to make it "golfy"; I think that basic idea is clear. – Dan – 2013-06-11T19:17:51.037