CSV to HTML translation: Difference between revisions

m
 
(117 intermediate revisions by 48 users not shown)
Line 1:
{{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
 
 
The task is to create a function that takes a string representation of the CSV data
;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!
 
 
For extra credit, ''optionally'' allow special formatting
;Extra credit:
for the first row of the table as if it is the tables header row
''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>’}
‘<’ {‘&lt;’}
‘>’ {‘&gt;’}
‘&’ {‘&amp;’}
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>&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|Ada}}==
Line 24 ⟶ 63:
{{libheader|AWS}}
csv2html.adb:
<langsyntaxhighlight lang=Ada>with Ada.Strings.Fixed;
with Ada.Text_IO;
with Templates_Parser;
Line 63 ⟶ 102:
(Templates_Parser.Parse ("table.tmplt", Translations));
end;
end Csv2Html;</langsyntaxhighlight>
 
table.tmplt:
<langsyntaxhighlight lang=html5><table>
@@TABLE@@
<tr>
Line 73 ⟶ 112:
</tr>
@@END_TABLE@@
</table></langsyntaxhighlight>
 
{{out}}
<langsyntaxhighlight lang=html5><table>
<tr>
<td>Character</td>
Line 101 ⟶ 140:
<td>Behold his mother! Behold his mother!</td>
</tr>
</table></langsyntaxhighlight>
 
=={{header|ALGOL 68}}==
{{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].}}
{{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''.}}
<langsyntaxhighlight lang=algol68>#!/usr/local/bin/a68g --script #
 
