CSV to HTML translation: Difference between revisions

Add Jsish, put Julia entry back into alphabetical order
(Add Jsish, put Julia entry back into alphabetical order)
Line 2,580:
</tr>
</table></lang>
 
=={{header|Jsish}}==
From Javascript entry.
 
<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!=
*/</lang>
 
{{out}}
<pre>prompt$ jsish -u csvToHTML.jsi
[PASS] csvToHTML.jsi</pre>
 
=={{header|Julia}}==
 
<lang Julia>function csv2html(fname::ASCIIString; header::Bool=false)
csv = readcsv(fname)
@assert(length(csv) > 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))</lang>
 
{{out}}
 
<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></lang>
 
=={{header|Kotlin}}==
Line 2,761 ⟶ 2,943:
</pre>
Rendered output is available at http://www.diga.me.uk/csvhtml.gif
 
=={{header|Julia}}==
 
<lang Julia>function csv2html(fname::ASCIIString; header::Bool=false)
csv = readcsv(fname)
@assert(length(csv) > 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))</lang>
 
{{out}}
 
<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></lang>
 
=={{header|Lua}}==
Anonymous user