CSV to HTML translation: Difference between revisions
→{{header|J}}: extra credit
(→{{header|J}}: tidy up) |
(→{{header|J}}: extra credit) |
||
Line 115:
=={{header|J}}==
'''Solution (extra credit)'''
<lang j>require 'strings tables/csv'
encodeHTML=: ('&';'&';'<';'<';'>';'>')&stringreplace
cell=: ('<td>' , ,&'</td>')&.>▼
tag=: adverb define
row=: <@('<tr>' , ,&('</tr>',LF))@;"1▼
'starttag endtag'=.m
table=: (('<table>',LF) , ,&('</table>'))@;"1▼
(,&.>/)"1 (starttag , ,&endtag) L:0 y
)
markupHdrCells=: ('<th>';'</th>') tag
makeHTMLtablefromCSV=: verb define
0 makeHTMLtablefromCSV y NB. default left arg is 0 (no header row)
:
t=. fixcsv encodeHTML y
if. x do. t=. (markupHdrCells@{. , markupCells@}.) t
else. t=. markupCells t
end.
;markupTable markupRows t
)</lang>
'''Example'''
<lang j>
Character,Speech
The multitude,The messiah! Show us the messiah!
Line 132 ⟶ 148:
The multitude,Behold his mother! Behold his mother!
)
1 makeHTMLtablefromCSV
HTML output:
<lang html><table>
<tr><
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
<tr><td>Brians mother</td><td><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry></td></tr>
|