[6]STRING rows := []STRING(
Line 174 ⟶ 214:
elbat;
ydob;
lmth</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang=html5><HTML>
<HEAD>
<TITLE>CSV to HTML translation - Extra Credit</TITLE>
Line 194 ⟶ 234:
</TABLE>
</BODY>
</HTML></langsyntaxhighlight>
 
=={{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("<","&lt;").replace(">","&gt;") + "</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) = "&LT;"
240 LET H$(3,0) = "&GT;"
250 LET H$(4,0) = "&AMP;"
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>&lt;angry&gt;Now you listen here! He&apos;s not the messiah; he&apos;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&apos;m his mother; that&apos;s who!</td></tr><tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr></table>
 
=={{header|AutoHotkey}}==
Very basic implementation
<langsyntaxhighlight lang=AutoHotkey>CSVData =
(
Character,Speech
Line 222 ⟶ 340:
return str
}
MsgBox % clipboard := TableData</langsyntaxhighlight>
{{out}}
<pre><table>
Line 234 ⟶ 352:
 
=={{header|AutoIt}}==
<langsyntaxhighlight lang=AutoIt>
Local $ascarray[4] = [34,38,60,62]
$String = "Character,Speech" & @CRLF
Line 263 ⟶ 381:
$newstring &= "</table>"
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $newstring = ' & $newstring & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
</syntaxhighlight>
</lang>
 
{{out}}
Line 294 ⟶ 412:
</tr>
</table></pre>
 
 
 
=={{header|ANTLR}}==
===Java===
<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("<","&lt;").replace(">","&gt;") + "</TD>");};
Field : ~[,\n\r]+;
</lang>
 
=={{header|AWK}}==
Line 316 ⟶ 418:
Includes extra credit.<br />
File csv2html.awk
<langsyntaxhighlight lang=awk>#!/usr/bin/awk -f
BEGIN {
FS=","
Line 339 ⟶ 441:
print "</table>"
}
</syntaxhighlight>
</lang>
 
<pre>$ awk -f csv2html.awk input.csv</pre>
<langsyntaxhighlight lang=html5><table>
<tr>
<td>Character</td>
Line 367 ⟶ 469:
<td>Behold his mother! Behold his mother!</td>
</tr>
</table></langsyntaxhighlight>
 
Extra credit:
 
<pre>$ awk -v header=1 -f csv2html.awk input.csv</pre>
<langsyntaxhighlight lang=html5><table>
<tr>
<th>Character</th>
Line 397 ⟶ 499:
<td>Behold his mother! Behold his mother!</td>
</tr>
</table></langsyntaxhighlight>
 
=={{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:&=&amp;!"
set "var=!var:<=&lt;!"
set "var=!var:>=&gt;!"
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>&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|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight 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>"
Line 443 ⟶ 588:
SYS "ShellExecute", @hwnd%, 0, "CSVtoHTML.htm", 0, 0, 1
</syntaxhighlight>
</lang>
{{out}}
<pre><HTML>
Line 462 ⟶ 607:
{{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>&lt;angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!&lt;/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>&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>
</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>&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>
<tr><td /></tr>
</tbody></table></syntaxhighlight>
 
=={{header|C}}==
 
<langsyntaxhighlight lang=c>#include <stdio.h>
 
const char *input =
Line 493 ⟶ 789:
 
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 500 ⟶ 796:
$ ./csv</pre>
 
<langsyntaxhighlight 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>
Line 507 ⟶ 803:
<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></langsyntaxhighlight>
 
=={{header|C++ sharp|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 ] = { "&lt;" , "&gt;" , " <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>
{{out}}
<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>
</lang>
 
=={{header|C sharp}}==
 
===Simple Solution===
 
<langsyntaxhighlight lang=C sharp>
using System;
using System.Collections.Generic;
Line 591 ⟶ 842:
}
}
</syntaxhighlight>
</lang>
 
{{out}} when using the text suggested:
<langsyntaxhighlight 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&#39;s not the messiah; he&#39;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&#39;m his mother; that&#39;s who!</td></tr><tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr></table>
</syntaxhighlight>
</lang>
 
===Extra Credit Solution===
 
<langsyntaxhighlight lang=C sharp>using System;
using System.Linq;
using System.Net;
Line 653 ⟶ 904:
}
}
}</langsyntaxhighlight>
 
{{out|Sample HTML Output}}
<langsyntaxhighlight lang=html5><table>
<tr>
<th>Character</th>
Line 681 ⟶ 932:
<td>Behold his mother! Behold his mother!</td>
</tr>
</table></langsyntaxhighlight>
 
=={{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 ] = { "&lt;" , "&gt;" , " <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>&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|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
{\< "&lt;", \> "&gt;", \& "&amp;"})
 
(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>&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></tbody></thead>
</syntaxhighlight>
 
=={{header|CoffeeScript}}==
Line 687 ⟶ 1,035:
{{works with|node.js}}
 
<langsyntaxhighlight lang=coffeescript>String::__defineGetter__ 'escaped', () ->
this.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
Line 725 ⟶ 1,073:
</tbody>
</table>
"""</langsyntaxhighlight>
 
{{out}}
<langsyntaxhighlight lang=html5><table cellspacing="0">
<thead>
<th scope="col">Character</th>
Line 745 ⟶ 1,093:
<td>Behold his mother! Behold his mother!</td>
</tbody>
</table></langsyntaxhighlight>
 
 
=={{header|Common Lisp}}==
 
<langsyntaxhighlight 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>
Line 802 ⟶ 1,149:
(dolist (row rows)
(format html "~C~A~C" #\Tab (html-row row nil) #\Newline))
(write-string "</table>" html))))</langsyntaxhighlight>
 
<pre>CL-USER> (csv->html *csv*)</pre>
 
{{out}}
<langsyntaxhighlight 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>
Line 814 ⟶ 1,161:
<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></langsyntaxhighlight>
 
=={{header|D}}==
{{trans|C}}
<langsyntaxhighlight lang=d>void main() {
import std.stdio;
 
Line 854 ⟶ 1,201:
 
"</td></tr>\n</tbody>\n</table>\n\n</body></html>".write;
}</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang=html5><html>
<head><meta charset="utf-8"></head>
<body>
Line 874 ⟶ 1,221:
 
</body>
</html></langsyntaxhighlight>
 
=={{header|Delphi}}==
Line 880 ⟶ 1,227:
This solution solves both the basic and extra credit tasks.
 
<langsyntaxhighlight lang=Delphi>program csv2html;
 
{$APPTYPE CONSOLE}
Line 1,052 ⟶ 1,399:
// Keep console window open
Readln;
end.</langsyntaxhighlight>
 
{{out|Basic output}}
<langsyntaxhighlight lang=html5><!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
Line 1,083 ⟶ 1,430:
</table>
</body>
</html></langsyntaxhighlight>
 
{{out|Extra credit output}}
<langsyntaxhighlight lang=html5><!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
Line 1,114 ⟶ 1,461:
</table>
</body>
</html></langsyntaxhighlight>
 
=={{header|EasyLang}}==
<syntaxhighlight>
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$ = "&lt;"
elif c$ = ">"
c$ = "&gt;"
elif c$ = "&"
c$ = "&amp;"
.
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>&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>
 
=={{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|Erlang}}==
Using functions from [[Create_an_HTML_table]]
<langsyntaxhighlight lang=Erlang>
-module( csv_to_html ).
 
Line 1,139 ⟶ 1,593:
Brians mother,I'm his mother; that's who!
The multitude,Behold his mother! Behold his mother!".
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,147 ⟶ 1,601:
{{trans|C}}
{{works with|Euphoria|4.*}}
<langsyntaxhighlight 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; " &
Line 1,166 ⟶ 1,620:
end switch
end for
puts(1,"</td></tr>\n</table>")</langsyntaxhighlight>
 
{{out}}
<presyntaxhighlight 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>
Line 1,176 ⟶ 1,630:
<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></presyntaxhighlight>
 
=={{header|F_Sharp|F#}}==
Use .NET XmlWriter.
Stylesheet styling is applied only when command line option <tt>-h</tt> ist given.
<langsyntaxhighlight lang=fsharp>open System
open System.Text
open System.Xml
Line 1,234 ⟶ 1,688:
x.WriteEndElement() // tr
x.Close()
0</langsyntaxhighlight>
{{out}} (stylesheet version)
<div style="font-size:70%">
<langsyntaxhighlight lang=html5><?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE HTML >
<html>
Line 1,281 ⟶ 1,735:
</table>
</body>
</html></langsyntaxhighlight></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; ">
&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 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 ." &lt;" ENDOF
[CHAR] > OF ." &gt;" ENDOF
[CHAR] & OF ." &amp;" 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>&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|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 &lt; and &gt; 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 "&lt;";
Case ">": Print "&gt;";
Case "&": Print "&amp;";
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}}==
<langsyntaxhighlight lang=go>package main
 
import (
Line 1,291 ⟶ 2,098:
"fmt"
"html/template"
"strings"
)
 
Line 1,313 ⟶ 2,121:
return "", err
}
var b bytesstrings.BufferBuilder
err = template.Must(template.New("").Parse(`<table>
{{range .}} <tr>{{range .}}<td>{{.}}</td>{{end}}</tr>
Line 1,319 ⟶ 2,127:
`)).Execute(&b, data)
return b.String(), err
}</langsyntaxhighlight>
Extra credit version accepts -h command line option to do the special formatting for the heading line.
<langsyntaxhighlight lang=go>package main
 
import (
Line 1,329 ⟶ 2,137:
"fmt"
"html/template"
"strings"
)
 
Line 1,357 ⟶ 2,166:
tStr = tHeadings
}
var b bytesstrings.BufferBuilder
err = template.Must(template.New("").Parse(tStr)).Execute(&b, data)
return b.String(), err
Line 1,376 ⟶ 2,185:
</table>
`
)</langsyntaxhighlight>
{{out}}
Extra credit version with -h
<langsyntaxhighlight lang=html><table>
<thead>
<tr><th>Character</th><th>Speech</th></tr>
Line 1,390 ⟶ 2,199:
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</tbody>
</table></langsyntaxhighlight>
Basic version, or extra credit version without -h
<langsyntaxhighlight lang=html><table>
<tr><td>Character</td><td>Speech</td></tr>
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
Line 1,399 ⟶ 2,208:
<tr><td>Brians mother</td><td>I&#39;m his mother; that&#39;s who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table></langsyntaxhighlight>
 
=={{header|Groovy}}==
Line 1,405 ⟶ 2,214:
===Solution #1: Nested GStrings===
Brute force solution using nested GStrings. It solves both the basic and extra credit tasks.
<langsyntaxhighlight lang=groovy>def formatCell = { cell ->
"<td>${cell.replaceAll('&','&amp;').replaceAll('<','&lt;')}</td>"
}
Line 1,442 ⟶ 2,251:
<body>${formatTable(csv, header)}</body>
</html>"""
}</langsyntaxhighlight>
 
'''Test:'''
<langsyntaxhighlight lang=groovy>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>
Line 1,461 ⟶ 2,270:
println '-----------------------------------------'
println (formatPage('Extra Credit', csv, true))
println '-----------------------------------------'</langsyntaxhighlight>
 
{{out|Basic output}}
<div style="height:30ex;overflow:scroll;"><langsyntaxhighlight lang=html5><html>
<head>
<title>Basic</title>
Line 1,482 ⟶ 2,291:
</table>
</body>
</html></langsyntaxhighlight></div>
[[File:Groovy-csv-to-html-basic.jpg]] <br>Appearance as rendered in Google Chrome.
 
{{out|Extra Credit output}}
<div style="height:30ex;overflow:scroll;"><langsyntaxhighlight lang=html5><html>
<head>
<title>Extra Credit</title>
Line 1,508 ⟶ 2,317:
</table>
</body>
</html></langsyntaxhighlight></div>
[[File:Groovy-csv-to-html-extra.jpg]] <br>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.
<langsyntaxhighlight lang=groovy>import groovy.xml.MarkupBuilder
 
def formatRow = { doc, row ->
Line 1,542 ⟶ 2,351:
}
writer.toString()
}</langsyntaxhighlight>
 
'''Test:'''<br/>
Line 1,548 ⟶ 2,357:
 
{{out|Basic output}}
<div style="height:30ex;overflow:scroll;"><langsyntaxhighlight lang=html5><html>
<head>
<title>Basic</title>
Line 1,584 ⟶ 2,393:
</table>
</body>
</html></langsyntaxhighlight></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.
 
'''Extra Credit output:'''
<div style="height:30ex;overflow:scroll;"><langsyntaxhighlight lang=html5><html>
<head>
<title>Extra Credit</title>
Line 1,629 ⟶ 2,438:
</table>
</body>
</html></langsyntaxhighlight></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.
Line 1,635 ⟶ 2,444:
=={{header|Haskell}}==
'''Simple solution'''
<langsyntaxhighlight lang=haskell>--import Data.List.Split (splitOn) -- if the import is available
splitOn :: Char -> String -> [String] -- otherwise
splitOn delim = foldr (\x rest ->
Line 1,660 ⟶ 2,469:
in "<table>\n" ++ html ++ "</table>"
 
main = interact csvToTable</langsyntaxhighlight>
 
'''Compact version'''
<langsyntaxhighlight lang=haskell>import Data.List (unfoldr)
split p = unfoldr (\s -> case dropWhile p s of [] -> Nothing
ss -> Just $ break p ss)
Line 1,673 ⟶ 2,482:
'&' -> "&amp;"; '"' -> "&quot;"; _ -> [c]}) x
++ "</td>\n") cols)
++ "</tr>") . split (==',')) $ lines csv) ++ "</table>")</langsyntaxhighlight>
 
