CSV to HTML translation: Difference between revisions
Added extra credit C# solution. |
|||
(222 intermediate revisions by 83 users not shown) | |||
Line 1: | Line 1: | ||
{{task}} |
{{task}} |
||
Consider a simplified CSV format where all rows are separated by a |
|||
newline and all columns are separated by commas. No commas are |
|||
allowed as field data, but the data may contain other characters |
|||
and character sequences that would normally be escaped when |
|||
converted to HTML |
|||
Consider a simplified CSV format where all rows are separated by a newline |
|||
The task is to create a function that takes a string representation of the CSV data |
|||
and all columns are separated by commas. |
|||
and returns a text string of an HTML table representing the CSV data. Use the following |
|||
data as the CSV text to convert, and show your output. |
|||
:Character,Speech |
|||
:The multitude,The messiah! Show us the messiah! |
|||
:Brians mother,<nowiki><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry></nowiki> |
|||
:The multitude,Who are you? |
|||
:Brians mother,I'm his mother; that's who! |
|||
:The multitude,Behold his mother! Behold his mother! |
|||
No commas are allowed as field data, but the data may contain |
|||
For extra credit, ''optionally'' allow special formatting for the |
|||
other characters and character sequences that would |
|||
first row of the table as if it is the tables header row. |
|||
normally be ''escaped'' when converted to HTML |
|||
;Task: |
|||
Create a function that takes a string representation of the CSV data |
|||
and returns a text string of an HTML table representing the CSV data. |
|||
Use the following data as the CSV text to convert, and show your output. |
|||
: Character,Speech |
|||
: The multitude,The messiah! Show us the messiah! |
|||
: Brians mother,<nowiki><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry></nowiki> |
|||
: The multitude,Who are you? |
|||
: Brians mother,I'm his mother; that's who! |
|||
: The multitude,Behold his mother! Behold his mother! |
|||
;Extra credit: |
|||
''Optionally'' allow special formatting for the first row of the table as if it is the tables header row |
|||
(via <nowiki><thead></nowiki> preferably; CSS if you must). |
|||
<br><br> |
|||
=={{header|11l}}== |
|||
{{trans|C}} |
|||
<syntaxhighlight lang=11l>V input_csv = ‘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!’ |
|||
print("<table>\n<tr><td>", end' ‘’) |
|||
L(c) input_csv |
|||
print(S c { |
|||
"\n"{"</td></tr>\n<tr><td>"} |
|||
‘,’ {‘</td><td>’} |
|||
‘<’ {‘<’} |
|||
‘>’ {‘>’} |
|||
‘&’ {‘&’} |
|||
E {c} |
|||
}, end' ‘’) |
|||
print("</td></tr>\n</table>")</syntaxhighlight> |
|||
{{out}} |
|||
<syntaxhighlight lang=html5><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>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> |
|||
<tr><td>The multitude</td><td>Who are you?</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></syntaxhighlight> |
|||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
Line 23: | Line 63: | ||
{{libheader|AWS}} |
{{libheader|AWS}} |
||
csv2html.adb: |
csv2html.adb: |
||
< |
<syntaxhighlight lang=Ada>with Ada.Strings.Fixed; |
||
with Ada.Text_IO; |
with Ada.Text_IO; |
||
with Templates_Parser; |
with Templates_Parser; |
||
Line 62: | Line 102: | ||
(Templates_Parser.Parse ("table.tmplt", Translations)); |
(Templates_Parser.Parse ("table.tmplt", Translations)); |
||
end; |
end; |
||
end Csv2Html;</ |
end Csv2Html;</syntaxhighlight> |
||
table.tmplt: |
table.tmplt: |
||
< |
<syntaxhighlight lang=html5><table> |
||
@@TABLE@@ |
@@TABLE@@ |
||
<tr> |
<tr> |
||
Line 72: | Line 112: | ||
</tr> |
</tr> |
||
@@END_TABLE@@ |
@@END_TABLE@@ |
||
</table></ |
</table></syntaxhighlight> |
||
{{out}} |
|||
Output: |
|||
< |
<syntaxhighlight lang=html5><table> |
||
<tr> |
<tr> |
||
<td>Character</td> |
<td>Character</td> |
||
Line 100: | Line 140: | ||
<td>Behold his mother! Behold his mother!</td> |
<td>Behold his mother! Behold his mother!</td> |
||
</tr> |
</tr> |
||
</table></ |
</table></syntaxhighlight> |
||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
{{works with|ALGOL 68|Revision 1 - no extensions to language used.}} |
{{works with|ALGOL 68|Revision 1 - no extensions to language used.}} |
||
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny].}} |
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny].}} |
||
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of '''format'''[ted] ''transput''.}} |
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of '''format'''[ted] ''transput''.}} |
||
< |
<syntaxhighlight lang=algol68>#!/usr/local/bin/a68g --script # |
||
[6]STRING rows := []STRING( |
[6]STRING rows := []STRING( |
||
Line 173: | Line 214: | ||
elbat; |
elbat; |
||
ydob; |
ydob; |
||
lmth</ |
lmth</syntaxhighlight> |
||
{{out}} |
|||
Output:<lang html5><HTML> |
|||
<syntaxhighlight lang=html5><HTML> |
|||
<HEAD> |
<HEAD> |
||
<TITLE>CSV to HTML translation - Extra Credit</TITLE> |
<TITLE>CSV to HTML translation - Extra Credit</TITLE> |
||
Line 192: | Line 234: | ||
</TABLE> |
</TABLE> |
||
</BODY> |
</BODY> |
||
</HTML></ |
</HTML></syntaxhighlight> |
||
=={{header|ANTLR}}== |
|||
===Java=== |
|||
<syntaxhighlight lang=java> |
|||
// Create an HTML Table from comma seperated values |
|||
// Nigel Galloway - June 2nd., 2013 |
|||
grammar csv2html; |
|||
dialog : {System.out.println("<HTML><Table>");}header body+{System.out.println("</Table></HTML>");} ; |
|||
header : {System.out.println("<THEAD align=\"center\"><TR bgcolor=\"blue\">");}row{System.out.println("</TR></THEAD");}; |
|||
body : {System.out.println("<TBODY><TR>");}row{System.out.println("</TR></TBODY");}; |
|||
row : field ',' field '\r'? '\n'; |
|||
field : Field{System.out.println("<TD>" + $Field.text.replace("<","<").replace(">",">") + "</TD>");}; |
|||
Field : ~[,\n\r]+; |
|||
</syntaxhighlight> |
|||
=={{header|Applesoft BASIC}}== |
|||
<syntaxhighlight lang=gwbasic> 100 DATA "Character,Speech" |
|||
110 DATA "The multitude,The messiah! Show us the messiah!" |
|||
120 DATA "Brian's mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>" |
|||
130 DATA "The multitude,Who are you?" |
|||
140 DATA "Brian's mother,I'm his mother; that's who!" |
|||
150 DATA "The multitude,Behold his mother! Behold his mother!" |
|||
160 DATA |
|||
170 LET M$ = CHR$ (13) |
|||
180 LET Q$ = CHR$ (34) |
|||
190 LET TRUE = NOT FALSE |
|||
200 LET HEADER = TRUE |
|||
210 DIM C(255),H$(4,1) |
|||
220 LET H$(1,0) = "</TD><TD>" |
|||
230 LET H$(2,0) = "<" |
|||
240 LET H$(3,0) = ">" |
|||
250 LET H$(4,0) = "&" |
|||
260 FOR I = 1 TO 4 |
|||
270 LET C( ASC ( MID$ (",<>&",I,1))) = I |
|||
280 LET H$(I,HEADER) = H$(I,0) |
|||
290 NEXT I |
|||
300 LET H$(1,1) = "</TH><TH>" |
|||
310 PRINT "<!DOCTYPE HTML>"M$"<HTML>"M$"<HEAD>"M$"</HEAD>"M$"<BODY>" |
|||
320 PRINT "<TABLE BORDER="Q$"1"Q$" CELLPADDING="Q$"10"Q$" CELLSPACING="Q$"0"Q$">" |
|||
330 READ CSV$ |
|||
340 FOR Q = 0 TO 1 STEP 0 |
|||
350 PRINT "<TR><T" MID$ ("DH",1 + HEADER,1)">"; |
|||
360 FOR I = 1 TO LEN (CSV$) |
|||
370 LET C$ = MID$ (CSV$,I,1) |
|||
380 LET H = C( ASC (C$)) |
|||
390 PRINT H$(H,HEADER) MID$ (C$,1,H = 0); |
|||
400 NEXT I |
|||
410 PRINT "</T" MID$ ("DH",1 + HEADER,1)"></TR>" |
|||
420 LET HEADER = FALSE |
|||
430 READ CSV$ |
|||
440 LET Q = CSV$ = "" |
|||
450 NEXT Q |
|||
460 PRINT "</TABLE>"M$"</BODY>"M$"</HTML>"</syntaxhighlight> |
|||
=={{header|Arturo}}== |
|||
<syntaxhighlight lang=rebol>in: { |
|||
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! |
|||
} |
|||
table: function [content] |
|||
-> join @["<table>" join content "</table>"] |
|||
row: function [data] |
|||
-> join @[ |
|||
"<tr><td>" escape.xml first data "</td>" |
|||
"<td>" escape.xml last data "</td></tr>" |
|||
] |
|||
print table map read.csv in => row</syntaxhighlight> |
|||
{{out}} |
|||
<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>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><tr><td>The multitude</td><td>Who are you?</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> |
|||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
Very basic implementation |
Very basic implementation |
||
< |
<syntaxhighlight lang=AutoHotkey>CSVData = |
||
( |
( |
||
Character,Speech |
Character,Speech |
||
Line 220: | Line 340: | ||
return str |
return str |
||
} |
} |
||
MsgBox % clipboard := TableData</ |
MsgBox % clipboard := TableData</syntaxhighlight> |
||
{{out}} |
|||
Output: |
|||
<pre><table> |
<pre><table> |
||
<tr><td>Character</td><td>Speech</td></tr> |
<tr><td>Character</td><td>Speech</td></tr> |
||
Line 230: | Line 350: | ||
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr> |
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr> |
||
</table></pre>(note the output has been modified slightly since this webpage is html.) |
</table></pre>(note the output has been modified slightly since this webpage is html.) |
||
=={{header|AutoIt}}== |
|||
<syntaxhighlight lang=AutoIt> |
|||
Local $ascarray[4] = [34,38,60,62] |
|||
$String = "Character,Speech" & @CRLF |
|||
$String &= "The multitude,The messiah! Show us the messiah!" & @CRLF |
|||
$String &= "Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>" & @CRLF |
|||
$String &= "The multitude,Who are you?" & @CRLF |
|||
$String &= "Brians mother,I'm his mother; that's who!" & @CRLF |
|||
$String &= "The multitude,Behold his mother! Behold his mother!" |
|||
For $i = 0 To UBound($ascarray) -1 |
|||
$String = Stringreplace($String, chr($ascarray[$i]), "&#"&$ascarray[$i]&";") |
|||
Next |
|||
$newstring = "<table>" & @CRLF |
|||
$crlfsplit = StringSplit($String, @CRLF, 1) |
|||
For $i = 1 To $crlfsplit[0] |
|||
If $i = 1 Then $newstring &= "<thead>" & @CRLF |
|||
$newstring &= "<tr>" & @CRLF |
|||
$komsplit = StringSplit($crlfsplit[$i], ",") |
|||
For $k = 1 To $komsplit[0] |
|||
If $i = 1 Then |
|||
$newstring &= "<th>" &$komsplit[$k] & "</th>" & @CRLF |
|||
Else |
|||
$newstring &= "<td>" &$komsplit[$k] & "</td>" & @CRLF |
|||
EndIf |
|||
Next |
|||
$newstring &= "</tr>" & @CRLF |
|||
If $i = 1 Then $newstring &= "</thead>" & @CRLF |
|||
Next |
|||
$newstring &= "</table>" |
|||
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $newstring = ' & $newstring & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre><table> |
|||
<thead> |
|||
<tr> |
|||
<th>Character</th> |
|||
<th>Speech</th> |
|||
</tr> |
|||
</thead> |
|||
<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> |
|||
<tr> |
|||
<td>The multitude</td> |
|||
<td>Who are you?</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></pre> |
|||
=={{header|AWK}}== |
|||
{{works with|Gawk}} |
|||
Includes extra credit.<br /> |
|||
File csv2html.awk |
|||
<syntaxhighlight lang=awk>#!/usr/bin/awk -f |
|||
BEGIN { |
|||
FS="," |
|||
print "<table>" |
|||
} |
|||
{ |
|||
gsub(/</, "\\<") |
|||
gsub(/>/, "\\>") |
|||
gsub(/&/, "\\>") |
|||
print "\t<tr>" |
|||
for(f = 1; f <= NF; f++) { |
|||
if(NR == 1 && header) { |
|||
printf "\t\t<th>%s</th>\n", $f |
|||
} |
|||
else printf "\t\t<td>%s</td>\n", $f |
|||
} |
|||
print "\t</tr>" |
|||
} |
|||
END { |
|||
print "</table>" |
|||
} |
|||
</syntaxhighlight> |
|||
<pre>$ awk -f csv2html.awk input.csv</pre> |
|||
<syntaxhighlight lang=html5><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>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>Who are you?</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></syntaxhighlight> |
|||
Extra credit: |
|||
<pre>$ awk -v header=1 -f csv2html.awk input.csv</pre> |
|||
<syntaxhighlight lang=html5><table> |
|||
<tr> |
|||
<th>Character</th> |
|||
<th>Speech</th> |
|||
</tr> |
|||
<tr> |
|||
<td>The multitude</td> |
|||
<td>The messiah! Show us the messiah!</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>Who are you?</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></syntaxhighlight> |
|||
=={{header|Batch File}}== |
|||
<syntaxhighlight lang=dos>::Batch Files are terrifying when it comes to string processing. |
|||
::But well, a decent implementation! |
|||
@echo off |
|||
REM Below is the CSV data to be converted. |
|||
REM Exactly three colons must be put before the actual line. |
|||
:::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! |
|||
setlocal disabledelayedexpansion |
|||
echo ^<table^> |
|||
for /f "delims=" %%A in ('findstr "^:::" "%~f0"') do ( |
|||
set "var=%%A" |
|||
setlocal enabledelayedexpansion |
|||
REM The next command removes the three colons... |
|||
set "var=!var:~3!" |
|||
REM The following commands to the substitions per line... |
|||
set "var=!var:&=&!" |
|||
set "var=!var:<=<!" |
|||
set "var=!var:>=>!" |
|||
set "var=!var:,=</td><td>!" |
|||
echo ^<tr^>^<td^>!var!^</td^>^</tr^> |
|||
endlocal |
|||
) |
|||
echo ^</table^></syntaxhighlight> |
|||
{{out}} |
|||
<syntaxhighlight lang=html5><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>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> |
|||
<tr><td>The multitude</td><td>Who are you?</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></syntaxhighlight> |
|||
=={{header|BBC BASIC}}== |
|||
{{works with|BBC BASIC for Windows}} |
|||
<syntaxhighlight lang=bbcbasic> DATA "Character,Speech" |
|||
DATA "The multitude,The messiah! Show us the messiah!" |
|||
DATA "Brian's mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>" |
|||
DATA "The multitude,Who are you?" |
|||
DATA "Brian's mother,I'm his mother; that's who!" |
|||
DATA "The multitude,Behold his mother! Behold his mother!" |
|||
DATA "***" |
|||
*SPOOL CSVtoHTML.htm |
|||
PRINT "<HTML>" |
|||
PRINT "<HEAD>" |
|||
PRINT "</HEAD>" |
|||
PRINT "<BODY>" |
|||
PRINT "<table border=1 cellpadding =10 cellspacing=0>" |
|||
header% = TRUE |
|||
REPEAT |
|||
READ csv$ |
|||
IF csv$ = "***" THEN EXIT REPEAT |
|||
IF header% PRINT "<tr><th>"; ELSE PRINT "<tr><td>"; |
|||
FOR i% = 1 TO LEN(csv$) |
|||
c$ = MID$(csv$, i%, 1) |
|||
CASE c$ OF |
|||
WHEN ",": IF header% PRINT "</th><th>"; ELSE PRINT "</td><td>"; |
|||
WHEN "<": PRINT "<"; |
|||
WHEN ">": PRINT ">"; |
|||
WHEN "&": PRINT "&"; |
|||
OTHERWISE: PRINT c$; |
|||
ENDCASE |
|||
NEXT i% |
|||
IF header% PRINT "</th></tr>" ELSE PRINT "</td></tr>" |
|||
header% = FALSE |
|||
UNTIL FALSE |
|||
PRINT "</table>" |
|||
PRINT "</BODY>" |
|||
PRINT "</HTML>" |
|||
*spool |
|||
SYS "ShellExecute", @hwnd%, 0, "CSVtoHTML.htm", 0, 0, 1 |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre><HTML> |
|||
<HEAD> |
|||
</HEAD> |
|||
<BODY> |
|||
<table border=1 cellpadding =10 cellspacing=0> |
|||
<tr><th>Character</th><th>Speech</th></tr> |
|||
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr> |
|||
<tr><td>Brian's 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>Who are you?</td></tr> |
|||
<tr><td>Brian's 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> |
|||
</BODY> |
|||
</HTML></pre> |
|||
{{out|Rendered output}} |
|||
[[File:CSVHTML_BBC.gif]] |
|||
=={{header|Befunge}}== |
|||
The most practical solution for Befunge was to read the CSV from stdin, so this implementation works best with command line interpreters that can accept redirected input. That said, it is still possible test with many of the GUI and online interpretors just by entering the input manually. |
|||
Note that right angle brackets are deliberately not escaped, since that is not strictly necessary for the markup to be valid. |
|||
<syntaxhighlight lang=Befunge><v_>#!,#:< "<table>" \0 +55 |
|||
v >0>::65*1+`\"~"`!*#v_4-5v > |
|||
v>#^~^<v"<tr><td>" < \v-1/<>">elb" |
|||
<^ >:#,_$10 |!:<>\#v_ vv"ta" |
|||
v-",":\-"&":\-"<":\<>5#05#<v+ >"/"v |
|||
>#v_$$$0">dt<>dt/<"vv"tr>"+<5 v"<"< |
|||
>^>\#v_$$0";pma&" v>"/<>d"v5 v , < |
|||
$ > \#v_$0";tl&"v v"</t"<0 > : | |
|||
^_>#!,#:<>#<0#<\#<<< >:#,_$#^_v@ $<</syntaxhighlight> |
|||
{{out}} |
|||
<syntaxhighlight lang=html5><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>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> |
|||
<tr><td>The multitude</td><td>Who are you?</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></syntaxhighlight> |
|||
=={{header|Bracmat}}== |
|||
===Extra credit solution using pattern matching === |
|||
This is not the most concise solution, but it is relatively efficient. To collect the lines we use a pattern that matches a line starting from position <code>[!p</code>. Each time a line is matched, <code>p</code> is updated, the two found elements are collected and the pattern is forced to fail, so the pattern matcher finds the next line. The found rows are collected in reverse order, because prepending to a list is faster than appending. When all lines are read, the collected lines are reversed, interspersed with newline characters. Finally the predefined function toML is used to create HTML. |
|||
<syntaxhighlight lang=bracmat>( ( CSVtoHTML |
|||
= p q Character Speech swor rows row |
|||
. 0:?p |
|||
& :?swor:?rows |
|||
& ( @( !arg |
|||
: ? |
|||
( [!p ?Character "," ?Speech \n [?q ? |
|||
& !q:?p |
|||
& (tr.,(th.,!Character) (th.,!Speech)) |
|||
!swor |
|||
: ?swor |
|||
& ~ |
|||
) |
|||
) |
|||
| whl |
|||
' ( !swor:%?row %?swor |
|||
& !row \n !rows:?rows |
|||
) |
|||
& toML |
|||
$ (table.,(thead.,!swor) \n (tbody.,!rows)) |
|||
) |
|||
) |
|||
& CSVtoHTML |
|||
$ "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! |
|||
" |
|||
) |
|||
</syntaxhighlight> |
|||
Output: |
|||
<syntaxhighlight lang=html><table><thead><tr><th>Character</th><th>Speech</th></tr></thead> |
|||
<tbody><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> |
|||
<tr><td>The multitude</td><td>Who are you?</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> |
|||
</tbody></table></syntaxhighlight> |
|||
===Simple solution NOT using pattern matching === |
|||
Newer versions of Bracmat have the built in function <code>vap</code> that splits an input string in |
|||
single characters or that splits it everywhere where a given separator character occurs. Each single character or slab of characters is |
|||
passed to a function. The values returned from this function become the elements in a list that is returned from the <code>vap</code> function. For example, <code>vap$(upp.Википедию)</code> "vaporizes" the word Википедию into characters and produces the list of |
|||
uppercased characters В И К И П Е Д И Ю. Instead of the name of a function we can choose to just give <code>vap</code> the definition of an anonymous function, like so: <code>vap$((=.!arg:~и|).Википедию)</code>. This returns a list of the characters in the word Википедию, except for the и character: В к п е д ю. |
|||
In the code below, we use <code>vap</code> with a third argument, a splitting separator character. The outer call to <code>vap</code> splits a text into rows. An embedded call to <code>vap</code> splits each row into cell elements. This code is very efficient. |
|||
<syntaxhighlight lang=Bracmat>( ( Csv2Html |
|||
= |
|||
. toML |
|||
$ ( table |
|||
. |
|||
, vap |
|||
$ ( ( |
|||
= .tr.,vap$((=.td.,!arg).!arg.",") |
|||
) |
|||
. !arg |
|||
. \n |
|||
) |
|||
) |
|||
) |
|||
& Csv2Html |
|||
$ "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! |
|||
" |
|||
)</syntaxhighlight> |
|||
===Extra credit solution=== |
|||
<syntaxhighlight lang=Bracmat>( ( Csv2Html |
|||
= |
|||
. toML |
|||
$ ( table |
|||
. |
|||
, vap |
|||
$ ( (=..vap$((=.,!arg).!arg.",")) |
|||
. !arg |
|||
. \n |
|||
) |
|||
: (.%?header) ?body |
|||
& ( thead |
|||
. |
|||
, (tr.,map$((=.th.!arg).!header)) |
|||
\n |
|||
) |
|||
( tbody |
|||
. |
|||
, map |
|||
$ ( ( |
|||
= |
|||
. !arg:(.?arg) |
|||
& (tr.,map$((=.td.!arg).!arg)) |
|||
\n |
|||
) |
|||
. !body |
|||
) |
|||
) |
|||
) |
|||
) |
|||
& Csv2Html |
|||
$ "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! |
|||
" |
|||
)</syntaxhighlight> |
|||
Output: |
|||
<syntaxhighlight lang=html> |
|||
<table><thead><tr><th>Character</th><th>Speech</th></tr> |
|||
</thead><tbody><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> |
|||
<tr><td>The multitude</td><td>Who are you?</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> |
|||
<tr><td /></tr> |
|||
</tbody></table></syntaxhighlight> |
|||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang=c>#include <stdio.h> |
||
char input |
const char *input = |
||
"Character,Speech\n" |
"Character,Speech\n" |
||
"The multitude,The messiah! Show us the messiah!\n" |
"The multitude,The messiah! Show us the messiah!\n" |
||
Line 246: | Line 774: | ||
int main() |
int main() |
||
{ |
{ |
||
char *s |
const char *s; |
||
printf("<table>\n<tr><td>"); |
printf("<table>\n<tr><td>"); |
||
for (s = input; *s; s++) { |
for (s = input; *s; s++) { |
||
Line 261: | Line 789: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
|||
Output: |
|||
<pre>$ gcc -Wall -W -ansi -pedantic csv.c -o csv |
<pre>$ gcc -Wall -W -ansi -pedantic csv.c -o csv |
||
$ ./csv</pre> |
$ ./csv</pre> |
||
< |
<syntaxhighlight lang=html5><table> |
||
<tr><td>Character</td><td>Speech</td></tr> |
<tr><td>Character</td><td>Speech</td></tr> |
||
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr> |
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr> |
||
Line 275: | Line 803: | ||
<tr><td>Brians mother</td><td>I'm his mother; that's who!</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> |
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr> |
||
</table></ |
</table></syntaxhighlight> |
||
=={{header|C++}}== |
|||
<lang cpp>#include <string> |
|||
#include <boost/regex.hpp> |
|||
#include <iostream> |
|||
std::string csvToHTML( const std::string & ) ; |
|||
int main( ) { |
|||
std::string text = "Character,Speech\n" |
|||
"The multitude,The messiah! Show us the messiah!\n" |
|||
"Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>\n" |
|||
"The multitude,Who are you?\n" |
|||
"Brians mother,I'm his mother; that's who!\n" |
|||
"The multitude,Behold his mother! Behold his mother!\n" ; |
|||
std::cout << csvToHTML( text ) ; |
|||
return 0 ; |
|||
} |
|||
std::string csvToHTML( const std::string & csvtext ) { |
|||
//the order of the regexes and the replacements is decisive! |
|||
std::string regexes[ 5 ] = { "<" , ">" , "^(.+?)\\b" , "," , "\n" } ; |
|||
const char* replacements [ 5 ] = { "<" , ">" , " <TR><TD>$1" , "</TD><TD>", "</TD></TR>\n" } ; |
|||
boost::regex e1( regexes[ 0 ] ) ; |
|||
std::string tabletext = boost::regex_replace( csvtext , e1 , |
|||
replacements[ 0 ] , boost::match_default | boost::format_all ) ; |
|||
for ( int i = 1 ; i < 5 ; i++ ) { |
|||
e1.assign( regexes[ i ] ) ; |
|||
tabletext = boost::regex_replace( tabletext , e1 , replacements[ i ] , boost::match_default | boost::format_all ) ; |
|||
} |
|||
tabletext = std::string( "<TABLE>\n" ) + tabletext ; |
|||
tabletext.append( "</TABLE>\n" ) ; |
|||
return tabletext ; |
|||
}</lang> |
|||
Output: |
|||
<lang html5> |
|||
<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>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> |
|||
<TR><TD>The multitude</TD><TD>Who are you?</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> |
|||
=={{header|C sharp}}== |
=={{header|C sharp|C#}}== |
||
===Simple Solution=== |
===Simple Solution=== |
||
< |
<syntaxhighlight lang=C sharp> |
||
using System; |
using System; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
Line 359: | Line 842: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} when using the text suggested: |
|||
< |
<syntaxhighlight lang=html5> |
||
<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>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><tr><td>The multitude</td><td>Who are you?</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> |
<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>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><tr><td>The multitude</td><td>Who are you?</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> |
||
</syntaxhighlight> |
|||
</lang> |
|||
===Extra Credit Solution=== |
===Extra Credit Solution=== |
||
< |
<syntaxhighlight lang=C sharp>using System; |
||
using System.Linq; |
using System.Linq; |
||
using System.Net; |
using System.Net; |
||
Line 421: | Line 904: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out|Sample HTML Output}} |
|||
< |
<syntaxhighlight lang=html5><table> |
||
<tr> |
<tr> |
||
<th>Character</th> |
<th>Character</th> |
||
Line 449: | Line 932: | ||
<td>Behold his mother! Behold his mother!</td> |
<td>Behold his mother! Behold his mother!</td> |
||
</tr> |
</tr> |
||
</table></ |
</table></syntaxhighlight> |
||
=={{header|C++}}== |
|||
<syntaxhighlight lang=cpp>#include <string> |
|||
#include <boost/regex.hpp> |
|||
#include <iostream> |
|||
std::string csvToHTML( const std::string & ) ; |
|||
int main( ) { |
|||
std::string text = "Character,Speech\n" |
|||
"The multitude,The messiah! Show us the messiah!\n" |
|||
"Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>\n" |
|||
"The multitude,Who are you?\n" |
|||
"Brians mother,I'm his mother; that's who!\n" |
|||
"The multitude,Behold his mother! Behold his mother!\n" ; |
|||
std::cout << csvToHTML( text ) ; |
|||
return 0 ; |
|||
} |
|||
std::string csvToHTML( const std::string & csvtext ) { |
|||
//the order of the regexes and the replacements is decisive! |
|||
std::string regexes[ 5 ] = { "<" , ">" , "^(.+?)\\b" , "," , "\n" } ; |
|||
const char* replacements [ 5 ] = { "<" , ">" , " <TR><TD>$1" , "</TD><TD>", "</TD></TR>\n" } ; |
|||
boost::regex e1( regexes[ 0 ] ) ; |
|||
std::string tabletext = boost::regex_replace( csvtext , e1 , |
|||
replacements[ 0 ] , boost::match_default | boost::format_all ) ; |
|||
for ( int i = 1 ; i < 5 ; i++ ) { |
|||
e1.assign( regexes[ i ] ) ; |
|||
tabletext = boost::regex_replace( tabletext , e1 , replacements[ i ] , boost::match_default | boost::format_all ) ; |
|||
} |
|||
tabletext = std::string( "<TABLE>\n" ) + tabletext ; |
|||
tabletext.append( "</TABLE>\n" ) ; |
|||
return tabletext ; |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<syntaxhighlight lang=html5> |
|||
<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>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> |
|||
<TR><TD>The multitude</TD><TD>Who are you?</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> |
|||
</syntaxhighlight> |
|||
=={{header|Clojure}}== |
|||
We assume the presence of a file, but the input could come from anywhere. |
|||
<syntaxhighlight lang=csv> |
|||
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! |
|||
</syntaxhighlight> |
|||
<syntaxhighlight lang=clojure> |
|||
(require 'clojure.string) |
|||
(def escapes |
|||
{\< "<", \> ">", \& "&"}) |
|||
(defn escape |
|||
[content] |
|||
(clojure.string/escape content escapes)) |
|||
(defn tr |
|||
[cells] |
|||
(format "<tr>%s</tr>" |
|||
(apply str (map #(str "<td>" (escape %) "</td>") cells)))) |
|||
;; turn a seq of seq of cells into a string. |
|||
(defn to-html |
|||
[tbl] |
|||
(format "<table><tbody>%s</tbody></thead>" |
|||
(apply str (map tr tbl)))) |
|||
;; Read from a string to a seq of seq of cells. |
|||
(defn from-csv |
|||
[text] |
|||
(map #(clojure.string/split % #",") |
|||
(clojure.string/split-lines text))) |
|||
(defn -main |
|||
[] |
|||
(let [lines (line-seq (java.io.BufferedReader. *in*)) |
|||
tbl (map #(clojure.string/split % #",") lines)] |
|||
(println (to-html tbl))) |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<syntaxhighlight lang=html> |
|||
<table><tbody><tr><td>Character</td><td>Speech</td></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><tr><td>The multitude</td><td>Who are you?</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></tbody></thead> |
|||
</syntaxhighlight> |
|||
=={{header|CoffeeScript}}== |
=={{header|CoffeeScript}}== |
||
Line 455: | Line 1,035: | ||
{{works with|node.js}} |
{{works with|node.js}} |
||
< |
<syntaxhighlight lang=coffeescript>String::__defineGetter__ 'escaped', () -> |
||
this.replace(/&/g, '&') |
this.replace(/&/g, '&') |
||
.replace(/</g, '<') |
.replace(/</g, '<') |
||
Line 493: | Line 1,073: | ||
</tbody> |
</tbody> |
||
</table> |
</table> |
||
"""</ |
"""</syntaxhighlight> |
||
{{out}} |
|||
Output: |
|||
< |
<syntaxhighlight lang=html5><table cellspacing="0"> |
||
<thead> |
<thead> |
||
<th scope="col">Character</th> |
<th scope="col">Character</th> |
||
Line 513: | Line 1,093: | ||
<td>Behold his mother! Behold his mother!</td> |
<td>Behold his mother! Behold his mother!</td> |
||
</tbody> |
</tbody> |
||
</table></ |
</table></syntaxhighlight> |
||
=={{header|Common Lisp}}== |
|||
<syntaxhighlight lang=lisp>(defvar *csv* "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!") |
|||
(defun split-string (string delim-char) |
|||
(let ((result '())) |
|||
(do* ((start 0 (1+ end)) |
|||
(end (position delim-char string) |
|||
(position delim-char string :start start))) |
|||
((not end) (reverse (cons (subseq string start) result))) |
|||
(push (subseq string start end) result)))) |
|||
;;; HTML escape code modified from: |
|||
;;; http://www.gigamonkeys.com/book/practical-an-html-generation-library-the-interpreter.html |
|||
(defun escape-char (char) |
|||
(case char |
|||
(#\& "&") |
|||
(#\< "<") |
|||
(#\> ">") |
|||
(t (format nil "&#~d;" (char-code char))))) |
|||
(defun escape (in) |
|||
(let ((to-escape "<>&")) |
|||
(flet ((needs-escape-p (char) (find char to-escape))) |
|||
(with-output-to-string (out) |
|||
(loop for start = 0 then (1+ pos) |
|||
for pos = (position-if #'needs-escape-p in :start start) |
|||
do (write-sequence in out :start start :end pos) |
|||
when pos do (write-sequence (escape-char (char in pos)) out) |
|||
while pos))))) |
|||
(defun html-row (values headerp) |
|||
(let ((tag (if headerp "th" "td"))) |
|||
(with-output-to-string (out) |
|||
(write-string "<tr>" out) |
|||
(dolist (val values) |
|||
(format out "<~A>~A</~A>" tag (escape val) tag)) |
|||
(write-string "</tr>" out)))) |
|||
(defun csv->html (csv) |
|||
(let* ((lines (split-string csv #\Newline)) |
|||
(cols (split-string (first lines) #\,)) |
|||
(rows (mapcar (lambda (row) (split-string row #\,)) (rest lines)))) |
|||
(with-output-to-string (html) |
|||
(format html "<table>~C" #\Newline) |
|||
(format html "~C~A~C" #\Tab (html-row cols t) #\Newline) |
|||
(dolist (row rows) |
|||
(format html "~C~A~C" #\Tab (html-row row nil) #\Newline)) |
|||
(write-string "</table>" html))))</syntaxhighlight> |
|||
<pre>CL-USER> (csv->html *csv*)</pre> |
|||
{{out}} |
|||
<syntaxhighlight lang=html5><table> |
|||
<tr><th>Character</th><th>Speech</th></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> |
|||
<tr><td>The multitude</td><td>Who are you?</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></syntaxhighlight> |
|||
=={{header|D}}== |
|||
{{trans|C}} |
|||
<syntaxhighlight lang=d>void main() { |
|||
import std.stdio; |
|||
immutable input = |
|||
"Character,Speech\n" ~ |
|||
"The multitude,The messiah! Show us the messiah!\n" ~ |
|||
"Brians mother,<angry>Now you listen here! He's not the messiah; " ~ |
|||
"he's a very naughty boy! Now go away!</angry>\n" ~ |
|||
"The multitude,Who are you?\n" ~ |
|||
"Brians mother,I'm his mother; that's who!\n" ~ |
|||
"The multitude,Behold his mother! Behold his mother!"; |
|||
"<html>\n<head><meta charset=\"utf-8\"></head>\n<body>\n\n".write; |
|||
"<table border=\"1\" cellpadding=\"5\" cellspacing=\"0\">\n<thead>\n <tr><td>".write; |
|||
bool theadDone = false; |
|||
foreach (immutable c; input) { |
|||
switch(c) { |
|||
case '\n': |
|||
if (theadDone) { |
|||
"</td></tr>\n <tr><td>".write; |
|||
} else { |
|||
"</td></tr>\n</thead>\n<tbody>\n <tr><td>".write; |
|||
theadDone = true; |
|||
} |
|||
break; |
|||
case ',': "</td><td>".write; break; |
|||
case '<': "<".write; break; |
|||
case '>': ">".write; break; |
|||
case '&': "&".write; break; |
|||
default: c.write; break; |
|||
} |
|||
} |
|||
"</td></tr>\n</tbody>\n</table>\n\n</body></html>".write; |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<syntaxhighlight lang=html5><html> |
|||
<head><meta charset="utf-8"></head> |
|||
<body> |
|||
<table border="1" cellpadding="5" cellspacing="0"> |
|||
<thead> |
|||
<tr><td>Character</td><td>Speech</td></tr> |
|||
</thead> |
|||
<tbody> |
|||
<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> |
|||
<tr><td>The multitude</td><td>Who are you?</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> |
|||
</tbody> |
|||
</table> |
|||
</body> |
|||
</html></syntaxhighlight> |
|||
=={{header|Delphi}}== |
=={{header|Delphi}}== |
||
Line 519: | Line 1,227: | ||
This solution solves both the basic and extra credit tasks. |
This solution solves both the basic and extra credit tasks. |
||
<syntaxhighlight lang=Delphi>program csv2html; |
|||
<lang Delphi> |
|||
program csv2html; |
|||
{$APPTYPE CONSOLE} |
{$APPTYPE CONSOLE} |
||
Line 566: | Line 1,272: | ||
'</body>'+CRLF+ |
'</body>'+CRLF+ |
||
'</html>'; |
'</html>'; |
||
{ Function to split a string into a list using a given delimiter } |
{ Function to split a string into a list using a given delimiter } |
||
Line 590: | Line 1,295: | ||
Rslt.Insert(0,fld); |
Rslt.Insert(0,fld); |
||
end; |
end; |
||
{ Simple CSV parser with option to specify that the first row is a header row } |
{ Simple CSV parser with option to specify that the first row is a header row } |
||
Line 649: | Line 1,353: | ||
end; |
end; |
||
end; |
end; |
||
var |
var |
||
Line 697: | Line 1,399: | ||
// Keep console window open |
// Keep console window open |
||
Readln; |
Readln; |
||
end.</syntaxhighlight> |
|||
{{out|Basic output}} |
|||
<syntaxhighlight lang=html5><!DOCTYPE html |
|||
end. |
|||
</lang> |
|||
'''Basic output:''' |
|||
<lang html5> |
|||
<!DOCTYPE html |
|||
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" |
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" |
||
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
||
Line 734: | Line 1,430: | ||
</table> |
</table> |
||
</body> |
</body> |
||
</html> |
</html></syntaxhighlight> |
||
</lang> |
|||
{{out|Extra credit output}} |
|||
<syntaxhighlight lang=html5><!DOCTYPE html |
|||
<lang html5> |
|||
<!DOCTYPE html |
|||
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" |
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" |
||
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
||
Line 767: | Line 1,461: | ||
</table> |
</table> |
||
</body> |
</body> |
||
</html> |
</html></syntaxhighlight> |
||
</lang> |
|||
=={{header|EasyLang}}== |
|||
<syntaxhighlight lang=text> |
|||
h$ = "<table border=1>\n<tr><th>" |
|||
repeat |
|||
s$ = input |
|||
until s$ = "" |
|||
write h$ |
|||
for c$ in strchars s$ |
|||
if c$ = "," |
|||
if scnd = 0 |
|||
c$ = "</th><th>" |
|||
else |
|||
c$ = "</td><td>" |
|||
. |
|||
elif c$ = "<" |
|||
c$ = "<" |
|||
elif c$ = ">" |
|||
c$ = ">" |
|||
elif c$ = "&" |
|||
c$ = "&" |
|||
. |
|||
write c$ |
|||
. |
|||
if scnd = 0 |
|||
h$ = "</th></tr>\n<tr><td>" |
|||
scnd = 1 |
|||
else |
|||
h$ = "</td></tr>\n<tr><td>" |
|||
. |
|||
. |
|||
print "</td></tr>\n</table>" |
|||
# |
|||
input_data |
|||
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! |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<table border=1> |
|||
<tr><th>Character</th><th>Speech</th></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> |
|||
<tr><td>The multitude</td><td>Who are you?</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> |
|||
=={{header|EchoLisp}}== |
|||
<syntaxhighlight lang=scheme> |
|||
;; CSV -> LISTS |
|||
(define (csv->row line) (string-split line ",")) |
|||
(define (csv->table csv) (map csv->row (string-split csv "\n"))) |
|||
;; LISTS->HTML |
|||
(define html 'html) |
|||
(define (emit-tag tag html-proc content ) |
|||
(if (style tag) |
|||
(push html (format "<%s style='%a'>" tag (style tag))) |
|||
(push html (format "<%s>" tag ))) |
|||
(html-proc content) |
|||
(push html (format "</%s> " tag ))) |
|||
;; html procs : 1 tag, 1 proc |
|||
(define (h-raw content) |
|||
(push html (format "%s" content))) |
|||
(define (h-header headers) |
|||
(for ((h headers)) (emit-tag 'th h-raw h))) |
|||
(define (h-row row) |
|||
(for ((item row)) (emit-tag 'td h-raw item))) |
|||
(define (h-table table ) |
|||
(emit-tag 'tr h-header (first table)) |
|||
(for ((row (rest table))) (emit-tag 'tr h-row row))) |
|||
(define (html-dump) (string-join (stack->list html) " ")) |
|||
;; STYLES |
|||
(style 'td "text-align:left") |
|||
(style 'table "border-spacing: 10px;border:28px ridge orange") ;; special biblical border |
|||
(style 'th "color:blue;") |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<syntaxhighlight lang=scheme> |
|||
;; changed <angry> to <b> to show that html tags inside text are correctly transmitted. |
|||
(define MontyPython #<< |
|||
Character,Speech |
|||
The multitude,The messiah! Show us the messiah! |
|||
Brians mother,<b>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</b> |
|||
The multitude,Who are you? |
|||
Brians mother,I'm his mother; that's who! |
|||
The multitude,Behold his mother! Behold his mother! |
|||
>>#) |
|||
(define (task speech) |
|||
(define table (csv->table speech)) |
|||
(stack html) |
|||
(emit-tag 'table h-table table) |
|||
(html-dump)) |
|||
(task MontyPython) |
|||
</syntaxhighlight> |
|||
<table style='border-spacing: 10px;border:28px ridge orange'> <tr> <th style='color:blue;'> Character </th> <th style='color:blue;'> Speech </th> </tr> <tr> <td style='text-align:left'> The multitude </td> <td style='text-align:left'> The messiah! Show us the messiah! </td> </tr> <tr> <td style='text-align:left'> Brians mother </td> <td style='text-align:left'> <b>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</b> </td> </tr> <tr> <td style='text-align:left'> The multitude </td> <td style='text-align:left'> Who are you? </td> </tr> <tr> <td style='text-align:left'> Brians mother </td> <td style='text-align:left'> I'm his mother; that's who! </td> </tr> <tr> <td style='text-align:left'> The multitude </td> <td style='text-align:left'> Behold his mother! Behold his mother! </td> </tr> </table> |
|||
=={{header|Ed}}== |
|||
Translation of [[Sed]] version |
|||
<syntaxhighlight> |
|||
H |
|||
g/&/s//\&/g |
|||
g/</s//\</g |
|||
g/>/s//\>/g |
|||
g/"/s//\"e;/g |
|||
g/^/s// <tr><td>/g |
|||
g/,/s//<\/td><td>/g |
|||
g/$/s//<\/td>\ |
|||
<\/tr>/ |
|||
1s/^/<table>\ |
|||
/ |
|||
$s/$/\ |
|||
<\/table>/ |
|||
,p |
|||
Q |
|||
</syntaxhighlight> |
|||
=={{header|Erlang}}== |
|||
Using functions from [[Create_an_HTML_table]] |
|||
<syntaxhighlight lang=Erlang> |
|||
-module( csv_to_html ). |
|||
-export( [table_translation/1, task/0] ). |
|||
table_translation( CSV ) -> |
|||
[Headers | Contents] = [string:tokens(X, ",") || X <- string:tokens( CSV, "\n")], |
|||
Table = create_html_table:html_table( [{border, "1"}, {cellpadding, "10"}], Headers, Contents ), |
|||
create_html_table:external_format( Table ). |
|||
task() -> table_translation( csv() ). |
|||
csv() -> |
|||
"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!". |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<table border=1 cellpadding=10><tr><th>Character</th><th>Speech</th></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><tr><td>The multitude</td><td>Who are you?</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> |
|||
=={{header|Euphoria}}== |
|||
{{trans|C}} |
|||
{{works with|Euphoria|4.*}} |
|||
<syntaxhighlight lang=euphoria>constant input = "Character,Speech\n" & |
|||
"The multitude,The messiah! Show us the messiah!\n" & |
|||
"Brians mother,<angry>Now you listen here! He's not the messiah; " & |
|||
"he's a very naughty boy! Now go away!</angry>\n" & |
|||
"The multitude,Who are you?\n" & |
|||
"Brians mother,I'm his mother; that's who!\n" & |
|||
"The multitude,Behold his mother! Behold his mother!" |
|||
puts(1,"<table>\n<tr><td>") |
|||
for i = 1 to length(input) do |
|||
switch input[i] do |
|||
case '\n' then puts(1,"</td></tr>\n<tr><td>") |
|||
case ',' then puts(1,"</td><td>") |
|||
case '<' then puts(1,"<") |
|||
case '>' then puts(1,">") |
|||
case '&' then puts(1,"&") |
|||
case else puts(1,input[i]) |
|||
end switch |
|||
end for |
|||
puts(1,"</td></tr>\n</table>")</syntaxhighlight> |
|||
{{out}} |
|||
<syntaxhighlight lang=html5><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>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> |
|||
<tr><td>The multitude</td><td>Who are you?</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></syntaxhighlight> |
|||
=={{header|F_Sharp|F#}}== |
|||
Use .NET XmlWriter. |
|||
Stylesheet styling is applied only when command line option <tt>-h</tt> ist given. |
|||
<syntaxhighlight lang=fsharp>open System |
|||
open System.Text |
|||
open System.Xml |
|||
let data = """ |
|||
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! |
|||
""" |
|||
let csv = |
|||
Array.map |
|||
(fun (line : string) -> line.Split(',')) |
|||
(data.Trim().Split([|'\n';'\r'|],StringSplitOptions.RemoveEmptyEntries)) |
|||
[<EntryPoint>] |
|||
let main argv = |
|||
let style = argv.Length > 0 && argv.[0] = "-h" |
|||
Console.OutputEncoding <- UTF8Encoding() |
|||
let xs = XmlWriterSettings() |
|||
xs.Indent <- true // be friendly to humans |
|||
use x = XmlWriter.Create(Console.Out, xs) |
|||
x.WriteStartDocument() |
|||
x.WriteDocType("HTML", null, null, null) // HTML5 |
|||
x.WriteStartElement("html") |
|||
x.WriteStartElement("head") |
|||
x.WriteElementString("title", "Rosettacode - CSV to HTML translation") |
|||
if style then |
|||
x.WriteStartElement("style"); x.WriteAttributeString("type", "text/css") |
|||
x.WriteString(""" |
|||
table { border-collapse: collapse; } |
|||
td, th { border: 1px solid black; padding: .25em} |
|||
th { background-color: #EEE; } |
|||
tbody th { font-weight: normal; font-size: 85%; } |
|||
""") |
|||
x.WriteEndElement() // style |
|||
x.WriteEndElement() // head |
|||
x.WriteStartElement("body") |
|||
x.WriteStartElement("table") |
|||
x.WriteStartElement("thead"); x.WriteStartElement("tr") |
|||
for part in csv.[0] do x.WriteElementString("th", part) |
|||
x.WriteEndElement(); x.WriteEndElement() // tr thead |
|||
x.WriteStartElement("tbody") |
|||
for line in csv.[1..] do |
|||
x.WriteStartElement("tr") |
|||
x.WriteElementString("th", line.[0]) |
|||
x.WriteElementString("td", line.[1]) |
|||
x.WriteEndElement() // tr |
|||
x.Close() |
|||
0</syntaxhighlight> |
|||
{{out}} (stylesheet version) |
|||
<div style="font-size:70%"> |
|||
<syntaxhighlight lang=html5><?xml version="1.0" encoding="utf-8"?> |
|||
<!DOCTYPE HTML > |
|||
<html> |
|||
<head> |
|||
<title>Rosettacode - CSV to HTML translation</title> |
|||
<style type="text/css"> |
|||
table { border-collapse: collapse; } |
|||
td, th { border: 1px solid black; padding: .25em} |
|||
th { background-color: #EEE; } |
|||
tbody th { font-weight: normal; font-size: 85%; } |
|||
</style> |
|||
</head> |
|||
<body> |
|||
<table> |
|||
<thead> |
|||
<tr> |
|||
<th>Character</th> |
|||
<th>Speech</th> |
|||
</tr> |
|||
</thead> |
|||
<tbody> |
|||
<tr> |
|||
<th>The multitude</th> |
|||
<td>The messiah! Show us the messiah!</td> |
|||
</tr> |
|||
<tr> |
|||
<th>Brians mother</th> |
|||
<td><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry></td> |
|||
</tr> |
|||
<tr> |
|||
<th>The multitude</th> |
|||
<td>Who are you?</td> |
|||
</tr> |
|||
<tr> |
|||
<th>Brians mother</th> |
|||
<td>I'm his mother; that's who!</td> |
|||
</tr> |
|||
<tr> |
|||
<th>The multitude</th> |
|||
<td>Behold his mother! Behold his mother!</td> |
|||
</tr> |
|||
</tbody> |
|||
</table> |
|||
</body> |
|||
</html></syntaxhighlight></div> |
|||
=={{header|Factor}}== |
|||
<syntaxhighlight lang=factor>USING: csv html.streams prettyprint xml.writer ; |
|||
"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!" |
|||
string>csv [ simple-table. ] with-html-writer pprint-xml</syntaxhighlight> |
|||
{{out}} |
|||
<syntaxhighlight lang=html5><table style="display: inline-table; border-collapse: collapse;"> |
|||
<tr> |
|||
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; "> |
|||
Character |
|||
</td> |
|||
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; "> |
|||
Speech |
|||
</td> |
|||
</tr> |
|||
<tr> |
|||
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; "> |
|||
The multitude |
|||
</td> |
|||
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; "> |
|||
The messiah! Show us the messiah! |
|||
</td> |
|||
</tr> |
|||
<tr> |
|||
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; "> |
|||
Brians mother |
|||
</td> |
|||
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; "> |
|||
<angry>Now you listen here! He's not the messiah; he's a very |
|||
naughty boy! Now go away!</angry> |
|||
</td> |
|||
</tr> |
|||
<tr> |
|||
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; "> |
|||
The multitude |
|||
</td> |
|||
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; "> |
|||
Who are you? |
|||
</td> |
|||
</tr> |
|||
<tr> |
|||
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; "> |
|||
Brians mother |
|||
</td> |
|||
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; "> |
|||
I'm his mother; that's who! |
|||
</td> |
|||
</tr> |
|||
<tr> |
|||
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; "> |
|||
The multitude |
|||
</td> |
|||
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; "> |
|||
Behold his mother! Behold his mother! |
|||
</td> |
|||
</tr> |
|||
</table> |
|||
<br/></syntaxhighlight> |
|||
=={{header|Forth}}== |
|||
{{works with|gforth}} |
|||
{{trans|C}} |
|||
<syntaxhighlight lang=forth>: BEGIN-COLUMN ." <td>" ; |
|||
: END-COLUMN ." </td>" ; |
|||
: BEGIN-ROW ." <tr>" BEGIN-COLUMN ; |
|||
: END-ROW END-COLUMN ." </tr>" CR ; |
|||
: CSV2HTML |
|||
." <table>" CR BEGIN-ROW |
|||
BEGIN KEY DUP #EOF <> WHILE |
|||
CASE |
|||
10 OF END-ROW BEGIN-ROW ENDOF |
|||
[CHAR] , OF END-COLUMN BEGIN-COLUMN ENDOF |
|||
[CHAR] < OF ." <" ENDOF |
|||
[CHAR] > OF ." >" ENDOF |
|||
[CHAR] & OF ." &" ENDOF |
|||
DUP EMIT |
|||
ENDCASE |
|||
REPEAT |
|||
END-ROW ." </table>" CR |
|||
; |
|||
CSV2HTML BYE</syntaxhighlight> |
|||
{{out}} |
|||
<syntaxhighlight lang=html5><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>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> |
|||
<tr><td>The multitude</td><td>Who are you?</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></syntaxhighlight> |
|||
=={{header|Fortran}}== |
|||
The plan here is to scan each line for commas to find the splitters between texts, then write the texts out with suitable td and /td swaddling. Text presented as a quoted string (that could thereby contain a comma that is not a delimiter) is explicitly not recognised and apostrophes are apostrophes. Similarly, no attempt is made to recognise characters within the texts that might cause trouble, either because they will not elicit the same glyph when rendered from .html, or because they trigger interpretative action rather than be passed through as-is. Thus the lurking "angry" markings in the texts are not given special conversion. Further, leading and trailing spaces in each text are not trimmed off as the .html rendition process ignores them anyway. To remove them would seem to be easy enough via the special intrinsic function TRIM (available in F90) were it not that it trims off only ''trailing'' spaces. Otherwise one could mess with arrays such as IST and LST to finger the first and last character position of each text and deal patiently with the complications attendant on entirely blank or null strings when scanning the input line to determine the values. |
|||
The source is only just F90. It uses an array with a specified lower bound (of zero) - with older Fortran you could play with EQUIVALENCE or else diligently remember the offset. More difficult is the arrangement for ascertaining the length of a record as it is being read, with protection against overflowing the scratchpad should it be longer than is allowed for. This requires the Q format code, and was a common extension to F77. As usual, in the absence of help from the filesystem, the length of the longest record could only be determined through reading the entire file, and for this example it is not worth the annoyance. In the absence of the Q format feature, the input record would be read into ALINE with possibly many trailing spaces added to fill out the scratchpad; more time would be lost in scanning backwards to find the last non-blank. Should a record be longer than the scratchpad then its tail end would be lost and no remark made. Some systems (such as Snobol) are able to read a record and prepare a text variable of the correct size for that record, whatever its length (up to some integer limit such as 65535, or even more) but Fortran is not one such. |
|||
The key statement is the compound WRITE statement and its associated FORMAT, and for non-furrytranners these proceedings may be obscure. A WRITE statement accepts a list of items to be written in the usual manner, and an item is usually the name of a variable or an expression (such as 2*X + 1), but it can also be an implied DO-list. In this case the item is enclosed in brackets, and within them starts with an item list followed by a comma and a DO-expression, as in <code>(a,b,c, I = 1,N)</code> where ''a'', ''b'' and ''c'' constitute a three-element list of items - any of which could themselves be an implied DO-list if nested lists were desired. Thus, by one means or another, the WRITE statement produces a sequence of values (of various types) to be sent forth. In association with the list of entries in the nominated FORMAT statement: processing proceeds in parallel through both lists in the manner of coroutines, not sequentially. It is possible with a READ statement that a value just read adjusts a part of a FORMAT statement not yet encountered during the processing of the READ statement. For instance FORMAT 13 could have <NCOLS> instead of 666 as its repeat count, but this is an extension to F77 and avoided here to stick with older abilities except where inconvenient. |
|||
Thus, the entire line for a table row can be written in one go, starting with the tr then many repetitions of (WOT,''text'', WOT), with each starting and ending WOT appropriately encased by < and > and / characters supplied by the FORMAT. This repeated pattern is unsuitable for starting the line (so the tr is provided by the FORMAT, with indentation) because then it would be out of step, but happily, after the last text is rolled, the required /tr can be sent to use the format codes that would normally be used for the start of the next text. Humm. If there ''were'' to be 666 texts to roll, this will exhaust the FORMAT statement and it will write the current line out and start a fresh one, resuming at the rightmost ( in the FORMAT specification. This rule does not always prove convenient so I'd prefer the vertical bar usage of musical notation to mark the resumption location. Interpreters of html do not care about layout but humans do, so, just in case, the repeat count should be 667 (or <NCOLS + 1>), or, if <NCOLS> were used, there could follow a <code>,A</code> in the FORMAT, or, the "/tr" could be removed from the WRITE and appended to end the FORMAT just as at its start, but enough. |
|||
The check for a comma is not <code>ALINE(I:I).EQ.","</code> because in other work this usage has been found to evoke astoundingly bad code, notably that ''both'' appearances of "I" are checked as being within bounds, and, the length is calculated by subtracting the "first" (I) from the "last" (also I) at run time! At least by the COMPAQ F90/95 compiler. By contrast, the ICHAR usage, which can only be for a single character, lacks this madness and far superior speed results. Not important in this example, but it explains why this puzzling usage appeared in a prog. at the Culham Science Centre in source from an IBM mainframe. |
|||
<syntaxhighlight lang=Fortran> |
|||
SUBROUTINE CSVTEXT2HTML(FNAME,HEADED) !Does not recognise quoted strings. |
|||
Converts without checking field counts, or noting special characters. |
|||
CHARACTER*(*) FNAME !Names the input file. |
|||
LOGICAL HEADED !Perhaps its first line is to be a heading. |
|||
INTEGER MANY !How long is a piece of string? |
|||
PARAMETER (MANY=666) !This should suffice. |
|||
CHARACTER*(MANY) ALINE !A scratchpad for the input. |
|||
INTEGER MARK(0:MANY + 1) !Fingers the commas on a line. |
|||
INTEGER I,L,N !Assistants. |
|||
CHARACTER*2 WOT(2) !I don't see why a "table datum" could not be for either. |
|||
PARAMETER (WOT = (/"th","td"/)) !A table heding or a table datum |
|||
INTEGER IT !But, one must select appropriately. |
|||
INTEGER KBD,MSG,IN !A selection. |
|||
COMMON /IOUNITS/ KBD,MSG,IN !The caller thus avoids collisions. |
|||
OPEN(IN,FILE=FNAME,STATUS="OLD",ACTION="READ",ERR=661) !Go for the file. |
|||
WRITE (MSG,1) !Start the blather. |
|||
1 FORMAT ("<Table border=1>") !By stating that a table follows. |
|||
MARK(0) = 0 !Syncopation for the comma fingers. |
|||
N = 0 !No records read. |
|||
10 READ (IN,11,END = 20) L,ALINE(1:MIN(L,MANY)) !Carefully acquire some text. |
|||
11 FORMAT (Q,A) !Q = number of characters yet to read, A = characters. |
|||
N = N + 1 !So, a record has been read. |
|||
IF (L.GT.MANY) THEN !Perhaps it is rather long? |
|||
WRITE (MSG,12) N,L,MANY !Alas! |
|||
12 FORMAT ("Line ",I0," has length ",I0,"! My limit is ",I0) !Squawk/ |
|||
L = MANY !The limit actually read. |
|||
END IF !So much for paranoia. |
|||
IF (N.EQ.1 .AND. HEADED) THEN !Is the first line to be treated specially? |
|||
WRITE (MSG,*) "<tHead>" !Yep. Nominate a heading. |
|||
IT = 1 !And select "th" rather than "td". |
|||
ELSE !But mostly, |
|||
IT = 2 !Just another row for the table. |
|||
END IF !So much for the first line. |
|||
NCOLS = 0 !No commas have been seen. |
|||
DO I = 1,L !So scan the text for them. |
|||
IF (ICHAR(ALINE(I:I)).EQ.ICHAR(",")) THEN !Here? |
|||
NCOLS = NCOLS + 1 !Yes! |
|||
MARK(NCOLS) = I !The texts are between commas. |
|||
END IF !So much for that character. |
|||
END DO !On to the next. |
|||
NCOLS = NCOLS + 1 !This is why the + 1 for the size of MARK. |
|||
MARK(NCOLS) = L + 1 !End-of-line is as if a comma was one further along. |
|||
WRITE (MSG,13) !Now roll all the texts. |
|||
1 (WOT(IT), !This starting a cell, |
|||
2 ALINE(MARK(I - 1) + 1:MARK(I) - 1), !This being the text between the commas, |
|||
3 WOT(IT), !And this ending each cell. |
|||
4 I = 1,NCOLS), !For this number of columns. |
|||
5 "/tr" !And this ends the row. |
|||
13 FORMAT (" <tr>",666("<",A,">",A,"</",A,">")) !How long is a piece of string? |
|||
IF (N.EQ.1 .AND. HEADED) WRITE (MSG,*) "</tHead>" !Finish the possible header. |
|||
GO TO 10 !And try for another record. |
|||
20 CLOSE (IN) !Finished with input. |
|||
WRITE (MSG,21) !And finished with output. |
|||
21 FORMAT ("</Table>") !This writes starting at column one. |
|||
RETURN !Done! |
|||
Confusions. |
|||
661 WRITE (MSG,*) "Can't open file ",FNAME !Alas. |
|||
END !So much for the conversion. |
|||
INTEGER KBD,MSG,IN |
|||
COMMON /IOUNITS/ KBD,MSG,IN |
|||
KBD = 5 !Standard input. |
|||
MSG = 6 !Standard output. |
|||
IN = 10 !Some unspecial number. |
|||
CALL CSVTEXT2HTML("Text.csv",.FALSE.) !The first line is not special. |
|||
WRITE (MSG,*) |
|||
CALL CSVTEXT2HTML("Text.csv",.TRUE.) !The first line is a heading. |
|||
END |
|||
</syntaxhighlight> |
|||
{{Output}} |
|||
<pre> |
|||
<Table border=1> |
|||
<tr><td>Character</td><td>Speech</td></tr> |
|||
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr> |
|||
<tr><td>Brian's 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> |
|||
<tr><td>The multitude</td><td>Who are you?</td></tr> |
|||
<tr><td>Brian's 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> |
|||
<Table border=1> |
|||
<tHead> |
|||
<tr><th>Character</th><th>Speech</th></tr> |
|||
</tHead> |
|||
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr> |
|||
<tr><td>Brian's 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> |
|||
<tr><td>The multitude</td><td>Who are you?</td></tr> |
|||
<tr><td>Brian's 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> |
|||
</pre> |
|||
And interpreted, first with no special heading: |
|||
<Table border=1> |
|||
<tr><td>Character</td><td>Speech</td></tr> |
|||
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr> |
|||
<tr><td>Brian's 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> |
|||
<tr><td>The multitude</td><td>Who are you?</td></tr> |
|||
<tr><td>Brian's 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> |
|||
<Table border=1> |
|||
<tHead> |
|||
<tr><th>Character</th><th>Speech</th></tr> |
|||
</tHead> |
|||
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr> |
|||
<tr><td>Brian's 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> |
|||
<tr><td>The multitude</td><td>Who are you?</td></tr> |
|||
<tr><td>Brian's 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> |
|||
=={{header|FreeBASIC}}== |
|||
<syntaxhighlight lang=freebasic>Data "Character,Speech" |
|||
Data "The multitude,The messiah! Show us the messiah!" |
|||
Data "Brian's mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>" |
|||
Data "The multitude,Who are you?" |
|||
Data "Brian's mother,I'm his mother; that's who!" |
|||
Data "The multitude,Behold his mother! Behold his mother!" |
|||
Data "***" |
|||
Print "<!DOCTYPE html>" & Chr(10) & "<html>" |
|||
Print "<head>" |
|||
Print "</head>" & Chr(10) |
|||
Print "<body>" |
|||
Print "<h1 style=""text-align:center"">CSV to html translation </h1>" |
|||
Print: Print "<table border = 1 cellpadding = 10 cellspacing = 0>" |
|||
Dim As Boolean header = true |
|||
Dim As String cadenaCSV, txt |
|||
Do |
|||
Read cadenaCSV |
|||
If cadenaCSV = "***" then Exit Do |
|||
If header then |
|||
Print "<thead bgcolor=""green"">" & Chr(10) & "<tr><th>"; |
|||
Else |
|||
Print "<tr><td>"; |
|||
End If |
|||
For i As Integer = 1 To Len(cadenaCSV) |
|||
txt = Mid(cadenaCSV, i, 1) |
|||
Select Case txt |
|||
Case ",": If header then Print "</th><th>"; Else Print "</td><td>"; |
|||
Case "<": Print "<"; |
|||
Case ">": Print ">"; |
|||
Case "&": Print "&"; |
|||
Case Else: Print txt; |
|||
End Select |
|||
Next i |
|||
If header then |
|||
Print "</th></tr>" & Chr(10) & "</thead>" & Chr(10) & "<tbody bgcolor=""yellow"">" |
|||
Else |
|||
Print "</td></tr>" |
|||
End If |
|||
header = false |
|||
Loop Until false |
|||
Print "</tbody>" & Chr(10) & "</table>" |
|||
Print Chr(10) & "</body>" |
|||
Print "</html>" |
|||
Sleep</syntaxhighlight> |
|||
=={{header|FutureBasic}}== |
|||
FB has a native Macintosh web browser, so the HTML output, along with extra credit, is shown in the accompanying screenshot. This code produces a complete, launchable application. |
|||
<syntaxhighlight lang=futurebasic> |
|||
output file "CSV2HTML" |
|||
include "Tlbx WebKit.incl" |
|||
_window = 1 |
|||
begin enum output 1 |
|||
_webView |
|||
end enum |
|||
void local fn BuildWindow |
|||
CGRect r = fn CGRectMake( 0, 0, 600, 220 ) |
|||
window _window, @"Rosetta Code CSV to HTML", r, NSWindowStyleMaskTitled + NSWindowStyleMaskClosable |
|||
r = fn CGRectMake( 20, 20, 560, 180 ) |
|||
wkwebview _webView, r,, _window |
|||
end fn |
|||
local fn CSV2HTML as CFStringRef |
|||
NSUInteger i, count |
|||
CFStringRef csvStr = @"Character,Speech\n¬ |
|||
The multitude,The messiah! Show us the messiah!\n¬ |
|||
Brians mother,Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!\n¬ |
|||
The multitude,Who are you\n¬ |
|||
Brians mother,I'm his mother; that's who!\n¬ |
|||
The multitude,Behold his mother! Behold his mother!" |
|||
CFArrayRef linesArray = fn StringComponentsSeparatedByString( csvStr, @"\n" ) |
|||
CFMutableStringRef htmlStr = fn MutableStringWithCapacity(0) |
|||
MutableStringAppendString( htmlStr, @"<table style=\"background:#eee;\">\n" ) |
|||
MutableStringAppendString( htmlStr, @"<tr bgcolor=wheat><th>Character</th><th>Speech</th></tr>" ) |
|||
MutableStringAppendString( htmlStr, @"<caption>From Monty Python's \"The Life of Brian\"</caption>\n" ) |
|||
count = len( linesArray ) |
|||
for i = 1 to count - 1 |
|||
CFStringRef tempStr = linesArray[i] |
|||
CFArrayRef tempArr = fn StringComponentsSeparatedByString( tempStr, @"," ) |
|||
MutableStringAppendString( htmlStr, @"<tr>\n" ) |
|||
MutableStringAppendString( htmlStr, fn StringWithFormat( @"<td style=\"width:120px;\"><b>%@</b></td>>\n", tempArr[0] ) ) |
|||
MutableStringAppendString( htmlStr, fn StringWithFormat( @"<td><i>%@</i></td>\n", tempArr[1] ) ) |
|||
MutableStringAppendString( htmlStr, @"</tr>\n" ) |
|||
next |
|||
MutableStringAppendString( htmlStr, @"</table><br></br>" ) |
|||
end fn = fn StringWithString( htmlStr ) |
|||
local fn LoadHTML2WebView |
|||
CFStringRef htmlStr = fn CSV2HTML |
|||
fn WKWebViewLoadHTMLString( _webView, htmlStr, NULL ) |
|||
end fn |
|||
void local fn DoDialog( ev as long, tag as long, wnd as long, obj as CFTypeRef ) |
|||
select (ev) |
|||
case _windowWillClose : end |
|||
end select |
|||
end fn |
|||
on dialog fn DoDialog |
|||
fn BuildWindow |
|||
fn LoadHTML2WebView |
|||
HandleEvents |
|||
</syntaxhighlight> |
|||
{{output}} |
|||
[[File:CSV to HTML.png]] |
|||
=={{header|Go}}== |
=={{header|Go}}== |
||
<syntaxhighlight lang=go>package main |
|||
Minimalist version satisfies basic task requirements: |
|||
<lang go>package main |
|||
import ( |
import ( |
||
"bytes" |
"bytes" |
||
"encoding/csv" |
|||
"fmt" |
"fmt" |
||
"html/template" |
|||
"strings" |
"strings" |
||
"template" |
|||
) |
) |
||
var c = `Character,Speech |
|||
func main() { |
|||
fmt.Print(csvToHtml(`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!` |
||
func main() { |
|||
if h, err := csvToHtml(c); err != nil { |
|||
fmt.Println(err) |
|||
} else { |
|||
fmt.Print(h) |
|||
} |
|||
} |
} |
||
func csvToHtml( |
func csvToHtml(c string) (string, error) { |
||
data, err := csv.NewReader(bytes.NewBufferString(c)).ReadAll() |
|||
lines := strings.Split(csv, "\n") |
|||
if err != nil { |
|||
data := make([][]string, len(lines)) |
|||
return "", err |
|||
data[i] = strings.Split(l, ",") |
|||
} |
} |
||
var b |
var b strings.Builder |
||
template.Must |
err = template.Must(template.New("").Parse(`<table> |
||
{{range .}} <tr>{{range .}}<td>{{ |
{{range .}} <tr>{{range .}}<td>{{.}}</td>{{end}}</tr> |
||
{{end}}</table> |
{{end}}</table> |
||
`)).Execute(&b, data) |
`)).Execute(&b, data) |
||
return b.String() |
return b.String(), err |
||
}</ |
}</syntaxhighlight> |
||
Extra credit version |
Extra credit version accepts -h command line option to do the special formatting for the heading line. |
||
< |
<syntaxhighlight lang=go>package main |
||
import ( |
import ( |
||
"bytes" |
"bytes" |
||
"encoding/csv" |
|||
"flag" |
"flag" |
||
"fmt" |
"fmt" |
||
" |
"html/template" |
||
"strings" |
"strings" |
||
"template" |
|||
) |
) |
||
var csvStr = `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> |
||
Line 824: | Line 2,172: | ||
headings := flag.Bool("h", false, "format first row as column headings") |
headings := flag.Bool("h", false, "format first row as column headings") |
||
flag.Parse() |
flag.Parse() |
||
if html, err := csvToHtml( |
if html, err := csvToHtml(csvStr, *headings); err != nil { |
||
fmt.Println( |
fmt.Println(err) |
||
} else { |
} else { |
||
fmt. |
fmt.Print(html) |
||
} |
} |
||
} |
} |
||
func csvToHtml(csvStr string, headings bool) (string, error) { |
|||
type line struct { |
|||
data, err := csv.NewReader(bytes.NewBufferString(csvStr)).ReadAll() |
|||
Character, Speech string |
|||
if err != nil { |
|||
} |
|||
return "", err |
|||
type script struct { |
|||
Headings line |
|||
Body []line |
|||
} |
|||
func csvToHtml(csv string, specialHeadings bool) (string, os.Error) { |
|||
lines := strings.Split(csv, "\n") |
|||
if len(lines) == 0 { |
|||
return "", os.NewError("no data") |
|||
} |
|||
var s script |
|||
if specialHeadings { |
|||
hl := strings.Split(lines[0], ",") |
|||
if len(hl) != 2 { |
|||
return "", fmt.Errorf( |
|||
"%d column headings found. 2 required", len(hl)) |
|||
} |
|||
s.Headings = line{hl[0], hl[1]} |
|||
lines = lines[1:] |
|||
} |
|||
s.Body = make([]line, len(lines)) |
|||
for i, l := range lines { |
|||
bl := strings.Split(l, ",") |
|||
if len(bl) != 2 { |
|||
return "", fmt.Errorf( |
|||
"%d csv items found in line %d of script. 2 required", |
|||
len(bl), i+1) |
|||
} |
|||
s.Body[i] = line{bl[0], bl[1]} |
|||
} |
} |
||
tStr := tPlain |
|||
set := template.SetMust(new(template.Set).Parse(tables)) |
|||
if headings { |
|||
tmpl := "tableNoHeadings" |
|||
tStr = tHeadings |
|||
if specialHeadings { |
|||
tmpl = "tableWithHeadings" |
|||
} |
} |
||
var b |
var b strings.Builder |
||
err |
err = template.Must(template.New("").Parse(tStr)).Execute(&b, data) |
||
return b.String(), err |
return b.String(), err |
||
} |
} |
||
const ( |
|||
var tables = ` |
|||
tPlain = `<table> |
|||
{{define "body"}}{{range .}} <tr><td>{{html .Character}}</td><td>{{html .Speech}}</td></tr> |
|||
{{range .}} <tr>{{range .}}<td>{{.}}</td>{{end}}</tr> |
|||
{{end}}{{end}} |
|||
{{end}}</table> |
|||
` |
|||
{{define "tableNoHeadings"}}<table> |
|||
tHeadings = `<table>{{if .}} |
|||
{{template "body" .Body}}</table> |
|||
{{range $x, $e := .}}{{if $x}} |
|||
{{end}} |
|||
<tr>{{range .}}<td>{{.}}</td>{{end}}</tr>{{else}} <thead> |
|||
<tr>{{range .}}<th>{{.}}</th>{{end}}</tr> |
|||
{{define "tableWithHeadings"}}<table> |
|||
</thead> |
|||
<tr><th>{{html .Headings.Character}}</th><th>{{html .Headings.Speech}}</th></tr> |
|||
<tbody>{{end}}{{end}} |
|||
{{template "body" .Body}}</table> |
|||
{{end}} |
</tbody>{{end}} |
||
Output of the extra credit version, with and without the -h option: |
|||
<lang html5> |
|||
> csv2html -h |
|||
<table> |
|||
<tr><th>Character</th><th>Speech</th></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> |
|||
<tr><td>The multitude</td><td>Who are you?</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> |
</table> |
||
` |
|||
)</syntaxhighlight> |
|||
> csv2html |
|||
{{out}} |
|||
<table> |
|||
Extra credit version with -h |
|||
<syntaxhighlight lang=html><table> |
|||
<thead> |
|||
<tr><th>Character</th><th>Speech</th></tr> |
|||
</thead> |
|||
<tbody> |
|||
<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> |
|||
<tr><td>The multitude</td><td>Who are you?</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> |
|||
</tbody> |
|||
</table></syntaxhighlight> |
|||
Basic version, or extra credit version without -h |
|||
<syntaxhighlight lang=html><table> |
|||
<tr><td>Character</td><td>Speech</td></tr> |
<tr><td>Character</td><td>Speech</td></tr> |
||
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr> |
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr> |
||
Line 907: | Line 2,230: | ||
<tr><td>Brians mother</td><td>I'm his mother; that's who!</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> |
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr> |
||
</table> |
</table></syntaxhighlight> |
||
</lang> |
|||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
Line 914: | Line 2,236: | ||
===Solution #1: Nested GStrings=== |
===Solution #1: Nested GStrings=== |
||
Brute force solution using nested GStrings. It solves both the basic and extra credit tasks. |
Brute force solution using nested GStrings. It solves both the basic and extra credit tasks. |
||
< |
<syntaxhighlight lang=groovy>def formatCell = { cell -> |
||
"<td>${cell.replaceAll('&','&').replaceAll('<','<')}</td>" |
"<td>${cell.replaceAll('&','&').replaceAll('<','<')}</td>" |
||
} |
} |
||
Line 951: | Line 2,273: | ||
<body>${formatTable(csv, header)}</body> |
<body>${formatTable(csv, header)}</body> |
||
</html>""" |
</html>""" |
||
}</ |
}</syntaxhighlight> |
||
'''Test:''' |
'''Test:''' |
||
< |
<syntaxhighlight lang=groovy>def csv = '''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> |
||
Line 970: | Line 2,292: | ||
println '-----------------------------------------' |
println '-----------------------------------------' |
||
println (formatPage('Extra Credit', csv, true)) |
println (formatPage('Extra Credit', csv, true)) |
||
println '-----------------------------------------'</ |
println '-----------------------------------------'</syntaxhighlight> |
||
{{out|Basic output}} |
|||
<div style="height:30ex;overflow:scroll;">< |
<div style="height:30ex;overflow:scroll;"><syntaxhighlight lang=html5><html> |
||
<head> |
<head> |
||
<title>Basic</title> |
<title>Basic</title> |
||
Line 991: | Line 2,313: | ||
</table> |
</table> |
||
</body> |
</body> |
||
</html></ |
</html></syntaxhighlight></div> |
||
[[File:Groovy-csv-to-html-basic.jpg]] <br>Appearance as rendered in Google Chrome. |
[[File:Groovy-csv-to-html-basic.jpg]] <br>Appearance as rendered in Google Chrome. |
||
{{out|Extra Credit output}} |
|||
<div style="height:30ex;overflow:scroll;">< |
<div style="height:30ex;overflow:scroll;"><syntaxhighlight lang=html5><html> |
||
<head> |
<head> |
||
<title>Extra Credit</title> |
<title>Extra Credit</title> |
||
Line 1,017: | Line 2,339: | ||
</table> |
</table> |
||
</body> |
</body> |
||
</html></ |
</html></syntaxhighlight></div> |
||
[[File:Groovy-csv-to-html-extra.jpg]] <br>Appearance as rendered in Google Chrome. |
[[File:Groovy-csv-to-html-extra.jpg]] <br>Appearance as rendered in Google Chrome. |
||
===Solution #2: MarkupBuilder=== |
===Solution #2: MarkupBuilder=== |
||
A much cleaner solution using the Groovy XML MarkupBuilder class. It solves both the basic and extra credit tasks. |
A much cleaner solution using the Groovy XML MarkupBuilder class. It solves both the basic and extra credit tasks. |
||
< |
<syntaxhighlight lang=groovy>import groovy.xml.MarkupBuilder |
||
def formatRow = { doc, row -> |
def formatRow = { doc, row -> |
||
Line 1,051: | Line 2,373: | ||
} |
} |
||
writer.toString() |
writer.toString() |
||
}</ |
}</syntaxhighlight> |
||
'''Test:'''<br/> |
'''Test:'''<br/> |
||
The interface is the same for both solutions, so we just reuse the same test as before. |
The interface is the same for both solutions, so we just reuse the same test as before. |
||
{{out|Basic output}} |
|||
<div style="height:30ex;overflow:scroll;">< |
<div style="height:30ex;overflow:scroll;"><syntaxhighlight lang=html5><html> |
||
<head> |
<head> |
||
<title>Basic</title> |
<title>Basic</title> |
||
Line 1,093: | Line 2,415: | ||
</table> |
</table> |
||
</body> |
</body> |
||
</html></ |
</html></syntaxhighlight></div> |
||
The HTML for this solution looks superficially different than that from the GString solution, but the appearance as rendered in Google Chrome is identical. |
The HTML for this solution looks superficially different than that from the GString solution, but the appearance as rendered in Google Chrome is identical. |
||
'''Extra Credit output:''' |
'''Extra Credit output:''' |
||
<div style="height:30ex;overflow:scroll;">< |
<div style="height:30ex;overflow:scroll;"><syntaxhighlight lang=html5><html> |
||
<head> |
<head> |
||
<title>Extra Credit</title> |
<title>Extra Credit</title> |
||
Line 1,138: | Line 2,460: | ||
</table> |
</table> |
||
</body> |
</body> |
||
</html></ |
</html></syntaxhighlight></div> |
||
The HTML for this solution looks superficially different than that from the GString solution, but the appearance as rendered in Google Chrome is identical. |
The HTML for this solution looks superficially different than that from the GString solution, but the appearance as rendered in Google Chrome is identical. |
||
Line 1,144: | Line 2,466: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
'''Simple solution''' |
'''Simple solution''' |
||
< |
<syntaxhighlight lang=haskell>--import Data.List.Split (splitOn) -- if the import is available |
||
splitOn :: Char -> String -> [String] -- otherwise |
splitOn :: Char -> String -> [String] -- otherwise |
||
splitOn delim = foldr (\x rest -> |
splitOn delim = foldr (\x rest -> |
||
Line 1,169: | Line 2,491: | ||
in "<table>\n" ++ html ++ "</table>" |
in "<table>\n" ++ html ++ "</table>" |
||
main = interact csvToTable</ |
main = interact csvToTable</syntaxhighlight> |
||
'''Compact version''' |
'''Compact version''' |
||
< |
<syntaxhighlight lang=haskell>import Data.List (unfoldr) |
||
split p = unfoldr (\s -> case dropWhile p s of [] -> Nothing |
split p = unfoldr (\s -> case dropWhile p s of [] -> Nothing |
||
ss -> Just $ break p ss) |
ss -> Just $ break p ss) |
||
Line 1,182: | Line 2,504: | ||
'&' -> "&"; '"' -> """; _ -> [c]}) x |
'&' -> "&"; '"' -> """; _ -> [c]}) x |
||
++ "</td>\n") cols) |
++ "</td>\n") cols) |
||
++ "</tr>") . split (==',')) $ lines csv) ++ "</table>")</ |
++ "</tr>") . split (==',')) $ lines csv) ++ "</table>")</syntaxhighlight> |
||
;Output: |
;Output: |
||
<div style="height:30ex;overflow:scroll;">< |
<div style="height:30ex;overflow:scroll;"><syntaxhighlight lang=html5><table> |
||
<tr> |
<tr> |
||
<td>Character</td> |
<td>Character</td> |
||
Line 1,210: | Line 2,532: | ||
<td>Behold his mother! Behold his mother!</td> |
<td>Behold his mother! Behold his mother!</td> |
||
</tr> |
</tr> |
||
</table></ |
</table></syntaxhighlight></div> |
||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
This solution for the extra credit works in both Icon and Unicon. |
This solution for the extra credit works in both Icon and Unicon. |
||
The simple CSV is read from standard input and written to standard output. |
The simple CSV is read from standard input and written to standard output. |
||
The presence/abscend of "-heading" in the argument list sets the variable thead |
|||
to the procedure writes or a 1 (for more on this see [[Icon%2BUnicon/Intro#Conjunction.2C_yielding_a_different_result|Introduction to Icon/Unicon - Conjunction yielding different results]]). |
|||
< |
<syntaxhighlight lang=Icon>procedure main(arglist) |
||
pchar := &letters ++ &digits ++ '!?;. ' # printable chars |
pchar := &letters ++ &digits ++ '!?;. ' # printable chars |
||
Line 1,235: | Line 2,559: | ||
write(" </TBODY>") |
write(" </TBODY>") |
||
write("</TABLE>") |
write("</TABLE>") |
||
end</ |
end</syntaxhighlight> |
||
{{Out}} |
|||
Output: |
|||
< |
<syntaxhighlight lang=html5><TABLE> |
||
<THEAD> |
<THEAD> |
||
<TR><TD>Character</TD><TD>Speech</TD></TR> |
<TR><TD>Character</TD><TD>Speech</TD></TR> |
||
Line 1,249: | Line 2,573: | ||
<TR><TD>The multitude</TD><TD>Behold his mother! Behold his mother!</TD></TR> |
<TR><TD>The multitude</TD><TD>Behold his mother! Behold his mother!</TD></TR> |
||
</TBODY> |
</TBODY> |
||
</TABLE></ |
</TABLE></syntaxhighlight> |
||
=={{header|J}}== |
=={{header|J}}== |
||
'''Solution (extra credit)''' |
'''Solution (extra credit)''' |
||
< |
<syntaxhighlight lang=j>require 'strings tables/csv' |
||
encodeHTML=: ('&';'&';'<';'<';'>';'>')&stringreplace |
encodeHTML=: ('&';'&';'<';'<';'>';'>')&stringreplace |
||
Line 1,274: | Line 2,598: | ||
end. |
end. |
||
;markupTable markupRows t |
;markupTable markupRows t |
||
)</ |
)</syntaxhighlight> |
||
For those interested, equivalent tacit versions of <code>tag</code> and <code>makeHTMLtablefromCSV</code> are: |
For those interested, equivalent tacit versions of <code>tag</code> and <code>makeHTMLtablefromCSV</code> are: |
||
< |
<syntaxhighlight lang=j>tag=: adverb def '[: (,&.>/)"1 m&(0&{::@[ , 1&{::@[ ,~ ]) L:0@]' |
||
makeHTMLtablefromCSV6=: 0&$: : ([: ; markupTable@markupRows@([ markupCells`(markupHdrCells@{. , markupCells@}.)@.[ fixcsv@encodeHTML))</ |
makeHTMLtablefromCSV6=: 0&$: : ([: ; markupTable@markupRows@([ markupCells`(markupHdrCells@{. , markupCells@}.)@.[ fixcsv@encodeHTML))</syntaxhighlight> |
||
'''Example''' |
'''Example''' |
||
< |
<syntaxhighlight 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! |
||
Line 1,289: | Line 2,613: | ||
The multitude,Behold his mother! Behold his mother! |
The multitude,Behold his mother! Behold his mother! |
||
) |
) |
||
1 makeHTMLtablefromCSV CSVstrng</ |
1 makeHTMLtablefromCSV CSVstrng</syntaxhighlight> |
||
HTML output |
{{Out|HTML output}} |
||
< |
<syntaxhighlight lang=html5><table> |
||
<tr><th>Character</th><th>Speech</th></tr> |
<tr><th>Character</th><th>Speech</th></tr> |
||
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr> |
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr> |
||
Line 1,298: | Line 2,622: | ||
<tr><td>Brians mother</td><td>I'm his mother; that's who!</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> |
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr> |
||
</table></ |
</table></syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
'''Simple solution without escaping for Java v8+''' |
|||
{{output?|Java}} |
|||
<syntaxhighlight lang=java>String csv = "..."; |
|||
// Use Collectors.joining(...) for streaming, otherwise StringJoiner |
|||
StringBuilder html = new StringBuilder("<table>\n"); |
|||
Collector collector = Collectors.joining("</td><td>", " <tr><td>", "</td></tr>\n"); |
|||
for (String row : csv.split("\n") ) { |
|||
html.append(Arrays.stream(row.split(",")).collect(collector)); |
|||
} |
|||
html.append("</table>\n");</syntaxhighlight> |
|||
'''Solution including simple and extra credit version''' |
'''Solution including simple and extra credit version''' |
||
<tt>for simple solution : java -cp . Csv2Html < text.csv |
<tt>for simple solution : java -cp . Csv2Html < text.csv</tt> |
||
for extended solution: java -cp . Csv2Html header < text.csv |
<tt>for extended solution: java -cp . Csv2Html header < text.csv</tt> |
||
< |
<syntaxhighlight lang=java>import java.io.BufferedReader; |
||
import java.io.InputStreamReader; |
import java.io.InputStreamReader; |
||
import java.io.PrintStream; |
import java.io.PrintStream; |
||
Line 1,368: | Line 2,702: | ||
PrintStream stdout = System.out; |
PrintStream stdout = System.out; |
||
stdout. |
stdout.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">"); |
||
stdout.println("<html xmlns=\"http://www.w3.org/1999/xhtml\">"); |
|||
stdout.println("<head><meta http-equiv=\"Content-type\" content=\"text/html;charset=UTF-8\"/>"); |
|||
+ "<html xmlns=\"http://www.w3.org/1999/xhtml\">" |
|||
stdout.println("<title>Csv2Html</title>"); |
|||
+ "<head><meta http-equiv=\"Content-type\" content=\"text/html;charset=UTF-8\"/>" |
|||
stdout.println("<style type=\"text/css\">"); |
|||
stdout.println("body{background-color:#FFF;color:#000;font-family:OpenSans,sans-serif;font-size:10px;}"); |
|||
stdout.println("table{border:0.2em solid #2F6FAB;border-collapse:collapse;}"); |
|||
stdout.println("th{border:0.15em solid #2F6FAB;padding:0.5em;background-color:#E9E9E9;}"); |
|||
stdout.println("td{border:0.1em solid #2F6FAB;padding:0.5em;background-color:#F9F9F9;}</style>"); |
|||
stdout.println("</head><body><h1>Csv2Html</h1>"); |
|||
stdout.println("<table>"); |
stdout.println("<table>"); |
||
Line 1,394: | Line 2,728: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out|simple solution}} |
|||
<syntaxhighlight lang=html5><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
|||
<html xmlns="http://www.w3.org/1999/xhtml"> |
|||
<head><meta http-equiv="Content-type" content="text/html;charset=UTF-8"/> |
|||
<title>Csv2Html</title> |
|||
<style type="text/css"> |
|||
body{background-color:#FFF;color:#000;font-family:OpenSans,sans-serif;font-size:10px;} |
|||
table{border:0.2em solid #2F6FAB;border-collapse:collapse;} |
|||
th{border:0.15em solid #2F6FAB;padding:0.5em;background-color:#E9E9E9;} |
|||
td{border:0.1em solid #2F6FAB;padding:0.5em;background-color:#F9F9F9;}</style> |
|||
</head><body><h1>Csv2Html</h1> |
|||
<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>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> |
|||
<tr><td>The multitude</td><td>Who are you?</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></body></html></syntaxhighlight> |
|||
{{out|extended}} |
|||
<syntaxhighlight lang=html5><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
|||
<html xmlns="http://www.w3.org/1999/xhtml"> |
|||
<head><meta http-equiv="Content-type" content="text/html;charset=UTF-8"/> |
|||
<title>Csv2Html</title> |
|||
<style type="text/css"> |
|||
body{background-color:#FFF;color:#000;font-family:OpenSans,sans-serif;font-size:10px;} |
|||
table{border:0.2em solid #2F6FAB;border-collapse:collapse;} |
|||
th{border:0.15em solid #2F6FAB;padding:0.5em;background-color:#E9E9E9;} |
|||
td{border:0.1em solid #2F6FAB;padding:0.5em;background-color:#F9F9F9;}</style> |
|||
</head><body><h1>Csv2Html</h1> |
|||
<table> |
|||
<tr><th>Character</th><th>Speech</th></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> |
|||
<tr><td>The multitude</td><td>Who are you?</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></body></html></syntaxhighlight> |
|||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
< |
<syntaxhighlight lang=JavaScript>var csv = "Character,Speech\n" + |
||
"The multitude,The messiah! Show us the messiah!\n" + |
"The multitude,The messiah! Show us the messiah!\n" + |
||
"Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>\n" + |
"Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>\n" + |
||
Line 1,405: | Line 2,778: | ||
"The multitude,Behold his mother! Behold his mother!"; |
"The multitude,Behold his mother! Behold his mother!"; |
||
var lines = csv.replace(/&/g, '&') |
|||
.replace(/</g, '<') |
|||
.replace(/>/g, '>') |
|||
.replace(/"/g, '"') |
|||
.split(/[\n\r]/) |
|||
.map(function(line) { return line.split(',')}) |
|||
.map(function(row) {return '\t\t<tr><td>' + row[0] + '</td><td>' + row[1] + '</td></tr>';}); |
|||
console.log('<table>\n\t<thead>\n' + lines[0] + |
|||
var lines = csv.split(/[\n\r]+/g), |
|||
'\n\t</thead>\n\t<tbody>\n' + lines.slice(1).join('\n') + |
|||
'\t</tbody>\n</table>'); |
|||
line, |
|||
rows = "", |
|||
thead = '<tr>'+ |
|||
'<th>'+header[0]+'</th>'+ |
|||
'<th>'+header[1]+'</th>'+ |
|||
'</tr>\n'; |
|||
</syntaxhighlight> |
|||
for (var i=0, len=lines.length; i<len; i++) { |
|||
line = lines[i].split(","); |
|||
<syntaxhighlight lang=html5><table> |
|||
rows += '<tr>'+ |
|||
<thead> |
|||
'<td>'+line[0]+'</td>'+ |
|||
<tr><td>Character</td><td>Speech</td></tr> |
|||
'<td>'+line[1]+'</td>'+ |
|||
</thead> |
|||
<tbody> |
|||
<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> |
|||
<tr><td>The multitude</td><td>Who are you?</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> |
|||
</tbody> |
|||
</table></syntaxhighlight> |
|||
{{out}} |
|||
<syntaxhighlight lang=html5><table> |
|||
<thead> |
|||
<tr><td>Character</td><td>Speech</td></tr> |
|||
</thead> |
|||
<tbody> |
|||
<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> |
|||
<tr><td>The multitude</td><td>Who are you?</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> </tbody> |
|||
</table></syntaxhighlight> |
|||
=={{header|jq}}== |
|||
We will assume the input is in a file named csv2html.csv, and that the jq program as given below is in a file named csv2html.jqn. To simplify things, we will invoke the jq processor twice -- the first invocation simply converts the text input into a sequence of JSON strings: |
|||
<syntaxhighlight lang=jq>jq -R . csv2html.csv | jq -r -s -f csv2html.jq |
|||
</syntaxhighlight><syntaxhighlight lang=jq>def headerrow2html: |
|||
[" <thead> <tr>"] |
|||
+ (split(",") | map(" <th>\(@html)</th>")) |
|||
+ [ " </tr> </thead>" ] |
|||
; |
|||
def row2html: |
|||
[" <tr>"] |
|||
+ (split(",") | map(" <td>\(@html)</td>")) |
|||
+ [ " </tr>" ] |
|||
; |
|||
def csv2html: |
|||
def rows: reduce .[] as $row |
|||
([]; . + ($row | row2html)); |
|||
["<table>"] |
|||
+ (.[0] | headerrow2html) |
|||
+ (.[1:] | rows) |
|||
+ [ "</table>"] |
|||
; |
|||
csv2html | .[]</syntaxhighlight> |
|||
===Output=== |
|||
<syntaxhighlight lang=html5><table> |
|||
<thead> <tr> |
|||
<th>Character</th> |
|||
<th>Speech </th> |
|||
</tr> </thead> |
|||
<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> |
|||
<tr> |
|||
<td>The multitude</td> |
|||
<td>Who are you? </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></syntaxhighlight> |
|||
=={{header|Jsish}}== |
|||
From Javascript entry. |
|||
<syntaxhighlight lang=javascript>/* CSV to HTML, in Jsish */ |
|||
var csv = "Character,Speech\n" + |
|||
"The multitude,The messiah! Show us the messiah!\n" + |
|||
"Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>\n" + |
|||
"The multitude,Who are you?\n" + |
|||
"Brians mother,I'm his mother; that's who!\n" + |
|||
"The multitude,Behold his mother! Behold his mother!"; |
|||
var lines = csv.replace(/&/g, '&') |
|||
.replace(/</g, '<') |
|||
.replace(/>/g, '>') |
|||
.replace(/"/g, '"') |
|||
.split('\n') |
|||
.map(function(line) { return line.split(','); }) |
|||
.map(function(row) { return '\t\t<tr><td>' + row[0] + '</td><td>' + row[1] + '</td></tr>'; }); |
|||
if (Interp.conf('unitTest')) { |
|||
puts('<table>\n\t<thead>\n' + lines[0] + '\n\t</thead>\n\t<tbody>\n' |
|||
+ lines.slice(1).join('\n') + '\t</tbody>\n</table>'); |
|||
} |
} |
||
/* |
|||
console.log('<table><thead>\n' + thead + '</thead><tbody>\n' + rows + '</tbody></table>' );</lang> |
|||
=!EXPECTSTART!= |
|||
<table> |
|||
<thead> |
|||
<tr><td>Character</td><td>Speech</td></tr> |
|||
</thead> |
|||
<tbody> |
|||
<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> |
|||
<tr><td>The multitude</td><td>Who are you?</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> </tbody> |
|||
</table> |
|||
=!EXPECTEND!= |
|||
*/</syntaxhighlight> |
|||
{{out}} |
|||
<lang html5><table><thead> |
|||
<pre>prompt$ jsish -u csvToHTML.jsi |
|||
<tr><th>Character</th><th>Speech</th></tr> |
|||
[PASS] csvToHTML.jsi</pre> |
|||
</thead><tbody> |
|||
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr> |
|||
=={{header|Julia}}== |
|||
<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> |
|||
<syntaxhighlight lang=Julia>using DataFrames, CSV |
|||
<tr><td>The multitude</td><td>Who are you?</td></tr> |
|||
<tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr> |
|||
using CSV, DataFrames |
|||
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr> |
|||
</tbody></table></lang> |
|||
function csv2html(fname; header::Bool=false) |
|||
csv = CSV.read(fname) |
|||
@assert(size(csv, 2) > 0) |
|||
str = """ |
|||
<html> |
|||
<head> |
|||
<style type="text/css"> |
|||
body { |
|||
margin: 2em; |
|||
} |
|||
h1 { |
|||
text-align: center; |
|||
} |
|||
table { |
|||
border-spacing: 0; |
|||
box-shadow: 0 0 0.25em #888; |
|||
margin: auto; |
|||
} |
|||
table, |
|||
tr, |
|||
th, |
|||
td { |
|||
border-collapse: collapse; |
|||
} |
|||
th { |
|||
color: white; |
|||
background-color: rgb(43, 53, 59); |
|||
} |
|||
th, |
|||
td { |
|||
padding: 0.5em; |
|||
} |
|||
table tr:nth-child(even) td { |
|||
background-color: rgba(218, 224, 229, 0.850); |
|||
} |
|||
</style> |
|||
</head> |
|||
<body> |
|||
<h1>csv2html Example</h1> |
|||
<table> |
|||
<tr> |
|||
""" |
|||
tags = header ? ("<th>", "</th>") : ("<td>", "</td>") |
|||
for i=1:size(csv, 2) |
|||
str *= " " * tags[1] * csv[1, i] * tags[2] * "\n" |
|||
end |
|||
str *= " "^8 * "</tr>\n" |
|||
for i=2:size(csv, 1) |
|||
str *= " <tr>\n" |
|||
for j=1:size(csv, 2) |
|||
str *= " " * "<td>" * csv[i, j] * "</td>\n" |
|||
end |
|||
str *= " </tr>\n" |
|||
end |
|||
str * " </table>\n</body>\n\n</html>\n" |
|||
end |
|||
print(csv2html("input.csv", header=true)) |
|||
</syntaxhighlight>{{out}} |
|||
<syntaxhighlight lang=html5><html> |
|||
<head> |
|||
<style type="text/css"> |
|||
body { |
|||
margin: 2em; |
|||
} |
|||
h1 { |
|||
text-align: center; |
|||
} |
|||
table { |
|||
border-spacing: 0; |
|||
box-shadow: 0 0 0.25em #888; |
|||
margin: auto; |
|||
} |
|||
table, |
|||
tr, |
|||
th, |
|||
td { |
|||
border-collapse: collapse; |
|||
} |
|||
th { |
|||
color: white; |
|||
background-color: rgb(43, 53, 59); |
|||
} |
|||
th, |
|||
td { |
|||
padding: 0.5em; |
|||
} |
|||
table tr:nth-child(even) td { |
|||
background-color: rgba(218, 224, 229, 0.850); |
|||
} |
|||
</style> |
|||
</head> |
|||
<body> |
|||
<h1>csv2html Example</h1> |
|||
<table> |
|||
<tr> |
|||
<th>Character</th> |
|||
<th>Speech</th> |
|||
</tr> |
|||
<tr> |
|||
<td>The multitude</td> |
|||
<td>The messiah! Show us the messiah!</td> |
|||
</tr> |
|||
<tr> |
|||
<td>Brians mother</td> |
|||
<td>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</td> |
|||
</tr> |
|||
<tr> |
|||
<td>The multitude</td> |
|||
<td>Who are you?</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> |
|||
</body> |
|||
</html></syntaxhighlight> |
|||
=={{header|Kotlin}}== |
|||
<syntaxhighlight lang=scala>// version 1.1.3 |
|||
val csv = |
|||
"Character,Speech\n" + |
|||
"The multitude,The messiah! Show us the messiah!\n" + |
|||
"Brians mother,<angry>Now you listen here! He's not the messiah; " + |
|||
"he's a very naughty boy! Now go away!</angry>\n" + |
|||
"The multitude,Who are you?\n" + |
|||
"Brians mother,I'm his mother; that's who!\n" + |
|||
"The multitude,Behold his mother! Behold his mother!" |
|||
fun main(args: Array<String>) { |
|||
val i = " " // indent |
|||
val sb = StringBuilder("<table>\n$i<tr>\n$i$i<td>") |
|||
for (c in csv) { |
|||
sb.append( when (c) { |
|||
'\n' -> "</td>\n$i</tr>\n$i<tr>\n$i$i<td>" |
|||
',' -> "</td>\n$i$i<td>" |
|||
'&' -> "&" |
|||
'\'' -> "'" |
|||
'<' -> "<" |
|||
'>' -> ">" |
|||
else -> c.toString() |
|||
}) |
|||
} |
|||
sb.append("</td>\n$i</tr>\n</table>") |
|||
println(sb.toString()) |
|||
println() |
|||
// now using first row as a table header |
|||
sb.setLength(0) |
|||
sb.append("<table>\n$i<thead>\n$i$i<tr>\n$i$i$i<td>") |
|||
val hLength = csv.indexOf('\n') + 1 // find length of first row including CR |
|||
for (c in csv.take(hLength)) { |
|||
sb.append( when (c) { |
|||
'\n' -> "</td>\n$i$i</tr>\n$i</thead>\n$i<tbody>\n$i$i<tr>\n$i$i$i<td>" |
|||
',' -> "</td>\n$i$i$i<td>" |
|||
else -> c.toString() |
|||
}) |
|||
} |
|||
for (c in csv.drop(hLength)) { |
|||
sb.append( when (c) { |
|||
'\n' -> "</td>\n$i$i</tr>\n$i$i<tr>\n$i$i$i<td>" |
|||
',' -> "</td>\n$i$i$i<td>" |
|||
'&' -> "&" |
|||
'\'' -> "'" |
|||
'<' -> "<" |
|||
'>' -> ">" |
|||
else -> c.toString() |
|||
}) |
|||
} |
|||
sb.append("</td>\n$i$i</tr>\n$i</tbody>\n</table>") |
|||
println(sb.toString()) |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<syntaxhighlight lang=html5><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>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> |
|||
<tr> |
|||
<td>The multitude</td> |
|||
<td>Who are you?</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> |
|||
<table> |
|||
<thead> |
|||
<tr> |
|||
<td>Character</td> |
|||
<td>Speech</td> |
|||
</tr> |
|||
</thead> |
|||
<tbody> |
|||
<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> |
|||
<tr> |
|||
<td>The multitude</td> |
|||
<td>Who are you?</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> |
|||
</tbody> |
|||
</table> |
|||
</syntaxhighlight> |
|||
=={{header|Lambdatalk}}== |
|||
<syntaxhighlight lang=scheme> |
|||
{def CSV |
|||
Character,Speech\n |
|||
The multitude,The messiah! Show us the messiah!\n |
|||
Brians mother,Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!\n |
|||
The multitude,Who are you\n |
|||
Brians mother,I'm his mother; that's who!\n |
|||
The multitude,Behold his mother! Behold his mother!\n |
|||
} |
|||
-> CSV |
|||
{def csv2html |
|||
{lambda {:csv} |
|||
{table {@ style="background:#eee;"} |
|||
{S.replace ([^,]*),([^_]*)_ |
|||
by {tr {td {@ style="width:120px;"}{b €1}} {td {i €2}}} |
|||
in {S.replace \\n by _ in :csv}}}}} |
|||
-> csv2html |
|||
{csv2html {CSV}} -> |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<table style="background:#eee;"><tr><td style="width:120px;"><b>Character</b></td> <td><i>Speech</i></td></tr><tr><td style="width:120px;"><b> The multitude</b></td> <td><i>The messiah! Show us the messiah!</i></td></tr><tr><td style="width:120px;"><b> Brians mother</b></td> <td><i>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</i></td></tr><tr><td style="width:120px;"><b> The multitude</b></td> <td><i>Who are you</i></td></tr><tr><td style="width:120px;"><b> Brians mother</b></td> <td><i>I'm his mother; that's who!</i></td></tr><tr><td style="width:120px;"><b> The multitude</b></td> <td><i>Behold his mother! Behold his mother!</i></td></tr></table> |
|||
=={{header|Liberty BASIC}}== |
|||
<syntaxhighlight lang=lb> |
|||
newline$ ="|" |
|||
' No escape behaviour, so can't refer to '/n'. |
|||
' Generally imported csv would have separator CR LF; easily converted first if needed |
|||
csv$ ="Character,Speech" +newline$+_ |
|||
"The multitude,The messiah! Show us the messiah!" +newline$+_ |
|||
"Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>" +newline$+_ |
|||
"The multitude,Who are you?" +newline$+_ |
|||
"Brians mother,I'm his mother; that's who!" +newline$+_ |
|||
"The multitude,Behold his mother! Behold his mother!" |
|||
print "<HTML>" |
|||
print "<HEAD>" |
|||
print "</HEAD>" |
|||
print "<BODY>" |
|||
print "<center><H1>CSV to HTML translation </H1></center>" |
|||
print "<table border=1 cellpadding =10>" |
|||
print "<tr><td>" |
|||
for i =1 to len( csv$) |
|||
c$ =mid$( csv$, i, 1) |
|||
select case c$ |
|||
case "|": print "</td></tr>": print "<tr><td>" |
|||
case ",": print "</td><td>"; |
|||
case "<": print "&"+"lt;"; |
|||
case ">": print "&"+"gt;"; |
|||
case "&": print "&"+"amp;"; |
|||
case else: print c$; |
|||
end select |
|||
next i |
|||
print "</td></tr>" |
|||
print "</table>" |
|||
print "</BODY>" |
|||
print "</HTML>" |
|||
end |
|||
</syntaxhighlight> |
|||
{{Out}} |
|||
<pre> |
|||
<HTML> |
|||
<HEAD> |
|||
</HEAD> |
|||
<BODY> |
|||
<center><H1>CSV to HTML translation </H1></center> |
|||
<table border=1 cellpadding =10> |
|||
<tr><td> |
|||
Character</td><td>Speech</td></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> |
|||
<tr><td> |
|||
The multitude</td><td>Who are you?</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> |
|||
</BODY> |
|||
</HTML> |
|||
</pre> |
|||
Rendered output is available at http://www.diga.me.uk/csvhtml.gif |
|||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang=lua>FS = "," -- field separator |
||
csv = [[ |
csv = [[ |
||
Line 1,467: | Line 3,288: | ||
for _, line in pairs(html) do |
for _, line in pairs(html) do |
||
print(line) |
print(line) |
||
end</ |
end</syntaxhighlight> |
||
< |
<syntaxhighlight lang=html5><table> |
||
<tr><td>Character</td><td>Speech</td></tr> |
<tr><td>Character</td><td>Speech</td></tr> |
||
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr> |
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr> |
||
Line 1,476: | Line 3,297: | ||
<tr><td>Brians mother</td><td>I'm his mother; that's who!</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> |
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr> |
||
</table></ |
</table></syntaxhighlight> |
||
=={{header| |
=={{header|M2000 Interpreter}}== |
||
{{incomplete|MATLAB|No escaping of '<' in '<angry' and '</angry'}} |
|||
The easiest way to import csv data into MATLAB is to save it in a csv file, then import the data using the "uiimport -file" command. The result of which will be a cell array with each entry being a string delimited by the newline character. |
|||
<syntaxhighlight lang="m2000 interpreter"> |
|||
Example: |
|||
module csv2html { |
|||
<lang MATLAB>>> data |
|||
nl$={ |
|||
} |
|||
Repl$=lambda$ (a$) ->{ |
|||
a$=replace$("&", "&",a$) |
|||
a$=replace$(">", ">",a$) |
|||
a$=replace$("""", """,a$) |
|||
// add any other replacement here |
|||
=replace$("<", "<",a$) |
|||
} |
|||
Tag$=lambda$ nl$, repl$ (a$, b$, n=4)->{ |
|||
if n>0 then |
|||
a$=rtrim$(replace$(nl$, nl$+string$(" ", n), nl$+a$)) |
|||
if right$(a$,2)<>nl$ then a$+=nl$ |
|||
else |
|||
if right$(a$, 2)=nl$ then if left$(a$,2)<>nl$ then a$=nl$+a$ |
|||
end if |
|||
prop=(,) : Read ? prop // forth parameter optional (we have to initalize first with an empty array) |
|||
p=each(prop) : prop$="" // p is an iteration object. |
|||
while p |
|||
prop$+=" "+repl$(prop#val$(p^))+"="+quote$(repl$(prop#val$(p^+1))) |
|||
p=each(prop,p^+2) // start new from p^+2 (p^ is the internal counter) |
|||
end while |
|||
="<"+b$+prop$+">"+a$+"</"+b$+">"+nl$ |
|||
} |
|||
// Prepare FILE csv |
|||
tofile$={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! |
|||
} |
|||
open "forHtml.csv" for wide output as #f |
|||
Print #f, tofile$; |
|||
close #f |
|||
// prepare the input style for Input from file statement |
|||
// "," - we use comma between fields |
|||
// "." - for numbers we use dot for decimal separator |
|||
// false - we didn't read json style strings to normal strings (so \n convert to code 13, \t to code 9) |
|||
// true - we read strings unquote |
|||
input with ",",".",false, true |
|||
// Read csv file |
|||
i=1 |
|||
document export$="" |
|||
Open "forHtml.csv" for wide input as #f |
|||
while not eof(#f) |
|||
input #f, a$, b$ |
|||
a$=repl$(a$) |
|||
b$=repl$(b$) |
|||
if i=1 then |
|||
export$+=tag$(tag$(a$, "th", 0)+tag$(b$,"th", 0), "tr", 4) |
|||
else |
|||
export$+=tag$(tag$(a$, "td", 0)+tag$(b$,"td", 0), "tr", 4) |
|||
end if |
|||
i++ |
|||
end while |
|||
close #f |
|||
style$=tag$({TD {background-color:#ddddff; } |
|||
thead TD {background-color:#ddffdd; text-align:center; } |
|||
},"style",4,("type", "text/css")) |
|||
title$= tag$("CSV to HTML translation - Extra Credit","title",0) |
|||
Head$= tag$(title$+ style$,"head") |
|||
html$={<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">}+nl$+tag$(head$+tag$(tag$(export$, "table"), "body"), "html") |
|||
clipboard html$ |
|||
Report html$ |
|||
Print "Press Esc to exit browser" |
|||
browser "about: "+html$ |
|||
} |
|||
csv2html |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> |
|||
<html> |
|||
<head> |
|||
<title>CSV to HTML translation - Extra Credit</title> |
|||
<style type="text/css"> |
|||
TD {background-color:#ddddff; } |
|||
thead TD {background-color:#ddffdd; text-align:center; } |
|||
</style> |
|||
</head> |
|||
<body> |
|||
<table> |
|||
<tr> |
|||
<th>Character</th> |
|||
<th>Speech</th> |
|||
</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> |
|||
<tr> |
|||
<td>The multitude</td> |
|||
<td>Who are you?</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> |
|||
</body> |
|||
</html> |
|||
</pre> |
|||
=={{header|Maple}}== |
|||
data = |
|||
<syntaxhighlight lang=maple> #A translation of the C code posted |
|||
html_table := proc(str) |
|||
local char; |
|||
printf("<table>\n<tr><td>"); |
|||
for char in str do |
|||
if char = "\n" then |
|||
printf("</td></tr>\n<tr><td>") |
|||
elif char = "," then |
|||
printf("</td><td>") |
|||
elif char = "<" then |
|||
printf("<") |
|||
elif char = ">" then |
|||
printf(">") |
|||
elif char = "&" then |
|||
printf("&") |
|||
else |
|||
printf(char) |
|||
end if; |
|||
end do; |
|||
printf("</td></tr>\n</table>"); |
|||
end proc; |
|||
html_table("Character,Speech |
|||
'The multitude,The messiah! Show us the messiah!' |
|||
[1x109 char] |
|||
'The multitude,Who are you?' |
|||
'Brians mother,I'm his mother; that's who!' |
|||
'The multitude,Behold his mother! Behold his mother!'</lang> |
|||
The other way is to copy and paste the data, but you will have to manually add all of the newline characters and string quotations your self. This is messy and infeasible for large amounts of data, but it is still a valid input. |
|||
Example: |
|||
<lang MATLAB>>> csvData = ['Character,Speech' sprintf('\n')... |
|||
'The multitude,The messiah! Show us the messiah!' sprintf('\n')... |
|||
'Brians mother,<angry>Now you listen here! He''s not the messiah; he''s a very naughty boy! Now go away!</angry>' sprintf('\n')... |
|||
'The multitude,Who are you?' sprintf('\n')... |
|||
'Brians mother,I''m his mother; that''s who!' sprintf('\n')... |
|||
'The multitude,Behold his mother! Behold his mother!'] |
|||
csvData = |
|||
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!"); |
||
</syntaxhighlight> |
|||
{{Out|Output}} |
|||
<pre> |
|||
<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>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> |
|||
<tr><td>The multitude</td><td>Who are you?</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> |
|||
</pre> |
|||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
|||
So, to be able to accept both type of inputs, the function "csvToHTHML()" tests to see if the input is a string as in example 2. If the input is a string it converts the data to a cell array of strings formatted in the exact same way MATLAB formats the first example. The output of the function will be a properly formatted HTML table, which includes the "<THEAD>" around the first row of data so that special formatting can be applied to the column titles. |
|||
<syntaxhighlight lang=Mathematica>a="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!"; |
|||
(*Naive*) |
|||
StringJoin["<table>\n",Map[StringJoin["<tr><td>",#,"</td></tr>\n"]&, |
|||
StringSplit[StringReplace[a,{","->"</td><td>","<"->"<",">"->">"}],"\n"]] |
|||
,"</table>"] |
|||
(*Extra*) |
|||
StringJoin["<table>\n",StringJoin["<tr><th>",#,"</th></tr>\n"]&[ |
|||
StringSplit[StringReplace[a,{","->"</td><td>","<"->"<",">"->">"}],"\n"]//First] |
|||
,Map[StringJoin["<tr><td>",#,"</td></tr>\n"]&, |
|||
StringSplit[StringReplace[a,{","->"</td><td>","<"->"<",">"->">"}],"\n"]//Rest] |
|||
,"</table>"] |
|||
</syntaxhighlight> |
|||
{{Out|Naive}} |
|||
<pre> |
|||
<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>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> |
|||
<tr><td>The multitude</td><td>Who are you?</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> |
|||
</pre> |
|||
{{Out|Extra}} |
|||
<pre> |
|||
<table> |
|||
<tr><th>Character</td><td>Speech</th></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> |
|||
<tr><td>The multitude</td><td>Who are you?</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></pre> |
|||
=={{header|MATLAB}}== |
|||
<lang MATLAB>function htmlOutput = csvToHTML(csvData) |
|||
Many specialized MATLAB file IO functions are painfully slow, |
|||
if ischar(csvData) |
|||
and with large files it is often better to use a lower-level functions such as fread. |
|||
newlineIndex = find( (csvData == char(10)) ); %find all newline characters |
|||
Here we use fileread, which does a little bit of error handling and calls fread, specifying the input data as text. |
|||
text = cell( numel(newlineIndex),1 ); %preallocate space for processed data |
|||
From this text string, we can easily convert any special html characters, |
|||
split it into a cell array, and print it out specifying format. |
|||
for i = (numel(newlineIndex):-1:1) %iterate backwards |
|||
text{i} = csvData(newlineIndex(i)+1:end); |
|||
csvData(newlineIndex(i):end) = []; %delete the newline and everything after it |
|||
end |
|||
csvData = text; |
|||
clear text; |
|||
end |
|||
htmlOutput = '<table>'; |
|||
for i = (1:numel(csvData)) |
|||
if i == 1 %If first entry, then include <thead> |
|||
csvData{i} = [char(10) char(9) '<thead><tr>' char(10) char(9) char(9) '<td>'... |
|||
csvData{i} '</td>' char(10) char(9) '</tr></thead>' char(10)]; |
|||
else %Otherwise, the data is a regular table row |
|||
csvData{i} = [char(9) '<tr>' char(10) char(9) char(9) '<td>' csvData{i}... |
|||
'</td>' char(10) char(9) '</tr>' char(10)]; |
|||
end %if |
|||
%Convert each comma to its HTML equivalent |
|||
for j = fliplr(find( (csvData{i} == ',') )) |
|||
csvData{i} = [csvData{i}(1:j-1) '</td>' char(10) char(9) char(9) '<td>' csvData{i}(j+1:end)]; |
|||
end %for |
|||
%We could make this faster by preallocating htmlOutput but that is |
|||
%more work than necessary to provide a basic solution |
|||
htmlOutput = [htmlOutput csvData{i}]; |
|||
end %for |
|||
<syntaxhighlight lang=MATLAB> |
|||
htmlOutput = [htmlOutput '</table>']; |
|||
inputString = fileread(csvFileName); |
|||
% using multiple regular expressions to clear up special chars |
|||
end %csvToHTML</lang> |
|||
htmlFriendly = regexprep(regexprep(regexprep(regexprep(inputString,... |
|||
'&','&'),... |
|||
Ouput: |
|||
'"','"'),... |
|||
<lang html5>>> csvToHTML(data) |
|||
'<','<'),... |
|||
'>','>'); |
|||
ans = |
|||
% split string into cell array |
|||
tableValues = regexp(regexp(htmlFriendly,'(\r\n|\r|\n)','split')',',','split'); |
|||
%%% print in html format %%% |
|||
% <Extra Credit> first line gets treated as header |
|||
fprintf(1,['<table>\n\t<tr>' sprintf('\n\t\t<th>%s</th>',tableValues{1,:}{:})]) |
|||
% print remaining lines of csv as html table (rows 2:end in cell array of csv values) |
|||
cellfun(@(x)fprintf(1,['\n\t<tr>' sprintf('\n\t\t<td>%s</td>',x{:}) '\n\t</tr>']),tableValues(2:end)) |
|||
fprintf(1,'\n</table>') |
|||
</syntaxhighlight> |
|||
As a single line: |
|||
<syntaxhighlight lang=MATLAB> |
|||
fprintf(1,['<table>\n\t<tr>\n\t\t<th>',regexprep(regexprep(regexprep(regexprep(regexprep(regexprep(regexprep(regexprep(fileread(cvsFileName),'&','&'),'"','"'),'<','<'),'>','>'),'(?<=(^[^\n\r]*)),','</th>\n\t\t<th>'),'(?<!>)(\r\n|\r|\n)','</td>\n\t<tr>\n\t</tr>\n\t\t<td>'),'</td>\n','</th>\n','once'),',','</td>\n\t\t<td>'),'</td>\n\t</tr>\n</table>\n']) |
|||
</syntaxhighlight> |
|||
{{Out}} |
|||
<syntaxhighlight lang=html5> |
|||
<table> |
<table> |
||
<tr> |
|||
< |
<th>Character</th> |
||
< |
<th>Speech</th> |
||
</tr></thead> |
|||
<tr> |
<tr> |
||
<td>The multitude</td> |
<td>The multitude</td> |
||
Line 1,573: | Line 3,541: | ||
<tr> |
<tr> |
||
<td>Brians mother</td> |
<td>Brians mother</td> |
||
<td> |
<td><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry></td> |
||
</tr> |
</tr> |
||
<tr> |
<tr> |
||
Line 1,587: | Line 3,555: | ||
<td>Behold his mother! Behold his mother!</td> |
<td>Behold his mother! Behold his mother!</td> |
||
</tr> |
</tr> |
||
</table |
</table> |
||
</syntaxhighlight> |
|||
=={{header|Maxima}}== |
|||
<syntaxhighlight lang=Maxima>infile: "input.csv"; |
|||
outfile: "table.html"; |
|||
instream: openr(infile); |
|||
outstream: openw(outfile); |
|||
printf(outstream, "<TABLE border=\"1\">~%"); |
|||
nr: 0; |
|||
while (line: readline(instream))#false do ( |
|||
nr: nr + 1, |
|||
line: ssubst("<", "<", line), |
|||
line: ssubst(">", ">", line), |
|||
value_list: map(lambda([f], strim(" ", f)), split(line, ",")), |
|||
if nr=1 then printf(outstream, " <THEAD bgcolor=\"yellow\">") else printf(outstream, " <TBODY bgcolor=\"orange\">"), |
|||
printf(outstream, "<TR>"), |
|||
for value in value_list do printf(outstream, "<TD>~a</TD>", value), |
|||
printf(outstream, "</TR>"), |
|||
if nr=1 then printf(outstream, "</THEAD>~%") else printf(outstream, "</TBODY>~%")); |
|||
printf(outstream, "</TABLE>~%"); |
|||
close(instream); |
|||
close(outstream);</syntaxhighlight> |
|||
<syntaxhighlight lang=html5><TABLE border="1"> |
|||
<THEAD bgcolor="yellow"><TR><TD>Character</TD><TD>Speech</TD></TR></THEAD> |
|||
<TBODY bgcolor="orange"><TR><TD>The multitude</TD><TD>The messiah! Show us the messiah!</TD></TR></TBODY> |
|||
<TBODY bgcolor="orange"><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></TBODY> |
|||
<TBODY bgcolor="orange"><TR><TD>The multitude</TD><TD>Who are you?</TD></TR></TBODY> |
|||
<TBODY bgcolor="orange"><TR><TD>Brians mother</TD><TD>I'm his mother; that's who!</TD></TR></TBODY> |
|||
<TBODY bgcolor="orange"><TR><TD>The multitude</TD><TD>Behold his mother! Behold his mother!</TD></TR></TBODY> |
|||
</TABLE></syntaxhighlight> |
|||
=={{header|ML/I}}== |
=={{header|ML/I}}== |
||
This example carries out the special formatting for extra credit. This is a macro rather than a function, though, due to the nature of ML/I. |
This example carries out the special formatting for extra credit. This is a macro rather than a function, though, due to the nature of ML/I. |
||
===Input=== |
===Input=== |
||
< |
<syntaxhighlight lang=ML/I>MCSKIP "WITH" NL |
||
"" CSV to HTML |
"" CSV to HTML |
||
"" assumes macros on input stream 1, terminal on stream 2 |
"" assumes macros on input stream 1, terminal on stream 2 |
||
Line 1,645: | Line 3,646: | ||
</body> |
</body> |
||
</html> |
</html> |
||
]</ |
]</syntaxhighlight> |
||
===Output=== |
===Output=== |
||
< |
<syntaxhighlight lang=ML/I><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> |
||
<html> |
<html> |
||
<head> |
<head> |
||
Line 1,697: | Line 3,698: | ||
</table> |
</table> |
||
</body> |
</body> |
||
</html></ |
</html></syntaxhighlight> |
||
=={{header| |
=={{header|Nanoquery}}== |
||
<syntaxhighlight lang=nanoquery>// a method that converts a csv row into a html table row as a string |
|||
def toHtmlRow(record, tag) |
|||
htmlrow = "\t<tr>\n" |
|||
// loop through the values in the current csv row |
|||
for i in range(1, len(record)) |
|||
htmlrow = htmlrow + "\t\t<" + tag + ">" + (record ~ i) + "</" + tag + ">\n" |
|||
end for |
|||
return htmlrow + "\t</tr>\n" |
|||
end def |
|||
// get the name of the csv file then open it |
|||
print "filename: " |
|||
input fname |
|||
open fname |
|||
// allocate a string to hold the table |
|||
htmltable = "<table>\n" |
|||
// add the column names to the table (#0 returns column names as a record object) |
|||
htmltable = (htmltable + toHtmlRow(#0, "th")) |
|||
// add all other rows to the table |
|||
for i in range(1, $dbsize) |
|||
htmltable = (htmltable + toHtmlRow(#i, "td")) |
|||
end for |
|||
// close the html table |
|||
htmltable = htmltable+"</table>" |
|||
println htmltable</syntaxhighlight> |
|||
=={{header|NetRexx}}== |
|||
Uses the [[NetRexx]] solution for [[Read_a_file_line_by_line#Using_Java_Scanner|Read a file line by line]] to read the CSV file into the program. |
|||
<syntaxhighlight lang=NetRexx>/* NetRexx */ |
|||
options replace format comments java crossref symbols nobinary |
|||
parse arg inFileName . |
|||
if inFileName = '' | inFileName = '.' then inFileName = './data/Brian.csv' |
|||
csv = RREadFileLineByLine01.scanFile(inFileName) |
|||
header = htmlHeader() |
|||
pre = htmlCsvText(csv, inFileName) |
|||
table = htmlCsvTable(csv, inFileName) |
|||
footer = htmlFooter() |
|||
say header |
|||
say pre |
|||
say table |
|||
say footer |
|||
return |
|||
method htmlHeader() public static returns Rexx |
|||
html = '<?xml version="1.0" encoding="UTF-8"?>\n' - |
|||
|| '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n' - |
|||
|| '<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">\n' - |
|||
|| '<head>\n' - |
|||
|| '<meta http-equiv="Content-type" content="text/html;charset=UTF-8"/>\n' - |
|||
|| '<title>RCsv2Html</title>\n' - |
|||
|| '<style type="text/css">\n' - |
|||
|| '<!--\n' - |
|||
|| '/* <![DATA[ */\n' - |
|||
|| 'body {\n' - |
|||
|| ' font-family: "Verdana", "Geneva", "Helvetica Neue", "Helvetica", "DejaVu Sans", "Arial", sans-serif;\n' - |
|||
|| '}\n' - |
|||
|| 'table, th, td {\n' - |
|||
|| ' border: 1px solid black;\n' - |
|||
|| ' border-collapse: collapse;\n' - |
|||
|| ' padding: 0.25em;\n' - |
|||
|| ' font-size: 85%;\n' - |
|||
|| '}\n' - |
|||
|| 'th {\n' - |
|||
|| ' color: white;\n' - |
|||
|| ' background-color: green;\n' - |
|||
|| '}\n' - |
|||
|| 'p.classname {\n' - |
|||
|| ' font-size: inherit;\n' - |
|||
|| '}\n' - |
|||
|| '/* ]] */\n' - |
|||
|| '//-->\n' - |
|||
|| '</style>\n' - |
|||
|| '</head>\n' - |
|||
|| '<body>\n' - |
|||
|| '<h1>Rosetta Code – NetRexx Sample Output</h2>\n' - |
|||
|| '<h2><a href="http://rosettacode.org/wiki/CSV_to_HTML_translation">CSV to HTML translation</a></h2>\n' - |
|||
|| '' |
|||
return html |
|||
method htmlFooter() public static returns Rexx |
|||
html = '</body>\n' - |
|||
|| '</html>\n' - |
|||
|| '' |
|||
return html |
|||
method htmlCsvText(csv, fileName = '.') public static returns Rexx |
|||
html = '<h3>Contents of CSV <code>'fileName'</code></h3>\n' - |
|||
|| '<pre>\n' - |
|||
|| '' |
|||
loop row = 1 to csv[0] |
|||
html = html || csv[row]'\n' |
|||
end row |
|||
html = html - |
|||
|| '</pre>\n' - |
|||
|| '' |
|||
return html |
|||
method htmlCsvTable(csv, fileName = '.') public static returns Rexx |
|||
html = '<table>\n' - |
|||
|| '<caption>Translation of CSV <code>'fileName'</code></caption>\n' - |
|||
|| '<thead>\n' - |
|||
|| '' |
|||
html = html - |
|||
|| htmlCsvTableRow(csv[1], 'th')'\n' - |
|||
|| '</thead>\n' - |
|||
|| '<tbody>\n' - |
|||
|| '' |
|||
loop r_ = 2 to csv[0] |
|||
html = html - |
|||
|| htmlCsvTableRow(csv[r_])'\n' - |
|||
|| '' |
|||
end r_ |
|||
html = html - |
|||
|| '</tbody>\n' - |
|||
|| '</table>\n' - |
|||
|| '' |
|||
return html |
|||
method htmlCsvTableRow(row, tag = 'td') public static returns Rexx |
|||
row = row.strip('t') |
|||
row = row.changestr('&', '&') -- need to do this one first to avoid double translation |
|||
row = row.changestr('"', '"') |
|||
row = row.changestr("'", ''') |
|||
row = row.changestr('<', '<') |
|||
row = row.changestr('>', '>') |
|||
elmts = '' |
|||
elmts[0] = 0 |
|||
e_ = 0 |
|||
loop while row.length() > 0 |
|||
parse row elmt ',' row |
|||
e_ = e_ + 1; elmts[0] = e_; elmts[e_] = elmt |
|||
end |
|||
html = '<tr>\n' - |
|||
|| '' |
|||
loop e_ = 1 to elmts[0] |
|||
html = html - |
|||
|| '<'tag'>'elmts[e_]'</'tag'>\n' - |
|||
|| '' |
|||
end e_ |
|||
html = html - |
|||
|| '</tr>\n' - |
|||
|| '' |
|||
return html |
|||
</syntaxhighlight> |
|||
'''Output:''' |
|||
<div style="height:30ex;overflow:scroll;"> |
|||
<syntaxhighlight lang=html5><?xml version="1.0" encoding="UTF-8"?> |
|||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
|||
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US"> |
|||
<head> |
|||
<meta http-equiv="Content-type" content="text/html;charset=UTF-8"/> |
|||
<title>RCsv2Html</title> |
|||
<style type="text/css"> |
|||
<!-- |
|||
/* <![DATA[ */ |
|||
body { |
|||
font-family: "Verdana", "Geneva", "Helvetica Neue", "Helvetica", "DejaVu Sans", "Arial", sans-serif; |
|||
} |
|||
table, th, td { |
|||
border: 1px solid black; |
|||
border-collapse: collapse; |
|||
padding: 0.25em; |
|||
font-size: 85%; |
|||
} |
|||
th { |
|||
color: white; |
|||
background-color: green; |
|||
} |
|||
p.classname { |
|||
font-size: inherit; |
|||
} |
|||
/* ]] */ |
|||
//--> |
|||
</style> |
|||
</head> |
|||
<body> |
|||
<h1>Rosetta Code – NetRexx Sample Output</h2> |
|||
<h2><a href="http://rosettacode.org/wiki/CSV_to_HTML_translation">CSV to HTML translation</a></h2> |
|||
<h3>Contents of CSV <code>./data/Brian.csv</code></h3> |
|||
<pre> |
|||
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! |
|||
</pre> |
|||
<table> |
|||
<caption>Contents of CSV <code>./data/Brian.csv</code></caption> |
|||
<thead> |
|||
<tr> |
|||
<th>Character</th> |
|||
<th>Speech</th> |
|||
</tr> |
|||
</thead> |
|||
<tbody> |
|||
<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> |
|||
<tr> |
|||
<td>The multitude</td> |
|||
<td>Who are you?</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> |
|||
</tbody> |
|||
</table> |
|||
</body> |
|||
</html> |
|||
</syntaxhighlight> |
|||
</div> |
|||
'''Rendered Output:''' |
|||
[[File:RCsv2HtmlNetRexx.png]] |
|||
=={{header|Nim}}== |
|||
{{trans|Python}} |
|||
<syntaxhighlight lang=nim>import cgi, strutils |
|||
const csvtext = """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!""" |
|||
proc row2tr(row: string): string = |
|||
result = "<tr>" |
|||
let cols = xmlEncode(row).split(",") |
|||
for col in cols: |
|||
result.add "<td>"&col&"</td>" |
|||
result.add "</tr>" |
|||
proc csv2html(txt: string): string = |
|||
result = "<table summary=\"csv2html program output\">\n" |
|||
for row in txt.splitLines(): |
|||
result.add " <tbody>"&row2tr(row)&"</tbody>\n" |
|||
result.add "</table>" |
|||
echo csv2html(csvtext)</syntaxhighlight> |
|||
{{Out}} |
|||
<pre><table summary="csv2html program output"> |
|||
<tbody><tr><td>Character</td><td>Speech</td></tr></tbody> |
|||
<tbody><tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr></tbody> |
|||
<tbody><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></tbody> |
|||
<tbody><tr><td>The multitude</td><td>Who are you?</td></tr></tbody> |
|||
<tbody><tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr></tbody> |
|||
<tbody><tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr></tbody> |
|||
</table></pre> |
|||
=={{header|Oberon-2}}== |
|||
{{Works with|oo2c Version 2}} |
|||
<syntaxhighlight lang=oberon2> |
|||
MODULE CSV2HTML; |
|||
IMPORT |
|||
Object, |
|||
IO, |
|||
IO:FileChannel, |
|||
IO:TextRider, |
|||
SB := ADT:StringBuffer, |
|||
NPCT:Tools, |
|||
NPCT:CGI:Utils, |
|||
Ex := Exception, |
|||
Out; |
|||
VAR |
|||
fileChannel: FileChannel.Channel; |
|||
rd: TextRider.Reader; |
|||
line: ARRAY 1024 OF CHAR; |
|||
table: SB.StringBuffer; |
|||
parts: ARRAY 2 OF STRING; |
|||
PROCEDURE DoTableHeader(sb: SB.StringBuffer;parts: ARRAY OF STRING); |
|||
BEGIN |
|||
sb.Append("<tr><th>"+Utils.EscapeHTML(parts[0])+"</th><th>"+Utils.EscapeHTML(parts[1])+"</th></tr>"); |
|||
sb.AppendLn |
|||
END DoTableHeader; |
|||
PROCEDURE DoTableRow(sb: SB.StringBuffer;parts: ARRAY OF STRING); |
|||
BEGIN |
|||
sb.Append("<tr><td>"+Utils.EscapeHTML(parts[0])+"</td><td>"+Utils.EscapeHTML(parts[1])+"</td></tr>"); |
|||
sb.AppendLn |
|||
END DoTableRow; |
|||
PROCEDURE DoTable(sb: SB.StringBuffer): STRING; |
|||
VAR |
|||
aux: SB.StringBuffer; |
|||
BEGIN |
|||
aux := SB.New("<table>");aux.AppendLn; |
|||
RETURN aux.ToString() + sb.ToString() + "</table>"; |
|||
END DoTable; |
|||
BEGIN |
|||
TRY |
|||
fileChannel := FileChannel.OpenUnbuffered("script.csv",{FileChannel.read}); |
|||
CATCH Ex.Exception(ex): |
|||
Out.Object(ex.GetMessage());Out.Ln; |
|||
HALT(1) |
|||
END; |
|||
rd := TextRider.ConnectReader(fileChannel); |
|||
(* Extract headers *) |
|||
TRY |
|||
rd.ReadLine(line); |
|||
table := NEW(SB.StringBuffer,2048); |
|||
Tools.Split(Object.NewLatin1(line),",",parts); |
|||
DoTableHeader(table,parts); |
|||
CATCH IO.Error(ex): |
|||
Out.Object(ex.Name() + ": " + ex.GetMessage());Out.Ln; |
|||
HALT(2) |
|||
END; |
|||
(* Extract data *) |
|||
LOOP |
|||
TRY |
|||
rd.ReadLine(line); |
|||
IF (line[0] # 0X)THEN (* skip empty lines *) |
|||
Tools.Split(Object.NewLatin1(line),",",parts); |
|||
DoTableRow(table,parts) |
|||
END |
|||
CATCH IO.Error(ex): |
|||
EXIT |
|||
END |
|||
END; |
|||
Out.Object(DoTable(table));Out.Ln; |
|||
fileChannel.Close() |
|||
END CSV2HTML. |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
<table> |
|||
<tr><th>Character</th><th>Speech</th></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> |
|||
<tr><td>The multitude</td><td>Who are you?</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> |
|||
</pre> |
|||
=={{header|Objeck}}== |
|||
{{trans|Java}} |
|||
<syntaxhighlight lang=objeck>use System.IO.File; |
|||
use Data.CSV; |
|||
class CsvToHtml { |
|||
function : Main(args : String[]) ~ Nil { |
|||
if(args->Size() = 1) { |
|||
table := CsvTable->New(FileReader->ReadFile(args[0])); |
|||
if(table->IsParsed()) { |
|||
buffer := "<html><body><table>"; |
|||
Header(table->GetHeaders(), buffer); |
|||
for(i := 1; i < table->Size(); i += 1;) { |
|||
Data(table->Get(i), buffer); |
|||
}; |
|||
buffer += "</table></body></html>"; |
|||
buffer->PrintLine(); |
|||
}; |
|||
}; |
|||
} |
|||
function : Header(row : CsvRow, buffer : String) ~ Nil { |
|||
buffer += "<tr>"; |
|||
each(i : row) { |
|||
buffer += "<th>"; |
|||
buffer += Encode(row->Get(i)); |
|||
buffer += "</th>"; |
|||
}; |
|||
buffer += "</tr>"; |
|||
} |
|||
function : Data(row : CsvRow, buffer : String) ~ Nil { |
|||
buffer += "<tr>"; |
|||
each(i : row) { |
|||
buffer += "<td>"; |
|||
buffer += Encode(row->Get(i)); |
|||
buffer += "</td>"; |
|||
}; |
|||
buffer += "</tr>"; |
|||
} |
|||
function : Encode(in : String) ~ String { |
|||
out := ""; |
|||
each(i : in) { |
|||
c := in->Get(i); |
|||
select(c) { |
|||
label '&': { |
|||
out->Append("&"); |
|||
} |
|||
label '\'': { |
|||
out->Append("'"); |
|||
} |
|||
label '<': { |
|||
out->Append("<"); |
|||
} |
|||
label '>': { |
|||
out->Append(">"); |
|||
} |
|||
other: { |
|||
out->Append(c); |
|||
} |
|||
}; |
|||
}; |
|||
return out; |
|||
} |
|||
} |
|||
</syntaxhighlight> |
|||
Output: |
|||
<pre> |
|||
<html><body><table><tr><th>Character</th><th>Speech</th></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><tr><td>The multitude</td><td>Who are you?</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></body></html> |
|||
</pre> |
|||
=={{header|OCaml}}== |
|||
===Simple solution=== |
===Simple solution=== |
||
OCaml possesses a [http://forge.ocamlcore.org/projects/csv/ CSV module] |
OCaml possesses a [http://forge.ocamlcore.org/projects/csv/ CSV module] |
||
but we do not use it hereafter because the CSV data does not contain comas. |
|||
< |
<syntaxhighlight lang=ocaml>open Printf |
||
let csv_data = "\ |
let csv_data = "\ |
||
Line 1,739: | Line 4,192: | ||
let () = |
let () = |
||
print_html_table (list_of_csv csv_data)</ |
print_html_table (list_of_csv csv_data)</syntaxhighlight> |
||
Sample html output |
{{Out|Sample html output}} |
||
<syntaxhighlight lang=html5><table> |
|||
<lang html5><table> |
|||
<tr><td>Character</td><td>Speech</td></tr> |
<tr><td>Character</td><td>Speech</td></tr> |
||
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr> |
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr> |
||
Line 1,750: | Line 4,202: | ||
<tr><td>Brians mother</td><td>I'm his mother; that's who!</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> |
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr> |
||
</table> </ |
</table> </syntaxhighlight> |
||
===Extra credit solution=== |
===Extra credit solution=== |
||
< |
<syntaxhighlight lang=ocaml>open Printf |
||
let csv_data = "\ |
let csv_data = "\ |
||
Line 1,797: | Line 4,249: | ||
let () = |
let () = |
||
print_html_table (list_of_csv csv_data)</ |
print_html_table (list_of_csv csv_data)</syntaxhighlight> |
||
{{out}} |
|||
Output: |
|||
< |
<syntaxhighlight lang=html5><html> |
||
<head> |
<head> |
||
<style type="text/css"> |
<style type="text/css"> |
||
Line 1,816: | Line 4,268: | ||
</tbody> |
</tbody> |
||
</table> |
</table> |
||
</html></ |
</html></syntaxhighlight> |
||
=={{header|OmniMark}}== |
|||
Only the "extra credit" version is provided here. It is also XHTML 1.0, complete, and validates (as noted in the comment to the 'process-end' rule). The validation could be done by OmniMark too, but that's going well beyond the scope of the task. Only the three XML character references, which appear in the input, are included - &gt;, &lt;, and &apos; (so, not &amp; and &quot;). Output XHTML, plus a thumbnail screenshot of the output and the Firefox Inspector output, is included. |
|||
<syntaxhighlight lang=omnimark> |
|||
global switch thead initial {true} |
|||
global switch tbody-first initial {true} |
|||
define function process-tr (value stream line) as |
|||
do when thead |
|||
output '<thead>%n<tr><th>' |
|||
else when tbody-first |
|||
output '</thead>%n' |
|||
output '<tbody>%n<tr><td>' |
|||
deactivate tbody-first |
|||
else |
|||
output '<tr><td>' |
|||
done |
|||
repeat scan line |
|||
match ',' any-text => char |
|||
output '</th><th>' when thead |
|||
output '</td><td>' unless thead |
|||
using group ents submit char |
|||
match any-text => char |
|||
using group ents submit char |
|||
again |
|||
output '</td>' unless thead |
|||
output '</th>' when thead |
|||
output '</tr>' |
|||
deactivate thead |
|||
process-start |
|||
output '<?xml version="1.0" encoding="UTF-8"?>%n' || |
|||
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">%n' || |
|||
'<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">%n' || |
|||
'<head>%n' || |
|||
'<title>Monty Python’s Life of Brian (1979). Scene 20 (extract).</title>%n' || |
|||
'<meta name="description" content="Rosetta Code. Task: CSV to HTML translation (extra credit)" />%n' || |
|||
'<meta name="keywords" content="OmniMark" />%n' || |
|||
'<meta name="author" content="Kennypete" />%n' || |
|||
'<style type="text/css">%nth { background-color: gold }%n</style>%n' || |
|||
'</head>%n' || |
|||
'<body>%n' || |
|||
'<table border=%"1%">%n' |
|||
process-end |
|||
output '</tbody>%n' || |
|||
'</table>%n' || |
|||
'</body>%n' || |
|||
'</html>%n' |
|||
; Output validates to XHTML 1.0 - https://validator.w3.org/check |
|||
process |
|||
local stream s |
|||
set s to "Character,Speech%n" || |
|||
"The multitude,The messiah! Show us the messiah!%n" || |
|||
"Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>%n" || |
|||
"The multitude,Who are you?%n" || |
|||
"Brians mother,I'm his mother; that's who!%n" || |
|||
"The multitude,Behold his mother! Behold his mother!%n" |
|||
using group line submit s |
|||
group ents |
|||
find '<' |
|||
output '<' |
|||
find '>' |
|||
output '>' |
|||
find "'" |
|||
output ''' |
|||
group line |
|||
find line-start any-text+ => line |
|||
process-tr(line) |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
[[File:20240714-CSV-to-HTML-translation.png|thumb|alt=OmniMark CSV-to-HTML-translation Firefox output and Inspector|Firefox output and Inspector]] |
|||
<syntaxhighlight lang=html> |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
|||
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> |
|||
<head> |
|||
<title>Monty Python’s Life of Brian (1979). Scene 20 (extract).</title> |
|||
<meta name="description" content="Rosetta Code. Task: CSV to HTML translation (extra credit)" /> |
|||
<meta name="keywords" content="OmniMark" /> |
|||
<meta name="author" content="Kennypete" /> |
|||
<style type="text/css"> |
|||
th { background-color: gold } |
|||
</style> |
|||
</head> |
|||
<body> |
|||
<table border="1"> |
|||
<thead> |
|||
<tr><th>Character</th><th>Speech</th></tr> |
|||
</thead> |
|||
<tbody> |
|||
<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> |
|||
<tr><td>The multitude</td><td>Who are you?</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> |
|||
</tbody> |
|||
</table> |
|||
</body> |
|||
</html> |
|||
</syntaxhighlight> |
|||
=={{header|OpenEdge/Progress}}== |
=={{header|OpenEdge/Progress}}== |
||
< |
<syntaxhighlight lang=Progress (OpenEdge ABL)> |
||
FUNCTION csvToHtml RETURNS CHARACTER ( |
FUNCTION csvToHtml RETURNS CHARACTER ( |
||
i_lhas_header AS LOGICAL, |
i_lhas_header AS LOGICAL, |
||
Line 1,869: | Line 4,434: | ||
"The multitude,Behold his mother! Behold his mother!" |
"The multitude,Behold his mother! Behold his mother!" |
||
) |
) |
||
VIEW-AS ALERT-BOX.</ |
VIEW-AS ALERT-BOX.</syntaxhighlight> |
||
{{out|with header enabled}} |
|||
< |
<syntaxhighlight lang=html><html> |
||
<table> |
<table> |
||
<tr> |
<tr> |
||
Line 1,898: | Line 4,463: | ||
</tr> |
</tr> |
||
</table> |
</table> |
||
</html></ |
</html></syntaxhighlight> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
Line 1,904: | Line 4,469: | ||
Provide the CSV data as standard input. With a command-line argument, the first row will use <code><th></code> instead of <code><td></code>. |
Provide the CSV data as standard input. With a command-line argument, the first row will use <code><th></code> instead of <code><td></code>. |
||
< |
<syntaxhighlight lang=perl>use HTML::Entities; |
||
sub row { |
sub row { |
||
Line 1,918: | Line 4,483: | ||
row @ARGV ? 'th' : 'td', $first; |
row @ARGV ? 'th' : 'td', $first; |
||
row 'td', $_ foreach @rest; |
row 'td', $_ foreach @rest; |
||
print "</table>\n";</ |
print "</table>\n";</syntaxhighlight> |
||
Output (with a command-line argument) |
{{Out|Output (with a command-line argument)}} |
||
<syntaxhighlight lang=html5><table> |
|||
<lang html5><table> |
|||
<tr><th>Character</th><th>Speech</th></tr> |
<tr><th>Character</th><th>Speech</th></tr> |
||
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr> |
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr> |
||
Line 1,929: | Line 4,493: | ||
<tr><td>Brians mother</td><td>I'm his mother; that's who!</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> |
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr> |
||
</table></ |
</table></syntaxhighlight> |
||
=={{header|Phix}}== |
|||
Based on [[CSV_to_HTML_translation#Euphoria|Euphoria]] but with a simpler multiline constant. |
|||
<!--<syntaxhighlight lang="phix">(phixonline)--> |
|||
<span style="color: #008080;">with javascript_semantics</span> |
|||
<span style="color: #008080;">constant</span> <span style="color: #000000;">input</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""" |
|||
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!"""</span> |
|||
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"<table>\n<tr><td>"</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">input</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span> |
|||
<span style="color: #008080;">switch</span> <span style="color: #000000;">input</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">do</span> |
|||
<span style="color: #008080;">case</span> <span style="color: #008000;">'\n'</span> <span style="color: #008080;">then</span> <span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"</td></tr>\n<tr><td>"</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #008080;">case</span> <span style="color: #008000;">','</span> <span style="color: #008080;">then</span> <span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"</td><td>"</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #008080;">case</span> <span style="color: #008000;">'<'</span> <span style="color: #008080;">then</span> <span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"&lt;"</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #008080;">case</span> <span style="color: #008000;">'>'</span> <span style="color: #008080;">then</span> <span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"&gt;"</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #008080;">case</span> <span style="color: #008000;">'&'</span> <span style="color: #008080;">then</span> <span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"&amp;"</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #008080;">case</span> <span style="color: #008080;">else</span> <span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">input</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">switch</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
|||
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"</td></tr>\n</table>"</span><span style="color: #0000FF;">)</span> |
|||
<!--</syntaxhighlight>--> |
|||
{{out}} |
|||
<pre> |
|||
<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>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>Who are you?</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></pre> |
|||
=={{header|PHP}}== |
|||
===Simple Solution=== |
|||
<Lang php> |
|||
<?php |
|||
$csv = <<<EOT |
|||
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! |
|||
EOT; |
|||
function convert($csv) |
|||
{ |
|||
$out = []; |
|||
array_map(function($ln) use(&$out) { |
|||
$ln = htmlentities($ln); |
|||
$out[] = count($out) == 0 |
|||
? '<thead><tr><th>'.implode('</th><th>',explode(',',$ln))."</th></tr></thead>\n" |
|||
: '<tr><td>'.implode('</td><td>',explode(',',$ln))."</td></tr>\n"; |
|||
}, explode("\n",$csv)); |
|||
return '<table>'.implode('',$out).'</table>'; |
|||
} |
|||
echo convert($csv); |
|||
</syntaxhighlight> |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
===Simple solution=== |
===Simple solution=== |
||
< |
<syntaxhighlight lang=PicoLisp>(load "@lib/http.l") |
||
(in "text.csv" |
(in "text.csv" |
||
Line 1,940: | Line 4,568: | ||
(while (split (line) ",") |
(while (split (line) ",") |
||
(<row> NIL (ht:Prin (pack (car @))) (ht:Prin (pack (cadr @)))) |
(<row> NIL (ht:Prin (pack (car @))) (ht:Prin (pack (cadr @)))) |
||
(prinl) ) ) )</ |
(prinl) ) ) )</syntaxhighlight> |
||
{{Out}} |
|||
Output: |
|||
< |
<syntaxhighlight lang=html5><table class="myStyle"> |
||
<tr><td>Character</td><td>Speech</td></tr> |
<tr><td>Character</td><td>Speech</td></tr> |
||
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr> |
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr> |
||
Line 1,949: | Line 4,577: | ||
<tr><td>Brians mother</td><td>I'm his mother; that's who!</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> |
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr> |
||
</table></ |
</table></syntaxhighlight> |
||
===Extra credit solution=== |
===Extra credit solution=== |
||
< |
<syntaxhighlight lang=PicoLisp>(load "@lib/http.l") |
||
(in "text.csv" |
(in "text.csv" |
||
Line 1,959: | Line 4,588: | ||
(while (split (line) ",") |
(while (split (line) ",") |
||
(<row> NIL (ht:Prin (pack (car @))) (ht:Prin (pack (cadr @)))) |
(<row> NIL (ht:Prin (pack (car @))) (ht:Prin (pack (cadr @)))) |
||
(prinl) ) ) ) )</ |
(prinl) ) ) ) )</syntaxhighlight> |
||
{{Out}} |
|||
Output: |
|||
< |
<syntaxhighlight lang=html5><table class="myStyle"><tr><th>Character</th><th>Speech</th></tr> |
||
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></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> |
<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> |
||
Line 1,967: | Line 4,596: | ||
<tr><td>Brians mother</td><td>I'm his mother; that's who!</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> |
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr> |
||
</table></ |
</table></syntaxhighlight> |
||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
===Simple solution=== |
===Simple solution=== |
||
< |
<syntaxhighlight lang=Powershell> |
||
Import-Csv -Path .\csv_html_test.csv | ConvertTo-Html -Fragment | Out-File .\csv_html_test.html |
Import-Csv -Path .\csv_html_test.csv | ConvertTo-Html -Fragment | Out-File .\csv_html_test.html |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
|||
Output: |
|||
< |
<syntaxhighlight lang=html5><table> |
||
<colgroup> |
<colgroup> |
||
<col/> |
<col/> |
||
Line 1,987: | Line 4,616: | ||
<tr><td>Brians mother</td><td>I'm his mother; that's who! </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> |
<tr><td>The multitude</td><td>Behold his mother! Behold his mother! </td></tr> |
||
</table></ |
</table></syntaxhighlight> |
||
===Extra credit solution=== |
===Extra credit solution=== |
||
< |
<syntaxhighlight lang=Powershell> |
||
$htmlformat = '<title>Csv to Html</title>' |
$htmlformat = '<title>Csv to Html</title>' |
||
$htmlformat += '<style type="text/css">' |
$htmlformat += '<style type="text/css">' |
||
Line 1,999: | Line 4,628: | ||
Import-Csv -Path .\csv_html_test.csv | ConvertTo-Html -Head $htmlformat -Body '<h1>Csv to Html</h1>' | Out-File .\csv_html_test.html |
Import-Csv -Path .\csv_html_test.csv | ConvertTo-Html -Head $htmlformat -Body '<h1>Csv to Html</h1>' | Out-File .\csv_html_test.html |
||
Invoke-Expression .\csv_html_test.html |
Invoke-Expression .\csv_html_test.html |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
|||
Output: |
|||
< |
<syntaxhighlight lang=html5> |
||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
||
<html xmlns="http://www.w3.org/1999/xhtml"> |
<html xmlns="http://www.w3.org/1999/xhtml"> |
||
Line 2,023: | Line 4,652: | ||
</table> |
</table> |
||
</body></html> |
</body></html> |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
Uses DCG. |
Uses DCG. |
||
{{Works with|SWI-Prolog}} |
|||
===Simple solution=== |
===Simple solution=== |
||
< |
<syntaxhighlight lang=Prolog>csv_html :- |
||
L = "Character,Speech |
L = "Character,Speech |
||
The multitude,The messiah! Show us the messiah! |
The multitude,The messiah! Show us the messiah! |
||
Line 2,081: | Line 4,711: | ||
csv_td_in(T, S). |
csv_td_in(T, S). |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
|||
OutPut : |
|||
< |
<syntaxhighlight lang=html5><TABLE> |
||
<TR> |
<TR> |
||
<TD>Character</TD><TD>Speech</TD> |
<TD>Character</TD><TD>Speech</TD> |
||
Line 2,103: | Line 4,733: | ||
</TR> |
</TR> |
||
</TABLE> |
</TABLE> |
||
</syntaxhighlight> |
|||
</lang> |
|||
===Extra credit solution=== |
===Extra credit solution=== |
||
< |
<syntaxhighlight lang=Prolog>csv_html_plus :- |
||
L = |
L = |
||
"Character,Speech |
"Character,Speech |
||
Line 2,195: | Line 4,825: | ||
[H], |
[H], |
||
csv_body_td_in(T, S). |
csv_body_td_in(T, S). |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
|||
Output : |
|||
< |
<syntaxhighlight lang=html5><TABLE> |
||
<THEAD> |
<THEAD> |
||
<TR> |
<TR> |
||
Line 2,221: | Line 4,851: | ||
</TBODY> |
</TBODY> |
||
</TABLE> |
</TABLE> |
||
</syntaxhighlight> |
|||
</lang> |
|||
===HTML outputs rendered in firefox browser=== |
===HTML outputs rendered in firefox browser=== |
||
Line 2,229: | Line 4,859: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
(Note: rendered versions of |
(Note: rendered versions of all three outputs are shown at the foot of this section). |
||
===Simple solution=== |
===Simple solution=== |
||
< |
<syntaxhighlight lang=python>csvtxt = '''\ |
||
Character,Speech |
Character,Speech |
||
The multitude,The messiah! Show us the messiah! |
The multitude,The messiah! Show us the messiah! |
||
Line 2,258: | Line 4,888: | ||
htmltxt = csv2html(csvtxt) |
htmltxt = csv2html(csvtxt) |
||
print(htmltxt)</ |
print(htmltxt)</syntaxhighlight> |
||
'''Sample HTML output''' |
'''Sample HTML output''' |
||
< |
<syntaxhighlight lang=html5><TABLE summary="csv2html program output"> |
||
<TBODY><TR><TD>Character</TD><TD>Speech</TD></TR></TBODY> |
<TBODY><TR><TD>Character</TD><TD>Speech</TD></TR></TBODY> |
||
<TBODY><TR><TD>The multitude</TD><TD>The messiah! Show us the messiah!</TD></TR></TBODY> |
<TBODY><TR><TD>The multitude</TD><TD>The messiah! Show us the messiah!</TD></TR></TBODY> |
||
Line 2,269: | Line 4,899: | ||
<TBODY><TR><TD>Brians mother</TD><TD>I'm his mother; that's who!</TD></TR></TBODY> |
<TBODY><TR><TD>Brians mother</TD><TD>I'm his mother; that's who!</TD></TR></TBODY> |
||
<TBODY><TR><TD>The multitude</TD><TD>Behold his mother! Behold his mother!</TD></TR></TBODY> |
<TBODY><TR><TD>The multitude</TD><TD>Behold his mother! Behold his mother!</TD></TR></TBODY> |
||
</TABLE></ |
</TABLE></syntaxhighlight> |
||
===Extra credit solution=== |
===Extra credit solution=== |
||
< |
<syntaxhighlight lang=python>def _row2trextra(row, attr=None): |
||
cols = escape(row).split(',') |
cols = escape(row).split(',') |
||
attr_tr = attr.get('TR', '') |
attr_tr = attr.get('TR', '') |
||
Line 2,301: | Line 4,931: | ||
) |
) |
||
) |
) |
||
print(htmltxt)</ |
print(htmltxt)</syntaxhighlight> |
||
'''Sample HTML output''' |
'''Sample HTML output''' |
||
Line 2,307: | Line 4,937: | ||
The raw HTML would not render correctly through the wiki interface but shows a suitably coloured table with cell borders. |
The raw HTML would not render correctly through the wiki interface but shows a suitably coloured table with cell borders. |
||
--> |
--> |
||
< |
<syntaxhighlight lang=html5><TABLE border="1" summary="csv2html extra program output"> |
||
<THEAD bgcolor="yellow"><TR><TD>Character</TD><TD>Speech</TD></TR></THEAD> |
<THEAD bgcolor="yellow"><TR><TD>Character</TD><TD>Speech</TD></TR></THEAD> |
||
<TBODY bgcolor="orange"><TR><TD>The multitude</TD><TD>The messiah! Show us the messiah!</TD></TR></TBODY> |
<TBODY bgcolor="orange"><TR><TD>The multitude</TD><TD>The messiah! Show us the messiah!</TD></TR></TBODY> |
||
Line 2,314: | Line 4,944: | ||
<TBODY bgcolor="orange"><TR><TD>Brians mother</TD><TD>I'm his mother; that's who!</TD></TR></TBODY> |
<TBODY bgcolor="orange"><TR><TD>Brians mother</TD><TD>I'm his mother; that's who!</TD></TR></TBODY> |
||
<TBODY bgcolor="orange"><TR><TD>The multitude</TD><TD>Behold his mother! Behold his mother!</TD></TR></TBODY> |
<TBODY bgcolor="orange"><TR><TD>The multitude</TD><TD>Behold his mother! Behold his mother!</TD></TR></TBODY> |
||
</TABLE></ |
</TABLE></syntaxhighlight> |
||
===Robust solution=== |
|||
This solution uses the CSV parser and HTML-capable XML serializer included in the Python standard library to produce the same fancy table as in the "extra credit" version. |
|||
While not strictly necessary for the very constrained input given in Rosetta Code, using readily available high-level APIs is idiomatic for Python, makes bugs easier to catch, and this also demonstrates the kind of "safe to use with more general inputs" code that is good to get in the habit of reaching for when dealing with real-world inputs and outputs. |
|||
(eg. Suppose an earlier stage in the pipeline is using a proper CSV-generating library but is running in a locale that uses commas for decimal separators and has a bug that causes it to unexpectedly start feeding pretty-printed floating-point in. Using a proper CSV reader minimizes the potential harm and helps the program to raise errors in the most elucidating place.) |
|||
Since the version of ElementTree in the standard library does not support pretty-printing, the output it produces is minified. Unlike the "extra credit" version, this doesn't put each <code><TR></code> element in its own <code><TBODY></code>. |
|||
<syntaxhighlight lang=python>from csv import DictReader |
|||
from xml.etree import ElementTree as ET |
|||
def csv2html_robust(txt, header=True, attr=None): |
|||
# Use DictReader because, despite what the docs say, reader() doesn't |
|||
# return an object with .fieldnames |
|||
# (DictReader expects an iterable that returns lines, so split on \n) |
|||
reader = DictReader(txt.split('\n')) |
|||
table = ET.Element("TABLE", **attr.get('TABLE', {})) |
|||
thead_tr = ET.SubElement( |
|||
ET.SubElement(table, "THEAD", **attr.get('THEAD', {})), |
|||
"TR") |
|||
tbody = ET.SubElement(table, "TBODY", **attr.get('TBODY', {})) |
|||
if header: |
|||
for name in reader.fieldnames: |
|||
ET.SubElement(thead_tr, "TD").text = name |
|||
for row in reader: |
|||
tr_elem = ET.SubElement(tbody, "TR", **attr.get('TR', {})) |
|||
# Use reader.fieldnames to query `row` in the correct order. |
|||
# (`row` isn't an OrderedDict prior to Python 3.6) |
|||
for field in reader.fieldnames: |
|||
td_elem = ET.SubElement(tr_elem, "TD", **attr.get('TD', {})) |
|||
td_elem.text = row[field] |
|||
return ET.tostring(table, method='html') |
|||
htmltxt = csv2html_robust(csvtxt, True, { |
|||
'TABLE': {'border': "1", 'summary': "csv2html extra program output"}, |
|||
'THEAD': {'bgcolor': "yellow"}, |
|||
'TBODY': {'bgcolor': "orange"} |
|||
}) |
|||
print(htmltxt.decode('utf8'))</syntaxhighlight> |
|||
'''Sample HTML output''' |
|||
<!-- |
|||
The only difference between this and The output is semantically identical to the "extra credit" version, but whitespace has been collapsed as if it had been run through a minifier. |
|||
--> |
|||
<syntaxhighlight lang=html5><TABLE border="1" summary="csv2html extra program output"><THEAD bgcolor="yellow"><TR><TD>Character</TD><TD>Speech</TD></TR></THEAD><TBODY bgcolor="orange"><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><TR><TD>The multitude</TD><TD>Who are you?</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></TBODY></TABLE></syntaxhighlight> |
|||
===HTML outputs rendered in firefox browser=== |
===HTML outputs rendered in firefox browser=== |
||
[[File:Csv2html.PNG|500px|thumb|none]] |
[[File:Csv2html.PNG|500px|thumb|none]] |
||
=={{header|R}}== |
|||
Using base R functions only, this is a very basic implementation and produces a simple HTML table |
|||
<syntaxhighlight lang=rsplus>File <- "~/test.csv" |
|||
Opened <- readLines(con = File) |
|||
Size <- length(Opened) |
|||
HTML <- "~/test.html" |
|||
Table <- list() |
|||
for(i in 1:Size) |
|||
{ |
|||
#i=1 |
|||
Split <- unlist(strsplit(Opened[i],split = ",")) |
|||
Table[i] <- paste0("<td>",Split,"</td>",collapse = "") |
|||
Table[i] <- paste0("<tr>",Table[i],"</tr>") |
|||
} |
|||
Table[1] <- paste0("<table>",Table[1]) |
|||
Table[length(Table)] <- paste0(Table[length(Table)],"</table>") |
|||
writeLines(as.character(Table), HTML)</syntaxhighlight> |
|||
'''Sample HTML output:''' |
|||
<syntaxhighlight lang=html5><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>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> |
|||
<tr><td>The multitude</td><td>Who are you?</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></syntaxhighlight> |
|||
=={{header|Racket}}== |
|||
Uses X-exprs: |
|||
<syntaxhighlight lang=racket>#lang racket |
|||
(define e.g.-CSV |
|||
(string-join |
|||
'("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!") |
|||
"\n")) |
|||
(define (CSV-lines->HTML-table csv) |
|||
(define csv-rows |
|||
(regexp-split "\n" csv)) |
|||
(define csv-row-cells |
|||
(map (lambda (row) (regexp-split "," row)) csv-rows)) |
|||
(define (cell-data->HTML-data data) |
|||
`(td () ,data)) |
|||
(define (row-data->HTML-row CSV-row) |
|||
`(tr () ,@(map cell-data->HTML-data CSV-row) "\n")) |
|||
`(table |
|||
(thead |
|||
,(row-data->HTML-row (car csv-row-cells))) |
|||
(tbody ,@(map row-data->HTML-row (cdr csv-row-cells))))) |
|||
(require xml) |
|||
(display (xexpr->string (CSV-lines->HTML-table e.g.-CSV)))</syntaxhighlight> |
|||
'''Sample HTML output:''' |
|||
<syntaxhighlight lang=html5><table><thead><tr><td>Character</td><td>Speech</td> |
|||
</tr></thead><tbody><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><tr><td>The multitude</td><td>Who are you?</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></tbody></table></syntaxhighlight> |
|||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
{{works with|rakudo|2015.09}} |
|||
A very lispy solution: |
|||
<syntaxhighlight lang=raku line>my $str = "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!"; |
|||
# comment the next line out, if you want to read from standard input instead of the hard-coded $str above |
|||
# my $str = $*IN.slurp; |
|||
my &escape = *.trans(« & < > » => « & < > »); # a function with one argument that escapes the entities |
|||
my &tag = {"<$^tag>"~$^what~"</$^tag>"}; |
|||
printf |
|||
'<!DOCTYPE html> |
|||
<html> |
|||
<head><title>Some Text</title></head> |
|||
<body><table> |
|||
%s |
|||
</table></body></html> |
|||
', [~] # concatenation reduction ('a', 'b', 'c') → 'abc' |
|||
(escape($str).split(/\n/) # escape the string and split at newline |
|||
==> map -> $line {tag 'tr', # feed that into a map, that map function will tag as 'tr, and has an argument called $line |
|||
([~] $line.split(/','/)\ # split $line at ',', |
|||
# that / at the end is just an unspace, you can omit it, but then you have to delete |
|||
# all whitespace and comments between split(…) and .map |
|||
.map({tag 'td', $^cell}))})\ # map those cells as td |
|||
.join("\n"); # append a newline for nicer output</syntaxhighlight> |
|||
{{Out}} |
|||
<syntaxhighlight lang=html><!DOCTYPE html> |
|||
<html> |
|||
<head><title>Some Text</title></head> |
|||
<body><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>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> |
|||
<tr><td>The multitude</td><td>Who are you?</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></body></html></syntaxhighlight> |
|||
=={{header|Red}}== |
|||
<syntaxhighlight lang=Red>Red [] |
|||
csv: {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!} |
|||
add2html: func [ bl ] [append html rejoin bl ] ;; helper function to add block data to html string |
|||
;;---------------------------------------------------------------------- |
|||
csv2html: func ["function to generate string with html table from csv data file" |
|||
;;---------------------------------------------------------------------- |
|||
s [string!] "input .csv data" |
|||
][ |
|||
arr: split s newline ;; generate array (series) from string |
|||
html: copy "<table border=1>^/" ;; init html string |
|||
forall arr [ ;; i use forall here so that i can test for head? of series ... |
|||
either head? arr [ append html "<tr bgcolor=wheat>"] |
|||
[ append html "<tr>"] |
|||
replace/all first arr "<" "<" ;; escape "<" and ">" characters |
|||
replace/all first arr ">" ">" |
|||
foreach col split first arr "," [ |
|||
either head? arr [ |
|||
add2html ['<th> col '</th>] |
|||
][ |
|||
add2html ['<td> col '</td>] |
|||
] |
|||
] |
|||
add2html ['</tr> newline] |
|||
] |
|||
return add2html ['</table>] |
|||
] |
|||
;;---------------------------------------------------------------------- |
|||
print csv2html csv ;; call function |
|||
write %data.html csv2html csv ;; write to file |
|||
</syntaxhighlight> |
|||
output |
|||
<pre> |
|||
<table border=1> |
|||
<tr bgcolor=wheat><th>Character</th><th>Speech</th></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> |
|||
<tr><td>The multitude</td><td>Who are you?</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> |
|||
</pre> |
|||
'''Sample HTML output''' |
|||
<table border=1> |
|||
<tr bgcolor=wheat><th>Character</th><th>Speech</th></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> |
|||
<tr><td>The multitude</td><td>Who are you?</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> |
|||
=={{header|Retro}}== |
|||
<syntaxhighlight lang=Retro>remapping off |
|||
"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!" remapping on |
|||
keepString constant CSV |
|||
: display ( c- ) |
|||
[ ', = ] [ drop "</td><td>" puts ] when |
|||
[ 10 = ] [ drop "</td></tr>\n<tr><td>" puts ] when |
|||
[ '< = ] [ drop "<" puts ] when |
|||
[ '> = ] [ drop ">" puts ] when |
|||
[ '& = ] [ drop "&" puts ] when |
|||
putc ; |
|||
: displayHTML ( $- ) |
|||
"<table>\n<tr><td>" puts |
|||
[ @ display ] ^types'STRING each@ |
|||
"</td></tr>\n</table>" puts ; |
|||
CSV displayHTML</syntaxhighlight> |
|||
{{Out}} |
|||
<syntaxhighlight lang=html5><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>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> |
|||
<tr><td>The multitude</td><td>Who are you?</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></syntaxhighlight> |
|||
=={{header|REXX}}== |
|||
The rendered output was verified using Firefox Aurora with: |
|||
:::* file:///c:/output.html |
|||
:::* file:///c:/outputh.html |
|||
<syntaxhighlight lang=rexx>/*REXX program converts CSV ───► HTML table representing the CSV data. */ |
|||
arg header_ . /*obtain an uppercase version of args. */ |
|||
wantsHdr= (header_=='HEADER') /*is the arg (low/upp/mix case)=HEADER?*/ |
|||
/* [↑] determine if user wants a hdr. */ |
|||
iFID= 'CSV_HTML.TXT' /*the input fileID to be used. */ |
|||
if wantsHdr then oFID= 'OUTPUTH.HTML' /*the output fileID with header.*/ |
|||
else oFID= 'OUTPUT.HTML' /* " " " without " */ |
|||
do rows=0 while lines(iFID)\==0 /*read the rows from a (text/txt) file.*/ |
|||
row.rows= strip( linein(iFID) ) |
|||
end /*rows*/ |
|||
convFrom= '& < > "' /*special characters to be converted. */ |
|||
convTo = '& < > "' /*display what they are converted into.*/ |
|||
call write , '<html>' |
|||
call write , '<table border=4 cellpadding=9 cellspacing=1>' |
|||
do j=0 for rows; call write 5, '<tr>' |
|||
tx= 'td' |
|||
if wantsHdr & j==0 then tx= 'th' /*if user wants a header, then oblige. */ |
|||
do while row.j\==''; parse var row.j yyy "," row.j |
|||
do k=1 for words(convFrom) |
|||
yyy=changestr( word( convFrom, k), yyy, word( convTo, k)) |
|||
end /*k*/ |
|||
call write 10, '<'tx">"yyy'</'tx">" |
|||
end /*forever*/ |
|||
end /*j*/ |
|||
call write 5, '<tr>' |
|||
call write , '</table>' |
|||
call write , '</html>' |
|||
exit /*stick a fork in it, we're all done. */ |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
write: call lineout oFID, left('', 0 || arg(1) )arg(2); return</syntaxhighlight> |
|||
Some older REXXes don't have a '''changestr''' BIF, so one is included here ──► [[CHANGESTR.REX]]. <br> |
|||
'''output''' |
|||
<pre> |
|||
<html> |
|||
<table border=4 cellpadding=9 cellspacing=1> |
|||
<tr> |
|||
<td>Character</td> |
|||
<td>Speech</td> |
|||
<tr> |
|||
<td>The multitude</td> |
|||
<td>The messiah! Show us the messiah!</td> |
|||
<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> |
|||
<td>The multitude</td> |
|||
<td>Who are you?</td> |
|||
<tr> |
|||
<td>Brians mother</td> |
|||
<td>I'm his mother; that's who!</td> |
|||
<tr> |
|||
<td>The multitude</td> |
|||
<td>Behold his mother! Behold his mother!</td> |
|||
<tr> |
|||
</table> |
|||
</html> |
|||
</pre> |
|||
'''rendered output''' <br> <br> |
|||
<table border=4 cellpadding=9 cellspacing=1> |
|||
<tr> |
|||
<td>Character</td> |
|||
<td>Speech</td> |
|||
<tr> |
|||
<td>The multitude</td> |
|||
<td>The messiah! Show us the messiah!</td> |
|||
<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> |
|||
<td>The multitude</td> |
|||
<td>Who are you?</td> |
|||
<tr> |
|||
<td>Brians mother</td> |
|||
<td>I'm his mother; that's who!</td> |
|||
<tr> |
|||
<td>The multitude</td> |
|||
<td>Behold his mother! Behold his mother!</td> |
|||
<tr> |
|||
</table> |
|||
<br> |
|||
'''output''' (extra credit solution) when the first argument is HEADER in upper/lower/mixed case (with/without leading/trailing blanks). |
|||
<pre> |
|||
<html> |
|||
<table border=4 cellpadding=9 cellspacing=1> |
|||
<tr> |
|||
<th>Character</th> |
|||
<th>Speech</th> |
|||
<tr> |
|||
<td>The multitude</td> |
|||
<td>The messiah! Show us the messiah!</td> |
|||
<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> |
|||
<td>The multitude</td> |
|||
<td>Who are you?</td> |
|||
<tr> |
|||
<td>Brians mother</td> |
|||
<td>I'm his mother; that's who!</td> |
|||
<tr> |
|||
<td>The multitude</td> |
|||
<td>Behold his mother! Behold his mother!</td> |
|||
<tr> |
|||
</table> |
|||
</html> |
|||
</pre> |
|||
<br> |
|||
'''rendered output''' <br> <br> |
|||
<table border=4 cellpadding=9 cellspacing=1> |
|||
<tr> |
|||
<th>Character</th> |
|||
<th>Speech</th> |
|||
<tr> |
|||
<td>The multitude</td> |
|||
<td>The messiah! Show us the messiah!</td> |
|||
<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> |
|||
<td>The multitude</td> |
|||
<td>Who are you?</td> |
|||
<tr> |
|||
<td>Brians mother</td> |
|||
<td>I'm his mother; that's who!</td> |
|||
<tr> |
|||
<td>The multitude</td> |
|||
<td>Behold his mother! Behold his mother!</td> |
|||
<tr> |
|||
</table> |
|||
<br> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
The extra credit version has one extra line compared to the non-extra credit version. |
The extra credit version has one extra line compared to the non-extra credit version. |
||
To output a header, simply add "header" to the command line: |
|||
ruby csv2html.rb header |
ruby csv2html.rb header |
||
I/O is done through standard input/output. |
I/O is done through standard input/output. |
||
< |
<syntaxhighlight lang=ruby>require 'cgi' |
||
puts '<table summary="csv2html program output">' |
puts '<table summary="csv2html program output">' |
||
Line 2,339: | Line 5,385: | ||
end |
end |
||
puts "</table>"</ |
puts "</table>"</syntaxhighlight> |
||
{{Out}} |
|||
<syntaxhighlight lang=html5><table summary="csv2html program output"> |
|||
<tr><th>Character</th><th>Speech</th></tr> |
<tr><th>Character</th><th>Speech</th></tr> |
||
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr> |
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr> |
||
Line 2,346: | Line 5,394: | ||
<tr><td>Brians mother</td><td>I'm his mother; that's who!</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> |
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr> |
||
</table></ |
</table></syntaxhighlight> |
||
=={{header|Run BASIC}}== |
|||
The extra credit version has 2 extra lines of code to get the heading. |
|||
<syntaxhighlight lang=rnbasic>csv$ = "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!" |
|||
k = instr(csv$,",") ' 2 exra lines to get heading |
|||
csv$ = left$(csv$,k - 1) + "</th><th> + mid$(csv$,k + 1) |
|||
csv$ = strRep$(csv$,",","</td><td>") |
|||
html "<table border=1><TR bgcolor=wheat align=center><th>";strRep$(csv$,chr$(13),"</td></tr><tr><td>");"</td></tr></table" |
|||
wait |
|||
' -------------------------------- |
|||
' string replace rep str with |
|||
' -------------------------------- |
|||
FUNCTION strRep$(strRep$,rep$,with$) |
|||
ln = len(rep$) |
|||
k = instr(strRep$,rep$) |
|||
while k |
|||
strRep$ = left$(strRep$,k - 1) + with$ + mid$(strRep$,k + ln) |
|||
k = instr(strRep$,rep$) |
|||
WEND |
|||
END FUNCTION</syntaxhighlight> |
|||
{{Out}} |
|||
<table border=1><TR bgcolor=wheat align=center><th>Character</th><th>Speech</th></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> |
|||
<tr><td>The multitude</td><td>Who are you?</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> |
|||
=={{header|Rust}}== |
|||
{{trans|C}} |
|||
<syntaxhighlight lang=rust>static INPUT : &'static str = |
|||
"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!"; |
|||
fn main() { |
|||
print!("<table>\n<tr><td>"); |
|||
for c in INPUT.chars() { |
|||
match c { |
|||
'\n' => print!("</td></tr>\n<tr><td>"), |
|||
',' => print!("</td><td>"), |
|||
'<' => print!("<"), |
|||
'>' => print!(">"), |
|||
'&' => print!("&"), |
|||
_ => print!("{}", c) |
|||
} |
|||
} |
|||
println!("</td></tr>\n</table>"); |
|||
} |
|||
</syntaxhighlight> |
|||
{{Out}} |
|||
<syntaxhighlight lang=html5><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>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> |
|||
<tr><td>The multitude</td><td>Who are you?</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></syntaxhighlight> |
|||
=={{header|Scala}}== |
|||
{{libheader|org.scala-lang.xml}} |
|||
Scala has built-in support for XML, so you can freely mix XML literals into your Scala source code. This is nice, because instead of using strings to represent XML, you create XML literals that the compiler can understand and verify. This approach lets you easily generate dynamic XML by interweaving Scala code and XML in the same expressions.<syntaxhighlight lang=scala>object CsvToHTML extends App { |
|||
val header = <head> |
|||
<title>CsvToHTML</title> |
|||
<style type="text/css"> |
|||
td {{background-color:#ddddff; }} thead td {{background-color:#ddffdd; text-align:center; }} |
|||
</style> |
|||
</head> |
|||
val csv = |
|||
"""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!""".stripMargin |
|||
def csv2html(csv: String, withHead: Boolean) = { |
|||
def processRow(text: String) = <tr> |
|||
{text.split(',').map(s => <td> |
|||
{s} |
|||
</td>)} |
|||
</tr> |
|||
val (first :: rest) = csv.lines.toList // Separate the header and the rest |
|||
def tableHead = if (withHead) |
|||
<thead> |
|||
{processRow(first)} |
|||
</thead> |
|||
else processRow(first) |
|||
<html> |
|||
{header}<body> |
|||
<table> |
|||
{tableHead}{rest.map(processRow)} |
|||
</table> |
|||
</body> |
|||
</html> |
|||
} |
|||
println(csv2html(csv, true)) |
|||
}</syntaxhighlight>{{out}}<syntaxhighlight lang=html><html> |
|||
<head> |
|||
<title>CsvToHTML</title> |
|||
<style type="text/css"> |
|||
td {background-color:#ddddff; } thead td {background-color:#ddffdd; text-align:center; } |
|||
</style> |
|||
</head><body> |
|||
<table> |
|||
<thead> |
|||
<tr> |
|||
<td> |
|||
Character |
|||
</td><td> |
|||
Speech |
|||
</td> |
|||
</tr> |
|||
</thead><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><tr> |
|||
<td> |
|||
The multitude |
|||
</td><td> |
|||
Who are you? |
|||
</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> |
|||
</body> |
|||
</html></syntaxhighlight> |
|||
=={{header|Sed}}== |
|||
File csv2html.sed |
|||
<syntaxhighlight lang=sed>#!/bin/sed -f |
|||
s|<|\<|g |
|||
s|>|\>|g |
|||
s|^| <tr>\n <td>| |
|||
s|,|</td>\n <td>| |
|||
s|$|</td>\n </tr>| |
|||
1s|^|<table>\n| |
|||
$s|$|\n</table>|</syntaxhighlight> |
|||
<pre>$ sed -f csv2html.sed input.csv</pre> |
|||
<syntaxhighlight lang=html5><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>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> |
|||
<tr> |
|||
<td>The multitude</td> |
|||
<td>Who are you?</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></syntaxhighlight> |
|||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
Line 2,352: | Line 5,605: | ||
function [http://seed7.sourceforge.net/libraries/html_ent.htm#encodeHtmlContent%28in_string%29 encodeHtmlContent], |
function [http://seed7.sourceforge.net/libraries/html_ent.htm#encodeHtmlContent%28in_string%29 encodeHtmlContent], |
||
which replaces characters with HTML entities. E.g.: '<' is replaced by ''&lt;''. |
which replaces characters with HTML entities. E.g.: '<' is replaced by ''&lt;''. |
||
< |
<syntaxhighlight lang=seed7>$ include "seed7_05.s7i"; |
||
include "html_ent.s7i"; |
include "html_ent.s7i"; |
||
Line 2,381: | Line 5,634: | ||
end for; |
end for; |
||
writeln("</table>"); |
writeln("</table>"); |
||
end func;</ |
end func;</syntaxhighlight> |
||
{{Out}} |
|||
Output: |
|||
< |
<syntaxhighlight lang=html5> |
||
<table> |
<table> |
||
<tr><th>Character</th><th>Speech</th></tr> |
<tr><th>Character</th><th>Speech</th></tr> |
||
Line 2,394: | Line 5,647: | ||
<tr><td></td></tr> |
<tr><td></td></tr> |
||
</table> |
</table> |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} viewed with a browser: |
|||
<table> |
<table> |
||
<tr><th>Character</th><th>Speech</th></tr> |
<tr><th>Character</th><th>Speech</th></tr> |
||
Line 2,406: | Line 5,659: | ||
<tr><td></td></tr> |
<tr><td></td></tr> |
||
</table> |
</table> |
||
=={{header|Sidef}}== |
|||
<syntaxhighlight lang=ruby>func escape(str) { str.trans(« & < > », « & < > ») } |
|||
func tag(t, d) { "<#{t}>#{d}</#{t}>" } |
|||
func csv2html(str) { |
|||
var template = <<-'EOT' |
|||
<!DOCTYPE html> |
|||
<html> |
|||
<head><title>Some Text</title></head> |
|||
<body><table> |
|||
%s |
|||
</table></body></html> |
|||
EOT |
|||
template.sprintf(escape(str).lines.map{ |line| |
|||
tag('tr', line.split(',').map{|cell| tag('td', cell) }.join) |
|||
}.join("\n") |
|||
) |
|||
} |
|||
var str = <<'EOT'; |
|||
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! |
|||
EOT |
|||
print csv2html(str)</syntaxhighlight> |
|||
{{out}} |
|||
<syntaxhighlight lang=html5><!DOCTYPE html> |
|||
<html> |
|||
<head><title>Some Text</title></head> |
|||
<body><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>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> |
|||
<tr><td>The multitude</td><td>Who are you?</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></body></html></syntaxhighlight> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
Line 2,411: | Line 5,708: | ||
{{tcllib|html}} |
{{tcllib|html}} |
||
{{tcllib|struct::queue}} |
{{tcllib|struct::queue}} |
||
< |
<syntaxhighlight lang=tcl>package require Tcl 8.5 |
||
package require csv |
package require csv |
||
package require html |
package require html |
||
Line 2,434: | Line 5,731: | ||
}] |
}] |
||
[html::closeTag] |
[html::closeTag] |
||
}]</ |
}]</syntaxhighlight> |
||
Extra credit version: |
Extra credit version: |
||
< |
<syntaxhighlight lang=tcl>package require Tcl 8.5 |
||
package require csv |
package require csv |
||
package require html |
package require html |
||
Line 2,481: | Line 5,778: | ||
}] |
}] |
||
}] |
}] |
||
}]</ |
}]</syntaxhighlight> |
||
{{out}} |
|||
Output: |
|||
< |
<syntaxhighlight lang=html5><table border="1" summary="csv2html program output"> |
||
<tr bgcolor="yellow"><td>Character</td><td>Speech</td></tr> |
<tr bgcolor="yellow"><td>Character</td><td>Speech</td></tr> |
||
<tr bgcolor="orange"><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr> |
<tr bgcolor="orange"><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr> |
||
Line 2,490: | Line 5,787: | ||
<tr bgcolor="orange"><td>Brians mother</td><td>I'm his mother; that's who!</td></tr> |
<tr bgcolor="orange"><td>Brians mother</td><td>I'm his mother; that's who!</td></tr> |
||
<tr bgcolor="orange"><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr> |
<tr bgcolor="orange"><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr> |
||
</table></ |
</table></syntaxhighlight> |
||
=={{header|TUSCRIPT}}== |
=={{header|TUSCRIPT}}== |
||
< |
<syntaxhighlight lang=tuscript> |
||
$$ MODE TUSCRIPT |
$$ MODE TUSCRIPT |
||
MODE DATA |
MODE DATA |
||
Line 2,531: | Line 5,829: | ||
WRITE html "</table></body></html>" |
WRITE html "</table></body></html>" |
||
ENDACCESS/PRINT html |
ENDACCESS/PRINT html |
||
</syntaxhighlight> |
|||
</lang> |
|||
=== Output (source code) === |
=== Output (source code) === |
||
< |
<syntaxhighlight lang=html5><!DOCTYPE html system> |
||
<html> |
<html> |
||
<head> |
<head> |
||
Line 2,548: | Line 5,846: | ||
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr> |
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr> |
||
</table></body></html> |
</table></body></html> |
||
</syntaxhighlight> |
|||
</lang> |
|||
=== Output (rendered) === |
=== Output (rendered) === |
||
[[file:tuscript_csv2html.png|500px|thumb|none|rendered by browser ]] |
[[file:tuscript_csv2html.png|500px|thumb|none|rendered by browser ]] |
||
=={{header|TXR}}== |
=={{header|TXR}}== |
||
====Simple==== |
|||
<lang txr>@(collect) |
|||
<syntaxhighlight lang=txr>@(collect) |
|||
@char,@speech |
@char,@speech |
||
@(end) |
@(end) |
||
Line 2,567: | Line 5,866: | ||
@ (end) |
@ (end) |
||
</table> |
</table> |
||
@(end)</ |
@(end)</syntaxhighlight> |
||
Note some similarities in the output generation to the AWS template system in the [[#Ada|Ada]] solution. |
|||
In AWS, <code>@@TABLE@@</code> does something similar to TXR's <code>@(repeat)</code>: repeating the section of a template as many times as the vector variables have items. |
|||
Output: |
|||
{{Out}} |
|||
<pre>$ txr csv.txr csv.txt</pre> |
<pre>$ txr csv.txr csv.txt</pre> |
||
< |
<syntaxhighlight lang=html5><table> |
||
<tr> |
<tr> |
||
<td>Character</td> |
<td>Character</td> |
||
Line 2,601: | Line 5,896: | ||
<td>Behold his mother! Behold his mother!</td> |
<td>Behold his mother! Behold his mother!</td> |
||
</tr> |
</tr> |
||
</table></ |
</table></syntaxhighlight> |
||
====With Styling==== |
|||
<syntaxhighlight lang=txr>@(collect) |
|||
@char,@speech |
|||
@(end) |
|||
@(output :filter :to_html) |
|||
<style type="text/css"> |
|||
tr.odd td { |
|||
background-color: #CC9999; color: black; |
|||
} |
|||
tr.even td { |
|||
background-color: #9999CC; color: black; |
|||
} |
|||
th { |
|||
background-color: #99CC99; color: black; |
|||
} |
|||
</style> |
|||
<table> |
|||
@ (repeat :counter row) |
|||
<tr class="@(if (evenp row) 'even 'odd)"> |
|||
<td>@char</td> |
|||
<td>@speech</td> |
|||
</tr> |
|||
@ (first) |
|||
<tr> |
|||
<th>@char</th> |
|||
<th>@speech</th> |
|||
</tr> |
|||
@ (end) |
|||
</table> |
|||
@(end)</syntaxhighlight> |
|||
{{Out}} |
|||
<pre>$ txr csv2.txr csv.txt</pre> |
|||
<syntaxhighlight lang=html5><style type="text/css"> |
|||
tr.odd td { |
|||
background-color: #CC9999; color: black; |
|||
} |
|||
tr.even td { |
|||
background-color: #9999CC; color: black; |
|||
} |
|||
th { |
|||
background-color: #99CC99; color: black; |
|||
} |
|||
</style> |
|||
<table> |
|||
<tr> |
|||
<th>Character</th> |
|||
<th>Speech</th> |
|||
</tr> |
|||
<tr class="odd"> |
|||
<td>The multitude</td> |
|||
<td>The messiah! Show us the messiah!</td> |
|||
</tr> |
|||
<tr class="even"> |
|||
<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> |
|||
<tr class="odd"> |
|||
<td>The multitude</td> |
|||
<td>Who are you?</td> |
|||
</tr> |
|||
<tr class="even"> |
|||
<td>Brians mother</td> |
|||
<td>I'm his mother; that's who!</td> |
|||
</tr> |
|||
<tr class="odd"> |
|||
<td>The multitude</td> |
|||
<td>Behold his mother! Behold his mother!</td> |
|||
</tr> |
|||
</table></syntaxhighlight> |
|||
=={{header|UNIX Shell}}== |
|||
{{works with|bash}} |
|||
<syntaxhighlight lang=bash>csv2html() { |
|||
IFS=, |
|||
echo "<table>" |
|||
echo "<thead>" |
|||
read -a fields |
|||
htmlrow th "${fields[@]}" |
|||
echo "</thead>" |
|||
echo "<tbody>" |
|||
while read -a fields |
|||
do htmlrow td "${fields[@]}" |
|||
done |
|||
echo "</tbody>" |
|||
echo "</table>" |
|||
} |
|||
htmlrow() { |
|||
cell=$1 |
|||
shift |
|||
echo "<tr>" |
|||
for field |
|||
do echo "<$cell>$(escape_html "$field")</$cell>" |
|||
done |
|||
echo "</tr>" |
|||
} |
|||
escape_html() { |
|||
str=${1//\&/&} |
|||
str=${str//</<} |
|||
str=${str//>/>} |
|||
echo "$str" |
|||
} |
|||
csv2html <<-END |
|||
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! |
|||
END</syntaxhighlight> |
|||
{{output}} |
|||
<syntaxhighlight lang=html5><table> |
|||
<thead> |
|||
<tr> |
|||
<th>Character</th> |
|||
<th>Speech</th> |
|||
</tr> |
|||
</thead> |
|||
<tbody> |
|||
<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> |
|||
<tr> |
|||
<td>The multitude</td> |
|||
<td>Who are you?</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> |
|||
</tbody> |
|||
</table></syntaxhighlight> |
|||
=={{header|VBA}}== |
|||
{{trans|Phix}}<syntaxhighlight lang=vb>Public Sub CSV_TO_HTML() |
|||
input_ = "Character,Speech\n" & _ |
|||
"The multitude,The messiah! Show us the messiah!\n" & _ |
|||
"Brians mother,<angry>Now you listen here! He's not the messiah; " & _ |
|||
"he's a very naughty boy! Now go away!</angry>\n" & _ |
|||
"The multitude,Who are you?\n" & _ |
|||
"Brians mother,I'm his mother; that's who!\n" & _ |
|||
"The multitude,Behold his mother! Behold his mother!" |
|||
Debug.Print "<table>" & vbCrLf & "<tr><td>" |
|||
For i = 1 To Len(input_) |
|||
Select Case Mid(input_, i, 1) |
|||
Case "\" |
|||
If Mid(input_, i + 1, 1) = "n" Then |
|||
Debug.Print "</td></tr>" & vbCrLf & "<tr><td>"; |
|||
i = i + 1 |
|||
Else |
|||
Debug.Print Mid(input_, i, 1); |
|||
End If |
|||
Case ",": Debug.Print "</td><td>"; |
|||
Case "<": Debug.Print "<"; |
|||
Case ">": Debug.Print ">"; |
|||
Case "&": Debug.Print "&"; |
|||
Case Else: Debug.Print Mid(input_, i, 1); |
|||
End Select |
|||
Next i |
|||
Debug.Print "</td></tr>" & vbCrLf & "</table>" |
|||
End Sub</syntaxhighlight>{{out}} |
|||
<syntaxhighlight lang=html5><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>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> |
|||
<tr><td>The multitude</td><td>Who are you?</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></syntaxhighlight> |
|||
=={{header|VBScript}}== |
|||
<syntaxhighlight lang=vb> |
|||
Set objfso = CreateObject("Scripting.FileSystemObject") |
|||
parent_folder = objfso.GetParentFolderName(WScript.ScriptFullName) & "\" |
|||
Set objcsv = objfso.OpenTextFile(parent_folder & "in.csv",1,False) |
|||
Set objhtml = objfso.OpenTextFile(paren_folder & "out.html",2,True) |
|||
objhtml.Write(csv_to_html(objcsv.ReadAll)) |
|||
objcsv.Close |
|||
objhtml.Close |
|||
Set objfso = Nothing |
|||
Function csv_to_html(s) |
|||
row = Split(s,vbCrLf) |
|||
'write the header |
|||
tmp = "<html><head><head/><body><table border=1 cellpadding=10 cellspacing=0>" |
|||
For i = 0 To UBound(row) |
|||
field = Split(row(i),",") |
|||
If i = 0 Then |
|||
tmp = tmp & "<tr><th>" & replace_chars(field(0)) & "</th><th>" & replace_chars(field(1)) & "</th><tr>" |
|||
Else |
|||
tmp = tmp & "<tr><td>" & replace_chars(field(0)) & "</td><td>" & replace_chars(field(1)) & "</td><tr>" |
|||
End If |
|||
Next |
|||
'write the footer |
|||
tmp = tmp & "</table></body></html>" |
|||
csv_to_html = tmp |
|||
End Function |
|||
Function replace_chars(s) |
|||
replace_chars = Replace(Replace(s,"<","<"),">",">") |
|||
End Function |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
Format derived from BBC BASIC output. |
|||
<table border=1 cellpadding=10 cellspacing=0><tr><th>Character</th><th>Speech</th><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><tr><td>The multitude</td><td>Who are you?</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> |
|||
=={{header|Vedit macro language}}== |
=={{header|Vedit macro language}}== |
||
Line 2,608: | Line 6,132: | ||
If a block is highlighted, only the block is converted. If no block highlighted, the entire file is converted. |
If a block is highlighted, only the block is converted. If no block highlighted, the entire file is converted. |
||
< |
<syntaxhighlight lang=vedit>if (BB < 0) { // block not set |
||
BB(0) // convert entire file |
BB(0) // convert entire file |
||
BE(File_Size) |
BE(File_Size) |
||
Line 2,634: | Line 6,158: | ||
Line(1) |
Line(1) |
||
} |
} |
||
BB(Clear)</ |
BB(Clear)</syntaxhighlight> |
||
{{out}} |
|||
Output: |
|||
< |
<syntaxhighlight lang=html5><table> |
||
<tr><td>Character</td><td>Speech </td></tr> |
<tr><td>Character</td><td>Speech </td></tr> |
||
<tr><td>The multitude</td><td>The messiah! Show us the messiah! </td></tr> |
<tr><td>The multitude</td><td>The messiah! Show us the messiah! </td></tr> |
||
Line 2,644: | Line 6,168: | ||
<tr><td>Brians mother</td><td>I'm his mother; that's who! </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> |
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr> |
||
</table></ |
</table></syntaxhighlight> |
||
=={{header|Visual Basic .NET}}== |
|||
{{works with|.NET Framework}} |
|||
Uses XML literals and the TextFieldParser class of the VB runtime, which can parse delimited or fixed-width files. |
|||
The optional first command-line argument denotes whether to use <thead /> for the first row. The optional second argument specifies the path of the CSV file. If no second argument is given, the program reads from the console until stop characters are encountered. |
|||
TextFieldParser is designed to work with files and so makes heavy use of peeking, which results in buggy behavior when signaling end-of-file using the console. The most reliable way seems to be alternately pressing enter and Ctrl+Z after the last character of the last line of data. |
|||
<syntaxhighlight lang=vbnet>Imports Microsoft.VisualBasic.FileIO |
|||
Module Program |
|||
Sub Main(args As String()) |
|||
Dim parser As TextFieldParser |
|||
Try |
|||
If args.Length > 1 Then |
|||
parser = My.Computer.FileSystem.OpenTextFieldParser(args(1), ",") |
|||
Else |
|||
parser = New TextFieldParser(Console.In) With {.Delimiters = {","}} |
|||
End If |
|||
Dim getLines = |
|||
Iterator Function() As IEnumerable(Of String()) |
|||
Do Until parser.EndOfData |
|||
Yield parser.ReadFields() |
|||
Loop |
|||
End Function |
|||
Dim result = CSVTOHTML(getLines(), If(args.Length > 0, Boolean.Parse(args(0)), False)) |
|||
Console.WriteLine(result) |
|||
Finally |
|||
If parser IsNot Nothing Then parser.Dispose() |
|||
End Try |
|||
End Sub |
|||
Function CSVTOHTML(lines As IEnumerable(Of IEnumerable(Of String)), useTHead As Boolean) As XElement |
|||
Dim getRow = Function(row As IEnumerable(Of String)) From field In row Select <td><%= field %></td> |
|||
CSVTOHTML = |
|||
<table> |
|||
<%= From l In lines.Select( |
|||
Function(line, i) |
|||
If useTHead AndAlso i = 0 Then |
|||
Return <thead><%= getRow(line) %></thead> |
|||
Else |
|||
Return <tr><%= getRow(line) %></tr> |
|||
End If |
|||
End Function) %> |
|||
</table> |
|||
End Function |
|||
End Module</syntaxhighlight> |
|||
{{out|input=true}} |
|||
<syntaxhighlight lang=html5>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! |
|||
^Z |
|||
^Z |
|||
<table> |
|||
<thead> |
|||
<td>Character</td> |
|||
<td>Speech</td> |
|||
</thead> |
|||
<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> |
|||
<tr> |
|||
<td>The multitude</td> |
|||
<td>Who are you?</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></syntaxhighlight> |
|||
=={{header|Wren}}== |
|||
{{trans|Kotlin}} |
|||
<syntaxhighlight lang="wren">var csv = |
|||
"Character,Speech\n" + |
|||
"The multitude,The messiah! Show us the messiah!\n" + |
|||
"Brians mother,<angry>Now you listen here! He's not the messiah; " + |
|||
"he's a very naughty boy! Now go away!</angry>\n" + |
|||
"The multitude,Who are you?\n" + |
|||
"Brians mother,I'm his mother; that's who!\n" + |
|||
"The multitude,Behold his mother! Behold his mother!" |
|||
var i = " " // indent |
|||
var ii = i + i // double indent |
|||
var iii = ii + i // triple indent |
|||
var sb = "<table>\n%(i)<tr>\n%(ii)<td>" |
|||
for (c in csv) { |
|||
sb = sb + ((c == "\n") ? "</td>\n%(i)</tr>\n%(i)<tr>\n%(ii)<td>" : |
|||
(c == ",") ? "</td>\n%(ii)<td>" : |
|||
(c == "&") ? "&" : |
|||
(c == "'") ? "'" : |
|||
(c == "<") ? "<" : |
|||
(c == ">") ? ">" : c) |
|||
} |
|||
sb = sb + "</td>\n%(i)</tr>\n</table>" |
|||
System.print(sb) |
|||
System.print() |
|||
// now using first row as a table header |
|||
sb = "<table>\n%(i)<thead>\n%(ii)<tr>\n%(iii)<td>" |
|||
var hLength = csv.indexOf("\n") + 1 // find length of first row including CR |
|||
for (c in csv.take(hLength)) { |
|||
sb = sb + ((c == "\n") ? "</td>\n%(ii)</tr>\n%(i)</thead>\n%(i)<tbody>\n%(ii)<tr>\n%(iii)<td>" : |
|||
(c == ",") ? "</td>\n%(iii)<td>" : c) |
|||
} |
|||
for (c in csv.skip(hLength)) { |
|||
sb = sb + ((c == "\n") ? "</td>\n%(ii)</tr>\n%(ii)<tr>\n%(iii)<td>" : |
|||
(c == ",") ? "</td>\n%(iii)<td>" : |
|||
(c == "&") ? "&" : |
|||
(c == "'") ? "'" : |
|||
(c == "<") ? "<" : |
|||
(c == ">") ? ">" : c) |
|||
} |
|||
sb = sb + "</td>\n%(ii)</tr>\n%(i)</tbody>\n</table>" |
|||
System.print(sb)</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Same as Kotlin entry. |
|||
</pre> |
|||
=={{header|XPL0}}== |
|||
{{trans|C}} |
|||
<syntaxhighlight lang "XPL0">string 0; |
|||
char Input, S; |
|||
[Input:= |
|||
"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!"; |
|||
Text(0, "<table>^m^j<tr><td>"); |
|||
S:= Input; |
|||
while S(0) do |
|||
[case S(0) of |
|||
$0A: Text(0, "</td></tr>^m^j<tr><td>"); |
|||
^,: Text(0, "</td><td>"); |
|||
^<: Text(0, "<"); |
|||
^>: Text(0, ">"); |
|||
^&: Text(0, "&") |
|||
other ChOut(0, S(0)); |
|||
S:= S+1; |
|||
]; |
|||
Text(0, "</td></tr>^m^j</table>"); |
|||
]</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
<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>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> |
|||
<tr><td>The multitude</td><td>Who are you?</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> |
|||
</pre> |
|||
=={{header|XSLT 2.0}}== |
|||
<h3>Setup</h3> |
|||
# Download to a local copy of the [http://pascaliburnus.seanbdurkin.id.au/index.php?/archives/17-A-Generalised-and-Comprehensive-Solution-to-CSV-to-XML-and-XML-to-CSV-Transformations.html#extended csv-to-xml.xslt library] style-sheet listed in the linked blog entry. Alternatively a copy of the style-sheet on this wiki can be found here: [[csv-to-xml.xslt]] |
|||
# Pass as the URI of the input csv as a parameter (named url-of-csv) to your XSLT 2.0 processor |
|||
<syntaxhighlight lang=text><xsl:stylesheet |
|||
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" |
|||
xmlns:xcsvt="http://www.seanbdurkin.id.au/xslt/csv-to-xml.xslt" |
|||
xmlns:xs="http://www.w3.org/2001/XMLSchema" |
|||
xmlns:xcsv="http://www.seanbdurkin.id.au/xslt/xcsv.xsd" |
|||
version="2.0" |
|||
exclude-result-prefixes="xsl xs xcsvt xcsv"> |
|||
<xsl:import href="csv-to-xml.xslt" /> |
|||
<xsl:output indent="yes" encoding="UTF-8" method="html" doctype-system="about:legacy-compat" /> |
|||
<xsl:import-schema schema-location="http://www.seanbdurkin.id.au/xslt/xcsv.xsd" |
|||
use-when="system-property('xsl:is-schema-aware')='yes'" /> |
|||
<xsl:param name="url-of-csv" as="xs:string" select="'roseta.csv'" /> |
|||
<xsl:variable name="phase-1-result"> |
|||
<xsl:call-template name="xcsvt:main" /> |
|||
</xsl:variable> |
|||
<xsl:template match="/"> |
|||
<html lang="en"> |
|||
<head><title>CSV to HTML translation - Extra Credit</title></head> |
|||
<body> |
|||
<xsl:apply-templates select="$phase-1-result" mode="phase-2" /> |
|||
</body> |
|||
</html> |
|||
</xsl:template> |
|||
<xsl:template match="xcsv:comma-separated-single-line-values" mode="phase-2"> |
|||
<table> |
|||
<xsl:apply-templates mode="phase-2" /> |
|||
</table> |
|||
</xsl:template> |
|||
<xsl:template match="xcsv:row[1]" mode="phase-2"> |
|||
<th> |
|||
<xsl:apply-templates mode="phase-2" /> |
|||
</th> |
|||
</xsl:template> |
|||
<xsl:template match="xcsv:row" mode="phase-2"> |
|||
<tr> |
|||
<xsl:apply-templates mode="phase-2" /> |
|||
</tr> |
|||
</xsl:template> |
|||
<xsl:template match="xcsv:value" mode="phase-2"> |
|||
<td> |
|||
<xsl:apply-templates mode="phase-2" /> |
|||
</td> |
|||
</xsl:template> |
|||
<xsl:template match="xcsv:notice" mode="phase-2" /> |
|||
</xsl:stylesheet></syntaxhighlight> |
|||
This style-sheet, when applied to the task input, yields... |
|||
<syntaxhighlight lang=html5><!DOCTYPE html |
|||
SYSTEM "about:legacy-compat"> |
|||
<html lang="en"> |
|||
<head> |
|||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> |
|||
<title>CSV to HTML translation - Extra Credit</title> |
|||
</head> |
|||
<body> |
|||
<table> |
|||
<th> |
|||
<td>Character</td> |
|||
<td>Speech</td> |
|||
</th> |
|||
<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> |
|||
<tr> |
|||
<td>The multitude</td> |
|||
<td>Who are you?</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> |
|||
</body> |
|||
</html></syntaxhighlight> |
|||
=={{header|zkl}}== |
|||
<syntaxhighlight lang=zkl>csvData:=Data(0,Int,"Character,Speech\n" |
|||
"The multitude,The messiah! Show us the messiah!\n" |
|||
"Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>\n" |
|||
"The multitude,Who are you\n" |
|||
"Brians mother,I'm his mother; that's who!\n" |
|||
"The multitude,Behold his mother! Behold his mother!"); |
|||
html:=csvData.pump("<table>\n",fcn(line){ |
|||
line.replace("&","&").replace("<","<") // <angry/> --> <angry/> |
|||
.split(",") |
|||
.pump("<tr>\n","strip",String.fpM("101"," <td>","</td>\n"))+"</tr>\n" |
|||
}) + "</table>"; |
|||
html.println();</syntaxhighlight> |
|||
The pump method writes or appends to a sink, in this case a string (eg "<table/>"). The fpM method is function/partial application and takes a string (ones and zeros) to indicate the positional input parameters. So String.fpM("101","a","c")("B") creates a new string "aBc". |
|||
{{out}} |
|||
<syntaxhighlight lang=html5><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>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> |
|||
<tr> |
|||
<td>The multitude</td> |
|||
<td>Who are you</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></syntaxhighlight> |
|||
[[Category:CSV]] |
|||
[[Category:HTML]] |
Latest revision as of 23:00, 22 July 2024
You are encouraged to solve this task according to the task description, using any language you may know.
Consider a simplified CSV format where all rows are separated by a newline and all columns are separated by commas.
No commas are allowed as field data, but the data may contain other characters and character sequences that would normally be escaped when converted to HTML
- Task
Create a function that takes a string representation of the CSV data and returns a text string of an HTML table representing the CSV data.
Use the following data as the CSV text to convert, and show your output.
- 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!
- Extra credit
Optionally allow special formatting for the first row of the table as if it is the tables header row
(via <thead> preferably; CSS if you must).
11l
V input_csv = ‘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!’
print("<table>\n<tr><td>", end' ‘’)
L(c) input_csv
print(S c {
"\n"{"</td></tr>\n<tr><td>"}
‘,’ {‘</td><td>’}
‘<’ {‘<’}
‘>’ {‘>’}
‘&’ {‘&’}
E {c}
}, end' ‘’)
print("</td></tr>\n</table>")
- Output:
<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>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>
<tr><td>The multitude</td><td>Who are you?</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>
Ada
csv2html.adb:
with Ada.Strings.Fixed;
with Ada.Text_IO;
with Templates_Parser;
procedure Csv2Html is
use type Templates_Parser.Vector_Tag;
Chars : Templates_Parser.Vector_Tag;
Speeches : Templates_Parser.Vector_Tag;
CSV_File : Ada.Text_IO.File_Type;
begin
-- read the csv data
Ada.Text_IO.Open (File => CSV_File,
Mode => Ada.Text_IO.In_File,
Name => "data.csv");
-- fill the tags
while not Ada.Text_IO.End_Of_File (CSV_File) loop
declare
Whole_Line : String := Ada.Text_IO.Get_Line (CSV_File);
Comma_Pos : Natural := Ada.Strings.Fixed.Index (Whole_Line, ",");
begin
Chars := Chars & Whole_Line (Whole_Line'First .. Comma_Pos - 1);
Speeches := Speeches & Whole_Line (Comma_Pos + 1 .. Whole_Line'Last);
end;
end loop;
Ada.Text_IO.Close (CSV_File);
-- build translation table and output html
declare
Translations : constant Templates_Parser.Translate_Table :=
(1 => Templates_Parser.Assoc ("CHAR", Chars),
2 => Templates_Parser.Assoc ("SPEECH", Speeches));
begin
Ada.Text_IO.Put_Line
(Templates_Parser.Parse ("table.tmplt", Translations));
end;
end Csv2Html;
table.tmplt:
<table>
@@TABLE@@
<tr>
<td>@_WEB_ESCAPE:CHAR_@</td>
<td>@_WEB_ESCAPE:SPEECH_@</td>
</tr>
@@END_TABLE@@
</table>
- Output:
<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>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>
<tr>
<td>The multitude</td>
<td>Who are you?</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>
ALGOL 68
#!/usr/local/bin/a68g --script #
[6]STRING rows := []STRING(
"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!"
);
[max abs char]STRING encoded; FOR i TO UPB encoded DO encoded[i]:=REPR i OD;
# encoded[ABS""""] := """; optional #
encoded[ABS "&"] := "&";
encoded[ABS "<"] := "<";
# encoded[ABS ">"] := ">"; optional #
OP ENCODE = (STRING s)STRING: (
STRING out := "";
FOR i TO UPB s DO out+:= encoded[ABS s[i]] OD;
out
);
PROC head = (STRING title)VOID: (
printf((
$"<HEAD>"l$,
$"<TITLE>"g"</TITLE>"l$, title,
$"<STYLE type=""text/css"">"l$,
$"TD {background-color:#ddddff; }"l$,
$"thead TD {background-color:#ddffdd; text-align:center; }"l$,
$"</STYLE>"l$,
$"</HEAD>"l$
))
);
# define HTML tags using Algol68's "reverent" block structuring #
PROC html = VOID: print(("<HTML>", new line)),
body = VOID: print(("<BODY>", new line)),
table = VOID: print(("<TABLE>", new line)),
table row = VOID: print(("<TR>")),
th = (STRING s)VOID: printf(($"<TH>"g"</TH>"$, s)),
td = (STRING s)VOID: printf(($"<TD>"g"</TD>"$, s)),
elbat row = VOID: print(("</TR>", new line)),
elbat = VOID: print(("</TABLE>", new line)),
ydob = VOID: print(("</BODY>", new line)),
lmth = VOID: print(("</HTML>", new line));
FILE row input; STRING row; CHAR ifs = ",";
associate(row input, row); make term(row input, ifs);
html;
head("CSV to HTML translation - Extra Credit");
body;
table;
FOR nr TO UPB rows DO
row := rows[nr];
table row;
on logical file end(row input, (REF FILE row input)BOOL: row end);
FOR nf DO
STRING field; get(row input,field);
(nr=1|th|td)(ENCODE field);
get(row input, space)
OD;
row end: reset(row input);
elbat row
OD;
elbat;
ydob;
lmth
- Output:
<HTML>
<HEAD>
<TITLE>CSV to HTML translation - Extra Credit</TITLE>
<STYLE type="text/css">
TD {background-color:#ddddff; }
thead TD {background-color:#ddffdd; text-align:center; }
</STYLE>
</HEAD>
<BODY>
<TABLE>
<TR><TH>Character</TH><TH>Speech</TH></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>
<TR><TD>The multitude</TD><TD>Who are you?</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>
</BODY>
</HTML>
ANTLR
Java
// Create an HTML Table from comma seperated values
// Nigel Galloway - June 2nd., 2013
grammar csv2html;
dialog : {System.out.println("<HTML><Table>");}header body+{System.out.println("</Table></HTML>");} ;
header : {System.out.println("<THEAD align=\"center\"><TR bgcolor=\"blue\">");}row{System.out.println("</TR></THEAD");};
body : {System.out.println("<TBODY><TR>");}row{System.out.println("</TR></TBODY");};
row : field ',' field '\r'? '\n';
field : Field{System.out.println("<TD>" + $Field.text.replace("<","<").replace(">",">") + "</TD>");};
Field : ~[,\n\r]+;
Applesoft BASIC
100 DATA "Character,Speech"
110 DATA "The multitude,The messiah! Show us the messiah!"
120 DATA "Brian's mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>"
130 DATA "The multitude,Who are you?"
140 DATA "Brian's mother,I'm his mother; that's who!"
150 DATA "The multitude,Behold his mother! Behold his mother!"
160 DATA
170 LET M$ = CHR$ (13)
180 LET Q$ = CHR$ (34)
190 LET TRUE = NOT FALSE
200 LET HEADER = TRUE
210 DIM C(255),H$(4,1)
220 LET H$(1,0) = "</TD><TD>"
230 LET H$(2,0) = "<"
240 LET H$(3,0) = ">"
250 LET H$(4,0) = "&"
260 FOR I = 1 TO 4
270 LET C( ASC ( MID$ (",<>&",I,1))) = I
280 LET H$(I,HEADER) = H$(I,0)
290 NEXT I
300 LET H$(1,1) = "</TH><TH>"
310 PRINT "<!DOCTYPE HTML>"M$"<HTML>"M$"<HEAD>"M$"</HEAD>"M$"<BODY>"
320 PRINT "<TABLE BORDER="Q$"1"Q$" CELLPADDING="Q$"10"Q$" CELLSPACING="Q$"0"Q$">"
330 READ CSV$
340 FOR Q = 0 TO 1 STEP 0
350 PRINT "<TR><T" MID$ ("DH",1 + HEADER,1)">";
360 FOR I = 1 TO LEN (CSV$)
370 LET C$ = MID$ (CSV$,I,1)
380 LET H = C( ASC (C$))
390 PRINT H$(H,HEADER) MID$ (C$,1,H = 0);
400 NEXT I
410 PRINT "</T" MID$ ("DH",1 + HEADER,1)"></TR>"
420 LET HEADER = FALSE
430 READ CSV$
440 LET Q = CSV$ = ""
450 NEXT Q
460 PRINT "</TABLE>"M$"</BODY>"M$"</HTML>"
Arturo
in: {
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!
}
table: function [content]
-> join @["<table>" join content "</table>"]
row: function [data]
-> join @[
"<tr><td>" escape.xml first data "</td>"
"<td>" escape.xml last data "</td></tr>"
]
print table map read.csv in => row
- Output:
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! |
AutoHotkey
Very basic implementation
CSVData =
(
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!
)
TableData := "<table>"
Loop Parse, CSVData,`n
{
TableData .= "`n <tr>"
Loop Parse, A_LoopField, CSV
TableData .= "<td>" HTMLEncode(A_LoopField) "</td>"
TableData .= "</tr>"
}
TableData .= "`n</table>"
HTMLEncode(str){
static rep := "&<lt;>gt;""quot"
Loop Parse, rep,;
StringReplace, str, str, % SubStr(A_LoopField, 1, 1), % "&" . SubStr(A_LoopField, 2) . ";", All
return str
}
MsgBox % clipboard := TableData
- Output:
<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>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> <tr><td>The multitude</td><td>Who are you?</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>
(note the output has been modified slightly since this webpage is html.)
AutoIt
Local $ascarray[4] = [34,38,60,62]
$String = "Character,Speech" & @CRLF
$String &= "The multitude,The messiah! Show us the messiah!" & @CRLF
$String &= "Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>" & @CRLF
$String &= "The multitude,Who are you?" & @CRLF
$String &= "Brians mother,I'm his mother; that's who!" & @CRLF
$String &= "The multitude,Behold his mother! Behold his mother!"
For $i = 0 To UBound($ascarray) -1
$String = Stringreplace($String, chr($ascarray[$i]), "&#"&$ascarray[$i]&";")
Next
$newstring = "<table>" & @CRLF
$crlfsplit = StringSplit($String, @CRLF, 1)
For $i = 1 To $crlfsplit[0]
If $i = 1 Then $newstring &= "<thead>" & @CRLF
$newstring &= "<tr>" & @CRLF
$komsplit = StringSplit($crlfsplit[$i], ",")
For $k = 1 To $komsplit[0]
If $i = 1 Then
$newstring &= "<th>" &$komsplit[$k] & "</th>" & @CRLF
Else
$newstring &= "<td>" &$komsplit[$k] & "</td>" & @CRLF
EndIf
Next
$newstring &= "</tr>" & @CRLF
If $i = 1 Then $newstring &= "</thead>" & @CRLF
Next
$newstring &= "</table>"
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $newstring = ' & $newstring & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
- Output:
<table> <thead> <tr> <th>Character</th> <th>Speech</th> </tr> </thead> <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> <tr> <td>The multitude</td> <td>Who are you?</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>
AWK
Includes extra credit.
File csv2html.awk
#!/usr/bin/awk -f
BEGIN {
FS=","
print "<table>"
}
{
gsub(/</, "\\<")
gsub(/>/, "\\>")
gsub(/&/, "\\>")
print "\t<tr>"
for(f = 1; f <= NF; f++) {
if(NR == 1 && header) {
printf "\t\t<th>%s</th>\n", $f
}
else printf "\t\t<td>%s</td>\n", $f
}
print "\t</tr>"
}
END {
print "</table>"
}
$ awk -f csv2html.awk input.csv
<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>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>Who are you?</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>
Extra credit:
$ awk -v header=1 -f csv2html.awk input.csv
<table>
<tr>
<th>Character</th>
<th>Speech</th>
</tr>
<tr>
<td>The multitude</td>
<td>The messiah! Show us the messiah!</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>Who are you?</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>
Batch File
::Batch Files are terrifying when it comes to string processing.
::But well, a decent implementation!
@echo off
REM Below is the CSV data to be converted.
REM Exactly three colons must be put before the actual line.
:::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!
setlocal disabledelayedexpansion
echo ^<table^>
for /f "delims=" %%A in ('findstr "^:::" "%~f0"') do (
set "var=%%A"
setlocal enabledelayedexpansion
REM The next command removes the three colons...
set "var=!var:~3!"
REM The following commands to the substitions per line...
set "var=!var:&=&!"
set "var=!var:<=<!"
set "var=!var:>=>!"
set "var=!var:,=</td><td>!"
echo ^<tr^>^<td^>!var!^</td^>^</tr^>
endlocal
)
echo ^</table^>
- Output:
<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>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>
<tr><td>The multitude</td><td>Who are you?</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>
BBC BASIC
DATA "Character,Speech"
DATA "The multitude,The messiah! Show us the messiah!"
DATA "Brian's mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>"
DATA "The multitude,Who are you?"
DATA "Brian's mother,I'm his mother; that's who!"
DATA "The multitude,Behold his mother! Behold his mother!"
DATA "***"
*SPOOL CSVtoHTML.htm
PRINT "<HTML>"
PRINT "<HEAD>"
PRINT "</HEAD>"
PRINT "<BODY>"
PRINT "<table border=1 cellpadding =10 cellspacing=0>"
header% = TRUE
REPEAT
READ csv$
IF csv$ = "***" THEN EXIT REPEAT
IF header% PRINT "<tr><th>"; ELSE PRINT "<tr><td>";
FOR i% = 1 TO LEN(csv$)
c$ = MID$(csv$, i%, 1)
CASE c$ OF
WHEN ",": IF header% PRINT "</th><th>"; ELSE PRINT "</td><td>";
WHEN "<": PRINT "<";
WHEN ">": PRINT ">";
WHEN "&": PRINT "&";
OTHERWISE: PRINT c$;
ENDCASE
NEXT i%
IF header% PRINT "</th></tr>" ELSE PRINT "</td></tr>"
header% = FALSE
UNTIL FALSE
PRINT "</table>"
PRINT "</BODY>"
PRINT "</HTML>"
*spool
SYS "ShellExecute", @hwnd%, 0, "CSVtoHTML.htm", 0, 0, 1
- Output:
<HTML> <HEAD> </HEAD> <BODY> <table border=1 cellpadding =10 cellspacing=0> <tr><th>Character</th><th>Speech</th></tr> <tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr> <tr><td>Brian's 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> <tr><td>The multitude</td><td>Who are you?</td></tr> <tr><td>Brian's 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> </BODY> </HTML>
- Rendered output:
Befunge
The most practical solution for Befunge was to read the CSV from stdin, so this implementation works best with command line interpreters that can accept redirected input. That said, it is still possible test with many of the GUI and online interpretors just by entering the input manually.
Note that right angle brackets are deliberately not escaped, since that is not strictly necessary for the markup to be valid.
<v_>#!,#:< "<table>" \0 +55
v >0>::65*1+`\"~"`!*#v_4-5v >
v>#^~^<v"<tr><td>" < \v-1/<>">elb"
<^ >:#,_$10 |!:<>\#v_ vv"ta"
v-",":\-"&":\-"<":\<>5#05#<v+ >"/"v
>#v_$$$0">dt<>dt/<"vv"tr>"+<5 v"<"<
>^>\#v_$$0";pma&" v>"/<>d"v5 v , <
$ > \#v_$0";tl&"v v"</t"<0 > : |
^_>#!,#:<>#<0#<\#<<< >:#,_$#^_v@ $<
- Output:
<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>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>
<tr><td>The multitude</td><td>Who are you?</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>
Bracmat
Extra credit solution using pattern matching
This is not the most concise solution, but it is relatively efficient. To collect the lines we use a pattern that matches a line starting from position [!p
. Each time a line is matched, p
is updated, the two found elements are collected and the pattern is forced to fail, so the pattern matcher finds the next line. The found rows are collected in reverse order, because prepending to a list is faster than appending. When all lines are read, the collected lines are reversed, interspersed with newline characters. Finally the predefined function toML is used to create HTML.
( ( CSVtoHTML
= p q Character Speech swor rows row
. 0:?p
& :?swor:?rows
& ( @( !arg
: ?
( [!p ?Character "," ?Speech \n [?q ?
& !q:?p
& (tr.,(th.,!Character) (th.,!Speech))
!swor
: ?swor
& ~
)
)
| whl
' ( !swor:%?row %?swor
& !row \n !rows:?rows
)
& toML
$ (table.,(thead.,!swor) \n (tbody.,!rows))
)
)
& CSVtoHTML
$ "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!
"
)
Output:
<table><thead><tr><th>Character</th><th>Speech</th></tr></thead>
<tbody><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>
<tr><td>The multitude</td><td>Who are you?</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>
</tbody></table>
Simple solution NOT using pattern matching
Newer versions of Bracmat have the built in function vap
that splits an input string in
single characters or that splits it everywhere where a given separator character occurs. Each single character or slab of characters is
passed to a function. The values returned from this function become the elements in a list that is returned from the vap
function. For example, vap$(upp.Википедию)
"vaporizes" the word Википедию into characters and produces the list of
uppercased characters В И К И П Е Д И Ю. Instead of the name of a function we can choose to just give vap
the definition of an anonymous function, like so: vap$((=.!arg:~и|).Википедию)
. This returns a list of the characters in the word Википедию, except for the и character: В к п е д ю.
In the code below, we use vap
with a third argument, a splitting separator character. The outer call to vap
splits a text into rows. An embedded call to vap
splits each row into cell elements. This code is very efficient.
( ( Csv2Html
=
. toML
$ ( table
.
, vap
$ ( (
= .tr.,vap$((=.td.,!arg).!arg.",")
)
. !arg
. \n
)
)
)
& Csv2Html
$ "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!
"
)
Extra credit solution
( ( Csv2Html
=
. toML
$ ( table
.
, vap
$ ( (=..vap$((=.,!arg).!arg.","))
. !arg
. \n
)
: (.%?header) ?body
& ( thead
.
, (tr.,map$((=.th.!arg).!header))
\n
)
( tbody
.
, map
$ ( (
=
. !arg:(.?arg)
& (tr.,map$((=.td.!arg).!arg))
\n
)
. !body
)
)
)
)
& Csv2Html
$ "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!
"
)
Output:
<table><thead><tr><th>Character</th><th>Speech</th></tr>
</thead><tbody><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>
<tr><td>The multitude</td><td>Who are you?</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>
<tr><td /></tr>
</tbody></table>
C
#include <stdio.h>
const char *input =
"Character,Speech\n"
"The multitude,The messiah! Show us the messiah!\n"
"Brians mother,<angry>Now you listen here! He's not the messiah; "
"he's a very naughty boy! Now go away!</angry>\n"
"The multitude,Who are you?\n"
"Brians mother,I'm his mother; that's who!\n"
"The multitude,Behold his mother! Behold his mother!";
int main()
{
const char *s;
printf("<table>\n<tr><td>");
for (s = input; *s; s++) {
switch(*s) {
case '\n': printf("</td></tr>\n<tr><td>"); break;
case ',': printf("</td><td>"); break;
case '<': printf("<"); break;
case '>': printf(">"); break;
case '&': printf("&"); break;
default: putchar(*s);
}
}
puts("</td></tr>\n</table>");
return 0;
}
- Output:
$ gcc -Wall -W -ansi -pedantic csv.c -o csv $ ./csv
<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>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>
<tr><td>The multitude</td><td>Who are you?</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>
C#
Simple Solution
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
class Program
{
private static string ConvertCsvToHtmlTable(string csvText)
{
//split the CSV, assume no commas or line breaks in text
List<List<string>> splitString = new List<List<string>>();
List<string> lineSplit = csvText.Split('\n').ToList();
foreach (string line in lineSplit)
{
splitString.Add(line.Split(',').ToList());
}
//encode text safely, and create table
string tableResult = "<table>";
foreach(List<string> splitLine in splitString)
{
tableResult += "<tr>";
foreach(string splitText in splitLine)
{
tableResult += "<td>" + WebUtility.HtmlEncode(splitText) + "</td>";
}
tableResult += "</tr>";
}
tableResult += "</table>";
return tableResult;
}
}
- Output:
when using the text suggested
<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>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><tr><td>The multitude</td><td>Who are you?</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>
Extra Credit Solution
using System;
using System.Linq;
using System.Net;
namespace CsvToHtml
{
class Program
{
static void Main(string[] args)
{
string csv =
@"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!";
Console.Write(ConvertCsvToHtmlTable(csv, true));
}
private static string ConvertCsvToHtmlTable(string csvText, bool formatHeaders)
{
var rows =
(from text in csvText.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries) /* Split the string by newline,
* removing any empty rows. */
select text.Split(',')).ToArray(); // Split each row by comma.
string output = "<table>"; // Initialize the output with the value of "<table>".
for (int index = 0; index < rows.Length; index++) // Iterate through each row.
{
var row = rows[index];
var tag = (index == 0 && formatHeaders) ? "th" : "td"; /* Check if this is the first row, and if to format headers.
* If so, then set the tags as table headers.
* Otherwise, set the tags as table data. */
output += "\r\n\t<tr>"; // Add table row tag to output string.
// Add escaped cell data with proper tags to output string for each cell in row.
output = row.Aggregate(output,
(current, cell) =>
current +
string.Format("\r\n\t\t<{0}>{1}</{0}>", tag, WebUtility.HtmlEncode(cell)));
output += "\r\n\t</tr>"; // Add closing table row tag to output string.
}
output += "\r\n</table>"; // Add closing table tag to output string.
return output;
}
}
}
- Sample HTML Output:
<table>
<tr>
<th>Character</th>
<th>Speech</th>
</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>
<tr>
<td>The multitude</td>
<td>Who are you?</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>
C++
#include <string>
#include <boost/regex.hpp>
#include <iostream>
std::string csvToHTML( const std::string & ) ;
int main( ) {
std::string text = "Character,Speech\n"
"The multitude,The messiah! Show us the messiah!\n"
"Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>\n"
"The multitude,Who are you?\n"
"Brians mother,I'm his mother; that's who!\n"
"The multitude,Behold his mother! Behold his mother!\n" ;
std::cout << csvToHTML( text ) ;
return 0 ;
}
std::string csvToHTML( const std::string & csvtext ) {
//the order of the regexes and the replacements is decisive!
std::string regexes[ 5 ] = { "<" , ">" , "^(.+?)\\b" , "," , "\n" } ;
const char* replacements [ 5 ] = { "<" , ">" , " <TR><TD>$1" , "</TD><TD>", "</TD></TR>\n" } ;
boost::regex e1( regexes[ 0 ] ) ;
std::string tabletext = boost::regex_replace( csvtext , e1 ,
replacements[ 0 ] , boost::match_default | boost::format_all ) ;
for ( int i = 1 ; i < 5 ; i++ ) {
e1.assign( regexes[ i ] ) ;
tabletext = boost::regex_replace( tabletext , e1 , replacements[ i ] , boost::match_default | boost::format_all ) ;
}
tabletext = std::string( "<TABLE>\n" ) + tabletext ;
tabletext.append( "</TABLE>\n" ) ;
return tabletext ;
}
- Output:
<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>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>
<TR><TD>The multitude</TD><TD>Who are you?</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>
Clojure
We assume the presence of a file, but the input could come from anywhere.
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!
(require 'clojure.string)
(def escapes
{\< "<", \> ">", \& "&"})
(defn escape
[content]
(clojure.string/escape content escapes))
(defn tr
[cells]
(format "<tr>%s</tr>"
(apply str (map #(str "<td>" (escape %) "</td>") cells))))
;; turn a seq of seq of cells into a string.
(defn to-html
[tbl]
(format "<table><tbody>%s</tbody></thead>"
(apply str (map tr tbl))))
;; Read from a string to a seq of seq of cells.
(defn from-csv
[text]
(map #(clojure.string/split % #",")
(clojure.string/split-lines text)))
(defn -main
[]
(let [lines (line-seq (java.io.BufferedReader. *in*))
tbl (map #(clojure.string/split % #",") lines)]
(println (to-html tbl)))
- Output:
<table><tbody><tr><td>Character</td><td>Speech</td></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><tr><td>The multitude</td><td>Who are you?</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></tbody></thead>
CoffeeScript
String::__defineGetter__ 'escaped', () ->
this.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"') // rosettacode doesn't like "
text = '''
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!
'''
lines = (line.split ',' for line in text.split /[\n\r]+/g)
header = lines.shift()
console.log """
<table cellspacing="0">
<thead>
<th scope="col">#{header[0]}</th>
<th scope="col">#{header[1]}</th>
</thead>
<tbody>
"""
for line in lines
[character, speech] = line
console.log """
<th scope="row">#{character}</th>
<td>#{speech.escaped}</td>
"""
console.log """
</tbody>
</table>
"""
- Output:
<table cellspacing="0">
<thead>
<th scope="col">Character</th>
<th scope="col">Speech</th>
</thead>
<tbody>
<th scope="row">The multitude</th>
<td>The messiah! Show us the messiah!</td>
<th scope="row">Brians mother</th>
<td><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry></td>
<th scope="row">The multitude</th>
<td>Who are you?</td>
<th scope="row">Brians mother</th>
<td>I'm his mother; that's who!</td>
<th scope="row">The multitude</th>
<td>Behold his mother! Behold his mother!</td>
</tbody>
</table>
Common Lisp
(defvar *csv* "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!")
(defun split-string (string delim-char)
(let ((result '()))
(do* ((start 0 (1+ end))
(end (position delim-char string)
(position delim-char string :start start)))
((not end) (reverse (cons (subseq string start) result)))
(push (subseq string start end) result))))
;;; HTML escape code modified from:
;;; http://www.gigamonkeys.com/book/practical-an-html-generation-library-the-interpreter.html
(defun escape-char (char)
(case char
(#\& "&")
(#\< "<")
(#\> ">")
(t (format nil "&#~d;" (char-code char)))))
(defun escape (in)
(let ((to-escape "<>&"))
(flet ((needs-escape-p (char) (find char to-escape)))
(with-output-to-string (out)
(loop for start = 0 then (1+ pos)
for pos = (position-if #'needs-escape-p in :start start)
do (write-sequence in out :start start :end pos)
when pos do (write-sequence (escape-char (char in pos)) out)
while pos)))))
(defun html-row (values headerp)
(let ((tag (if headerp "th" "td")))
(with-output-to-string (out)
(write-string "<tr>" out)
(dolist (val values)
(format out "<~A>~A</~A>" tag (escape val) tag))
(write-string "</tr>" out))))
(defun csv->html (csv)
(let* ((lines (split-string csv #\Newline))
(cols (split-string (first lines) #\,))
(rows (mapcar (lambda (row) (split-string row #\,)) (rest lines))))
(with-output-to-string (html)
(format html "<table>~C" #\Newline)
(format html "~C~A~C" #\Tab (html-row cols t) #\Newline)
(dolist (row rows)
(format html "~C~A~C" #\Tab (html-row row nil) #\Newline))
(write-string "</table>" html))))
CL-USER> (csv->html *csv*)
- Output:
<table>
<tr><th>Character</th><th>Speech</th></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>
<tr><td>The multitude</td><td>Who are you?</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>
D
void main() {
import std.stdio;
immutable input =
"Character,Speech\n" ~
"The multitude,The messiah! Show us the messiah!\n" ~
"Brians mother,<angry>Now you listen here! He's not the messiah; " ~
"he's a very naughty boy! Now go away!</angry>\n" ~
"The multitude,Who are you?\n" ~
"Brians mother,I'm his mother; that's who!\n" ~
"The multitude,Behold his mother! Behold his mother!";
"<html>\n<head><meta charset=\"utf-8\"></head>\n<body>\n\n".write;
"<table border=\"1\" cellpadding=\"5\" cellspacing=\"0\">\n<thead>\n <tr><td>".write;
bool theadDone = false;
foreach (immutable c; input) {
switch(c) {
case '\n':
if (theadDone) {
"</td></tr>\n <tr><td>".write;
} else {
"</td></tr>\n</thead>\n<tbody>\n <tr><td>".write;
theadDone = true;
}
break;
case ',': "</td><td>".write; break;
case '<': "<".write; break;
case '>': ">".write; break;
case '&': "&".write; break;
default: c.write; break;
}
}
"</td></tr>\n</tbody>\n</table>\n\n</body></html>".write;
}
- Output:
<html>
<head><meta charset="utf-8"></head>
<body>
<table border="1" cellpadding="5" cellspacing="0">
<thead>
<tr><td>Character</td><td>Speech</td></tr>
</thead>
<tbody>
<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>
<tr><td>The multitude</td><td>Who are you?</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>
</tbody>
</table>
</body>
</html>
Delphi
This solution solves both the basic and extra credit tasks.
program csv2html;
{$APPTYPE CONSOLE}
uses
SysUtils,
Classes;
const
// Carriage Return/Line Feed
CRLF = #13#10;
// The CSV data
csvData =
'Character,Speech'+CRLF+
'The multitude,The messiah! Show us the messiah!'+CRLF+
'Brians mother,<angry>Now you listen here! He''s not the messiah; he''s a very naughty boy! Now go away!</angry>'+CRLF+
'The multitude,Who are you?'+CRLF+
'Brians mother,I''m his mother; that''s who!'+CRLF+
'The multitude,Behold his mother! Behold his mother!';
// HTML header
htmlHead =
'<!DOCTYPE html'+CRLF+
'PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"'+CRLF+
'"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'+CRLF+
'<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">'+CRLF+
'<head>'+CRLF+
'<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />'+CRLF+
'<title>CSV-to-HTML Conversion</title>'+CRLF+
'<style type="text/css">'+CRLF+
'body {font-family:verdana,helvetica,sans-serif;font-size:100%}'+CRLF+
'table {width:70%;border:0;font-size:80%;margin:auto}'+CRLF+
'th,td {padding:4px}'+CRLF+
'th {text-align:left;background-color:#eee}'+CRLF+
'th.c {width:15%}'+CRLF+
'td.c {width:15%}'+CRLF+
'</style>'+CRLF+
'</head>'+CRLF+
'<body>'+CRLF;
// HTML footer
htmlFoot =
'</body>'+CRLF+
'</html>';
{ Function to split a string into a list using a given delimiter }
procedure SplitString(S, Delim: string; Rslt: TStrings);
var
i: integer;
fld: string;
begin
fld := '';
for i := Length(S) downto 1 do
begin
if S[i] = Delim then
begin
Rslt.Insert(0,fld);
fld := '';
end
else
fld := S[i]+fld;
end;
if (fld <> '') then
Rslt.Insert(0,fld);
end;
{ Simple CSV parser with option to specify that the first row is a header row }
procedure ParseCSV(const csvIn: string; htmlOut: TStrings; FirstRowIsHeader: Boolean = True);
const
rowstart = '<tr><td class="c">';
rowend = '</td></tr>';
cellendstart = '</td><td class="s">';
hcellendstart = '</th><th class="s">';
hrowstart = '<tr><th class="c">';
hrowend = '</th></tr>';
var
tmp,pieces: TStrings;
i: Integer;
begin
// HTML header
htmlOut.Text := htmlHead + CRLF + CRLF;
// Start the HTML table
htmlOut.Text := htmlOut.Text + '<table summary="csv2table conversion">' + CRLF;
// Create stringlist
tmp := TStringList.Create;
try
// Assign CSV data to stringlist and fix occurences of '<' and '>'
tmp.Text := StringReplace(csvIn,'<','<',[rfReplaceAll]);
tmp.Text := StringReplace(tmp.Text,'>','>',[rfReplaceAll]);
// Create stringlist to hold the parts of the split data
pieces := TStringList.Create;
try
// Loop through the CSV rows
for i := 0 to Pred(tmp.Count) do
begin
// Split the current row
SplitString(tmp[i],',',pieces);
// Check if first row and FirstRowIsHeader flag set
if (i = 0) and FirstRowIsHeader then
// Render HTML
htmlOut.Text := htmlOut.Text + hrowstart + pieces[0] + hcellendstart + pieces[1] + hrowend + CRLF
else
htmlOut.Text := htmlOut.Text + rowstart + pieces[0] + cellendstart + pieces[1] + rowend + CRLF;
end;
// Finish the HTML table and end the HTML page
htmlOut.Text := htmlOut.Text + '</table>' + CRLF + htmlFoot;
finally
pieces.Free;
end;
finally
tmp.Free;
end;
end;
var
HTML: TStrings;
begin
// Create stringlist to hold HTML output
HTML := TStringList.Create;
try
Writeln('Basic:');
Writeln('');
// Load and parse the CSV data
ParseCSV(csvData,HTML,False);
// Output the HTML to the console
Writeln(HTML.Text);
// Save the HTML to a file (in application's folder)
HTML.SaveToFile('csv2html_basic.html');
Writeln('');
Writeln('=====================================');
Writeln('');
HTML.Clear;
Writeln('Extra Credit:');
Writeln('');
// Load and parse the CSV data
ParseCSV(csvData,HTML,True);
// Output the HTML to the console
Writeln(HTML.Text);
// Save the HTML to a file (in application's folder)
HTML.SaveToFile('csv2html_extra.html');
Writeln('');
Writeln('=====================================');
finally
HTML.Free;
end;
// Keep console window open
Readln;
end.
- Basic output:
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
<title>CSV-to-HTML Conversion</title>
<style type="text/css">
body {font-family:verdana,helvetica,sans-serif;font-size:100%}
table {width:70%;border:0;font-size:80%;margin:auto}
th,td {padding:4px}
th {text-align:left;background-color:#eee}
th.c {width:15%}
td.c {width:15%}
</style>
</head>
<body>
<table summary="csv2table conversion">
<tr><td class="c">Character</td><td class="s">Speech</td></tr>
<tr><td class="c">The multitude</td><td class="s">The messiah! Show us the messiah!</td></tr>
<tr><td class="c">Brians mother</td><td class="s"><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry></td></tr>
<tr><td class="c">The multitude</td><td class="s">Who are you?</td></tr>
<tr><td class="c">Brians mother</td><td class="s">I'm his mother; that's who!</td></tr>
<tr><td class="c">The multitude</td><td class="s">Behold his mother! Behold his mother!</td></tr>
</table>
</body>
</html>
- Extra credit output:
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
<title>CSV-to-HTML Conversion</title>
<style type="text/css">
body {font-family:verdana,helvetica,sans-serif;font-size:100%}
table {width:70%;border:0;font-size:80%;margin:auto}
th,td {padding:4px}
th {text-align:left;background-color:#eee}
th.c {width:15%}
td.c {width:15%}
</style>
</head>
<body>
<table summary="csv2table conversion">
<tr><th class="c">Character</th><th class="s">Speech</th></tr>
<tr><td class="c">The multitude</td><td class="s">The messiah! Show us the messiah!</td></tr>
<tr><td class="c">Brians mother</td><td class="s"><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry></td></tr>
<tr><td class="c">The multitude</td><td class="s">Who are you?</td></tr>
<tr><td class="c">Brians mother</td><td class="s">I'm his mother; that's who!</td></tr>
<tr><td class="c">The multitude</td><td class="s">Behold his mother! Behold his mother!</td></tr>
</table>
</body>
</html>
EasyLang
h$ = "<table border=1>\n<tr><th>"
repeat
s$ = input
until s$ = ""
write h$
for c$ in strchars s$
if c$ = ","
if scnd = 0
c$ = "</th><th>"
else
c$ = "</td><td>"
.
elif c$ = "<"
c$ = "<"
elif c$ = ">"
c$ = ">"
elif c$ = "&"
c$ = "&"
.
write c$
.
if scnd = 0
h$ = "</th></tr>\n<tr><td>"
scnd = 1
else
h$ = "</td></tr>\n<tr><td>"
.
.
print "</td></tr>\n</table>"
#
input_data
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!
- Output:
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! |
EchoLisp
;; CSV -> LISTS
(define (csv->row line) (string-split line ","))
(define (csv->table csv) (map csv->row (string-split csv "\n")))
;; LISTS->HTML
(define html 'html)
(define (emit-tag tag html-proc content )
(if (style tag)
(push html (format "<%s style='%a'>" tag (style tag)))
(push html (format "<%s>" tag )))
(html-proc content)
(push html (format "</%s> " tag )))
;; html procs : 1 tag, 1 proc
(define (h-raw content)
(push html (format "%s" content)))
(define (h-header headers)
(for ((h headers)) (emit-tag 'th h-raw h)))
(define (h-row row)
(for ((item row)) (emit-tag 'td h-raw item)))
(define (h-table table )
(emit-tag 'tr h-header (first table))
(for ((row (rest table))) (emit-tag 'tr h-row row)))
(define (html-dump) (string-join (stack->list html) " "))
;; STYLES
(style 'td "text-align:left")
(style 'table "border-spacing: 10px;border:28px ridge orange") ;; special biblical border
(style 'th "color:blue;")
- Output:
;; changed <angry> to <b> to show that html tags inside text are correctly transmitted.
(define MontyPython #<<
Character,Speech
The multitude,The messiah! Show us the messiah!
Brians mother,<b>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</b>
The multitude,Who are you?
Brians mother,I'm his mother; that's who!
The multitude,Behold his mother! Behold his mother!
>>#)
(define (task speech)
(define table (csv->table speech))
(stack html)
(emit-tag 'table h-table table)
(html-dump))
(task MontyPython)
Character | Speech |
---|---|
The multitude | The messiah! Show us the messiah! |
Brians mother | Now you listen here! He's not the messiah; he's a very naughty boy! Now go away! |
The multitude | Who are you? |
Brians mother | I'm his mother; that's who! |
The multitude | Behold his mother! Behold his mother! |
Ed
Translation of Sed version
H
g/&/s//\&/g
g/</s//\</g
g/>/s//\>/g
g/"/s//\"e;/g
g/^/s// <tr><td>/g
g/,/s//<\/td><td>/g
g/$/s//<\/td>\
<\/tr>/
1s/^/<table>\
/
$s/$/\
<\/table>/
,p
Q
Erlang
Using functions from Create_an_HTML_table
-module( csv_to_html ).
-export( [table_translation/1, task/0] ).
table_translation( CSV ) ->
[Headers | Contents] = [string:tokens(X, ",") || X <- string:tokens( CSV, "\n")],
Table = create_html_table:html_table( [{border, "1"}, {cellpadding, "10"}], Headers, Contents ),
create_html_table:external_format( Table ).
task() -> table_translation( csv() ).
csv() ->
"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!".
- Output:
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! |
Euphoria
constant input = "Character,Speech\n" &
"The multitude,The messiah! Show us the messiah!\n" &
"Brians mother,<angry>Now you listen here! He's not the messiah; " &
"he's a very naughty boy! Now go away!</angry>\n" &
"The multitude,Who are you?\n" &
"Brians mother,I'm his mother; that's who!\n" &
"The multitude,Behold his mother! Behold his mother!"
puts(1,"<table>\n<tr><td>")
for i = 1 to length(input) do
switch input[i] do
case '\n' then puts(1,"</td></tr>\n<tr><td>")
case ',' then puts(1,"</td><td>")
case '<' then puts(1,"<")
case '>' then puts(1,">")
case '&' then puts(1,"&")
case else puts(1,input[i])
end switch
end for
puts(1,"</td></tr>\n</table>")
- Output:
<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>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>
<tr><td>The multitude</td><td>Who are you?</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>
F#
Use .NET XmlWriter. Stylesheet styling is applied only when command line option -h ist given.
open System
open System.Text
open System.Xml
let data = """
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!
"""
let csv =
Array.map
(fun (line : string) -> line.Split(','))
(data.Trim().Split([|'\n';'\r'|],StringSplitOptions.RemoveEmptyEntries))
[<EntryPoint>]
let main argv =
let style = argv.Length > 0 && argv.[0] = "-h"
Console.OutputEncoding <- UTF8Encoding()
let xs = XmlWriterSettings()
xs.Indent <- true // be friendly to humans
use x = XmlWriter.Create(Console.Out, xs)
x.WriteStartDocument()
x.WriteDocType("HTML", null, null, null) // HTML5
x.WriteStartElement("html")
x.WriteStartElement("head")
x.WriteElementString("title", "Rosettacode - CSV to HTML translation")
if style then
x.WriteStartElement("style"); x.WriteAttributeString("type", "text/css")
x.WriteString("""
table { border-collapse: collapse; }
td, th { border: 1px solid black; padding: .25em}
th { background-color: #EEE; }
tbody th { font-weight: normal; font-size: 85%; }
""")
x.WriteEndElement() // style
x.WriteEndElement() // head
x.WriteStartElement("body")
x.WriteStartElement("table")
x.WriteStartElement("thead"); x.WriteStartElement("tr")
for part in csv.[0] do x.WriteElementString("th", part)
x.WriteEndElement(); x.WriteEndElement() // tr thead
x.WriteStartElement("tbody")
for line in csv.[1..] do
x.WriteStartElement("tr")
x.WriteElementString("th", line.[0])
x.WriteElementString("td", line.[1])
x.WriteEndElement() // tr
x.Close()
0
- Output:
(stylesheet version)
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE HTML >
<html>
<head>
<title>Rosettacode - CSV to HTML translation</title>
<style type="text/css">
table { border-collapse: collapse; }
td, th { border: 1px solid black; padding: .25em}
th { background-color: #EEE; }
tbody th { font-weight: normal; font-size: 85%; }
</style>
</head>
<body>
<table>
<thead>
<tr>
<th>Character</th>
<th>Speech</th>
</tr>
</thead>
<tbody>
<tr>
<th>The multitude</th>
<td>The messiah! Show us the messiah!</td>
</tr>
<tr>
<th>Brians mother</th>
<td><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry></td>
</tr>
<tr>
<th>The multitude</th>
<td>Who are you?</td>
</tr>
<tr>
<th>Brians mother</th>
<td>I'm his mother; that's who!</td>
</tr>
<tr>
<th>The multitude</th>
<td>Behold his mother! Behold his mother!</td>
</tr>
</tbody>
</table>
</body>
</html>
Factor
USING: csv html.streams prettyprint xml.writer ;
"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!"
string>csv [ simple-table. ] with-html-writer pprint-xml
- Output:
<table style="display: inline-table; border-collapse: collapse;">
<tr>
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; ">
Character
</td>
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; ">
Speech
</td>
</tr>
<tr>
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; ">
The multitude
</td>
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; ">
The messiah! Show us the messiah!
</td>
</tr>
<tr>
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; ">
Brians mother
</td>
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; ">
<angry>Now you listen here! He's not the messiah; he's a very
naughty boy! Now go away!</angry>
</td>
</tr>
<tr>
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; ">
The multitude
</td>
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; ">
Who are you?
</td>
</tr>
<tr>
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; ">
Brians mother
</td>
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; ">
I'm his mother; that's who!
</td>
</tr>
<tr>
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; ">
The multitude
</td>
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; ">
Behold his mother! Behold his mother!
</td>
</tr>
</table>
<br/>
Forth
: BEGIN-COLUMN ." <td>" ;
: END-COLUMN ." </td>" ;
: BEGIN-ROW ." <tr>" BEGIN-COLUMN ;
: END-ROW END-COLUMN ." </tr>" CR ;
: CSV2HTML
." <table>" CR BEGIN-ROW
BEGIN KEY DUP #EOF <> WHILE
CASE
10 OF END-ROW BEGIN-ROW ENDOF
[CHAR] , OF END-COLUMN BEGIN-COLUMN ENDOF
[CHAR] < OF ." <" ENDOF
[CHAR] > OF ." >" ENDOF
[CHAR] & OF ." &" ENDOF
DUP EMIT
ENDCASE
REPEAT
END-ROW ." </table>" CR
;
CSV2HTML BYE
- Output:
<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>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>
<tr><td>The multitude</td><td>Who are you?</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>
Fortran
The plan here is to scan each line for commas to find the splitters between texts, then write the texts out with suitable td and /td swaddling. Text presented as a quoted string (that could thereby contain a comma that is not a delimiter) is explicitly not recognised and apostrophes are apostrophes. Similarly, no attempt is made to recognise characters within the texts that might cause trouble, either because they will not elicit the same glyph when rendered from .html, or because they trigger interpretative action rather than be passed through as-is. Thus the lurking "angry" markings in the texts are not given special conversion. Further, leading and trailing spaces in each text are not trimmed off as the .html rendition process ignores them anyway. To remove them would seem to be easy enough via the special intrinsic function TRIM (available in F90) were it not that it trims off only trailing spaces. Otherwise one could mess with arrays such as IST and LST to finger the first and last character position of each text and deal patiently with the complications attendant on entirely blank or null strings when scanning the input line to determine the values.
The source is only just F90. It uses an array with a specified lower bound (of zero) - with older Fortran you could play with EQUIVALENCE or else diligently remember the offset. More difficult is the arrangement for ascertaining the length of a record as it is being read, with protection against overflowing the scratchpad should it be longer than is allowed for. This requires the Q format code, and was a common extension to F77. As usual, in the absence of help from the filesystem, the length of the longest record could only be determined through reading the entire file, and for this example it is not worth the annoyance. In the absence of the Q format feature, the input record would be read into ALINE with possibly many trailing spaces added to fill out the scratchpad; more time would be lost in scanning backwards to find the last non-blank. Should a record be longer than the scratchpad then its tail end would be lost and no remark made. Some systems (such as Snobol) are able to read a record and prepare a text variable of the correct size for that record, whatever its length (up to some integer limit such as 65535, or even more) but Fortran is not one such.
The key statement is the compound WRITE statement and its associated FORMAT, and for non-furrytranners these proceedings may be obscure. A WRITE statement accepts a list of items to be written in the usual manner, and an item is usually the name of a variable or an expression (such as 2*X + 1), but it can also be an implied DO-list. In this case the item is enclosed in brackets, and within them starts with an item list followed by a comma and a DO-expression, as in (a,b,c, I = 1,N)
where a, b and c constitute a three-element list of items - any of which could themselves be an implied DO-list if nested lists were desired. Thus, by one means or another, the WRITE statement produces a sequence of values (of various types) to be sent forth. In association with the list of entries in the nominated FORMAT statement: processing proceeds in parallel through both lists in the manner of coroutines, not sequentially. It is possible with a READ statement that a value just read adjusts a part of a FORMAT statement not yet encountered during the processing of the READ statement. For instance FORMAT 13 could have <NCOLS> instead of 666 as its repeat count, but this is an extension to F77 and avoided here to stick with older abilities except where inconvenient.
Thus, the entire line for a table row can be written in one go, starting with the tr then many repetitions of (WOT,text, WOT), with each starting and ending WOT appropriately encased by < and > and / characters supplied by the FORMAT. This repeated pattern is unsuitable for starting the line (so the tr is provided by the FORMAT, with indentation) because then it would be out of step, but happily, after the last text is rolled, the required /tr can be sent to use the format codes that would normally be used for the start of the next text. Humm. If there were to be 666 texts to roll, this will exhaust the FORMAT statement and it will write the current line out and start a fresh one, resuming at the rightmost ( in the FORMAT specification. This rule does not always prove convenient so I'd prefer the vertical bar usage of musical notation to mark the resumption location. Interpreters of html do not care about layout but humans do, so, just in case, the repeat count should be 667 (or <NCOLS + 1>), or, if <NCOLS> were used, there could follow a ,A
in the FORMAT, or, the "/tr" could be removed from the WRITE and appended to end the FORMAT just as at its start, but enough.
The check for a comma is not ALINE(I:I).EQ.","
because in other work this usage has been found to evoke astoundingly bad code, notably that both appearances of "I" are checked as being within bounds, and, the length is calculated by subtracting the "first" (I) from the "last" (also I) at run time! At least by the COMPAQ F90/95 compiler. By contrast, the ICHAR usage, which can only be for a single character, lacks this madness and far superior speed results. Not important in this example, but it explains why this puzzling usage appeared in a prog. at the Culham Science Centre in source from an IBM mainframe.
SUBROUTINE CSVTEXT2HTML(FNAME,HEADED) !Does not recognise quoted strings.
Converts without checking field counts, or noting special characters.
CHARACTER*(*) FNAME !Names the input file.
LOGICAL HEADED !Perhaps its first line is to be a heading.
INTEGER MANY !How long is a piece of string?
PARAMETER (MANY=666) !This should suffice.
CHARACTER*(MANY) ALINE !A scratchpad for the input.
INTEGER MARK(0:MANY + 1) !Fingers the commas on a line.
INTEGER I,L,N !Assistants.
CHARACTER*2 WOT(2) !I don't see why a "table datum" could not be for either.
PARAMETER (WOT = (/"th","td"/)) !A table heding or a table datum
INTEGER IT !But, one must select appropriately.
INTEGER KBD,MSG,IN !A selection.
COMMON /IOUNITS/ KBD,MSG,IN !The caller thus avoids collisions.
OPEN(IN,FILE=FNAME,STATUS="OLD",ACTION="READ",ERR=661) !Go for the file.
WRITE (MSG,1) !Start the blather.
1 FORMAT ("<Table border=1>") !By stating that a table follows.
MARK(0) = 0 !Syncopation for the comma fingers.
N = 0 !No records read.
10 READ (IN,11,END = 20) L,ALINE(1:MIN(L,MANY)) !Carefully acquire some text.
11 FORMAT (Q,A) !Q = number of characters yet to read, A = characters.
N = N + 1 !So, a record has been read.
IF (L.GT.MANY) THEN !Perhaps it is rather long?
WRITE (MSG,12) N,L,MANY !Alas!
12 FORMAT ("Line ",I0," has length ",I0,"! My limit is ",I0) !Squawk/
L = MANY !The limit actually read.
END IF !So much for paranoia.
IF (N.EQ.1 .AND. HEADED) THEN !Is the first line to be treated specially?
WRITE (MSG,*) "<tHead>" !Yep. Nominate a heading.
IT = 1 !And select "th" rather than "td".
ELSE !But mostly,
IT = 2 !Just another row for the table.
END IF !So much for the first line.
NCOLS = 0 !No commas have been seen.
DO I = 1,L !So scan the text for them.
IF (ICHAR(ALINE(I:I)).EQ.ICHAR(",")) THEN !Here?
NCOLS = NCOLS + 1 !Yes!
MARK(NCOLS) = I !The texts are between commas.
END IF !So much for that character.
END DO !On to the next.
NCOLS = NCOLS + 1 !This is why the + 1 for the size of MARK.
MARK(NCOLS) = L + 1 !End-of-line is as if a comma was one further along.
WRITE (MSG,13) !Now roll all the texts.
1 (WOT(IT), !This starting a cell,
2 ALINE(MARK(I - 1) + 1:MARK(I) - 1), !This being the text between the commas,
3 WOT(IT), !And this ending each cell.
4 I = 1,NCOLS), !For this number of columns.
5 "/tr" !And this ends the row.
13 FORMAT (" <tr>",666("<",A,">",A,"</",A,">")) !How long is a piece of string?
IF (N.EQ.1 .AND. HEADED) WRITE (MSG,*) "</tHead>" !Finish the possible header.
GO TO 10 !And try for another record.
20 CLOSE (IN) !Finished with input.
WRITE (MSG,21) !And finished with output.
21 FORMAT ("</Table>") !This writes starting at column one.
RETURN !Done!
Confusions.
661 WRITE (MSG,*) "Can't open file ",FNAME !Alas.
END !So much for the conversion.
INTEGER KBD,MSG,IN
COMMON /IOUNITS/ KBD,MSG,IN
KBD = 5 !Standard input.
MSG = 6 !Standard output.
IN = 10 !Some unspecial number.
CALL CSVTEXT2HTML("Text.csv",.FALSE.) !The first line is not special.
WRITE (MSG,*)
CALL CSVTEXT2HTML("Text.csv",.TRUE.) !The first line is a heading.
END
- Output:
<Table border=1> <tr><td>Character</td><td>Speech</td></tr> <tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr> <tr><td>Brian's 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> <tr><td>The multitude</td><td>Who are you?</td></tr> <tr><td>Brian's 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> <Table border=1> <tHead> <tr><th>Character</th><th>Speech</th></tr> </tHead> <tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr> <tr><td>Brian's 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> <tr><td>The multitude</td><td>Who are you?</td></tr> <tr><td>Brian's 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>
And interpreted, first with no special heading:
Character | Speech |
The multitude | The messiah! Show us the messiah! |
Brian's 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? |
Brian's mother | I'm his mother; that's who! |
The multitude | Behold his mother! Behold his mother! |
</tHead>
Character | Speech |
---|---|
The multitude | The messiah! Show us the messiah! |
Brian's 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? |
Brian's mother | I'm his mother; that's who! |
The multitude | Behold his mother! Behold his mother! |
FreeBASIC
Data "Character,Speech"
Data "The multitude,The messiah! Show us the messiah!"
Data "Brian's mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>"
Data "The multitude,Who are you?"
Data "Brian's mother,I'm his mother; that's who!"
Data "The multitude,Behold his mother! Behold his mother!"
Data "***"
Print "<!DOCTYPE html>" & Chr(10) & "<html>"
Print "<head>"
Print "</head>" & Chr(10)
Print "<body>"
Print "<h1 style=""text-align:center"">CSV to html translation </h1>"
Print: Print "<table border = 1 cellpadding = 10 cellspacing = 0>"
Dim As Boolean header = true
Dim As String cadenaCSV, txt
Do
Read cadenaCSV
If cadenaCSV = "***" then Exit Do
If header then
Print "<thead bgcolor=""green"">" & Chr(10) & "<tr><th>";
Else
Print "<tr><td>";
End If
For i As Integer = 1 To Len(cadenaCSV)
txt = Mid(cadenaCSV, i, 1)
Select Case txt
Case ",": If header then Print "</th><th>"; Else Print "</td><td>";
Case "<": Print "<";
Case ">": Print ">";
Case "&": Print "&";
Case Else: Print txt;
End Select
Next i
If header then
Print "</th></tr>" & Chr(10) & "</thead>" & Chr(10) & "<tbody bgcolor=""yellow"">"
Else
Print "</td></tr>"
End If
header = false
Loop Until false
Print "</tbody>" & Chr(10) & "</table>"
Print Chr(10) & "</body>"
Print "</html>"
Sleep
FutureBasic
FB has a native Macintosh web browser, so the HTML output, along with extra credit, is shown in the accompanying screenshot. This code produces a complete, launchable application.
output file "CSV2HTML"
include "Tlbx WebKit.incl"
_window = 1
begin enum output 1
_webView
end enum
void local fn BuildWindow
CGRect r = fn CGRectMake( 0, 0, 600, 220 )
window _window, @"Rosetta Code CSV to HTML", r, NSWindowStyleMaskTitled + NSWindowStyleMaskClosable
r = fn CGRectMake( 20, 20, 560, 180 )
wkwebview _webView, r,, _window
end fn
local fn CSV2HTML as CFStringRef
NSUInteger i, count
CFStringRef csvStr = @"Character,Speech\n¬
The multitude,The messiah! Show us the messiah!\n¬
Brians mother,Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!\n¬
The multitude,Who are you\n¬
Brians mother,I'm his mother; that's who!\n¬
The multitude,Behold his mother! Behold his mother!"
CFArrayRef linesArray = fn StringComponentsSeparatedByString( csvStr, @"\n" )
CFMutableStringRef htmlStr = fn MutableStringWithCapacity(0)
MutableStringAppendString( htmlStr, @"<table style=\"background:#eee;\">\n" )
MutableStringAppendString( htmlStr, @"<tr bgcolor=wheat><th>Character</th><th>Speech</th></tr>" )
MutableStringAppendString( htmlStr, @"<caption>From Monty Python's \"The Life of Brian\"</caption>\n" )
count = len( linesArray )
for i = 1 to count - 1
CFStringRef tempStr = linesArray[i]
CFArrayRef tempArr = fn StringComponentsSeparatedByString( tempStr, @"," )
MutableStringAppendString( htmlStr, @"<tr>\n" )
MutableStringAppendString( htmlStr, fn StringWithFormat( @"<td style=\"width:120px;\"><b>%@</b></td>>\n", tempArr[0] ) )
MutableStringAppendString( htmlStr, fn StringWithFormat( @"<td><i>%@</i></td>\n", tempArr[1] ) )
MutableStringAppendString( htmlStr, @"</tr>\n" )
next
MutableStringAppendString( htmlStr, @"</table><br></br>" )
end fn = fn StringWithString( htmlStr )
local fn LoadHTML2WebView
CFStringRef htmlStr = fn CSV2HTML
fn WKWebViewLoadHTMLString( _webView, htmlStr, NULL )
end fn
void local fn DoDialog( ev as long, tag as long, wnd as long, obj as CFTypeRef )
select (ev)
case _windowWillClose : end
end select
end fn
on dialog fn DoDialog
fn BuildWindow
fn LoadHTML2WebView
HandleEvents
- Output:
Go
package main
import (
"bytes"
"encoding/csv"
"fmt"
"html/template"
"strings"
)
var c = `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!`
func main() {
if h, err := csvToHtml(c); err != nil {
fmt.Println(err)
} else {
fmt.Print(h)
}
}
func csvToHtml(c string) (string, error) {
data, err := csv.NewReader(bytes.NewBufferString(c)).ReadAll()
if err != nil {
return "", err
}
var b strings.Builder
err = template.Must(template.New("").Parse(`<table>
{{range .}} <tr>{{range .}}<td>{{.}}</td>{{end}}</tr>
{{end}}</table>
`)).Execute(&b, data)
return b.String(), err
}
Extra credit version accepts -h command line option to do the special formatting for the heading line.
package main
import (
"bytes"
"encoding/csv"
"flag"
"fmt"
"html/template"
"strings"
)
var csvStr = `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!`
func main() {
headings := flag.Bool("h", false, "format first row as column headings")
flag.Parse()
if html, err := csvToHtml(csvStr, *headings); err != nil {
fmt.Println(err)
} else {
fmt.Print(html)
}
}
func csvToHtml(csvStr string, headings bool) (string, error) {
data, err := csv.NewReader(bytes.NewBufferString(csvStr)).ReadAll()
if err != nil {
return "", err
}
tStr := tPlain
if headings {
tStr = tHeadings
}
var b strings.Builder
err = template.Must(template.New("").Parse(tStr)).Execute(&b, data)
return b.String(), err
}
const (
tPlain = `<table>
{{range .}} <tr>{{range .}}<td>{{.}}</td>{{end}}</tr>
{{end}}</table>
`
tHeadings = `<table>{{if .}}
{{range $x, $e := .}}{{if $x}}
<tr>{{range .}}<td>{{.}}</td>{{end}}</tr>{{else}} <thead>
<tr>{{range .}}<th>{{.}}</th>{{end}}</tr>
</thead>
<tbody>{{end}}{{end}}
</tbody>{{end}}
</table>
`
)
- Output:
Extra credit version with -h
<table>
<thead>
<tr><th>Character</th><th>Speech</th></tr>
</thead>
<tbody>
<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>
<tr><td>The multitude</td><td>Who are you?</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>
</tbody>
</table>
Basic version, or extra credit version without -h
<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>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>
<tr><td>The multitude</td><td>Who are you?</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>
Groovy
Solution #1: Nested GStrings
Brute force solution using nested GStrings. It solves both the basic and extra credit tasks.
def formatCell = { cell ->
"<td>${cell.replaceAll('&','&').replaceAll('<','<')}</td>"
}
def formatRow = { row ->
"""<tr>${row.split(',').collect { cell -> formatCell(cell) }.join('')}</tr>
"""
}
def formatTable = { csv, header=false ->
def rows = csv.split('\n').collect { row -> formatRow(row) }
header \
? """
<table>
<thead>
${rows[0]}</thead>
<tbody>
${rows[1..-1].join('')}</tbody>
</table>
""" \
: """
<table>
${rows.join('')}</table>
"""
}
def formatPage = { title, csv, header=false ->
"""<html>
<head>
<title>${title}</title>
<style type="text/css">
td {background-color:#ddddff; }
thead td {background-color:#ddffdd; text-align:center; }
</style>
</head>
<body>${formatTable(csv, header)}</body>
</html>"""
}
Test:
def csv = '''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!'''
println 'Basic:'
println '-----------------------------------------'
println (formatPage('Basic', csv))
println '-----------------------------------------'
println()
println()
println 'Extra Credit:'
println '-----------------------------------------'
println (formatPage('Extra Credit', csv, true))
println '-----------------------------------------'
- Basic output:
<html>
<head>
<title>Basic</title>
<style type="text/css">
td {background-color:#ddddff; }
thead td {background-color:#ddffdd; text-align:center; }
</style>
</head>
<body>
<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>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>
<tr><td>The multitude</td><td>Who are you?</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>
</body>
</html>
Appearance as rendered in Google Chrome.
- Extra Credit output:
<html>
<head>
<title>Extra Credit</title>
<style type="text/css">
td {background-color:#ddddff; }
thead td {background-color:#ddffdd; text-align:center; }
</style>
</head>
<body>
<table>
<thead>
<tr><td>Character</td><td>Speech</td></tr>
</thead>
<tbody>
<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>
<tr><td>The multitude</td><td>Who are you?</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>
</tbody>
</table>
</body>
</html>
Appearance as rendered in Google Chrome.
Solution #2: MarkupBuilder
A much cleaner solution using the Groovy XML MarkupBuilder class. It solves both the basic and extra credit tasks.
import groovy.xml.MarkupBuilder
def formatRow = { doc, row ->
doc.tr { row.each { cell -> td { mkp.yield(cell) } } }
}
def formatPage = { titleString, csv, header=false ->
def writer = new StringWriter()
def doc = new MarkupBuilder(writer)
def rows = csv.split('\n').collect { row -> row.split(',') }
doc.html {
head {
title (titleString)
style (type:"text/css") {
mkp.yield('''
td {background-color:#ddddff; }
thead td {background-color:#ddffdd; text-align:center; }
''')
}
}
body {
table {
header && thead { formatRow(doc, rows[0]) }
header && tbody { rows[1..-1].each { formatRow(doc, it) } }
header || rows.each { formatRow(doc, it) }
}
}
}
writer.toString()
}
Test:
The interface is the same for both solutions, so we just reuse the same test as before.
- Basic output:
<html>
<head>
<title>Basic</title>
<style type='text/css'>
td {background-color:#ddddff; }
thead td {background-color:#ddffdd; text-align:center; }
</style>
</head>
<body>
<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>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>
<tr>
<td>The multitude</td>
<td>Who are you?</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>
</body>
</html>
The HTML for this solution looks superficially different than that from the GString solution, but the appearance as rendered in Google Chrome is identical.
Extra Credit output:
<html>
<head>
<title>Extra Credit</title>
<style type='text/css'>
td {background-color:#ddddff; }
thead td {background-color:#ddffdd; text-align:center; }
</style>
</head>
<body>
<table>
<thead>
<tr>
<td>Character</td>
<td>Speech</td>
</tr>
</thead>
<tbody>
<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>
<tr>
<td>The multitude</td>
<td>Who are you?</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>
</tbody>
</table>
</body>
</html>
The HTML for this solution looks superficially different than that from the GString solution, but the appearance as rendered in Google Chrome is identical.
Haskell
Simple solution
--import Data.List.Split (splitOn) -- if the import is available
splitOn :: Char -> String -> [String] -- otherwise
splitOn delim = foldr (\x rest ->
if x == delim then "" : rest
else (x:head rest):tail rest) [""]
htmlEscape :: String -> String
htmlEscape = concatMap escapeChar
where escapeChar '<' = "<"
escapeChar '>' = ">"
escapeChar '&' = "&"
escapeChar '"' = """ --"
escapeChar c = [c]
toHtmlRow :: [String] -> String
toHtmlRow [] = "<tr></tr>"
toHtmlRow cols = let htmlColumns = concatMap toHtmlCol cols
in "<tr>\n" ++ htmlColumns ++ "</tr>"
where toHtmlCol x = " <td>" ++ htmlEscape x ++ "</td>\n"
csvToTable :: String -> String
csvToTable csv = let rows = map (splitOn ',') $ lines csv
html = unlines $ map toHtmlRow rows
in "<table>\n" ++ html ++ "</table>"
main = interact csvToTable
Compact version
import Data.List (unfoldr)
split p = unfoldr (\s -> case dropWhile p s of [] -> Nothing
ss -> Just $ break p ss)
main = interact (\csv -> "<table>\n" ++
(unlines $ map ((\cols -> "<tr>\n" ++
(concatMap (\x -> " <td>" ++ concatMap (\c ->
case c of {'<' -> "<"; '>' -> ">";
'&' -> "&"; '"' -> """; _ -> [c]}) x
++ "</td>\n") cols)
++ "</tr>") . split (==',')) $ lines csv) ++ "</table>")
- Output
<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>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>
<tr>
<td>The multitude</td>
<td>Who are you?</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>
Icon and Unicon
This solution for the extra credit works in both Icon and Unicon. The simple CSV is read from standard input and written to standard output. The presence/abscend of "-heading" in the argument list sets the variable thead to the procedure writes or a 1 (for more on this see Introduction to Icon/Unicon - Conjunction yielding different results).
- Output:
<TABLE>
<THEAD>
<TR><TD>Character</TD><TD>Speech</TD></TR>
</THEAD>
<TBODY>
<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>
<TR><TD>The multitude</TD><TD>Who are you?</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>
</TBODY>
</TABLE>
J
Solution (extra credit)
require 'strings tables/csv'
encodeHTML=: ('&';'&';'<';'<';'>';'>')&stringreplace
tag=: adverb define
'starttag endtag'=.m
(,&.>/)"1 (starttag , ,&endtag) L:0 y
)
markupCells=: ('<td>';'</td>') tag
markupHdrCells=: ('<th>';'</th>') tag
markupRows=: ('<tr>';'</tr>',LF) tag
markupTable=: (('<table>',LF);'</table>') 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
)
For those interested, equivalent tacit versions of tag
and makeHTMLtablefromCSV
are:
tag=: adverb def '[: (,&.>/)"1 m&(0&{::@[ , 1&{::@[ ,~ ]) L:0@]'
makeHTMLtablefromCSV6=: 0&$: : ([: ; markupTable@markupRows@([ markupCells`(markupHdrCells@{. , markupCells@}.)@.[ fixcsv@encodeHTML))
Example
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!
)
1 makeHTMLtablefromCSV CSVstrng
- HTML output:
<table>
<tr><th>Character</th><th>Speech</th></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>
<tr><td>The multitude</td><td>Who are you?</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>
Java
Simple solution without escaping for Java v8+
String csv = "...";
// Use Collectors.joining(...) for streaming, otherwise StringJoiner
StringBuilder html = new StringBuilder("<table>\n");
Collector collector = Collectors.joining("</td><td>", " <tr><td>", "</td></tr>\n");
for (String row : csv.split("\n") ) {
html.append(Arrays.stream(row.split(",")).collect(collector));
}
html.append("</table>\n");
Solution including simple and extra credit version
for simple solution : java -cp . Csv2Html < text.csv
for extended solution: java -cp . Csv2Html header < text.csv
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
class Csv2Html {
public static String escapeChars(String lineIn) {
StringBuilder sb = new StringBuilder();
int lineLength = lineIn.length();
for (int i = 0; i < lineLength; i++) {
char c = lineIn.charAt(i);
switch (c) {
case '"':
sb.append(""");
break;
case '&':
sb.append("&");
break;
case '\'':
sb.append("'");
break;
case '<':
sb.append("<");
break;
case '>':
sb.append(">");
break;
default: sb.append(c);
}
}
return sb.toString();
}
public static void tableHeader(PrintStream ps, String[] columns) {
ps.print("<tr>");
for (int i = 0; i < columns.length; i++) {
ps.print("<th>");
ps.print(columns[i]);
ps.print("</th>");
}
ps.println("</tr>");
}
public static void tableRow(PrintStream ps, String[] columns) {
ps.print("<tr>");
for (int i = 0; i < columns.length; i++) {
ps.print("<td>");
ps.print(columns[i]);
ps.print("</td>");
}
ps.println("</tr>");
}
public static void main(String[] args) throws Exception {
boolean withTableHeader = (args.length != 0);
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
PrintStream stdout = System.out;
stdout.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
stdout.println("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
stdout.println("<head><meta http-equiv=\"Content-type\" content=\"text/html;charset=UTF-8\"/>");
stdout.println("<title>Csv2Html</title>");
stdout.println("<style type=\"text/css\">");
stdout.println("body{background-color:#FFF;color:#000;font-family:OpenSans,sans-serif;font-size:10px;}");
stdout.println("table{border:0.2em solid #2F6FAB;border-collapse:collapse;}");
stdout.println("th{border:0.15em solid #2F6FAB;padding:0.5em;background-color:#E9E9E9;}");
stdout.println("td{border:0.1em solid #2F6FAB;padding:0.5em;background-color:#F9F9F9;}</style>");
stdout.println("</head><body><h1>Csv2Html</h1>");
stdout.println("<table>");
String stdinLine;
boolean firstLine = true;
while ((stdinLine = br.readLine()) != null) {
String[] columns = escapeChars(stdinLine).split(",");
if (withTableHeader == true && firstLine == true) {
tableHeader(stdout, columns);
firstLine = false;
} else {
tableRow(stdout, columns);
}
}
stdout.println("</table></body></html>");
}
}
- simple solution:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="Content-type" content="text/html;charset=UTF-8"/>
<title>Csv2Html</title>
<style type="text/css">
body{background-color:#FFF;color:#000;font-family:OpenSans,sans-serif;font-size:10px;}
table{border:0.2em solid #2F6FAB;border-collapse:collapse;}
th{border:0.15em solid #2F6FAB;padding:0.5em;background-color:#E9E9E9;}
td{border:0.1em solid #2F6FAB;padding:0.5em;background-color:#F9F9F9;}</style>
</head><body><h1>Csv2Html</h1>
<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>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>
<tr><td>The multitude</td><td>Who are you?</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></body></html>
- extended:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="Content-type" content="text/html;charset=UTF-8"/>
<title>Csv2Html</title>
<style type="text/css">
body{background-color:#FFF;color:#000;font-family:OpenSans,sans-serif;font-size:10px;}
table{border:0.2em solid #2F6FAB;border-collapse:collapse;}
th{border:0.15em solid #2F6FAB;padding:0.5em;background-color:#E9E9E9;}
td{border:0.1em solid #2F6FAB;padding:0.5em;background-color:#F9F9F9;}</style>
</head><body><h1>Csv2Html</h1>
<table>
<tr><th>Character</th><th>Speech</th></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>
<tr><td>The multitude</td><td>Who are you?</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></body></html>
JavaScript
var csv = "Character,Speech\n" +
"The multitude,The messiah! Show us the messiah!\n" +
"Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>\n" +
"The multitude,Who are you?\n" +
"Brians mother,I'm his mother; that's who!\n" +
"The multitude,Behold his mother! Behold his mother!";
var lines = csv.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.split(/[\n\r]/)
.map(function(line) { return line.split(',')})
.map(function(row) {return '\t\t<tr><td>' + row[0] + '</td><td>' + row[1] + '</td></tr>';});
console.log('<table>\n\t<thead>\n' + lines[0] +
'\n\t</thead>\n\t<tbody>\n' + lines.slice(1).join('\n') +
'\t</tbody>\n</table>');
<table>
<thead>
<tr><td>Character</td><td>Speech</td></tr>
</thead>
<tbody>
<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>
<tr><td>The multitude</td><td>Who are you?</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>
</tbody>
</table>
- Output:
<table>
<thead>
<tr><td>Character</td><td>Speech</td></tr>
</thead>
<tbody>
<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>
<tr><td>The multitude</td><td>Who are you?</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> </tbody>
</table>
jq
We will assume the input is in a file named csv2html.csv, and that the jq program as given below is in a file named csv2html.jqn. To simplify things, we will invoke the jq processor twice -- the first invocation simply converts the text input into a sequence of JSON strings:
jq -R . csv2html.csv | jq -r -s -f csv2html.jq
def headerrow2html:
[" <thead> <tr>"]
+ (split(",") | map(" <th>\(@html)</th>"))
+ [ " </tr> </thead>" ]
;
def row2html:
[" <tr>"]
+ (split(",") | map(" <td>\(@html)</td>"))
+ [ " </tr>" ]
;
def csv2html:
def rows: reduce .[] as $row
([]; . + ($row | row2html));
["<table>"]
+ (.[0] | headerrow2html)
+ (.[1:] | rows)
+ [ "</table>"]
;
csv2html | .[]
Output
<table>
<thead> <tr>
<th>Character</th>
<th>Speech </th>
</tr> </thead>
<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>
<tr>
<td>The multitude</td>
<td>Who are you? </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>
Jsish
From Javascript entry.
/* CSV to HTML, in Jsish */
var csv = "Character,Speech\n" +
"The multitude,The messiah! Show us the messiah!\n" +
"Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>\n" +
"The multitude,Who are you?\n" +
"Brians mother,I'm his mother; that's who!\n" +
"The multitude,Behold his mother! Behold his mother!";
var lines = csv.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.split('\n')
.map(function(line) { return line.split(','); })
.map(function(row) { return '\t\t<tr><td>' + row[0] + '</td><td>' + row[1] + '</td></tr>'; });
if (Interp.conf('unitTest')) {
puts('<table>\n\t<thead>\n' + lines[0] + '\n\t</thead>\n\t<tbody>\n'
+ lines.slice(1).join('\n') + '\t</tbody>\n</table>');
}
/*
=!EXPECTSTART!=
<table>
<thead>
<tr><td>Character</td><td>Speech</td></tr>
</thead>
<tbody>
<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>
<tr><td>The multitude</td><td>Who are you?</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> </tbody>
</table>
=!EXPECTEND!=
*/
- Output:
prompt$ jsish -u csvToHTML.jsi [PASS] csvToHTML.jsi
Julia
using DataFrames, CSV
using CSV, DataFrames
function csv2html(fname; header::Bool=false)
csv = CSV.read(fname)
@assert(size(csv, 2) > 0)
str = """
<html>
<head>
<style type="text/css">
body {
margin: 2em;
}
h1 {
text-align: center;
}
table {
border-spacing: 0;
box-shadow: 0 0 0.25em #888;
margin: auto;
}
table,
tr,
th,
td {
border-collapse: collapse;
}
th {
color: white;
background-color: rgb(43, 53, 59);
}
th,
td {
padding: 0.5em;
}
table tr:nth-child(even) td {
background-color: rgba(218, 224, 229, 0.850);
}
</style>
</head>
<body>
<h1>csv2html Example</h1>
<table>
<tr>
"""
tags = header ? ("<th>", "</th>") : ("<td>", "</td>")
for i=1:size(csv, 2)
str *= " " * tags[1] * csv[1, i] * tags[2] * "\n"
end
str *= " "^8 * "</tr>\n"
for i=2:size(csv, 1)
str *= " <tr>\n"
for j=1:size(csv, 2)
str *= " " * "<td>" * csv[i, j] * "</td>\n"
end
str *= " </tr>\n"
end
str * " </table>\n</body>\n\n</html>\n"
end
print(csv2html("input.csv", header=true))
- Output:
<html>
<head>
<style type="text/css">
body {
margin: 2em;
}
h1 {
text-align: center;
}
table {
border-spacing: 0;
box-shadow: 0 0 0.25em #888;
margin: auto;
}
table,
tr,
th,
td {
border-collapse: collapse;
}
th {
color: white;
background-color: rgb(43, 53, 59);
}
th,
td {
padding: 0.5em;
}
table tr:nth-child(even) td {
background-color: rgba(218, 224, 229, 0.850);
}
</style>
</head>
<body>
<h1>csv2html Example</h1>
<table>
<tr>
<th>Character</th>
<th>Speech</th>
</tr>
<tr>
<td>The multitude</td>
<td>The messiah! Show us the messiah!</td>
</tr>
<tr>
<td>Brians mother</td>
<td>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</td>
</tr>
<tr>
<td>The multitude</td>
<td>Who are you?</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>
</body>
</html>
Kotlin
// version 1.1.3
val csv =
"Character,Speech\n" +
"The multitude,The messiah! Show us the messiah!\n" +
"Brians mother,<angry>Now you listen here! He's not the messiah; " +
"he's a very naughty boy! Now go away!</angry>\n" +
"The multitude,Who are you?\n" +
"Brians mother,I'm his mother; that's who!\n" +
"The multitude,Behold his mother! Behold his mother!"
fun main(args: Array<String>) {
val i = " " // indent
val sb = StringBuilder("<table>\n$i<tr>\n$i$i<td>")
for (c in csv) {
sb.append( when (c) {
'\n' -> "</td>\n$i</tr>\n$i<tr>\n$i$i<td>"
',' -> "</td>\n$i$i<td>"
'&' -> "&"
'\'' -> "'"
'<' -> "<"
'>' -> ">"
else -> c.toString()
})
}
sb.append("</td>\n$i</tr>\n</table>")
println(sb.toString())
println()
// now using first row as a table header
sb.setLength(0)
sb.append("<table>\n$i<thead>\n$i$i<tr>\n$i$i$i<td>")
val hLength = csv.indexOf('\n') + 1 // find length of first row including CR
for (c in csv.take(hLength)) {
sb.append( when (c) {
'\n' -> "</td>\n$i$i</tr>\n$i</thead>\n$i<tbody>\n$i$i<tr>\n$i$i$i<td>"
',' -> "</td>\n$i$i$i<td>"
else -> c.toString()
})
}
for (c in csv.drop(hLength)) {
sb.append( when (c) {
'\n' -> "</td>\n$i$i</tr>\n$i$i<tr>\n$i$i$i<td>"
',' -> "</td>\n$i$i$i<td>"
'&' -> "&"
'\'' -> "'"
'<' -> "<"
'>' -> ">"
else -> c.toString()
})
}
sb.append("</td>\n$i$i</tr>\n$i</tbody>\n</table>")
println(sb.toString())
}
- Output:
<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>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>
<tr>
<td>The multitude</td>
<td>Who are you?</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>
<table>
<thead>
<tr>
<td>Character</td>
<td>Speech</td>
</tr>
</thead>
<tbody>
<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>
<tr>
<td>The multitude</td>
<td>Who are you?</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>
</tbody>
</table>
Lambdatalk
{def CSV
Character,Speech\n
The multitude,The messiah! Show us the messiah!\n
Brians mother,Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!\n
The multitude,Who are you\n
Brians mother,I'm his mother; that's who!\n
The multitude,Behold his mother! Behold his mother!\n
}
-> CSV
{def csv2html
{lambda {:csv}
{table {@ style="background:#eee;"}
{S.replace ([^,]*),([^_]*)_
by {tr {td {@ style="width:120px;"}{b €1}} {td {i €2}}}
in {S.replace \\n by _ in :csv}}}}}
-> csv2html
{csv2html {CSV}} ->
- Output:
Character | Speech |
The multitude | The messiah! Show us the messiah! |
Brians mother | Now you listen here! He's not the messiah; he's a very naughty boy! Now go away! |
The multitude | Who are you |
Brians mother | I'm his mother; that's who! |
The multitude | Behold his mother! Behold his mother! |
Liberty BASIC
newline$ ="|"
' No escape behaviour, so can't refer to '/n'.
' Generally imported csv would have separator CR LF; easily converted first if needed
csv$ ="Character,Speech" +newline$+_
"The multitude,The messiah! Show us the messiah!" +newline$+_
"Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>" +newline$+_
"The multitude,Who are you?" +newline$+_
"Brians mother,I'm his mother; that's who!" +newline$+_
"The multitude,Behold his mother! Behold his mother!"
print "<HTML>"
print "<HEAD>"
print "</HEAD>"
print "<BODY>"
print "<center><H1>CSV to HTML translation </H1></center>"
print "<table border=1 cellpadding =10>"
print "<tr><td>"
for i =1 to len( csv$)
c$ =mid$( csv$, i, 1)
select case c$
case "|": print "</td></tr>": print "<tr><td>"
case ",": print "</td><td>";
case "<": print "&"+"lt;";
case ">": print "&"+"gt;";
case "&": print "&"+"amp;";
case else: print c$;
end select
next i
print "</td></tr>"
print "</table>"
print "</BODY>"
print "</HTML>"
end
- Output:
<HTML> <HEAD> </HEAD> <BODY> <center><H1>CSV to HTML translation </H1></center> <table border=1 cellpadding =10> <tr><td> Character</td><td>Speech</td></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> <tr><td> The multitude</td><td>Who are you?</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> </BODY> </HTML>
Rendered output is available at http://www.diga.me.uk/csvhtml.gif
Lua
FS = "," -- field separator
csv = [[
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!
]]
csv = csv:gsub( "<", "<" )
csv = csv:gsub( ">", "&gr;" )
html = { "<table>" }
for line in string.gmatch( csv, "(.-\n)" ) do
str = "<tr>"
for field in string.gmatch( line, "(.-)["..FS.."?\n?]" ) do
str = str .. "<td>" .. field .. "</td>"
end
str = str .. "</tr>"
html[#html+1] = str;
end
html[#html+1] = "</table>"
for _, line in pairs(html) do
print(line)
end
<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>Brians mother</td><td><angry&gr;Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry&gr;</td></tr>
<tr><td>The multitude</td><td>Who are you</td><td></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>
M2000 Interpreter
module csv2html {
nl$={
}
Repl$=lambda$ (a$) ->{
a$=replace$("&", "&",a$)
a$=replace$(">", ">",a$)
a$=replace$("""", """,a$)
// add any other replacement here
=replace$("<", "<",a$)
}
Tag$=lambda$ nl$, repl$ (a$, b$, n=4)->{
if n>0 then
a$=rtrim$(replace$(nl$, nl$+string$(" ", n), nl$+a$))
if right$(a$,2)<>nl$ then a$+=nl$
else
if right$(a$, 2)=nl$ then if left$(a$,2)<>nl$ then a$=nl$+a$
end if
prop=(,) : Read ? prop // forth parameter optional (we have to initalize first with an empty array)
p=each(prop) : prop$="" // p is an iteration object.
while p
prop$+=" "+repl$(prop#val$(p^))+"="+quote$(repl$(prop#val$(p^+1)))
p=each(prop,p^+2) // start new from p^+2 (p^ is the internal counter)
end while
="<"+b$+prop$+">"+a$+"</"+b$+">"+nl$
}
// Prepare FILE csv
tofile$={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!
}
open "forHtml.csv" for wide output as #f
Print #f, tofile$;
close #f
// prepare the input style for Input from file statement
// "," - we use comma between fields
// "." - for numbers we use dot for decimal separator
// false - we didn't read json style strings to normal strings (so \n convert to code 13, \t to code 9)
// true - we read strings unquote
input with ",",".",false, true
// Read csv file
i=1
document export$=""
Open "forHtml.csv" for wide input as #f
while not eof(#f)
input #f, a$, b$
a$=repl$(a$)
b$=repl$(b$)
if i=1 then
export$+=tag$(tag$(a$, "th", 0)+tag$(b$,"th", 0), "tr", 4)
else
export$+=tag$(tag$(a$, "td", 0)+tag$(b$,"td", 0), "tr", 4)
end if
i++
end while
close #f
style$=tag$({TD {background-color:#ddddff; }
thead TD {background-color:#ddffdd; text-align:center; }
},"style",4,("type", "text/css"))
title$= tag$("CSV to HTML translation - Extra Credit","title",0)
Head$= tag$(title$+ style$,"head")
html$={<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">}+nl$+tag$(head$+tag$(tag$(export$, "table"), "body"), "html")
clipboard html$
Report html$
Print "Press Esc to exit browser"
browser "about: "+html$
}
csv2html
- Output:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> <html> <head> <title>CSV to HTML translation - Extra Credit</title> <style type="text/css"> TD {background-color:#ddddff; } thead TD {background-color:#ddffdd; text-align:center; } </style> </head> <body> <table> <tr> <th>Character</th> <th>Speech</th> </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> <tr> <td>The multitude</td> <td>Who are you?</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> </body> </html>
Maple
#A translation of the C code posted
html_table := proc(str)
local char;
printf("<table>\n<tr><td>");
for char in str do
if char = "\n" then
printf("</td></tr>\n<tr><td>")
elif char = "," then
printf("</td><td>")
elif char = "<" then
printf("<")
elif char = ">" then
printf(">")
elif char = "&" then
printf("&")
else
printf(char)
end if;
end do;
printf("</td></tr>\n</table>");
end proc;
html_table("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!");
- Output:
<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>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> <tr><td>The multitude</td><td>Who are you?</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>
Mathematica / Wolfram Language
a="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!";
(*Naive*)
StringJoin["<table>\n",Map[StringJoin["<tr><td>",#,"</td></tr>\n"]&,
StringSplit[StringReplace[a,{","->"</td><td>","<"->"<",">"->">"}],"\n"]]
,"</table>"]
(*Extra*)
StringJoin["<table>\n",StringJoin["<tr><th>",#,"</th></tr>\n"]&[
StringSplit[StringReplace[a,{","->"</td><td>","<"->"<",">"->">"}],"\n"]//First]
,Map[StringJoin["<tr><td>",#,"</td></tr>\n"]&,
StringSplit[StringReplace[a,{","->"</td><td>","<"->"<",">"->">"}],"\n"]//Rest]
,"</table>"]
- Naive:
<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>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> <tr><td>The multitude</td><td>Who are you?</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>
- Extra:
<table> <tr><th>Character</td><td>Speech</th></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> <tr><td>The multitude</td><td>Who are you?</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>
MATLAB
Many specialized MATLAB file IO functions are painfully slow, and with large files it is often better to use a lower-level functions such as fread. Here we use fileread, which does a little bit of error handling and calls fread, specifying the input data as text. From this text string, we can easily convert any special html characters, split it into a cell array, and print it out specifying format.
inputString = fileread(csvFileName);
% using multiple regular expressions to clear up special chars
htmlFriendly = regexprep(regexprep(regexprep(regexprep(inputString,...
'&','&'),...
'"','"'),...
'<','<'),...
'>','>');
% split string into cell array
tableValues = regexp(regexp(htmlFriendly,'(\r\n|\r|\n)','split')',',','split');
%%% print in html format %%%
% <Extra Credit> first line gets treated as header
fprintf(1,['<table>\n\t<tr>' sprintf('\n\t\t<th>%s</th>',tableValues{1,:}{:})])
% print remaining lines of csv as html table (rows 2:end in cell array of csv values)
cellfun(@(x)fprintf(1,['\n\t<tr>' sprintf('\n\t\t<td>%s</td>',x{:}) '\n\t</tr>']),tableValues(2:end))
fprintf(1,'\n</table>')
As a single line:
fprintf(1,['<table>\n\t<tr>\n\t\t<th>',regexprep(regexprep(regexprep(regexprep(regexprep(regexprep(regexprep(regexprep(fileread(cvsFileName),'&','&'),'"','"'),'<','<'),'>','>'),'(?<=(^[^\n\r]*)),','</th>\n\t\t<th>'),'(?<!>)(\r\n|\r|\n)','</td>\n\t<tr>\n\t</tr>\n\t\t<td>'),'</td>\n','</th>\n','once'),',','</td>\n\t\t<td>'),'</td>\n\t</tr>\n</table>\n'])
- Output:
<table>
<tr>
<th>Character</th>
<th>Speech</th>
<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>
<tr>
<td>The multitude</td>
<td>Who are you?</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>
Maxima
infile: "input.csv";
outfile: "table.html";
instream: openr(infile);
outstream: openw(outfile);
printf(outstream, "<TABLE border=\"1\">~%");
nr: 0;
while (line: readline(instream))#false do (
nr: nr + 1,
line: ssubst("<", "<", line),
line: ssubst(">", ">", line),
value_list: map(lambda([f], strim(" ", f)), split(line, ",")),
if nr=1 then printf(outstream, " <THEAD bgcolor=\"yellow\">") else printf(outstream, " <TBODY bgcolor=\"orange\">"),
printf(outstream, "<TR>"),
for value in value_list do printf(outstream, "<TD>~a</TD>", value),
printf(outstream, "</TR>"),
if nr=1 then printf(outstream, "</THEAD>~%") else printf(outstream, "</TBODY>~%"));
printf(outstream, "</TABLE>~%");
close(instream);
close(outstream);
<TABLE border="1">
<THEAD bgcolor="yellow"><TR><TD>Character</TD><TD>Speech</TD></TR></THEAD>
<TBODY bgcolor="orange"><TR><TD>The multitude</TD><TD>The messiah! Show us the messiah!</TD></TR></TBODY>
<TBODY bgcolor="orange"><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></TBODY>
<TBODY bgcolor="orange"><TR><TD>The multitude</TD><TD>Who are you?</TD></TR></TBODY>
<TBODY bgcolor="orange"><TR><TD>Brians mother</TD><TD>I'm his mother; that's who!</TD></TR></TBODY>
<TBODY bgcolor="orange"><TR><TD>The multitude</TD><TD>Behold his mother! Behold his mother!</TD></TR></TBODY>
</TABLE>
ML/I
This example carries out the special formatting for extra credit. This is a macro rather than a function, though, due to the nature of ML/I.
Input
MCSKIP "WITH" NL
"" CSV to HTML
"" assumes macros on input stream 1, terminal on stream 2
MCSKIP MT,[]
MCSKIP SL WITH ~
MCINS %.
"" C1=th before header output, td afterwards
MCCVAR 1,2
MCSET C1=[th]
"" HTML escapes
MCDEF < AS [[<]]
MCDEF > AS [[>]]
MCDEF & AS [[&]]
"" Main line processing
MCDEF SL N1 OPT , N1 OR NL ALL
AS [[ <tr>]
MCSET T2=1
%L1.MCGO L2 IF T2 GR T1
[<]%C1.[>]%AT2.[</]%C1.[>]
MCSET T2=T2+1
MCGO L1
%L2.[ </tr>]
MCSET C1=[td]
]
[<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>HTML converted from CSV</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css"><!--
th {
font-weight:bold;
text-align:left
}
table,td,th {
border:1px solid;
border-collapse:collapse
}
td,th {
padding:10px
}
//-->
</style>
</head>
<body>
<table>]
MCSET S1=1
~MCSET S10=2
~MCSET S1=0
[</table>
</body>
</html>
]
Output
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>HTML converted from CSV</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css"><!--
th {
font-weight:bold;
text-align:left
}
table,td,th {
border:1px solid;
border-collapse:collapse
}
td,th {
padding:10px
}
//-->
</style>
</head>
<body>
<table>
<tr>
<th>Character</th>
<th>Speech</th>
</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>
<tr>
<td>The multitude</td>
<td>Who are you?</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>
</body>
</html>
Nanoquery
// a method that converts a csv row into a html table row as a string
def toHtmlRow(record, tag)
htmlrow = "\t<tr>\n"
// loop through the values in the current csv row
for i in range(1, len(record))
htmlrow = htmlrow + "\t\t<" + tag + ">" + (record ~ i) + "</" + tag + ">\n"
end for
return htmlrow + "\t</tr>\n"
end def
// get the name of the csv file then open it
print "filename: "
input fname
open fname
// allocate a string to hold the table
htmltable = "<table>\n"
// add the column names to the table (#0 returns column names as a record object)
htmltable = (htmltable + toHtmlRow(#0, "th"))
// add all other rows to the table
for i in range(1, $dbsize)
htmltable = (htmltable + toHtmlRow(#i, "td"))
end for
// close the html table
htmltable = htmltable+"</table>"
println htmltable
NetRexx
Uses the NetRexx solution for Read a file line by line to read the CSV file into the program.
/* NetRexx */
options replace format comments java crossref symbols nobinary
parse arg inFileName .
if inFileName = '' | inFileName = '.' then inFileName = './data/Brian.csv'
csv = RREadFileLineByLine01.scanFile(inFileName)
header = htmlHeader()
pre = htmlCsvText(csv, inFileName)
table = htmlCsvTable(csv, inFileName)
footer = htmlFooter()
say header
say pre
say table
say footer
return
method htmlHeader() public static returns Rexx
html = '<?xml version="1.0" encoding="UTF-8"?>\n' -
|| '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n' -
|| '<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">\n' -
|| '<head>\n' -
|| '<meta http-equiv="Content-type" content="text/html;charset=UTF-8"/>\n' -
|| '<title>RCsv2Html</title>\n' -
|| '<style type="text/css">\n' -
|| '<!--\n' -
|| '/* <![DATA[ */\n' -
|| 'body {\n' -
|| ' font-family: "Verdana", "Geneva", "Helvetica Neue", "Helvetica", "DejaVu Sans", "Arial", sans-serif;\n' -
|| '}\n' -
|| 'table, th, td {\n' -
|| ' border: 1px solid black;\n' -
|| ' border-collapse: collapse;\n' -
|| ' padding: 0.25em;\n' -
|| ' font-size: 85%;\n' -
|| '}\n' -
|| 'th {\n' -
|| ' color: white;\n' -
|| ' background-color: green;\n' -
|| '}\n' -
|| 'p.classname {\n' -
|| ' font-size: inherit;\n' -
|| '}\n' -
|| '/* ]] */\n' -
|| '//-->\n' -
|| '</style>\n' -
|| '</head>\n' -
|| '<body>\n' -
|| '<h1>Rosetta Code – NetRexx Sample Output</h2>\n' -
|| '<h2><a href="http://rosettacode.org/wiki/CSV_to_HTML_translation">CSV to HTML translation</a></h2>\n' -
|| ''
return html
method htmlFooter() public static returns Rexx
html = '</body>\n' -
|| '</html>\n' -
|| ''
return html
method htmlCsvText(csv, fileName = '.') public static returns Rexx
html = '<h3>Contents of CSV <code>'fileName'</code></h3>\n' -
|| '<pre>\n' -
|| ''
loop row = 1 to csv[0]
html = html || csv[row]'\n'
end row
html = html -
|| '</pre>\n' -
|| ''
return html
method htmlCsvTable(csv, fileName = '.') public static returns Rexx
html = '<table>\n' -
|| '<caption>Translation of CSV <code>'fileName'</code></caption>\n' -
|| '<thead>\n' -
|| ''
html = html -
|| htmlCsvTableRow(csv[1], 'th')'\n' -
|| '</thead>\n' -
|| '<tbody>\n' -
|| ''
loop r_ = 2 to csv[0]
html = html -
|| htmlCsvTableRow(csv[r_])'\n' -
|| ''
end r_
html = html -
|| '</tbody>\n' -
|| '</table>\n' -
|| ''
return html
method htmlCsvTableRow(row, tag = 'td') public static returns Rexx
row = row.strip('t')
row = row.changestr('&', '&') -- need to do this one first to avoid double translation
row = row.changestr('"', '"')
row = row.changestr("'", ''')
row = row.changestr('<', '<')
row = row.changestr('>', '>')
elmts = ''
elmts[0] = 0
e_ = 0
loop while row.length() > 0
parse row elmt ',' row
e_ = e_ + 1; elmts[0] = e_; elmts[e_] = elmt
end
html = '<tr>\n' -
|| ''
loop e_ = 1 to elmts[0]
html = html -
|| '<'tag'>'elmts[e_]'</'tag'>\n' -
|| ''
end e_
html = html -
|| '</tr>\n' -
|| ''
return html
Output:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8"/>
<title>RCsv2Html</title>
<style type="text/css">
<!--
/* <![DATA[ */
body {
font-family: "Verdana", "Geneva", "Helvetica Neue", "Helvetica", "DejaVu Sans", "Arial", sans-serif;
}
table, th, td {
border: 1px solid black;
border-collapse: collapse;
padding: 0.25em;
font-size: 85%;
}
th {
color: white;
background-color: green;
}
p.classname {
font-size: inherit;
}
/* ]] */
//-->
</style>
</head>
<body>
<h1>Rosetta Code – NetRexx Sample Output</h2>
<h2><a href="http://rosettacode.org/wiki/CSV_to_HTML_translation">CSV to HTML translation</a></h2>
<h3>Contents of CSV <code>./data/Brian.csv</code></h3>
<pre>
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!
</pre>
<table>
<caption>Contents of CSV <code>./data/Brian.csv</code></caption>
<thead>
<tr>
<th>Character</th>
<th>Speech</th>
</tr>
</thead>
<tbody>
<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>
<tr>
<td>The multitude</td>
<td>Who are you?</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>
</tbody>
</table>
</body>
</html>
Rendered Output:
Nim
import cgi, strutils
const csvtext = """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!"""
proc row2tr(row: string): string =
result = "<tr>"
let cols = xmlEncode(row).split(",")
for col in cols:
result.add "<td>"&col&"</td>"
result.add "</tr>"
proc csv2html(txt: string): string =
result = "<table summary=\"csv2html program output\">\n"
for row in txt.splitLines():
result.add " <tbody>"&row2tr(row)&"</tbody>\n"
result.add "</table>"
echo csv2html(csvtext)
- Output:
<table summary="csv2html program output"> <tbody><tr><td>Character</td><td>Speech</td></tr></tbody> <tbody><tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr></tbody> <tbody><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></tbody> <tbody><tr><td>The multitude</td><td>Who are you?</td></tr></tbody> <tbody><tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr></tbody> <tbody><tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr></tbody> </table>
Oberon-2
MODULE CSV2HTML;
IMPORT
Object,
IO,
IO:FileChannel,
IO:TextRider,
SB := ADT:StringBuffer,
NPCT:Tools,
NPCT:CGI:Utils,
Ex := Exception,
Out;
VAR
fileChannel: FileChannel.Channel;
rd: TextRider.Reader;
line: ARRAY 1024 OF CHAR;
table: SB.StringBuffer;
parts: ARRAY 2 OF STRING;
PROCEDURE DoTableHeader(sb: SB.StringBuffer;parts: ARRAY OF STRING);
BEGIN
sb.Append("<tr><th>"+Utils.EscapeHTML(parts[0])+"</th><th>"+Utils.EscapeHTML(parts[1])+"</th></tr>");
sb.AppendLn
END DoTableHeader;
PROCEDURE DoTableRow(sb: SB.StringBuffer;parts: ARRAY OF STRING);
BEGIN
sb.Append("<tr><td>"+Utils.EscapeHTML(parts[0])+"</td><td>"+Utils.EscapeHTML(parts[1])+"</td></tr>");
sb.AppendLn
END DoTableRow;
PROCEDURE DoTable(sb: SB.StringBuffer): STRING;
VAR
aux: SB.StringBuffer;
BEGIN
aux := SB.New("<table>");aux.AppendLn;
RETURN aux.ToString() + sb.ToString() + "</table>";
END DoTable;
BEGIN
TRY
fileChannel := FileChannel.OpenUnbuffered("script.csv",{FileChannel.read});
CATCH Ex.Exception(ex):
Out.Object(ex.GetMessage());Out.Ln;
HALT(1)
END;
rd := TextRider.ConnectReader(fileChannel);
(* Extract headers *)
TRY
rd.ReadLine(line);
table := NEW(SB.StringBuffer,2048);
Tools.Split(Object.NewLatin1(line),",",parts);
DoTableHeader(table,parts);
CATCH IO.Error(ex):
Out.Object(ex.Name() + ": " + ex.GetMessage());Out.Ln;
HALT(2)
END;
(* Extract data *)
LOOP
TRY
rd.ReadLine(line);
IF (line[0] # 0X)THEN (* skip empty lines *)
Tools.Split(Object.NewLatin1(line),",",parts);
DoTableRow(table,parts)
END
CATCH IO.Error(ex):
EXIT
END
END;
Out.Object(DoTable(table));Out.Ln;
fileChannel.Close()
END CSV2HTML.
- Output:
<table> <tr><th>Character</th><th>Speech</th></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> <tr><td>The multitude</td><td>Who are you?</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>
Objeck
use System.IO.File;
use Data.CSV;
class CsvToHtml {
function : Main(args : String[]) ~ Nil {
if(args->Size() = 1) {
table := CsvTable->New(FileReader->ReadFile(args[0]));
if(table->IsParsed()) {
buffer := "<html><body><table>";
Header(table->GetHeaders(), buffer);
for(i := 1; i < table->Size(); i += 1;) {
Data(table->Get(i), buffer);
};
buffer += "</table></body></html>";
buffer->PrintLine();
};
};
}
function : Header(row : CsvRow, buffer : String) ~ Nil {
buffer += "<tr>";
each(i : row) {
buffer += "<th>";
buffer += Encode(row->Get(i));
buffer += "</th>";
};
buffer += "</tr>";
}
function : Data(row : CsvRow, buffer : String) ~ Nil {
buffer += "<tr>";
each(i : row) {
buffer += "<td>";
buffer += Encode(row->Get(i));
buffer += "</td>";
};
buffer += "</tr>";
}
function : Encode(in : String) ~ String {
out := "";
each(i : in) {
c := in->Get(i);
select(c) {
label '&': {
out->Append("&");
}
label '\'': {
out->Append("'");
}
label '<': {
out->Append("<");
}
label '>': {
out->Append(">");
}
other: {
out->Append(c);
}
};
};
return out;
}
}
Output:
<html><body><table><tr><th>Character</th><th>Speech</th></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><tr><td>The multitude</td><td>Who are you?</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></body></html>
OCaml
Simple solution
OCaml possesses a CSV module but we do not use it hereafter because the CSV data does not contain comas.
open Printf
let csv_data = "\
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!"
(* General HTML escape *)
let escape =
let html_escapes = Str.regexp "\\([^A-Za-z0-9 ;!?'/]\\)" in
let esc s = sprintf "&#%04d;" (Char.code s.[Str.group_beginning 1]) in
Str.global_substitute html_escapes esc
let nl = Str.regexp "\n\r?"
let coma = Str.regexp ","
let list_of_csv csv =
List.map (fun l -> Str.split coma l) (Str.split nl csv)
let print_html_table segments =
printf "<table>\n";
List.iter (fun line ->
printf "<tr>";
List.iter (fun c -> printf "<td>%s</td>" (escape c)) line;
printf "</tr>\n";
) segments;
printf "</table>\n";
;;
let () =
print_html_table (list_of_csv csv_data)
- Sample html output:
<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>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>
<tr><td>The multitude</td><td>Who are you?</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>
Extra credit solution
open Printf
let csv_data = "\
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!"
(* General HTML escape *)
let escape =
let html_escapes = Str.regexp "\\([^A-Za-z0-9 ;!?'/]\\)" in
let esc s = sprintf "&#%04d;" (Char.code s.[Str.group_beginning 1]) in
Str.global_substitute html_escapes esc
let nl = Str.regexp "\n\r?"
let coma = Str.regexp ","
let list_of_csv csv =
List.map (fun l -> Str.split coma l) (Str.split nl csv)
let print_html_table segments =
let print_row line =
printf "<tr>";
List.iter (fun c -> printf "<td>%s</td>" (escape c)) line;
printf "</tr>\n" in
printf "<html>
<head>
<style type=\"text/css\">
td {background-color:#ddddff; }
thead td {background-color:#ddffdd; text-align:center; }
</style>
</head>";
printf "<table>\n<thead>";
print_row (List.hd segments);
printf "</thead><tbody>\n";
List.iter print_row (List.tl segments);
printf "</tbody>\n</table>\n</html>";
;;
let () =
print_html_table (list_of_csv csv_data)
- Output:
<html>
<head>
<style type="text/css">
td {background-color:#ddddff; }
thead td {background-color:#ddffdd; text-align:center; }
</style>
</head><table>
<thead><tr><td>Character</td><td>Speech</td></tr>
</thead><tbody>
<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>
<tr><td>The multitude</td><td>Who are you?</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>
</tbody>
</table>
</html>
OmniMark
Only the "extra credit" version is provided here. It is also XHTML 1.0, complete, and validates (as noted in the comment to the 'process-end' rule). The validation could be done by OmniMark too, but that's going well beyond the scope of the task. Only the three XML character references, which appear in the input, are included - >, <, and ' (so, not & and "). Output XHTML, plus a thumbnail screenshot of the output and the Firefox Inspector output, is included.
global switch thead initial {true}
global switch tbody-first initial {true}
define function process-tr (value stream line) as
do when thead
output '<thead>%n<tr><th>'
else when tbody-first
output '</thead>%n'
output '<tbody>%n<tr><td>'
deactivate tbody-first
else
output '<tr><td>'
done
repeat scan line
match ',' any-text => char
output '</th><th>' when thead
output '</td><td>' unless thead
using group ents submit char
match any-text => char
using group ents submit char
again
output '</td>' unless thead
output '</th>' when thead
output '</tr>'
deactivate thead
process-start
output '<?xml version="1.0" encoding="UTF-8"?>%n' ||
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">%n' ||
'<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">%n' ||
'<head>%n' ||
'<title>Monty Python’s Life of Brian (1979). Scene 20 (extract).</title>%n' ||
'<meta name="description" content="Rosetta Code. Task: CSV to HTML translation (extra credit)" />%n' ||
'<meta name="keywords" content="OmniMark" />%n' ||
'<meta name="author" content="Kennypete" />%n' ||
'<style type="text/css">%nth { background-color: gold }%n</style>%n' ||
'</head>%n' ||
'<body>%n' ||
'<table border=%"1%">%n'
process-end
output '</tbody>%n' ||
'</table>%n' ||
'</body>%n' ||
'</html>%n'
; Output validates to XHTML 1.0 - https://validator.w3.org/check
process
local stream s
set s to "Character,Speech%n" ||
"The multitude,The messiah! Show us the messiah!%n" ||
"Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>%n" ||
"The multitude,Who are you?%n" ||
"Brians mother,I'm his mother; that's who!%n" ||
"The multitude,Behold his mother! Behold his mother!%n"
using group line submit s
group ents
find '<'
output '<'
find '>'
output '>'
find "'"
output '''
group line
find line-start any-text+ => line
process-tr(line)
- Output:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Monty Python’s Life of Brian (1979). Scene 20 (extract).</title>
<meta name="description" content="Rosetta Code. Task: CSV to HTML translation (extra credit)" />
<meta name="keywords" content="OmniMark" />
<meta name="author" content="Kennypete" />
<style type="text/css">
th { background-color: gold }
</style>
</head>
<body>
<table border="1">
<thead>
<tr><th>Character</th><th>Speech</th></tr>
</thead>
<tbody>
<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>
<tr><td>The multitude</td><td>Who are you?</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>
</tbody>
</table>
</body>
</html>
OpenEdge/Progress
FUNCTION csvToHtml RETURNS CHARACTER (
i_lhas_header AS LOGICAL,
i_cinput AS CHARACTER
):
DEFINE VARIABLE coutput AS CHARACTER NO-UNDO.
DEFINE VARIABLE irow AS INTEGER NO-UNDO.
DEFINE VARIABLE icolumn AS INTEGER NO-UNDO.
DEFINE VARIABLE crow AS CHARACTER NO-UNDO.
DEFINE VARIABLE ccell AS CHARACTER NO-UNDO.
coutput = "<html>~n~t<table>".
DO irow = 1 TO NUM-ENTRIES( i_cinput, "~n":U ):
coutput = coutput + "~n~t~t<tr>".
crow = ENTRY( irow, i_cinput, "~n":U ).
DO icolumn = 1 TO NUM-ENTRIES( crow ):
ccell = ENTRY( icolumn, crow ).
coutput = coutput + "~n~t~t~t" + IF i_lhas_header AND irow = 1 THEN "<th>" ELSE "<td>".
coutput = coutput + REPLACE( REPLACE( REPLACE( ccell, "&", "&" ), "<", "<" ), ">", ">" ).
coutput = coutput + IF i_lhas_header AND irow = 1 THEN "</th>" ELSE "</td>".
END.
coutput = coutput + "~n~t~t</tr>".
END.
coutput = coutput + "~n~t</table>~n</html>".
RETURN coutput.
END FUNCTION. /* csvToHtml */
MESSAGE
csvToHtml(
TRUE,
"Character,Speech" + "~n" +
"The multitude,The messiah! Show us the messiah!" + "~n" +
"Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>" + "~n" +
"The multitude,Who are you?" + "~n" +
"Brians mother,I'm his mother; that's who!" + "~n" +
"The multitude,Behold his mother! Behold his mother!"
)
VIEW-AS ALERT-BOX.
- with header enabled:
<html>
<table>
<tr>
<th>Character</th>
<th>Speech</th>
</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>
<tr>
<td>The multitude</td>
<td>Who are you?</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>
</html>
Perl
Provide the CSV data as standard input. With a command-line argument, the first row will use instead of
.
use HTML::Entities;
sub row {
my $elem = shift;
my @cells = map {"<$elem>$_</$elem>"} split ',', shift;
print '<tr>', @cells, "</tr>\n";
}
my ($first, @rest) = map
{my $x = $_; chomp $x; encode_entities $x}
<STDIN>;
print "<table>\n";
row @ARGV ? 'th' : 'td', $first;
row 'td', $_ foreach @rest;
print "</table>\n";
- Output (with a command-line argument):
<table>
<tr><th>Character</th><th>Speech</th></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>
<tr><td>The multitude</td><td>Who are you?</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>
Phix
Based on Euphoria but with a simpler multiline constant.
with javascript_semantics constant input = """ 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!""" puts(1,"<table>\n<tr><td>") for i = 1 to length(input) do switch input[i] do case '\n' then puts(1,"</td></tr>\n<tr><td>") case ',' then puts(1,"</td><td>") case '<' then puts(1,"<") case '>' then puts(1,">") case '&' then puts(1,"&") case else puts(1,input[i]) end switch end for puts(1,"</td></tr>\n</table>")
- Output:
<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>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> <tr><td>The multitude</td><td>Who are you?</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>
PHP
Simple Solution
<Lang php> <?php $csv = <<<EOT 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! EOT;
function convert($csv) {
$out = []; array_map(function($ln) use(&$out) { $ln = htmlentities($ln); $out[] = count($out) == 0
? '<thead>'.implode('',explode(',',$ln))."</thead>\n" : ''.implode('',explode(',',$ln))."\n";
}, explode("\n",$csv));
return '
'.implode(,$out).'';
}
echo convert($csv); </syntaxhighlight>
PicoLisp
Simple solution
(load "@lib/http.l")
(in "text.csv"
(<table> 'myStyle NIL NIL
(prinl)
(while (split (line) ",")
(<row> NIL (ht:Prin (pack (car @))) (ht:Prin (pack (cadr @))))
(prinl) ) ) )
- Output:
<table class="myStyle">
<tr><td>Character</td><td>Speech</td></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>
<tr><td>The multitude</td><td>Who are you?</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>
Extra credit solution
(load "@lib/http.l")
(in "text.csv"
(when (split (line) ",")
(<table> 'myStyle NIL (mapcar '((S) (list NIL (pack S))) @)
(prinl)
(while (split (line) ",")
(<row> NIL (ht:Prin (pack (car @))) (ht:Prin (pack (cadr @))))
(prinl) ) ) ) )
- Output:
<table class="myStyle"><tr><th>Character</th><th>Speech</th></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>
<tr><td>The multitude</td><td>Who are you?</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>
PowerShell
Simple solution
Import-Csv -Path .\csv_html_test.csv | ConvertTo-Html -Fragment | Out-File .\csv_html_test.html
- Output:
<table>
<colgroup>
<col/>
<col/>
</colgroup>
<tr><th>Character</th><th>Speech </th></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>
<tr><td>The multitude</td><td>Who are you? </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>
Extra credit solution
$htmlformat = '<title>Csv to Html</title>'
$htmlformat += '<style type="text/css">'
$htmlformat += 'BODY{background-color:#663300;color:#FFCC00;font-family:Arial Narrow,sans-serif;font-size:17px;}'
$htmlformat += 'TABLE{border-width: 3px;border-style: solid;border-color: black;border-collapse: collapse;}'
$htmlformat += 'TH{border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color:#663333}'
$htmlformat += 'TD{border-width: 1px;padding: 8px;border-style: solid;border-color: black;background-color:#660033}'
$htmlformat += '</style>'
Import-Csv -Path .\csv_html_test.csv | ConvertTo-Html -Head $htmlformat -Body '<h1>Csv to Html</h1>' | Out-File .\csv_html_test.html
Invoke-Expression .\csv_html_test.html
- Output:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Csv to Html</title><style type="text/css">BODY{background-color:#663300;color:#FFCC00;font-family:Arial Narrow,sans-serif;font-size:17px;}TABLE{border-width: 3px;border-style: solid;border-color: black;border-collapse: collapse;}TH{border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color:#663333}TD{border-width: 1px;padding: 8px;border-style: solid;border-color: black;background-color:#660033}</style>
</head><body>
<h1>Csv to Html</h1>
<table>
<colgroup>
<col/>
<col/>
</colgroup>
<tr><th>Character</th><th>Speech </th></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>
<tr><td>The multitude</td><td>Who are you? </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>
</body></html>
Prolog
Uses DCG.
Simple solution
csv_html :-
L = "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!",
csv_html(L, Out, []),
string_to_list(Str, Out),
writeln(Str).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% simple HTML
%
csv_html(L) -->
"<TABLE>\n",
csv_tr(L),
"</TABLE>".
csv_tr([]) --> [].
csv_tr(L) -->
"<TR>\n",
csv_td(L, S),
"\n</TR>\n",
csv_tr(S).
csv_td(L, S) -->
"<TD>",
csv_td_in(L, S),
"</TD>".
csv_td_in([], []) --> [].
csv_td_in([10|L], L) --> [].
csv_td_in([44|L], S) -->
"</TD><TD>",
csv_td_in(L,S).
csv_td_in([60|T], S) -->
"<",
csv_td_in(T, S).
csv_td_in([62|T], S) -->
">",
csv_td_in(T, S).
csv_td_in([H|T], S) -->
[H],
csv_td_in(T, S).
- Output:
<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>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>
<TR>
<TD>The multitude</TD><TD>Who are you?</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>
Extra credit solution
csv_html_plus :-
L =
"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!",
csv_html_plus(L, Out1, []),
string_to_list(Str1, Out1),
writeln(Str1).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% HTML +
%
csv_html_plus(L) -->
"<TABLE>\n",
csv_head(L, R),
csv_body(R),
"</TABLE>".
csv_head(L, R) -->
"<THEAD>\n",
csv_head_tr(L, R),
"</THEAD>\n".
csv_head_tr(L, R) -->
"<TR>\n",
csv_head_th(L, R),
"\n</TR>\n".
csv_head_th(L, S) -->
"<TH style='color:#000; background:#FF0;'>",
csv_head_th_in(L, S),
"</TH>".
csv_head_th_in([], []) --> [].
csv_head_th_in([10|L], L) --> [].
csv_head_th_in([44|L], S) -->
"</TH><TH style='color:#000; background:#FF0;'>",
csv_head_th_in(L,S).
csv_head_th_in([H|T], S) -->
[H],
csv_head_th_in(T, S).
csv_body(L) -->
"<TBODY>\n",
csv_body_tr(L),
"</TBODY>\n".
csv_body_tr([]) --> [].
csv_body_tr(L) -->
"<TR>\n",
csv_body_td(L, S),
"\n</TR>\n",
csv_body_tr(S).
csv_body_td(L, S) -->
"<TD style='color:#000; background:#8FF; border:1px #000 solid; padding:0.6em;'>",
csv_body_td_in(L, S),
"</TD>".
csv_body_td_in([], []) --> [].
csv_body_td_in([10|L], L) --> [].
csv_body_td_in([44|L], S) -->
"</TD><TD style='color:#000; background:#8FF; border:1px #000 solid; padding:0.6em;'>",
csv_body_td_in(L,S).
csv_body_td_in([60|T], S) -->
"<",
csv_body_td_in(T, S).
csv_body_td_in([62|T], S) -->
">",
csv_body_td_in(T, S).
csv_body_td_in([H|T], S) -->
[H],
csv_body_td_in(T, S).
- Output:
<TABLE>
<THEAD>
<TR>
<TH style='color:#000; background:#FF0;'>Character</TH><TH style='color:#000; background:#FF0;'>Speech</TH>
</TR>
</THEAD>
<TBODY>
<TR>
<TD style='color:#000; background:#8FF; border:1px #000 solid; padding:0.6em;'>The multitude</TD><TD style='color:#000; background:#8FF; border:1px #000 solid; padding:0.6em;'>The messiah! Show us the messiah!</TD>
</TR>
<TR>
<TD style='color:#000; background:#8FF; border:1px #000 solid; padding:0.6em;'>Brians mother</TD><TD style='color:#000; background:#8FF; border:1px #000 solid; padding:0.6em;'><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!></angry></TD>
</TR>
<TR>
<TD style='color:#000; background:#8FF; border:1px #000 solid; padding:0.6em;'>The multitude</TD><TD style='color:#000; background:#8FF; border:1px #000 solid; padding:0.6em;'>Who are you?</TD>
</TR>
<TR>
<TD style='color:#000; background:#8FF; border:1px #000 solid; padding:0.6em;'>Brians mother</TD><TD style='color:#000; background:#8FF; border:1px #000 solid; padding:0.6em;'>I'm his mother; that's who!</TD>
</TR>
<TR>
<TD style='color:#000; background:#8FF; border:1px #000 solid; padding:0.6em;'>The multitude</TD><TD style='color:#000; background:#8FF; border:1px #000 solid; padding:0.6em;'>Behold his mother! Behold his mother!</TD>
</TR>
</TBODY>
</TABLE>
HTML outputs rendered in firefox browser
Python
(Note: rendered versions of all three outputs are shown at the foot of this section).
Simple solution
csvtxt = '''\
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!\
'''
from cgi import escape
def _row2tr(row, attr=None):
cols = escape(row).split(',')
return ('<TR>'
+ ''.join('<TD>%s</TD>' % data for data in cols)
+ '</TR>')
def csv2html(txt):
htmltxt = '<TABLE summary="csv2html program output">\n'
for rownum, row in enumerate(txt.split('\n')):
htmlrow = _row2tr(row)
htmlrow = ' <TBODY>%s</TBODY>\n' % htmlrow
htmltxt += htmlrow
htmltxt += '</TABLE>\n'
return htmltxt
htmltxt = csv2html(csvtxt)
print(htmltxt)
Sample HTML output
<TABLE summary="csv2html program output">
<TBODY><TR><TD>Character</TD><TD>Speech</TD></TR></TBODY>
<TBODY><TR><TD>The multitude</TD><TD>The messiah! Show us the messiah!</TD></TR></TBODY>
<TBODY><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></TBODY>
<TBODY><TR><TD>The multitude</TD><TD>Who are you?</TD></TR></TBODY>
<TBODY><TR><TD>Brians mother</TD><TD>I'm his mother; that's who!</TD></TR></TBODY>
<TBODY><TR><TD>The multitude</TD><TD>Behold his mother! Behold his mother!</TD></TR></TBODY>
</TABLE>
Extra credit solution
def _row2trextra(row, attr=None):
cols = escape(row).split(',')
attr_tr = attr.get('TR', '')
attr_td = attr.get('TD', '')
return (('<TR%s>' % attr_tr)
+ ''.join('<TD%s>%s</TD>' % (attr_td, data) for data in cols)
+ '</TR>')
def csv2htmlextra(txt, header=True, attr=None):
' attr is a dictionary mapping tags to attributes to add to that tag'
attr_table = attr.get('TABLE', '')
attr_thead = attr.get('THEAD', '')
attr_tbody = attr.get('TBODY', '')
htmltxt = '<TABLE%s>\n' % attr_table
for rownum, row in enumerate(txt.split('\n')):
htmlrow = _row2trextra(row, attr)
rowclass = ('THEAD%s' % attr_thead) if (header and rownum == 0) else ('TBODY%s' % attr_tbody)
htmlrow = ' <%s>%s</%s>\n' % (rowclass, htmlrow, rowclass[:5])
htmltxt += htmlrow
htmltxt += '</TABLE>\n'
return htmltxt
htmltxt = csv2htmlextra(csvtxt, True,
dict(TABLE=' border="1" summary="csv2html extra program output"',
THEAD=' bgcolor="yellow"',
TBODY=' bgcolor="orange"'
)
)
print(htmltxt)
Sample HTML output
<TABLE border="1" summary="csv2html extra program output">
<THEAD bgcolor="yellow"><TR><TD>Character</TD><TD>Speech</TD></TR></THEAD>
<TBODY bgcolor="orange"><TR><TD>The multitude</TD><TD>The messiah! Show us the messiah!</TD></TR></TBODY>
<TBODY bgcolor="orange"><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></TBODY>
<TBODY bgcolor="orange"><TR><TD>The multitude</TD><TD>Who are you?</TD></TR></TBODY>
<TBODY bgcolor="orange"><TR><TD>Brians mother</TD><TD>I'm his mother; that's who!</TD></TR></TBODY>
<TBODY bgcolor="orange"><TR><TD>The multitude</TD><TD>Behold his mother! Behold his mother!</TD></TR></TBODY>
</TABLE>
Robust solution
This solution uses the CSV parser and HTML-capable XML serializer included in the Python standard library to produce the same fancy table as in the "extra credit" version.
While not strictly necessary for the very constrained input given in Rosetta Code, using readily available high-level APIs is idiomatic for Python, makes bugs easier to catch, and this also demonstrates the kind of "safe to use with more general inputs" code that is good to get in the habit of reaching for when dealing with real-world inputs and outputs.
(eg. Suppose an earlier stage in the pipeline is using a proper CSV-generating library but is running in a locale that uses commas for decimal separators and has a bug that causes it to unexpectedly start feeding pretty-printed floating-point in. Using a proper CSV reader minimizes the potential harm and helps the program to raise errors in the most elucidating place.)
Since the version of ElementTree in the standard library does not support pretty-printing, the output it produces is minified. Unlike the "extra credit" version, this doesn't put each <TR>
element in its own <TBODY>
.
from csv import DictReader
from xml.etree import ElementTree as ET
def csv2html_robust(txt, header=True, attr=None):
# Use DictReader because, despite what the docs say, reader() doesn't
# return an object with .fieldnames
# (DictReader expects an iterable that returns lines, so split on \n)
reader = DictReader(txt.split('\n'))
table = ET.Element("TABLE", **attr.get('TABLE', {}))
thead_tr = ET.SubElement(
ET.SubElement(table, "THEAD", **attr.get('THEAD', {})),
"TR")
tbody = ET.SubElement(table, "TBODY", **attr.get('TBODY', {}))
if header:
for name in reader.fieldnames:
ET.SubElement(thead_tr, "TD").text = name
for row in reader:
tr_elem = ET.SubElement(tbody, "TR", **attr.get('TR', {}))
# Use reader.fieldnames to query `row` in the correct order.
# (`row` isn't an OrderedDict prior to Python 3.6)
for field in reader.fieldnames:
td_elem = ET.SubElement(tr_elem, "TD", **attr.get('TD', {}))
td_elem.text = row[field]
return ET.tostring(table, method='html')
htmltxt = csv2html_robust(csvtxt, True, {
'TABLE': {'border': "1", 'summary': "csv2html extra program output"},
'THEAD': {'bgcolor': "yellow"},
'TBODY': {'bgcolor': "orange"}
})
print(htmltxt.decode('utf8'))
Sample HTML output
<TABLE border="1" summary="csv2html extra program output"><THEAD bgcolor="yellow"><TR><TD>Character</TD><TD>Speech</TD></TR></THEAD><TBODY bgcolor="orange"><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><TR><TD>The multitude</TD><TD>Who are you?</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></TBODY></TABLE>
HTML outputs rendered in firefox browser
R
Using base R functions only, this is a very basic implementation and produces a simple HTML table
File <- "~/test.csv"
Opened <- readLines(con = File)
Size <- length(Opened)
HTML <- "~/test.html"
Table <- list()
for(i in 1:Size)
{
#i=1
Split <- unlist(strsplit(Opened[i],split = ","))
Table[i] <- paste0("<td>",Split,"</td>",collapse = "")
Table[i] <- paste0("<tr>",Table[i],"</tr>")
}
Table[1] <- paste0("<table>",Table[1])
Table[length(Table)] <- paste0(Table[length(Table)],"</table>")
writeLines(as.character(Table), HTML)
Sample HTML output:
<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>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>
<tr><td>The multitude</td><td>Who are you?</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>
Racket
Uses X-exprs:
#lang racket
(define e.g.-CSV
(string-join
'("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!")
"\n"))
(define (CSV-lines->HTML-table csv)
(define csv-rows
(regexp-split "\n" csv))
(define csv-row-cells
(map (lambda (row) (regexp-split "," row)) csv-rows))
(define (cell-data->HTML-data data)
`(td () ,data))
(define (row-data->HTML-row CSV-row)
`(tr () ,@(map cell-data->HTML-data CSV-row) "\n"))
`(table
(thead
,(row-data->HTML-row (car csv-row-cells)))
(tbody ,@(map row-data->HTML-row (cdr csv-row-cells)))))
(require xml)
(display (xexpr->string (CSV-lines->HTML-table e.g.-CSV)))
Sample HTML output:
<table><thead><tr><td>Character</td><td>Speech</td>
</tr></thead><tbody><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><tr><td>The multitude</td><td>Who are you?</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></tbody></table>
Raku
(formerly Perl 6)
A very lispy solution:
my $str = "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!";
# comment the next line out, if you want to read from standard input instead of the hard-coded $str above
# my $str = $*IN.slurp;
my &escape = *.trans(« & < > » => « & < > »); # a function with one argument that escapes the entities
my &tag = {"<$^tag>"~$^what~"</$^tag>"};
printf
'<!DOCTYPE html>
<html>
<head><title>Some Text</title></head>
<body><table>
%s
</table></body></html>
', [~] # concatenation reduction ('a', 'b', 'c') → 'abc'
(escape($str).split(/\n/) # escape the string and split at newline
==> map -> $line {tag 'tr', # feed that into a map, that map function will tag as 'tr, and has an argument called $line
([~] $line.split(/','/)\ # split $line at ',',
# that / at the end is just an unspace, you can omit it, but then you have to delete
# all whitespace and comments between split(…) and .map
.map({tag 'td', $^cell}))})\ # map those cells as td
.join("\n"); # append a newline for nicer output
- Output:
<!DOCTYPE html>
<html>
<head><title>Some Text</title></head>
<body><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>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>
<tr><td>The multitude</td><td>Who are you?</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></body></html>
Red
Red []
csv: {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!}
add2html: func [ bl ] [append html rejoin bl ] ;; helper function to add block data to html string
;;----------------------------------------------------------------------
csv2html: func ["function to generate string with html table from csv data file"
;;----------------------------------------------------------------------
s [string!] "input .csv data"
][
arr: split s newline ;; generate array (series) from string
html: copy "<table border=1>^/" ;; init html string
forall arr [ ;; i use forall here so that i can test for head? of series ...
either head? arr [ append html "<tr bgcolor=wheat>"]
[ append html "<tr>"]
replace/all first arr "<" "<" ;; escape "<" and ">" characters
replace/all first arr ">" ">"
foreach col split first arr "," [
either head? arr [
add2html ['<th> col '</th>]
][
add2html ['<td> col '</td>]
]
]
add2html ['</tr> newline]
]
return add2html ['</table>]
]
;;----------------------------------------------------------------------
print csv2html csv ;; call function
write %data.html csv2html csv ;; write to file
output
<table border=1> <tr bgcolor=wheat><th>Character</th><th>Speech</th></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> <tr><td>The multitude</td><td>Who are you?</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>
Sample HTML output
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! |
Retro
remapping off
"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!" remapping on
keepString constant CSV
: display ( c- )
[ ', = ] [ drop "</td><td>" puts ] when
[ 10 = ] [ drop "</td></tr>\n<tr><td>" puts ] when
[ '< = ] [ drop "<" puts ] when
[ '> = ] [ drop ">" puts ] when
[ '& = ] [ drop "&" puts ] when
putc ;
: displayHTML ( $- )
"<table>\n<tr><td>" puts
[ @ display ] ^types'STRING each@
"</td></tr>\n</table>" puts ;
CSV displayHTML
- Output:
<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>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>
<tr><td>The multitude</td><td>Who are you?</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>
REXX
The rendered output was verified using Firefox Aurora with:
- file:///c:/output.html
- file:///c:/outputh.html
/*REXX program converts CSV ───► HTML table representing the CSV data. */
arg header_ . /*obtain an uppercase version of args. */
wantsHdr= (header_=='HEADER') /*is the arg (low/upp/mix case)=HEADER?*/
/* [↑] determine if user wants a hdr. */
iFID= 'CSV_HTML.TXT' /*the input fileID to be used. */
if wantsHdr then oFID= 'OUTPUTH.HTML' /*the output fileID with header.*/
else oFID= 'OUTPUT.HTML' /* " " " without " */
do rows=0 while lines(iFID)\==0 /*read the rows from a (text/txt) file.*/
row.rows= strip( linein(iFID) )
end /*rows*/
convFrom= '& < > "' /*special characters to be converted. */
convTo = '& < > "' /*display what they are converted into.*/
call write , '<html>'
call write , '<table border=4 cellpadding=9 cellspacing=1>'
do j=0 for rows; call write 5, '<tr>'
tx= 'td'
if wantsHdr & j==0 then tx= 'th' /*if user wants a header, then oblige. */
do while row.j\==''; parse var row.j yyy "," row.j
do k=1 for words(convFrom)
yyy=changestr( word( convFrom, k), yyy, word( convTo, k))
end /*k*/
call write 10, '<'tx">"yyy'</'tx">"
end /*forever*/
end /*j*/
call write 5, '<tr>'
call write , '</table>'
call write , '</html>'
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
write: call lineout oFID, left('', 0 || arg(1) )arg(2); return
Some older REXXes don't have a changestr BIF, so one is included here ──► CHANGESTR.REX.
output
<html> <table border=4 cellpadding=9 cellspacing=1> <tr> <td>Character</td> <td>Speech</td> <tr> <td>The multitude</td> <td>The messiah! Show us the messiah!</td> <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> <td>The multitude</td> <td>Who are you?</td> <tr> <td>Brians mother</td> <td>I'm his mother; that's who!</td> <tr> <td>The multitude</td> <td>Behold his mother! Behold his mother!</td> <tr> </table> </html>
rendered output
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! |
output (extra credit solution) when the first argument is HEADER in upper/lower/mixed case (with/without leading/trailing blanks).
<html> <table border=4 cellpadding=9 cellspacing=1> <tr> <th>Character</th> <th>Speech</th> <tr> <td>The multitude</td> <td>The messiah! Show us the messiah!</td> <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> <td>The multitude</td> <td>Who are you?</td> <tr> <td>Brians mother</td> <td>I'm his mother; that's who!</td> <tr> <td>The multitude</td> <td>Behold his mother! Behold his mother!</td> <tr> </table> </html>
rendered output
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! |
Ruby
The extra credit version has one extra line compared to the non-extra credit version. To output a header, simply add "header" to the command line:
ruby csv2html.rb header
I/O is done through standard input/output.
require 'cgi'
puts '<table summary="csv2html program output">'
def row2html str, wrap = "td"
"<tr>" +
str.split(",").map { |cell| "<#{wrap}>#{CGI.escapeHTML cell}</#{wrap}>" }.join +
"</tr>"
end
puts row2html gets.chomp, "th" if ARGV.delete "header"
while str = gets
puts row2html str.chomp
end
puts "</table>"
- Output:
<table summary="csv2html program output">
<tr><th>Character</th><th>Speech</th></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>
<tr><td>The multitude</td><td>Who are you?</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>
Run BASIC
The extra credit version has 2 extra lines of code to get the heading.
csv$ = "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!"
k = instr(csv$,",") ' 2 exra lines to get heading
csv$ = left$(csv$,k - 1) + "</th><th> + mid$(csv$,k + 1)
csv$ = strRep$(csv$,",","</td><td>")
html "<table border=1><TR bgcolor=wheat align=center><th>";strRep$(csv$,chr$(13),"</td></tr><tr><td>");"</td></tr></table"
wait
' --------------------------------
' string replace rep str with
' --------------------------------
FUNCTION strRep$(strRep$,rep$,with$)
ln = len(rep$)
k = instr(strRep$,rep$)
while k
strRep$ = left$(strRep$,k - 1) + with$ + mid$(strRep$,k + ln)
k = instr(strRep$,rep$)
WEND
END FUNCTION
- Output:
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! |
Rust
static INPUT : &'static str =
"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!";
fn main() {
print!("<table>\n<tr><td>");
for c in INPUT.chars() {
match c {
'\n' => print!("</td></tr>\n<tr><td>"),
',' => print!("</td><td>"),
'<' => print!("<"),
'>' => print!(">"),
'&' => print!("&"),
_ => print!("{}", c)
}
}
println!("</td></tr>\n</table>");
}
- Output:
<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>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>
<tr><td>The multitude</td><td>Who are you?</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>
Scala
Scala has built-in support for XML, so you can freely mix XML literals into your Scala source code. This is nice, because instead of using strings to represent XML, you create XML literals that the compiler can understand and verify. This approach lets you easily generate dynamic XML by interweaving Scala code and XML in the same expressions.
object CsvToHTML extends App {
val header = <head>
<title>CsvToHTML</title>
<style type="text/css">
td {{background-color:#ddddff; }} thead td {{background-color:#ddffdd; text-align:center; }}
</style>
</head>
val csv =
"""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!""".stripMargin
def csv2html(csv: String, withHead: Boolean) = {
def processRow(text: String) = <tr>
{text.split(',').map(s => <td>
{s}
</td>)}
</tr>
val (first :: rest) = csv.lines.toList // Separate the header and the rest
def tableHead = if (withHead)
<thead>
{processRow(first)}
</thead>
else processRow(first)
<html>
{header}<body>
<table>
{tableHead}{rest.map(processRow)}
</table>
</body>
</html>
}
println(csv2html(csv, true))
}
- Output:
<html>
<head>
<title>CsvToHTML</title>
<style type="text/css">
td {background-color:#ddddff; } thead td {background-color:#ddffdd; text-align:center; }
</style>
</head><body>
<table>
<thead>
<tr>
<td>
Character
</td><td>
Speech
</td>
</tr>
</thead><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><tr>
<td>
The multitude
</td><td>
Who are you?
</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>
</body>
</html>
Sed
File csv2html.sed
#!/bin/sed -f
s|<|\<|g
s|>|\>|g
s|^| <tr>\n <td>|
s|,|</td>\n <td>|
s|$|</td>\n </tr>|
1s|^|<table>\n|
$s|$|\n</table>|
$ sed -f csv2html.sed input.csv
<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>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>
<tr>
<td>The multitude</td>
<td>Who are you?</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>
Seed7
The library html_ent.s7i defines the function encodeHtmlContent, which replaces characters with HTML entities. E.g.: '<' is replaced by <.
$ include "seed7_05.s7i";
include "html_ent.s7i";
const string: csvData is "\
\Character,Speech\n\
\The multitude,The messiah! Show us the messiah!\n\
\Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>\n\
\The multitude,Who are you?\n\
\Brians mother,I'm his mother; that's who!\n\
\The multitude,Behold his mother! Behold his mother!\n";
const proc: main is func
local
var string: line is "";
var string: column is "";
const array [boolean] string: columnStartTag is [boolean] ("<td>", "<th>");
const array [boolean] string: columnEndTag is [boolean] ("</td>", "</th>");
var boolean: firstLine is TRUE;
begin
writeln("<table>");
for line range split(csvData, '\n') do
write("<tr>");
for column range split(line, ',') do
write(columnStartTag[firstLine] <& encodeHtmlContent(column) <& columnEndTag[firstLine]);
end for;
writeln("</tr>");
firstLine := FALSE;
end for;
writeln("</table>");
end func;
- Output:
<table>
<tr><th>Character</th><th>Speech</th></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>
<tr><td>The multitude</td><td>Who are you?</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>
<tr><td></td></tr>
</table>
- Output:
viewed with a browser
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! |
Sidef
func escape(str) { str.trans(« & < > », « & < > ») }
func tag(t, d) { "<#{t}>#{d}</#{t}>" }
func csv2html(str) {
var template = <<-'EOT'
<!DOCTYPE html>
<html>
<head><title>Some Text</title></head>
<body><table>
%s
</table></body></html>
EOT
template.sprintf(escape(str).lines.map{ |line|
tag('tr', line.split(',').map{|cell| tag('td', cell) }.join)
}.join("\n")
)
}
var str = <<'EOT';
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!
EOT
print csv2html(str)
- Output:
<!DOCTYPE html>
<html>
<head><title>Some Text</title></head>
<body><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>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>
<tr><td>The multitude</td><td>Who are you?</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></body></html>
Tcl
package require Tcl 8.5
package require csv
package require html
package require struct::queue
set csvData "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!"
struct::queue rows
foreach line [split $csvData "\n"] {
csv::split2queue rows $line
}
html::init
puts [subst {
[html::openTag table {summary="csv2html program output"}]
[html::while {[rows size]} {
[html::row {*}[html::quoteFormValue [rows get]]]
}]
[html::closeTag]
}]
Extra credit version:
package require Tcl 8.5
package require csv
package require html
package require struct::queue
set csvData "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!"
html::init {
table.border 1
table.summary "csv2html program output"
tr.bgcolor orange
}
# Helpers; the html package is a little primitive otherwise
proc table {contents {opts ""}} {
set out [html::openTag table $opts]
append out [uplevel 1 [list subst $contents]]
append out [html::closeTag]
}
proc tr {list {ropt ""}} {
set out [html::openTag tr $ropt]
foreach x $list {append out [html::cell "" $x td]}
append out [html::closeTag]
}
# Parse the CSV data
struct::queue rows
foreach line [split $csvData "\n"] {
csv::split2queue rows $line
}
# Generate the output
puts [subst {
[table {
[tr [html::quoteFormValue [rows get]] {bgcolor="yellow"}]
[html::while {[rows size]} {
[tr [html::quoteFormValue [rows get]]]
}]
}]
}]
- Output:
<table border="1" summary="csv2html program output">
<tr bgcolor="yellow"><td>Character</td><td>Speech</td></tr>
<tr bgcolor="orange"><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
<tr bgcolor="orange"><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>
<tr bgcolor="orange"><td>The multitude</td><td>Who are you?</td></tr>
<tr bgcolor="orange"><td>Brians mother</td><td>I'm his mother; that's who!</td></tr>
<tr bgcolor="orange"><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table>
TUSCRIPT
$$ MODE TUSCRIPT
MODE DATA
$$ csv=*
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!
$$ htmlhead=*
<!DOCTYPE html system>
<html>
<head>
<title>Life of Brian</title>
<style type="text/css">
th {background-color:orange}
td {background-color:yellow}
</style></head><body><table>
$$ BUILD X_TABLE txt2html=*
<< <
>> >
$$ MODE TUSCRIPT
file="brian.html"
ERROR/STOP CREATE (file,FDF-o,-std-)
csv=EXCHANGE (csv,txt2html)
x=SPLIT (csv,":,:",row1,row2)
ACCESS html: WRITE/ERASE/RECORDS/UTF8 $file s,html
WRITE html htmlhead
LOOP n,td1=row1,td2=row2
IF (n==1) THEN
row=CONCAT ("<tr><th>",td1,"</th><th>",td2,"</th></tr>")
ELSE
row=CONCAT ("<tr><td>",td1,"</td><td>",td2,"</td></tr>")
ENDIF
WRITE html row
ENDLOOP
WRITE html "</table></body></html>"
ENDACCESS/PRINT html
Output (source code)
<!DOCTYPE html system>
<html>
<head>
<title>Life of Brian</title>
<style type="text/css">
th {background-color:orange}
td {background-color:yellow}
</style></head><body><table>
<tr><th>Character</th><th>Speech</th></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>
<tr><td>The multitude</td><td>Who are you?</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></body></html>
Output (rendered)
TXR
Simple
@(collect)
@char,@speech
@(end)
@(output :filter :to_html)
<table>
@ (repeat)
<tr>
<td>@char</td>
<td>@speech</td>
</tr>
@ (end)
</table>
@(end)
- Output:
$ txr csv.txr csv.txt
<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>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>
<tr>
<td>The multitude</td>
<td>Who are you?</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>
With Styling
@(collect)
@char,@speech
@(end)
@(output :filter :to_html)
<style type="text/css">
tr.odd td {
background-color: #CC9999; color: black;
}
tr.even td {
background-color: #9999CC; color: black;
}
th {
background-color: #99CC99; color: black;
}
</style>
<table>
@ (repeat :counter row)
<tr class="@(if (evenp row) 'even 'odd)">
<td>@char</td>
<td>@speech</td>
</tr>
@ (first)
<tr>
<th>@char</th>
<th>@speech</th>
</tr>
@ (end)
</table>
@(end)
- Output:
$ txr csv2.txr csv.txt
<style type="text/css">
tr.odd td {
background-color: #CC9999; color: black;
}
tr.even td {
background-color: #9999CC; color: black;
}
th {
background-color: #99CC99; color: black;
}
</style>
<table>
<tr>
<th>Character</th>
<th>Speech</th>
</tr>
<tr class="odd">
<td>The multitude</td>
<td>The messiah! Show us the messiah!</td>
</tr>
<tr class="even">
<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>
<tr class="odd">
<td>The multitude</td>
<td>Who are you?</td>
</tr>
<tr class="even">
<td>Brians mother</td>
<td>I'm his mother; that's who!</td>
</tr>
<tr class="odd">
<td>The multitude</td>
<td>Behold his mother! Behold his mother!</td>
</tr>
</table>
UNIX Shell
csv2html() {
IFS=,
echo "<table>"
echo "<thead>"
read -a fields
htmlrow th "${fields[@]}"
echo "</thead>"
echo "<tbody>"
while read -a fields
do htmlrow td "${fields[@]}"
done
echo "</tbody>"
echo "</table>"
}
htmlrow() {
cell=$1
shift
echo "<tr>"
for field
do echo "<$cell>$(escape_html "$field")</$cell>"
done
echo "</tr>"
}
escape_html() {
str=${1//\&/&}
str=${str//</<}
str=${str//>/>}
echo "$str"
}
csv2html <<-END
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!
END
- Output:
<table>
<thead>
<tr>
<th>Character</th>
<th>Speech</th>
</tr>
</thead>
<tbody>
<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>
<tr>
<td>The multitude</td>
<td>Who are you?</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>
</tbody>
</table>
VBA
Public Sub CSV_TO_HTML()
input_ = "Character,Speech\n" & _
"The multitude,The messiah! Show us the messiah!\n" & _
"Brians mother,<angry>Now you listen here! He's not the messiah; " & _
"he's a very naughty boy! Now go away!</angry>\n" & _
"The multitude,Who are you?\n" & _
"Brians mother,I'm his mother; that's who!\n" & _
"The multitude,Behold his mother! Behold his mother!"
Debug.Print "<table>" & vbCrLf & "<tr><td>"
For i = 1 To Len(input_)
Select Case Mid(input_, i, 1)
Case "\"
If Mid(input_, i + 1, 1) = "n" Then
Debug.Print "</td></tr>" & vbCrLf & "<tr><td>";
i = i + 1
Else
Debug.Print Mid(input_, i, 1);
End If
Case ",": Debug.Print "</td><td>";
Case "<": Debug.Print "<";
Case ">": Debug.Print ">";
Case "&": Debug.Print "&";
Case Else: Debug.Print Mid(input_, i, 1);
End Select
Next i
Debug.Print "</td></tr>" & vbCrLf & "</table>"
End Sub
- Output:
<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>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>
<tr><td>The multitude</td><td>Who are you?</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>
VBScript
Set objfso = CreateObject("Scripting.FileSystemObject")
parent_folder = objfso.GetParentFolderName(WScript.ScriptFullName) & "\"
Set objcsv = objfso.OpenTextFile(parent_folder & "in.csv",1,False)
Set objhtml = objfso.OpenTextFile(paren_folder & "out.html",2,True)
objhtml.Write(csv_to_html(objcsv.ReadAll))
objcsv.Close
objhtml.Close
Set objfso = Nothing
Function csv_to_html(s)
row = Split(s,vbCrLf)
'write the header
tmp = "<html><head><head/><body><table border=1 cellpadding=10 cellspacing=0>"
For i = 0 To UBound(row)
field = Split(row(i),",")
If i = 0 Then
tmp = tmp & "<tr><th>" & replace_chars(field(0)) & "</th><th>" & replace_chars(field(1)) & "</th><tr>"
Else
tmp = tmp & "<tr><td>" & replace_chars(field(0)) & "</td><td>" & replace_chars(field(1)) & "</td><tr>"
End If
Next
'write the footer
tmp = tmp & "</table></body></html>"
csv_to_html = tmp
End Function
Function replace_chars(s)
replace_chars = Replace(Replace(s,"<","<"),">",">")
End Function
- Output:
Format derived from BBC BASIC output.
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! |
Vedit macro language
This solution converts the table in-place in current edit buffer. If a block is highlighted, only the block is converted. If no block highlighted, the entire file is converted.
if (BB < 0) { // block not set
BB(0) // convert entire file
BE(File_Size)
}
// Convert special characters into entities
Replace_Block("&","&", BB, BE, BEGIN+ALL+NOERR)
Replace_Block("<","<", BB, BE, BEGIN+ALL+NOERR)
Replace_Block(">",">", BB, BE, BEGIN+ALL+NOERR)
// Convert CSV into HTML table
Goto_Pos(BB)
IT('<table>') IN
#80 = Cur_Pos
Goto_Pos(BE)
IT("</table>")
#81 = Cur_Line
IN
Goto_Pos(#80)
while (Cur_Line < #81) {
IT(" <tr><td>")
Replace_Block(",","</td><td>",Cur_Pos,EOL_Pos,ALL+NOERR)
EOL
IT("</td></tr>")
Line(1)
}
BB(Clear)
- Output:
<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>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>
<tr><td>The multitude</td><td>Who are you? </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>
Visual Basic .NET
Uses XML literals and the TextFieldParser class of the VB runtime, which can parse delimited or fixed-width files.
The optional first command-line argument denotes whether to use <thead /> for the first row. The optional second argument specifies the path of the CSV file. If no second argument is given, the program reads from the console until stop characters are encountered.
TextFieldParser is designed to work with files and so makes heavy use of peeking, which results in buggy behavior when signaling end-of-file using the console. The most reliable way seems to be alternately pressing enter and Ctrl+Z after the last character of the last line of data.
Imports Microsoft.VisualBasic.FileIO
Module Program
Sub Main(args As String())
Dim parser As TextFieldParser
Try
If args.Length > 1 Then
parser = My.Computer.FileSystem.OpenTextFieldParser(args(1), ",")
Else
parser = New TextFieldParser(Console.In) With {.Delimiters = {","}}
End If
Dim getLines =
Iterator Function() As IEnumerable(Of String())
Do Until parser.EndOfData
Yield parser.ReadFields()
Loop
End Function
Dim result = CSVTOHTML(getLines(), If(args.Length > 0, Boolean.Parse(args(0)), False))
Console.WriteLine(result)
Finally
If parser IsNot Nothing Then parser.Dispose()
End Try
End Sub
Function CSVTOHTML(lines As IEnumerable(Of IEnumerable(Of String)), useTHead As Boolean) As XElement
Dim getRow = Function(row As IEnumerable(Of String)) From field In row Select <td><%= field %></td>
CSVTOHTML =
<table>
<%= From l In lines.Select(
Function(line, i)
If useTHead AndAlso i = 0 Then
Return <thead><%= getRow(line) %></thead>
Else
Return <tr><%= getRow(line) %></tr>
End If
End Function) %>
</table>
End Function
End Module
- Output — for input
true
:
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!
^Z
^Z
<table>
<thead>
<td>Character</td>
<td>Speech</td>
</thead>
<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>
<tr>
<td>The multitude</td>
<td>Who are you?</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>
Wren
var csv =
"Character,Speech\n" +
"The multitude,The messiah! Show us the messiah!\n" +
"Brians mother,<angry>Now you listen here! He's not the messiah; " +
"he's a very naughty boy! Now go away!</angry>\n" +
"The multitude,Who are you?\n" +
"Brians mother,I'm his mother; that's who!\n" +
"The multitude,Behold his mother! Behold his mother!"
var i = " " // indent
var ii = i + i // double indent
var iii = ii + i // triple indent
var sb = "<table>\n%(i)<tr>\n%(ii)<td>"
for (c in csv) {
sb = sb + ((c == "\n") ? "</td>\n%(i)</tr>\n%(i)<tr>\n%(ii)<td>" :
(c == ",") ? "</td>\n%(ii)<td>" :
(c == "&") ? "&" :
(c == "'") ? "'" :
(c == "<") ? "<" :
(c == ">") ? ">" : c)
}
sb = sb + "</td>\n%(i)</tr>\n</table>"
System.print(sb)
System.print()
// now using first row as a table header
sb = "<table>\n%(i)<thead>\n%(ii)<tr>\n%(iii)<td>"
var hLength = csv.indexOf("\n") + 1 // find length of first row including CR
for (c in csv.take(hLength)) {
sb = sb + ((c == "\n") ? "</td>\n%(ii)</tr>\n%(i)</thead>\n%(i)<tbody>\n%(ii)<tr>\n%(iii)<td>" :
(c == ",") ? "</td>\n%(iii)<td>" : c)
}
for (c in csv.skip(hLength)) {
sb = sb + ((c == "\n") ? "</td>\n%(ii)</tr>\n%(ii)<tr>\n%(iii)<td>" :
(c == ",") ? "</td>\n%(iii)<td>" :
(c == "&") ? "&" :
(c == "'") ? "'" :
(c == "<") ? "<" :
(c == ">") ? ">" : c)
}
sb = sb + "</td>\n%(ii)</tr>\n%(i)</tbody>\n</table>"
System.print(sb)
- Output:
Same as Kotlin entry.
XPL0
string 0;
char Input, S;
[Input:=
"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!";
Text(0, "<table>^m^j<tr><td>");
S:= Input;
while S(0) do
[case S(0) of
$0A: Text(0, "</td></tr>^m^j<tr><td>");
^,: Text(0, "</td><td>");
^<: Text(0, "<");
^>: Text(0, ">");
^&: Text(0, "&")
other ChOut(0, S(0));
S:= S+1;
];
Text(0, "</td></tr>^m^j</table>");
]
- Output:
<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>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> <tr><td>The multitude</td><td>Who are you?</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>
XSLT 2.0
Setup
- Download to a local copy of the csv-to-xml.xslt library style-sheet listed in the linked blog entry. Alternatively a copy of the style-sheet on this wiki can be found here: csv-to-xml.xslt
- Pass as the URI of the input csv as a parameter (named url-of-csv) to your XSLT 2.0 processor
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xcsvt="http://www.seanbdurkin.id.au/xslt/csv-to-xml.xslt"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xcsv="http://www.seanbdurkin.id.au/xslt/xcsv.xsd"
version="2.0"
exclude-result-prefixes="xsl xs xcsvt xcsv">
<xsl:import href="csv-to-xml.xslt" />
<xsl:output indent="yes" encoding="UTF-8" method="html" doctype-system="about:legacy-compat" />
<xsl:import-schema schema-location="http://www.seanbdurkin.id.au/xslt/xcsv.xsd"
use-when="system-property('xsl:is-schema-aware')='yes'" />
<xsl:param name="url-of-csv" as="xs:string" select="'roseta.csv'" />
<xsl:variable name="phase-1-result">
<xsl:call-template name="xcsvt:main" />
</xsl:variable>
<xsl:template match="/">
<html lang="en">
<head><title>CSV to HTML translation - Extra Credit</title></head>
<body>
<xsl:apply-templates select="$phase-1-result" mode="phase-2" />
</body>
</html>
</xsl:template>
<xsl:template match="xcsv:comma-separated-single-line-values" mode="phase-2">
<table>
<xsl:apply-templates mode="phase-2" />
</table>
</xsl:template>
<xsl:template match="xcsv:row[1]" mode="phase-2">
<th>
<xsl:apply-templates mode="phase-2" />
</th>
</xsl:template>
<xsl:template match="xcsv:row" mode="phase-2">
<tr>
<xsl:apply-templates mode="phase-2" />
</tr>
</xsl:template>
<xsl:template match="xcsv:value" mode="phase-2">
<td>
<xsl:apply-templates mode="phase-2" />
</td>
</xsl:template>
<xsl:template match="xcsv:notice" mode="phase-2" />
</xsl:stylesheet>
This style-sheet, when applied to the task input, yields...
<!DOCTYPE html
SYSTEM "about:legacy-compat">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>CSV to HTML translation - Extra Credit</title>
</head>
<body>
<table>
<th>
<td>Character</td>
<td>Speech</td>
</th>
<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>
<tr>
<td>The multitude</td>
<td>Who are you?</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>
</body>
</html>
zkl
csvData:=Data(0,Int,"Character,Speech\n"
"The multitude,The messiah! Show us the messiah!\n"
"Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>\n"
"The multitude,Who are you\n"
"Brians mother,I'm his mother; that's who!\n"
"The multitude,Behold his mother! Behold his mother!");
html:=csvData.pump("<table>\n",fcn(line){
line.replace("&","&").replace("<","<") // <angry/> --> <angry/>
.split(",")
.pump("<tr>\n","strip",String.fpM("101"," <td>","</td>\n"))+"</tr>\n"
}) + "</table>";
html.println();
The pump method writes or appends to a sink, in this case a string (eg "
"). The fpM method is function/partial application and takes a string (ones and zeros) to indicate the positional input parameters. So String.fpM("101","a","c")("B") creates a new string "aBc".- Output:
<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>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>
<tr>
<td>The multitude</td>
<td>Who are you</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>
- Programming Tasks
- Solutions by Programming Task
- 11l
- Ada
- AWS
- ALGOL 68
- ANTLR
- Applesoft BASIC
- Arturo
- AutoHotkey
- AutoIt
- AWK
- Batch File
- BBC BASIC
- Befunge
- Bracmat
- C
- C sharp
- C++
- Clojure
- CoffeeScript
- Common Lisp
- D
- Delphi
- EasyLang
- EchoLisp
- Ed
- Erlang
- Euphoria
- F Sharp
- Factor
- Forth
- Fortran
- FreeBASIC
- FutureBasic
- Go
- Groovy
- Haskell
- Icon
- Unicon
- J
- Java
- JavaScript
- Jq
- Jsish
- Julia
- Kotlin
- Lambdatalk
- Liberty BASIC
- Lua
- M2000 Interpreter
- Maple
- Mathematica
- Wolfram Language
- MATLAB
- Maxima
- ML/I
- Nanoquery
- NetRexx
- Nim
- Oberon-2
- Objeck
- OCaml
- OmniMark
- OpenEdge/Progress
- Perl
- Phix
- PHP
- PicoLisp
- PowerShell
- Prolog
- Python
- R
- Racket
- Raku
- Red
- Retro
- REXX
- Ruby
- Run BASIC
- Rust
- Scala
- Org.scala-lang.xml
- Sed
- Seed7
- Sidef
- Tcl
- Tcllib
- TUSCRIPT
- TXR
- UNIX Shell
- VBA
- VBScript
- Vedit macro language
- Visual Basic .NET
- Wren
- XPL0
- XSLT 2.0
- Zkl
- CSV
- HTML
- Pages with too many expensive parser function calls