CSV to HTML translation: Difference between revisions

Content added Content deleted
m (→‎{{header|J}}: display output using lang html)
(→‎{{header|J}}: tidy up)
Line 118: Line 118:
<lang j>require 'strings tables/csv'
<lang j>require 'strings tables/csv'
encodeHTML=: ('&';'&amp;';'<';'&lt;';'>';'&gt;')&stringreplace
encodeHTML=: ('&';'&amp;';'<';'&lt;';'>';'&gt;')&stringreplace
cell=: '<td>' , ,&'</td>'
cell=: ('<td>' , ,&'</td>')&.>
row=: <@('<tr>' , ,&('</tr>',LF))@;"1
row=: <@('<tr>' , ,&('</tr>',LF))@;"1
table=: ('<table>' , ,&'</table>')@;"1
table=: (('<table>',LF) , ,&('</table>'))@;"1
makeHTMLtablefromCSV=: [: table [: row [: cell&.> fixcsv@encodeHTML</lang>
makeHTMLtablefromCSV=: table@:row@:cell@fixcsv@encodeHTML</lang>


'''Example'''
'''Example'''
<lang j> csvstrng=: noun define
<lang j> csvstrng=: noun define
Character,Speech
Character,Speech
The multitude,The messiah! Show us the messiah!
The multitude,The messiah! Show us the messiah!
Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>
Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>
The multitude,Who are you?
The multitude,Who are you?
Brians mother,I'm his mother; that's who!
Brians mother,I'm his mother; that's who!
The multitude,Behold his mother! Behold his mother!
The multitude,Behold his mother! Behold his mother!
)
)</lang>
makeHTMLtablefromCSV csvstrng</lang>

HTML output:
<lang html> makeHTMLtablefromCSV csvstrng
<lang html><table>
<table><tr><td>Character</td><td>Speech </td></tr>
<tr><td>The multitude</td><td>The messiah! Show us the messiah! </td></tr>
<tr><td>Character</td><td>Speech</td></tr>
<tr><td>Brians mother</td><td>&lt;angry&gt;Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!&lt;/angry&gt; </td></tr>
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
<tr><td>The multitude</td><td>Who are you? </td></tr>
<tr><td>Brians mother</td><td>&lt;angry&gt;Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!&lt;/angry&gt;</td></tr>
<tr><td>Brians mother</td><td>I'm his mother; that's who! </td></tr>
<tr><td>The multitude</td><td>Who are you?</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother! </td></tr>
<tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table></lang>
</table></lang>