;Output:
<div style="height:30ex;overflow:scroll;"><langsyntaxhighlight lang=html5><table>
<tr>
<td>Character</td>
Line 1,701 ⟶ 2,510:
<td>Behold his mother! Behold his mother!</td>
</tr>
</table></langsyntaxhighlight></div>
 
=={{header|Icon}} and {{header|Unicon}}==
Line 1,709 ⟶ 2,518:
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]]).
 
<langsyntaxhighlight lang=Icon>procedure main(arglist)
pchar := &letters ++ &digits ++ '!?;. ' # printable chars
 
Line 1,728 ⟶ 2,537:
write(" </TBODY>")
write("</TABLE>")
end</langsyntaxhighlight>
 
{{Out}}
<langsyntaxhighlight lang=html5><TABLE>
<THEAD>
<TR><TD>Character</TD><TD>Speech</TD></TR>
Line 1,742 ⟶ 2,551:
<TR><TD>The multitude</TD><TD>Behold his mother! Behold his mother!</TD></TR>
</TBODY>
</TABLE></langsyntaxhighlight>
 
=={{header|J}}==
'''Solution (extra credit)'''
<langsyntaxhighlight lang=j>require 'strings tables/csv'
encodeHTML=: ('&';'&amp;';'<';'&lt;';'>';'&gt;')&stringreplace
 
Line 1,767 ⟶ 2,576:
end.
;markupTable markupRows t
)</langsyntaxhighlight>
 
