Jump to content

CSV to HTML translation: Difference between revisions

m (→‎{{header|J}}: display output using lang html)
(→‎{{header|J}}: tidy up)
Line 118:
<lang j>require 'strings tables/csv'
encodeHTML=: ('&';'&amp;';'<';'&lt;';'>';'&gt;')&stringreplace
cell=: ('<td>' , ,&'</td>')&.>
row=: <@('<tr>' , ,&('</tr>',LF))@;"1
table=: (('<table>',LF) , ,&('</table>'))@;"1
makeHTMLtablefromCSV=: [: table [@: row [@: cell&.> @fixcsv@encodeHTML</lang>
 
'''Example'''
<lang j> csvstrng=: noun define
Character,Speech
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>
The multitude,Who are you?
Brians mother,I'm his mother; that's who!
The multitude,Behold his mother! Behold his mother!
)
)</lang>
<lang html> makeHTMLtablefromCSV csvstrng</lang>
 
HTML output:
<lang html> makeHTMLtablefromCSV csvstrng
<lang html><table>
<table><tr><td>Character</td><td>Speech </td></tr>
<tr><td>The multitudeCharacter</td><td>The messiah! Show us the messiah! Speech</td></tr>
<tr><td>BriansThe mothermultitude</td><td>&lt;angry&gt;NowThe you listen heremessiah! He'sShow notus the messiah; he's a very naughty boy! Now go away!&lt;/angry&gt; </td></tr>
<tr><td>TheBrians multitudemother</td><td>Who are&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>BriansThe mothermultitude</td><td>I'mWho his mother; that's who!are you?</td></tr>
<tr><td>TheBrians multitudemother</td><td>BeholdI'm his mother!; Beholdthat's his motherwho! </td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table></lang>
 
892

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.