For those interested, equivalent tacit versions of <code>tag</code> and <code>makeHTMLtablefromCSV</code> are:
<langsyntaxhighlight lang=j>tag=: adverb def '[: (,&.>/)"1 m&(0&{::@[ , 1&{::@[ ,~ ]) L:0@]'
makeHTMLtablefromCSV6=: 0&$: : ([: ; markupTable@markupRows@([ markupCells`(markupHdrCells@{. , markupCells@}.)@.[ fixcsv@encodeHTML))</langsyntaxhighlight>
 
'''Example'''
<langsyntaxhighlight lang=j> CSVstrng=: noun define
Character,Speech
The multitude,The messiah! Show us the messiah!
Line 1,782 ⟶ 2,591:
The multitude,Behold his mother! Behold his mother!
)
1 makeHTMLtablefromCSV CSVstrng</langsyntaxhighlight>
{{Out|HTML output}}
<langsyntaxhighlight 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>
Line 1,791 ⟶ 2,600:
<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></langsyntaxhighlight>
 
=={{header|Java}}==
'''Simple solution without escaping for Java v8+'''
 
<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'''
 
<tt>for simple solution : java -cp . Csv2Html < text.csv</tt>
 
<tt>for extended solution: java -cp . Csv2Html header < text.csv</tt>
 
<langsyntaxhighlight lang=java>import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
Line 1,886 ⟶ 2,706:
}
}
</syntaxhighlight>
</lang>
 
'''output {{out|simple solution'''}}
<langsyntaxhighlight 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"/>
Line 1,906 ⟶ 2,726:
<tr><td>Brians mother</td><td>I&apos;m his mother; that&apos;s who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table></body></html></langsyntaxhighlight>
 
'''output {{out|extended'''}}
<langsyntaxhighlight 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"/>
Line 1,926 ⟶ 2,746:
<tr><td>Brians mother</td><td>I&apos;m his mother; that&apos;s who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table></body></html></langsyntaxhighlight>
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang=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" +
Line 1,948 ⟶ 2,768:
'\t</tbody>\n</table>');
 
</syntaxhighlight>
</lang>
 
<langsyntaxhighlight lang=html5><table>
<thead>
<tr><td>Character</td><td>Speech</td></tr>
Line 1,961 ⟶ 2,781:
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</tbody>
</table></langsyntaxhighlight>
 
{{out}}
No escaping:
<syntaxhighlight lang=html5><table>
{{output?|JavaScript}}
<thead>
<lang javascript>//uses native DOM calls to construct HTML elements
<tr><td>Character</td><td>Speech</td></tr>
function csv_to_table(s) {
</thead>
function ce(t) { return document.createElement(t); }
<tbody>
function ap(t) { document.body.appendChild(t); }
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
var t = ce('table'), f = 0; //1;
<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>
s.split('\n').map(function(l) {
<tr><td>The multitude</td><td>Who are you?</td></tr>
var r = ce(f ? 'tr': 'thead');
<tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr>
l.split(',').map(function (w) {
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr> </tbody>
var c = ce(f ? 'td' : 'th');
</table></syntaxhighlight>
c.textContent = w;
r.appendChild(c);
});
t.appendChild(r);
f = 1; //0;
});
//return t.innerHTML;
return t.outerHTML;
}
/*
but also with this changes is very dependent by javascript engine and/or browser version (in: IE>=9, chrome )
*/</lang>
 
=={{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:
<langsyntaxhighlight lang=jq>jq -R . csv2html.csv | jq -r -s -f csv2html.jq
</langsyntaxhighlight><syntaxhighlight lang =jq>def headerrow2html:
[" <thead> <tr>"]
+ (split(",") | map(" <th>\(@html)</th>"))
Line 2,011 ⟶ 2,820:
;
 
csv2html | .[]</langsyntaxhighlight>
 
===Output===
<langsyntaxhighlight lang=html5><table>
<thead> <tr>
<th>Character</th>
Line 2,039 ⟶ 2,848:
<td>Behold his mother! Behold his mother! </td>
</tr>
</table></langsyntaxhighlight>
 
=={{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, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.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>&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> </tbody>
</table>
=!EXPECTEND!=
*/</syntaxhighlight>
 
{{out}}
<pre>prompt$ jsish -u csvToHTML.jsi
[PASS] csvToHTML.jsi</pre>
 
=={{header|Julia}}==
<syntaxhighlight lang=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))
</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>"
'&' -> "&amp;"
'\'' -> "&apos;"
'<' -> "&lt;"
'>' -> "&gt;"
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>"
'&' -> "&amp;"
'\'' -> "&apos;"
'<' -> "&lt;"
'>' -> "&gt;"
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>&lt;angry&gt;Now you listen here! He&apos;s not the messiah; he&apos;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&apos;m his mother; that&apos;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>&lt;angry&gt;Now you listen here! He&apos;s not the messiah; he&apos;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&apos;m his mother; that&apos;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>
<lang lb>
newline$ ="|"
' No escape behaviour, so can't refer to '/n'.
Line 2,079 ⟶ 3,211:
print "</HTML>"
end
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 2,107 ⟶ 3,239:
 
=={{header|Lua}}==
<langsyntaxhighlight lang=lua>FS = "," -- field separator
 
csv = [[
Line 2,134 ⟶ 3,266:
for _, line in pairs(html) do
print(line)
end</langsyntaxhighlight>
 
<langsyntaxhighlight 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>
Line 2,143 ⟶ 3,275:
<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></langsyntaxhighlight>
 
=={{header|M2000 Interpreter}}==
 
<syntaxhighlight lang="m2000 interpreter">
module csv2html {
nl$={
}
Repl$=lambda$ (a$) ->{
a$=replace$("&", "&amp;",a$)
a$=replace$(">", "&gt;",a$)
a$=replace$("""", "&quot;",a$)
// add any other replacement here
=replace$("<", "&lt;",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>&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>
</body>
</html>
</pre>
 
=={{header|Maple}}==
<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("&lt;")
elif char = ">" then
printf("&gt;")
elif char = "&" then
printf("&amp;")
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!");
</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>&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|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight 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>
Line 2,162 ⟶ 3,454:
StringSplit[StringReplace[a,{","->"</td><td>","<"->"&lt;",">"->"&gt;"}],"\n"]//Rest]
,"</table>"]
</syntaxhighlight>
</lang>
{{Out|Naive}}
<pre>
Line 2,193 ⟶ 3,485:
split it into a cell array, and print it out specifying format.
 
<langsyntaxhighlight lang=MATLAB>
inputString = fileread(csvFileName);
% using multiple regular expressions to clear up special chars
Line 2,209 ⟶ 3,501:
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>
</lang>
As a single line:
<langsyntaxhighlight lang=MATLAB>
fprintf(1,['<table>\n\t<tr>\n\t\t<th>',regexprep(regexprep(regexprep(regexprep(regexprep(regexprep(regexprep(regexprep(fileread(cvsFileName),'&','&amp;'),'"','&quot;'),'<','&lt;'),'>','&gt;'),'(?<=(^[^\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>
</lang>
 
{{Out}}
<langsyntaxhighlight lang=html5>
<table>
<tr>
Line 2,242 ⟶ 3,534:
</tr>
</table>
</syntaxhighlight>
</lang>
 
=={{header|Maxima}}==
<langsyntaxhighlight lang=Maxima>infile: "input.csv";
outfile: "table.html";
instream: openr(infile);
Line 2,265 ⟶ 3,557:
 
close(instream);
close(outstream);</langsyntaxhighlight>
 
<langsyntaxhighlight 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>
Line 2,274 ⟶ 3,566:
<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></langsyntaxhighlight>
 
=={{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.
===Input===
<langsyntaxhighlight lang=ML/I>MCSKIP "WITH" NL
"" CSV to HTML
"" assumes macros on input stream 1, terminal on stream 2
Line 2,332 ⟶ 3,624:
</body>
</html>
]</langsyntaxhighlight>
===Output===
<langsyntaxhighlight lang=ML/I><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
Line 2,384 ⟶ 3,676:
</table>
</body>
</html></langsyntaxhighlight>
 
=={{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.
<langsyntaxhighlight lang=NetRexx>/* NetRexx */
options replace format comments java crossref symbols nobinary
 
Line 2,508 ⟶ 3,834:
|| ''
return html
</syntaxhighlight>
</lang>
'''Output:'''
<div style="height:30ex;overflow:scroll;">
<langsyntaxhighlight 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">
Line 2,594 ⟶ 3,920:
</body>
</html>
</syntaxhighlight>
</lang>
</div>
 
Line 2,601 ⟶ 3,927:
[[File:RCsv2HtmlNetRexx.png]]
 
=={{header|NimrodNim}}==
{{trans|Python}}
<langsyntaxhighlight nimrodlang=nim>import cgi, strutils
 
const csvtext = """Character,Speech
Line 2,612 ⟶ 3,938:
The multitude,Behold his mother! Behold his mother!"""
 
proc row2tr(row: string): string =
result = "<tr>"
let cols = xmlEncode(row).split(",")
Line 2,619 ⟶ 3,945:
result.add "</tr>"
 
proc csv2html(txt: string): string =
result = "<table summary=\"csv2html program output\">\n"
for row in txt.splitLines():
Line 2,625 ⟶ 3,951:
result.add "</table>"
 
echo csv2html(csvtext)</langsyntaxhighlight>
{{Out}}
<pre><table summary="csv2html program output">
Line 2,636 ⟶ 3,962:
</table></pre>
 
=={{header|OCamlOberon-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>&lt;angry&gt;Now you listen here! He&apos;s not the messiah; he&apos;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&apos;m his mother; that&apos;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("&amp;");
}
 
label '\'': {
out->Append("&apos;");
}
 
label '<': {
out->Append("&lt;");
}
 
label '>': {
out->Append("&gt;");
}
 
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>&lt;angry&gt;Now you listen here! He&apos;s not the messiah; he&apos;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&apos;m his mother; that&apos;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===
 
Line 2,643 ⟶ 4,136:
but we do not use it hereafter because the CSV data does not contain comas.
 
<langsyntaxhighlight lang=ocaml>open Printf
 
let csv_data = "\
Line 2,677 ⟶ 4,170:
 
let () =
print_html_table (list_of_csv csv_data)</langsyntaxhighlight>
 
{{Out|Sample html output}}
<langsyntaxhighlight 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>
Line 2,687 ⟶ 4,180:
<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> </langsyntaxhighlight>
 
===Extra credit solution===
 
<langsyntaxhighlight lang=ocaml>open Printf
 
let csv_data = "\
Line 2,734 ⟶ 4,227:
 
let () =
print_html_table (list_of_csv csv_data)</langsyntaxhighlight>
 
{{out}}
<langsyntaxhighlight lang=html5><html>
<head>
<style type="text/css">
Line 2,753 ⟶ 4,246:
</tbody>
</table>
</html></langsyntaxhighlight>
 
=={{header|OpenEdge/Progress}}==
<langsyntaxhighlight lang=Progress (OpenEdge ABL)>
FUNCTION csvToHtml RETURNS CHARACTER (
i_lhas_header AS LOGICAL,
Line 2,806 ⟶ 4,299:
"The multitude,Behold his mother! Behold his mother!"
)
VIEW-AS ALERT-BOX.</langsyntaxhighlight>
{{out|with header enabled}}
<langsyntaxhighlight lang=html><html>
<table>
<tr>
Line 2,835 ⟶ 4,328:
</tr>
</table>
</html></langsyntaxhighlight>
 
=={{header|Perl}}==
Line 2,841 ⟶ 4,334:
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>.
 
<langsyntaxhighlight lang=perl>use HTML::Entities;
 
sub row {
Line 2,855 ⟶ 4,348:
row @ARGV ? 'th' : 'td', $first;
row 'td', $_ foreach @rest;
print "</table>\n";</langsyntaxhighlight>
 
{{Out|Output (with a command-line argument):}}
<syntaxhighlight lang=html5><table>
 
<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>
Line 2,866 ⟶ 4,358:
<tr><td>Brians mother</td><td>I&#39;m his mother; that&#39;s who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table></langsyntaxhighlight>
 
=={{header|Perl 6Phix}}==
Based on [[CSV_to_HTML_translation#Euphoria|Euphoria]] but with a simpler multiline constant.
 
<!--<syntaxhighlight lang="phix">(phixonline)-->
A very lispy solution:
<span style="color: #008080;">with javascript_semantics</span>
<lang perl6>my $str = "Character,Speech
<span style="color: #008080;">constant</span> <span style="color: #000000;">input</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
The multitude,The messiah! Show us the messiah!
Character,Speech
Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>
The multitude,WhoThe messiah! Show us arethe you?messiah!
Brians mother,&lt;angry&gt;Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!&lt;/angry&gt;
Brians mother,I'm his mother; that's who!
The multitude,BeholdWho hisare mother! Behold his mother!";you?
Brians mother,I'm his mother; that's who!
 
The multitude,Behold his mother! Behold his mother!"""</span>
# comment the next line out, if you want to read from standard input instead of the hard-coded $str above
# my $str = $*IN.slurp;
<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;table&gt;\n&lt;tr&gt;&lt;td&gt;"</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>
my &escape = *.trans([ <& < \>>] => [<&amp; &lt; &gt;> ]); # a function with one argument that escapes the entities
<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>
my &tag = {"<$^tag>"~$^what~"</$^tag>"};
<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;">"&lt;/td&gt;&lt;/tr&gt;\n&lt;tr&gt;&lt;td&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;">"&lt;/td&gt;&lt;td&gt;"</span><span style="color: #0000FF;">)</span>
printf
<span style="color: #008080;">case</span> <span style="color: #008000;">'&lt;'</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;lt;"</span><span style="color: #0000FF;">)</span>
'<!DOCTYPE html>
<span style="color: #008080;">case</span> <span style="color: #008000;">'&gt;'</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;gt;"</span><span style="color: #0000FF;">)</span>
<html>
<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;amp;"</span><span style="color: #0000FF;">)</span>
<head><title>Some Text</title></head>
<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>
<body><table>
<span style="color: #008080;">end</span> <span style="color: #008080;">switch</span>
%s
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
</table></body></html>
<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;/td&gt;&lt;/tr&gt;\n&lt;/table&gt;"</span><span style="color: #0000FF;">)</span>
', [~] # concatenation reduction ('a', 'b', 'c') → 'abc'
<!--</syntaxhighlight>-->
(escape($str).split(/\n/) # escape the string and split at newline
{{out}}
==> map -> $line {tag 'tr', # feed that into a map, that map function will tag as 'tr, and has an argument called $line
<pre>
([~] $line.split(/','/)\ # split $line at ',',
&lt;table&gt;
# that / at the end is just an unspace, you can omit it, but then you have to delete
&lt;tr&gt;&lt;td&gt;Character&lt;/td&gt;&lt;td&gt;Speech&lt;/td&gt;&lt;/tr&gt;
# all whitespace and comments between split(…) and .map
&lt;tr&gt;&lt;td&gt;The multitude&lt;/td&gt;&lt;td&gt;The messiah! Show us the messiah!&lt;/td&gt;&lt;/tr&gt;
.map({tag 'td', $^cell}))})\ # map those cells as td
&lt;tr&gt;&lt;td&gt;Brians mother&lt;/td&gt;&lt;td&gt;&amp;lt;angry&amp;gt;Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!&amp;lt;/angry&amp;gt;&lt;/td&gt;&lt;/tr&gt;
.join("\n"); # append a newline for nicer output</lang>
&lt;tr&gt;&lt;td&gt;The multitude&lt;/td&gt;&lt;td&gt;Who are you?&lt;/td&gt;&lt;/tr&gt;
 
&lt;tr&gt;&lt;td&gt;Brians mother&lt;/td&gt;&lt;td&gt;I'm his mother; that's who!&lt;/td&gt;&lt;/tr&gt;
Output:
&lt;tr&gt;&lt;td&gt;The multitude&lt;/td&gt;&lt;td&gt;Behold his mother! Behold his mother!&lt;/td&gt;&lt;/tr&gt;
<lang html><!DOCTYPE html>
&lt;/table&gt;</pre>
<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>&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></body></html></lang>
 
=={{header|PHP}}==
Line 2,939 ⟶ 4,422:
 
echo convert($csv);
</syntaxhighlight>
</lang>
 
=={{header|PicoLisp}}==
===Simple solution===
<langsyntaxhighlight lang=PicoLisp>(load "@lib/http.l")
 
(in "text.csv"
Line 2,950 ⟶ 4,433:
(while (split (line) ",")
(<row> NIL (ht:Prin (pack (car @))) (ht:Prin (pack (cadr @))))
(prinl) ) ) )</langsyntaxhighlight>
{{Out}}
Output:
<langsyntaxhighlight lang=html5><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>
Line 2,959 ⟶ 4,442:
<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></langsyntaxhighlight>
 
===Extra credit solution===
<langsyntaxhighlight lang=PicoLisp>(load "@lib/http.l")
 
(in "text.csv"
Line 2,969 ⟶ 4,453:
(while (split (line) ",")
(<row> NIL (ht:Prin (pack (car @))) (ht:Prin (pack (cadr @))))
(prinl) ) ) ) )</langsyntaxhighlight>
{{Out}}
Output:
<langsyntaxhighlight 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>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>
Line 2,977 ⟶ 4,461:
<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></langsyntaxhighlight>
 
=={{header|PowerShell}}==
===Simple solution===
<langsyntaxhighlight lang=Powershell>
Import-Csv -Path .\csv_html_test.csv | ConvertTo-Html -Fragment | Out-File .\csv_html_test.html
</syntaxhighlight>
</lang>
{{Out}}
Output:
<langsyntaxhighlight lang=html5><table>
<colgroup>
<col/>
Line 2,997 ⟶ 4,481:
<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></langsyntaxhighlight>
===Extra credit solution===
<langsyntaxhighlight lang=Powershell>
$htmlformat = '<title>Csv to Html</title>'
$htmlformat += '<style type="text/css">'
Line 3,009 ⟶ 4,493:
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
</syntaxhighlight>
</lang>
{{Out}}
Output:
<langsyntaxhighlight 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">
Line 3,033 ⟶ 4,517:
</table>
</body></html>
</syntaxhighlight>
</lang>
 
=={{header|Prolog}}==
Uses DCG. Works with SWI-Prolog.<br>
{{Works with|SWI-Prolog}}
===Simple solution===
<langsyntaxhighlight lang=Prolog>csv_html :-
L = "Character,Speech
The multitude,The messiah! Show us the messiah!
Line 3,091 ⟶ 4,576:
csv_td_in(T, S).
 
</syntaxhighlight>
</lang>
{{out}}
OutPut :
<langsyntaxhighlight lang=html5><TABLE>
<TR>
<TD>Character</TD><TD>Speech</TD>
Line 3,113 ⟶ 4,598:
</TR>
</TABLE>
</syntaxhighlight>
</lang>
===Extra credit solution===
<langsyntaxhighlight lang=Prolog>csv_html_plus :-
L =
"Character,Speech
Line 3,205 ⟶ 4,690:
[H],
csv_body_td_in(T, S).
</syntaxhighlight>
</lang>
{{out}}
Output :
<langsyntaxhighlight lang=html5><TABLE>
<THEAD>
<TR>
Line 3,231 ⟶ 4,716:
</TBODY>
</TABLE>
</syntaxhighlight>
</lang>
 
===HTML outputs rendered in firefox browser===
Line 3,239 ⟶ 4,724:
 
=={{header|Python}}==
(Note: rendered versions of bothall three outputs are shown at the foot of this section).
===Simple solution===
<langsyntaxhighlight lang=python>csvtxt = '''\
Character,Speech
The multitude,The messiah! Show us the messiah!
Line 3,268 ⟶ 4,753:
 
htmltxt = csv2html(csvtxt)
print(htmltxt)</langsyntaxhighlight>
 
'''Sample HTML output'''
 
<langsyntaxhighlight lang=html5><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>
Line 3,279 ⟶ 4,764:
<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></langsyntaxhighlight>
 
===Extra credit solution===
<langsyntaxhighlight lang=python>def _row2trextra(row, attr=None):
cols = escape(row).split(',')
attr_tr = attr.get('TR', '')
Line 3,311 ⟶ 4,796:
)
)
print(htmltxt)</langsyntaxhighlight>
 
'''Sample HTML output'''
Line 3,317 ⟶ 4,802:
The raw HTML would not render correctly through the wiki interface but shows a suitably coloured table with cell borders.
-->
<langsyntaxhighlight 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></TBODY>
Line 3,324 ⟶ 4,809:
<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></langsyntaxhighlight>
 
===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>&lt;TR&gt;</code> element in its own <code>&lt;TBODY&gt;</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>&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></TBODY></TABLE></syntaxhighlight>
 
===HTML outputs rendered in firefox browser===
[[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:
<langsyntaxhighlight lang=racket>#lang racket
 
(define e.g.-CSV
Line 3,359 ⟶ 4,931:
 
(require xml)
(display (xexpr->string (CSV-lines->HTML-table e.g.-CSV)))</langsyntaxhighlight>
 
'''Sample HTML output:'''
 
<langsyntaxhighlight 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>&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>
Line 3,369 ⟶ 4,941:
</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></langsyntaxhighlight>
 
=={{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(« & < > » => « &amp; &lt; &gt; »); # 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>&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></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 "<" "&lt;" ;; escape "<" and ">" characters
replace/all first arr ">" "&gt;"
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>&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>
'''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>&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>
 
=={{header|Retro}}==
<langsyntaxhighlight lang=Retro>remapping off
"Character,Speech
The multitude,The messiah! Show us the messiah!
Line 3,394 ⟶ 5,074:
"</td></tr>\n</table>" puts ;
 
CSV displayHTML</langsyntaxhighlight>
 
{{Out}}
Ouput:
<syntaxhighlight lang=html5><table>
 
<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>
Line 3,405 ⟶ 5,084:
<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></langsyntaxhighlight>
 
=={{header|REXX}}==
Line 3,411 ⟶ 5,090:
:::* &nbsp; file:///c:/output.html
:::* &nbsp; file:///c:/outputh.html
<langsyntaxhighlight lang=rexx>/*REXX program toconverts convert CSV ───► HTML table representing the CSV data. */
arg header_ . /*seeobtain ifan theuppercase userversion wantsof a headerargs. */
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 iFID= 'CSV_HTML.TXT' /*read the rows from inputa (text/txt) fileIDfile. */
row.rows= strip( linein(iFID) )
if wantsHdr then oFID= 'OUTPUTH.HTML' /*output fileID with header, */
else oFID= 'OUTPUT.HTML' /* " " without header. */
 
do rows=0 while lines(iFID)\==0 /*read the rows from a (txt) file*/
row.rows=strip(linein(iFID))
end /*rows*/
 
convFrom= '& < > "' /*special characters to convertbe converted. */
convTo = '&amp; &lt; &gt; &quot;' /*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">"
Line 3,444 ⟶ 5,123:
call write , '</table>'
call write , '</html>'
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────WRITE subroutine────────────────────*/
write: call lineout oFID, left('', 0 || arg(1) )arg(2); return</langsyntaxhighlight>
Some older REXXes don't have a &nbsp; '''changestr''' &nbsp; BIF, so one is included here &nbsp; ──► &nbsp; [[CHANGESTR.REX]]. <br>
 
'''output'''
<pre>
<pre style="height:40ex">
<html>
<table border=4 cellpadding=9 cellspacing=1>
Line 3,475 ⟶ 5,154:
</html>
</pre>
'''rendered output''' <br> <br>
<table border=4 cellpadding=9 cellspacing=1>
<tr>
Line 3,497 ⟶ 5,176:
<tr>
</table>
<br>
'''output''' (extra credit solution),
<br>'''output''' &nbsp; (extra credit solution) &nbsp; when the first argument is HEADER in upper/lower/mixed case (with/without leading/trailing blanks).
<pre>
<br>in upper/lower/mixed case, with/without leading/trailing blanks.
<pre style="height:40ex">
<html>
<table border=4 cellpadding=9 cellspacing=1>
Line 3,525 ⟶ 5,203:
</html>
</pre>
<br>
'''rendered output'''
'''rendered output''' <br> <br>
<table border=4 cellpadding=9 cellspacing=1>
<tr>
Line 3,547 ⟶ 5,226:
<tr>
</table>
 
<br>
 
=={{header|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:
To output a header, simply add "header" to the command line:
ruby csv2html.rb header
I/O is done through standard input/output.
<langsyntaxhighlight lang=ruby>require 'cgi'
 
puts '<table summary="csv2html program output">'
Line 3,568 ⟶ 5,250:
end
 
puts "</table>"</lang>Sample output:<lang html5><table summary="csv2html program output"syntaxhighlight>
{{Out}}
<syntaxhighlight lang=html5><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>
Line 3,575 ⟶ 5,259:
<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></langsyntaxhighlight>
 
=={{header|Run BASIC}}==
The extra credit version has 2 extra lines of code to get the heading.
<langsyntaxhighlight 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>
Line 3,602 ⟶ 5,286:
k = instr(strRep$,rep$)
WEND
END FUNCTION</langsyntaxhighlight>
{{Out}}
----- Output ------
<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>
Line 3,610 ⟶ 5,294:
<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!("&lt;"),
'>' => print!("&gt;"),
'&' => print!("&amp;"),
_ => 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>&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|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.<langsyntaxhighlight lang=scala>object CsvToHTML extends App {
val header = <head>
<title>CsvToHTML</title>
Line 3,654 ⟶ 5,373:
 
println(csv2html(csv, true))
}</langsyntaxhighlight>{{out}}<syntaxhighlight lang =html><html>
<head>
<title>CsvToHTML</title>
Line 3,703 ⟶ 5,422:
</table>
</body>
</html></langsyntaxhighlight>
 
=={{header|Sed}}==
 
File csv2html.sed
<langsyntaxhighlight lang=sed>#!/bin/sed -f
 
s|<|\&lt;|g
Line 3,716 ⟶ 5,435:
s|$|</td>\n </tr>|
1s|^|<table>\n|
$s|$|\n</table>|</langsyntaxhighlight>
 
<pre>$ sed -f csv2html.sed input.csv</pre>
 
<langsyntaxhighlight lang=html5><table>
<tr>
<td>Character</td>
Line 3,745 ⟶ 5,464:
<td>Behold his mother! Behold his mother!</td>
</tr>
</table></langsyntaxhighlight>
 
=={{header|Seed7}}==
Line 3,751 ⟶ 5,470:
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 ''&amp;lt;''.
<langsyntaxhighlight lang=seed7>$ include "seed7_05.s7i";
include "html_ent.s7i";
 
Line 3,780 ⟶ 5,499:
end for;
writeln("</table>");
end func;</langsyntaxhighlight>
 
{{Out}}
Output:
<langsyntaxhighlight lang=html5>
<table>
<tr><th>Character</th><th>Speech</th></tr>
Line 3,793 ⟶ 5,512:
<tr><td></td></tr>
</table>
</syntaxhighlight>
</lang>
 
Output{{Out}} viewed with a browser:
<table>
<tr><th>Character</th><th>Speech</th></tr>
Line 3,805 ⟶ 5,524:
<tr><td></td></tr>
</table>
 
=={{header|Sidef}}==
<syntaxhighlight lang=ruby>func escape(str) { str.trans(« & < > », « &amp; &lt; &gt; ») }
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>&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></body></html></syntaxhighlight>
 
=={{header|Tcl}}==
Line 3,810 ⟶ 5,573:
{{tcllib|html}}
{{tcllib|struct::queue}}
<langsyntaxhighlight lang=tcl>package require Tcl 8.5
package require csv
package require html
Line 3,833 ⟶ 5,596:
}]
[html::closeTag]
}]</langsyntaxhighlight>
 
Extra credit version:
<langsyntaxhighlight lang=tcl>package require Tcl 8.5
package require csv
package require html
Line 3,880 ⟶ 5,643:
}]
}]
}]</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang=html5><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>
Line 3,889 ⟶ 5,652:
<tr bgcolor="orange"><td>Brians mother</td><td>I&#39;m his mother; that&#39;s who!</td></tr>
<tr bgcolor="orange"><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table></langsyntaxhighlight>
 
=={{header|TUSCRIPT}}==
<langsyntaxhighlight lang=tuscript>
$$ MODE TUSCRIPT
MODE DATA
Line 3,931 ⟶ 5,694:
WRITE html "</table></body></html>"
ENDACCESS/PRINT html
</syntaxhighlight>
</lang>
=== Output (source code) ===
<langsyntaxhighlight lang=html5><!DOCTYPE html system>
<html>
<head>
Line 3,948 ⟶ 5,711:
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table></body></html>
</syntaxhighlight>
</lang>
=== Output (rendered) ===
[[file:tuscript_csv2html.png|500px|thumb|none|rendered by browser ]]
 
 
=={{header|TXR}}==
Line 3,957 ⟶ 5,719:
====Simple====
 
<langsyntaxhighlight lang=txr>@(collect)
@char,@speech
@(end)
Line 3,969 ⟶ 5,731:
@ (end)
</table>
@(end)</langsyntaxhighlight>
 
{{Out}}
<pre>$ txr csv.txr csv.txt</pre>
 
<langsyntaxhighlight lang=html5><table>
<tr>
<td>Character</td>
Line 3,999 ⟶ 5,761:
<td>Behold his mother! Behold his mother!</td>
</tr>
</table></langsyntaxhighlight>
 
====With Styling====
 
<langsyntaxhighlight lang=txr>@(collect)
@char,@speech
@(end)
Line 4,031 ⟶ 5,793:
@ (end)
</table>
@(end)</langsyntaxhighlight>
 
{{Out}}
<pre>$ txr csv2.txr csv.txt</pre>
<langsyntaxhighlight lang=html5><style type="text/css">
tr.odd td {
background-color: #CC9999; color: black;
Line 4,071 ⟶ 5,833:
<td>Behold his mother! Behold his mother!</td>
</tr>
</table></langsyntaxhighlight>
 
=={{header|UNIX Shell}}==
{{works with|bash}}
<syntaxhighlight lang=bash>csv2html() {
{{works with|ksh}}
<lang bash>csv2html() {
IFS=,
echo "<table>"
 
echo "<thead>"
read -ra speaker textfields
htmlrow "$speaker"th "$text{fields[@]}" th
echo "</thead>"
 
echo "<tbody>"
while read -ra speaker text; dofields
do htmlrow "$speaker"td "$text{fields[@]}"
done
echo "</tbody>"
echo "</table>"
}
 
htmlrow() {
cell=${3:-td}1
shift
printf "<tr><%s>%s</%s><%s>%s</%s></tr>\n" \
echo "<tr>"
"$cell" "$(escape_html "$1")" "$cell" \
for field
"$cell" "$(escape_html "$2")" "$cell"
do echo "<$cell>$(escape_html "$field")</$cell>"
done
echo "</tr>"
}
 
escape_html() {
str=${1//\&/&amp;}
Line 4,107 ⟶ 5,871:
}
 
csv2html <<-END
html=$(
Character,Speech
csv2html <<-END
The multitude,The messiah! Show us the messiah!
Character,Speech
Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>
The multitude,The messiah! Show us the messiah!
The multitude,Who are you?
Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>
Brians mother,I'm his mother; that's who!
The multitude,Who are you?
BriansThe mothermultitude,I'mBehold his mother;! that'sBehold whohis mother!
END</syntaxhighlight>
The multitude,Behold his mother! Behold his mother!
END
)
echo "$html"</lang>
 
{{output}}
<langsyntaxhighlight lang=html5><table>
<thead>
<tr>
<tr><th>Character</th><th>Speech</th></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>&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>
</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 "&lt;";
Case ">": Debug.Print "&gt;";
Case "&": Debug.Print "&amp;";
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>&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>
Line 4,130 ⟶ 5,949:
<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>
</tbody>
 
</table></lang>
=={{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,"<","&lt;"),">","&gt;")
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>&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>
 
=={{header|Vedit macro language}}==
Line 4,138 ⟶ 5,997:
If a block is highlighted, only the block is converted. If no block highlighted, the entire file is converted.
 
<langsyntaxhighlight lang=vedit>if (BB < 0) { // block not set
BB(0) // convert entire file
BE(File_Size)
Line 4,164 ⟶ 6,023:
Line(1)
}
BB(Clear)</langsyntaxhighlight>
 
{{out}}
<langsyntaxhighlight 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>
Line 4,174 ⟶ 6,033:
<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></langsyntaxhighlight>
 
=={{header|XSLTVisual Basic 2.0NET}}==
{{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>&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|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 == "&") ? "&amp;" :
(c == "'") ? "&apos;" :
(c == "<") ? "&lt;" :
(c == ">") ? "&gt;" : 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 == "&") ? "&amp;" :
(c == "'") ? "&apos;" :
(c == "<") ? "&lt;" :
(c == ">") ? "&gt;" : 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, "&lt;");
^>: Text(0, "&gt;");
^&: Text(0, "&amp;")
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>&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|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"
Line 4,236 ⟶ 6,271:
<xsl:template match="xcsv:notice" mode="phase-2" />
 
</xsl:stylesheet></langsyntaxhighlight>
 
This style-sheet, when applied to the task input, yields...
 
<langsyntaxhighlight lang=html5><!DOCTYPE html
SYSTEM "about:legacy-compat">
<html lang="en">
Line 4,277 ⟶ 6,312:
</table>
</body>
</html></langsyntaxhighlight>
 
=={{header|zkl}}==
<langsyntaxhighlight 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"
Line 4,292 ⟶ 6,327:
.pump("<tr>\n","strip",String.fpM("101"," <td>","</td>\n"))+"</tr>\n"
}) + "</table>";
html.println();</langsyntaxhighlight>
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>
<table>
<tr>
<td>Character</td>
Line 4,320 ⟶ 6,355:
<td>Behold his mother! Behold his mother!</td>
</tr>
</table></syntaxhighlight>
 
{{out}}
[[Category:CSV]]
<pre>
[[Category:HTML]]
<table>
<tr>
<td>Character</td>
<td>Speech</td>
</tr>
<tr>
<td>The multitude</td>
...
</pre>
2,060

edits