CSV to HTML translation: Difference between revisions

m
m (→‎{{header|Phix}}: fixed bad markup)
 
(16 intermediate revisions by 10 users not shown)
Line 29:
=={{header|11l}}==
{{trans|C}}
<langsyntaxhighlight 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>
Line 48:
}, end' ‘’)
 
print("</td></tr>\n</table>")</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 57:
<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|Ada}}==
Line 63:
{{libheader|AWS}}
csv2html.adb:
<langsyntaxhighlight lang=Ada>with Ada.Strings.Fixed;
with Ada.Text_IO;
with Templates_Parser;
Line 102:
(Templates_Parser.Parse ("table.tmplt", Translations));
end;
end Csv2Html;</langsyntaxhighlight>
 
table.tmplt:
<langsyntaxhighlight lang=html5><table>
@@TABLE@@
<tr>
Line 112:
</tr>
@@END_TABLE@@
</table></langsyntaxhighlight>
 
{{out}}
<langsyntaxhighlight lang=html5><table>
<tr>
<td>Character</td>
Line 140:
<td>Behold his mother! Behold his mother!</td>
</tr>
</table></langsyntaxhighlight>
 
=={{header|ALGOL 68}}==
Line 146:
{{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 214:
elbat;
ydob;
lmth</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang=html5><HTML>
<HEAD>
<TITLE>CSV to HTML translation - Extra Credit</TITLE>
Line 234:
</TABLE>
</BODY>
</HTML></langsyntaxhighlight>
 
=={{header|ANTLR}}==
===Java===
<langsyntaxhighlight lang=java>
// Create an HTML Table from comma seperated values
// Nigel Galloway - June 2nd., 2013
Line 248:
field : Field{System.out.println("<TD>" + $Field.text.replace("<","&lt;").replace(">","&gt;") + "</TD>");};
Field : ~[,\n\r]+;
</syntaxhighlight>
</lang>
 
=={{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}}==
 
<langsyntaxhighlight lang=rebol>in: {
Character,Speech
The multitude,The messiah! Show us the messiah!
Line 270 ⟶ 308:
]
 
print table map read.csv in => row</langsyntaxhighlight>
 
{{out}}
Line 278 ⟶ 316:
=={{header|AutoHotkey}}==
Very basic implementation
<langsyntaxhighlight lang=AutoHotkey>CSVData =
(
Character,Speech
Line 302 ⟶ 340:
return str
}
MsgBox % clipboard := TableData</langsyntaxhighlight>
{{out}}
<pre><table>
Line 314 ⟶ 352:
 
=={{header|AutoIt}}==
<langsyntaxhighlight lang=AutoIt>
Local $ascarray[4] = [34,38,60,62]
$String = "Character,Speech" & @CRLF
Line 343 ⟶ 381:
$newstring &= "</table>"
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $newstring = ' & $newstring & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
</syntaxhighlight>
</lang>
 
{{out}}
Line 380 ⟶ 418:
Includes extra credit.<br />
File csv2html.awk
<langsyntaxhighlight lang=awk>#!/usr/bin/awk -f
BEGIN {
FS=","
Line 403 ⟶ 441:
print "</table>"
}
</syntaxhighlight>
</lang>
 
<pre>$ awk -f csv2html.awk input.csv</pre>
<langsyntaxhighlight lang=html5><table>
<tr>
<td>Character</td>
Line 431 ⟶ 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 461 ⟶ 499:
<td>Behold his mother! Behold his mother!</td>
</tr>
</table></langsyntaxhighlight>
 
=={{header|Batch File}}==
<langsyntaxhighlight lang=dos>::Batch Files are terrifying when it comes to string processing.
::But well, a decent implementation!
@echo off
Line 495 ⟶ 533:
endlocal
)
echo ^</table^></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 504 ⟶ 542:
<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|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 550 ⟶ 588:
SYS "ShellExecute", @hwnd%, 0, "CSVtoHTML.htm", 0, 0, 1
</syntaxhighlight>
</lang>
{{out}}
<pre><HTML>
Line 575 ⟶ 613:
Note that right angle brackets are deliberately not escaped, since that is not strictly necessary for the markup to be valid.
 
<langsyntaxhighlight lang=Befunge><v_>#!,#:< "<table>" \0 +55
v >0>::65*1+`\"~"`!*#v_4-5v >
v>#^~^<v"<tr><td>" < \v-1/<>">elb"
Line 583 ⟶ 621:
>^>\#v_$$0";pma&" v>"/<>d"v5 v , <
$ > \#v_$0";tl&"v v"</t"<0 > : |
^_>#!,#:<>#<0#<\#<<< >:#,_$#^_v@ $<</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 592 ⟶ 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></langsyntaxhighlight>
 
=={{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.
<langsyntaxhighlight lang=bracmat>( ( CSVtoHTML
= p q Character Speech swor rows row
. 0:?p
Line 628 ⟶ 666:
"
)
</syntaxhighlight>
</lang>
Output:
<langsyntaxhighlight 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>
Line 636 ⟶ 674:
<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>
 
===Simple solution NOT using pattern matching ===
Line 645 ⟶ 683:
 
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.
<langsyntaxhighlight lang=Bracmat>( ( Csv2Html
=
. toML
Line 667 ⟶ 705:
The multitude,Behold his mother! Behold his mother!
"
)</langsyntaxhighlight>
 
===Extra credit solution===
<langsyntaxhighlight lang=Bracmat>( ( Csv2Html
=
. toML
Line 708 ⟶ 746:
The multitude,Behold his mother! Behold his mother!
"
)</langsyntaxhighlight>
 
Output:
<langsyntaxhighlight 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>
Line 719 ⟶ 757:
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
<tr><td /></tr>
</tbody></table></langsyntaxhighlight>
 
=={{header|C}}==
 
<langsyntaxhighlight lang=c>#include <stdio.h>
 
const char *input =
Line 751 ⟶ 789:
 
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 758 ⟶ 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 765 ⟶ 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#}}==
Line 771 ⟶ 809:
===Simple Solution===
 
<langsyntaxhighlight lang=C sharp>
using System;
using System.Collections.Generic;
Line 804 ⟶ 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 866 ⟶ 904:
}
}
}</langsyntaxhighlight>
 
{{out|Sample HTML Output}}
<langsyntaxhighlight lang=html5><table>
<tr>
<th>Character</th>
Line 894 ⟶ 932:
<td>Behold his mother! Behold his mother!</td>
</tr>
</table></langsyntaxhighlight>
 
=={{header|C++}}==
<langsyntaxhighlight lang=cpp>#include <string>
#include <boost/regex.hpp>
#include <iostream>
Line 928 ⟶ 966:
tabletext.append( "</TABLE>\n" ) ;
return tabletext ;
}</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang=html5>
<TABLE>
<TR><TD>Character</TD><TD>Speech</TD></TR>
Line 939 ⟶ 977:
<TR><TD>The multitude</TD><TD>Behold his mother! Behold his mother!</TD></TR>
</TABLE>
</syntaxhighlight>
</lang>
 
=={{header|Clojure}}==
Line 945 ⟶ 983:
We assume the presence of a file, but the input could come from anywhere.
 
<langsyntaxhighlight lang=csv>
Character,Speech
The multitude,The messiah! Show us the messiah!
Line 952 ⟶ 990:
Brians mother,I'm his mother; that's who!
The multitude,Behold his mother! Behold his mother!
</syntaxhighlight>
</lang>
 
<langsyntaxhighlight lang=clojure>
(require 'clojure.string)
 
Line 986 ⟶ 1,024:
tbl (map #(clojure.string/split % #",") lines)]
(println (to-html tbl)))
</syntaxhighlight>
</lang>
 
{{out}}
<langsyntaxhighlight 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>
</lang>
 
=={{header|CoffeeScript}}==
Line 997 ⟶ 1,035:
{{works with|node.js}}
 
<langsyntaxhighlight lang=coffeescript>String::__defineGetter__ 'escaped', () ->
this.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
Line 1,035 ⟶ 1,073:
</tbody>
</table>
"""</langsyntaxhighlight>
 
{{out}}
<langsyntaxhighlight lang=html5><table cellspacing="0">
<thead>
<th scope="col">Character</th>
Line 1,055 ⟶ 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 1,111 ⟶ 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 1,123 ⟶ 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 1,163 ⟶ 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 1,183 ⟶ 1,221:
 
</body>
</html></langsyntaxhighlight>
 
=={{header|Delphi}}==
Line 1,189 ⟶ 1,227:
This solution solves both the basic and extra credit tasks.
 
<langsyntaxhighlight lang=Delphi>program csv2html;
 
{$APPTYPE CONSOLE}
Line 1,361 ⟶ 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,392 ⟶ 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,423 ⟶ 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}}==
<langsyntaxhighlight lang=scheme>
;; CSV -> LISTS
(define (csv->row line) (string-split line ","))
Line 1,457 ⟶ 1,546:
(style 'table "border-spacing: 10px;border:28px ridge orange") ;; special biblical border
(style 'th "color:blue;")
</syntaxhighlight>
</lang>
{{out}}
<langsyntaxhighlight lang=scheme>
;; changed <angry> to <b> to show that html tags inside text are correctly transmitted.
(define MontyPython #<<
Line 1,477 ⟶ 1,566:
(task MontyPython)
</syntaxhighlight>
</lang>
 
<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>
Line 1,483 ⟶ 1,572:
=={{header|Erlang}}==
Using functions from [[Create_an_HTML_table]]
<langsyntaxhighlight lang=Erlang>
-module( csv_to_html ).
 
Line 1,504 ⟶ 1,593:
Brians mother,I'm his mother; that's who!
The multitude,Behold his mother! Behold his mother!".
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,512 ⟶ 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,531 ⟶ 1,620:
end switch
end for
puts(1,"</td></tr>\n</table>")</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 1,541 ⟶ 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></langsyntaxhighlight>
 
=={{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,599 ⟶ 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,646 ⟶ 1,735:
</table>
</body>
</html></langsyntaxhighlight></div>
 
=={{header|Factor}}==
<langsyntaxhighlight lang=factor>USING: csv html.streams prettyprint xml.writer ;
 
"Character,Speech
Line 1,658 ⟶ 1,747:
The multitude,Behold his mother! Behold his mother!"
 
string>csv [ simple-table. ] with-html-writer pprint-xml</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang=html5><table style="display: inline-table; border-collapse: collapse;">
<tr>
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; ">
Line 1,711 ⟶ 1,800:
</tr>
</table>
<br/></langsyntaxhighlight>
 
=={{header|Forth}}==
Line 1,718 ⟶ 1,807:
{{trans|C}}
 
<langsyntaxhighlight lang=forth>: BEGIN-COLUMN ." <td>" ;
: END-COLUMN ." </td>" ;
 
Line 1,739 ⟶ 1,828:
;
 
CSV2HTML BYE</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 1,750 ⟶ 1,839:
<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|Fortran}}==
Line 1,762 ⟶ 1,851:
 
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.
<langsyntaxhighlight lang=Fortran>
SUBROUTINE CSVTEXT2HTML(FNAME,HEADED) !Does not recognise quoted strings.
Converts without checking field counts, or noting special characters.
Line 1,834 ⟶ 1,923:
CALL CSVTEXT2HTML("Text.csv",.TRUE.) !The first line is a heading.
END
</syntaxhighlight>
</lang>
{{Output}}
<pre>
Line 1,880 ⟶ 1,969:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight 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>"
Line 1,928 ⟶ 2,017:
Print Chr(10) & "</body>"
Print "</html>"
Sleep</langsyntaxhighlight>
 
 
 
 
=={{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,968 ⟶ 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 2,026 ⟶ 2,185:
</table>
`
)</langsyntaxhighlight>
{{out}}
Extra credit version with -h
<langsyntaxhighlight lang=html><table>
<thead>
<tr><th>Character</th><th>Speech</th></tr>
Line 2,040 ⟶ 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 2,049 ⟶ 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 2,055 ⟶ 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 2,092 ⟶ 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 2,111 ⟶ 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 2,132 ⟶ 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 2,158 ⟶ 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 2,192 ⟶ 2,351:
}
writer.toString()
}</langsyntaxhighlight>
 
'''Test:'''<br/>
Line 2,198 ⟶ 2,357:
 
{{out|Basic output}}
<div style="height:30ex;overflow:scroll;"><langsyntaxhighlight lang=html5><html>
<head>
<title>Basic</title>
Line 2,234 ⟶ 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 2,279 ⟶ 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 2,285 ⟶ 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 2,310 ⟶ 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 2,323 ⟶ 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 2,351 ⟶ 2,510:
<td>Behold his mother! Behold his mother!</td>
</tr>
</table></langsyntaxhighlight></div>
 
=={{header|Icon}} and {{header|Unicon}}==
Line 2,359 ⟶ 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 2,378 ⟶ 2,537:
write(" </TBODY>")
write("</TABLE>")
end</langsyntaxhighlight>
 
{{Out}}
<langsyntaxhighlight lang=html5><TABLE>
<THEAD>
<TR><TD>Character</TD><TD>Speech</TD></TR>
Line 2,392 ⟶ 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 2,417 ⟶ 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 2,432 ⟶ 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 2,441 ⟶ 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+'''
 
<langsyntaxhighlight lang=java>String csv = "...";
// Use Collectors.joining(...) for streaming, otherwise StringJoiner
StringBuilder html = new StringBuilder("<table>\n");
Line 2,453 ⟶ 2,612:
html.append(Arrays.stream(row.split(",")).collect(collector));
}
html.append("</table>\n");</langsyntaxhighlight>
 
'''Solution including simple and extra credit version'''
Line 2,461 ⟶ 2,620:
<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 2,547 ⟶ 2,706:
}
}
</syntaxhighlight>
</lang>
 
{{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 2,567 ⟶ 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>
 
{{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 2,587 ⟶ 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 2,609 ⟶ 2,768:
'\t</tbody>\n</table>');
 
</syntaxhighlight>
</lang>
 
<langsyntaxhighlight lang=html5><table>
<thead>
<tr><td>Character</td><td>Speech</td></tr>
Line 2,622 ⟶ 2,781:
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</tbody>
</table></langsyntaxhighlight>
 
{{out}}
<langsyntaxhighlight lang=html5><table>
<thead>
<tr><td>Character</td><td>Speech</td></tr>
Line 2,635 ⟶ 2,794:
<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|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,661 ⟶ 2,820:
;
 
csv2html | .[]</langsyntaxhighlight>
 
===Output===
<langsyntaxhighlight lang=html5><table>
<thead> <tr>
<th>Character</th>
Line 2,689 ⟶ 2,848:
<td>Behold his mother! Behold his mother! </td>
</tr>
</table></langsyntaxhighlight>
 
=={{header|Jsish}}==
From Javascript entry.
 
<langsyntaxhighlight lang=javascript>/* CSV to HTML, in Jsish */
var csv = "Character,Speech\n" +
"The multitude,The messiah! Show us the messiah!\n" +
Line 2,729 ⟶ 2,888:
</table>
=!EXPECTEND!=
*/</langsyntaxhighlight>
 
{{out}}
Line 2,736 ⟶ 2,895:
 
=={{header|Julia}}==
<langsyntaxhighlight lang=Julia>using DataFrames, CSV
 
using CSV, DataFrames
Line 2,806 ⟶ 2,965:
print(csv2html("input.csv", header=true))
</langsyntaxhighlight>{{out}}
<langsyntaxhighlight lang=html5><html>
 
<head>
Line 2,871 ⟶ 3,030:
</table>
</body>
</html></langsyntaxhighlight>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang=scala>// version 1.1.3
 
val csv =
Line 2,927 ⟶ 3,086:
sb.append("</td>\n$i$i</tr>\n$i</tbody>\n</table>")
println(sb.toString())
}</langsyntaxhighlight>
 
{{out}}
<langsyntaxhighlight lang=html5><table>
<tr>
<td>Character</td>
Line 2,987 ⟶ 3,146:
</tbody>
</table>
</syntaxhighlight>
</lang>
 
=={{header|Lambdatalk}}==
<langsyntaxhighlight lang=scheme>
{def CSV
Character,Speech\n
Line 3,010 ⟶ 3,169:
 
{csv2html {CSV}} ->
</syntaxhighlight>
</lang>
{{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 3,052 ⟶ 3,211:
print "</HTML>"
end
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 3,080 ⟶ 3,239:
 
=={{header|Lua}}==
<langsyntaxhighlight lang=lua>FS = "," -- field separator
 
csv = [[
Line 3,107 ⟶ 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 3,116 ⟶ 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}}==
<langsyntaxhighlight lang=maple> #A translation of the C code posted
html_table := proc(str)
local char;
Line 3,147 ⟶ 3,424:
Brians mother,I'm his mother; that's who!
The multitude,Behold his mother! Behold his mother!");
</syntaxhighlight>
</lang>
{{Out|Output}}
<pre>
Line 3,161 ⟶ 3,438:
 
=={{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 3,177 ⟶ 3,454:
StringSplit[StringReplace[a,{","->"</td><td>","<"->"&lt;",">"->"&gt;"}],"\n"]//Rest]
,"</table>"]
</syntaxhighlight>
</lang>
{{Out|Naive}}
<pre>
Line 3,208 ⟶ 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 3,224 ⟶ 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 3,257 ⟶ 3,534:
</tr>
</table>
</syntaxhighlight>
</lang>
 
=={{header|Maxima}}==
<langsyntaxhighlight lang=Maxima>infile: "input.csv";
outfile: "table.html";
instream: openr(infile);
Line 3,280 ⟶ 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 3,289 ⟶ 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 3,347 ⟶ 3,624:
</body>
</html>
]</langsyntaxhighlight>
===Output===
<langsyntaxhighlight lang=ML/I><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
Line 3,399 ⟶ 3,676:
</table>
</body>
</html></langsyntaxhighlight>
 
=={{header|Nanoquery}}==
<langsyntaxhighlight 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"
Line 3,433 ⟶ 3,710:
htmltable = htmltable+"</table>"
println htmltable</langsyntaxhighlight>
 
=={{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 3,557 ⟶ 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 3,643 ⟶ 3,920:
</body>
</html>
</syntaxhighlight>
</lang>
</div>
 
Line 3,652 ⟶ 3,929:
=={{header|Nim}}==
{{trans|Python}}
<langsyntaxhighlight lang=nim>import cgi, strutils
 
const csvtext = """Character,Speech
Line 3,674 ⟶ 3,951:
result.add "</table>"
 
echo csv2html(csvtext)</langsyntaxhighlight>
{{Out}}
<pre><table summary="csv2html program output">
Line 3,687 ⟶ 3,964:
=={{header|Oberon-2}}==
{{Works with|oo2c Version 2}}
<langsyntaxhighlight lang=oberon2>
MODULE CSV2HTML;
IMPORT
Line 3,760 ⟶ 4,037:
fileChannel.Close()
END CSV2HTML.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,775 ⟶ 4,052:
=={{header|Objeck}}==
{{trans|Java}}
<langsyntaxhighlight lang=objeck>use System.IO.File;
use Data.CSV;
 
Line 3,846 ⟶ 4,123:
}
}
</syntaxhighlight>
</lang>
 
Output:
Line 3,859 ⟶ 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 3,893 ⟶ 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 3,903 ⟶ 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 3,950 ⟶ 4,227:
 
let () =
print_html_table (list_of_csv csv_data)</langsyntaxhighlight>
 
{{out}}
<langsyntaxhighlight lang=html5><html>
<head>
<style type="text/css">
Line 3,969 ⟶ 4,246:
</tbody>
</table>
</html></langsyntaxhighlight>
 
=={{header|OpenEdge/Progress}}==
<langsyntaxhighlight lang=Progress (OpenEdge ABL)>
FUNCTION csvToHtml RETURNS CHARACTER (
i_lhas_header AS LOGICAL,
Line 4,022 ⟶ 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 4,051 ⟶ 4,328:
</tr>
</table>
</html></langsyntaxhighlight>
 
=={{header|Perl}}==
Line 4,057 ⟶ 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 4,071 ⟶ 4,348:
row @ARGV ? 'th' : 'td', $first;
row 'td', $_ foreach @rest;
print "</table>\n";</langsyntaxhighlight>
 
{{Out|Output (with a command-line argument)}}
<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 4,081 ⟶ 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|Phix}}==
Based on [[CSV_to_HTML_translation#Euphoria|Euphoria]] but with a simpler multiline constant.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #0000FF;">&</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">input</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
Character,Speech
Line 4,107 ⟶ 4,384:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"&lt;/td&gt;&lt;/tr&gt;\n&lt;/table&gt;"</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 4,145 ⟶ 4,422:
 
echo convert($csv);
</syntaxhighlight>
</lang>
 
=={{header|PicoLisp}}==
===Simple solution===
<langsyntaxhighlight lang=PicoLisp>(load "@lib/http.l")
 
(in "text.csv"
Line 4,156 ⟶ 4,433:
(while (split (line) ",")
(<row> NIL (ht:Prin (pack (car @))) (ht:Prin (pack (cadr @))))
(prinl) ) ) )</langsyntaxhighlight>
{{Out}}
<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 4,165 ⟶ 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 4,176 ⟶ 4,453:
(while (split (line) ",")
(<row> NIL (ht:Prin (pack (car @))) (ht:Prin (pack (cadr @))))
(prinl) ) ) ) )</langsyntaxhighlight>
{{Out}}
<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 4,184 ⟶ 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}}
<langsyntaxhighlight lang=html5><table>
<colgroup>
<col/>
Line 4,204 ⟶ 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 4,216 ⟶ 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}}
<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 4,240 ⟶ 4,517:
</table>
</body></html>
</syntaxhighlight>
</lang>
 
=={{header|Prolog}}==
Line 4,246 ⟶ 4,523:
{{Works with|SWI-Prolog}}
===Simple solution===
<langsyntaxhighlight lang=Prolog>csv_html :-
L = "Character,Speech
The multitude,The messiah! Show us the messiah!
Line 4,299 ⟶ 4,576:
csv_td_in(T, S).
 
</syntaxhighlight>
</lang>
{{out}}
<langsyntaxhighlight lang=html5><TABLE>
<TR>
<TD>Character</TD><TD>Speech</TD>
Line 4,321 ⟶ 4,598:
</TR>
</TABLE>
</syntaxhighlight>
</lang>
===Extra credit solution===
<langsyntaxhighlight lang=Prolog>csv_html_plus :-
L =
"Character,Speech
Line 4,413 ⟶ 4,690:
[H],
csv_body_td_in(T, S).
</syntaxhighlight>
</lang>
{{out}}
<langsyntaxhighlight lang=html5><TABLE>
<THEAD>
<TR>
Line 4,439 ⟶ 4,716:
</TBODY>
</TABLE>
</syntaxhighlight>
</lang>
 
===HTML outputs rendered in firefox browser===
Line 4,449 ⟶ 4,726:
(Note: rendered versions of all 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 4,476 ⟶ 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 4,487 ⟶ 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 4,519 ⟶ 4,796:
)
)
print(htmltxt)</langsyntaxhighlight>
 
'''Sample HTML output'''
Line 4,525 ⟶ 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 4,532 ⟶ 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===
Line 4,544 ⟶ 4,821:
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>.
 
<langsyntaxhighlight lang=python>from csv import DictReader
from xml.etree import ElementTree as ET
 
Line 4,580 ⟶ 4,857:
})
 
print(htmltxt.decode('utf8'))</langsyntaxhighlight>
 
'''Sample HTML output'''
Line 4,587 ⟶ 4,864:
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.
-->
<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><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></langsyntaxhighlight>
 
===HTML outputs rendered in firefox browser===
Line 4,595 ⟶ 4,872:
 
Using base R functions only, this is a very basic implementation and produces a simple HTML table
<langsyntaxhighlight lang=rsplus>File <- "~/test.csv"
Opened <- readLines(con = File)
Size <- length(Opened)
Line 4,614 ⟶ 4,891:
Table[length(Table)] <- paste0(Table[length(Table)],"</table>")
 
writeLines(as.character(Table), HTML)</langsyntaxhighlight>
 
'''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>
<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></langsyntaxhighlight>
 
=={{header|Racket}}==
 
Uses X-exprs:
<langsyntaxhighlight lang=racket>#lang racket
 
(define e.g.-CSV
Line 4,654 ⟶ 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 4,664 ⟶ 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}}==
Line 4,670 ⟶ 4,947:
{{works with|rakudo|2015.09}}
A very lispy solution:
<syntaxhighlight lang=raku perl6line>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>
Line 4,697 ⟶ 4,974:
# 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</langsyntaxhighlight>
 
{{Out}}
<langsyntaxhighlight lang=html><!DOCTYPE html>
<html>
<head><title>Some Text</title></head>
Line 4,710 ⟶ 4,987:
<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></langsyntaxhighlight>
 
=={{header|Red}}==
<langsyntaxhighlight lang=Red>Red []
 
csv: {Character,Speech
Line 4,752 ⟶ 5,029:
print csv2html csv ;; call function
write %data.html csv2html csv ;; write to file
</syntaxhighlight>
</lang>
output
<pre>
Line 4,775 ⟶ 5,052:
 
=={{header|Retro}}==
<langsyntaxhighlight lang=Retro>remapping off
"Character,Speech
The multitude,The messiah! Show us the messiah!
Line 4,797 ⟶ 5,074:
"</td></tr>\n</table>" puts ;
 
CSV displayHTML</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,807 ⟶ 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 4,813 ⟶ 5,090:
:::* &nbsp; file:///c:/output.html
:::* &nbsp; file:///c:/outputh.html
<langsyntaxhighlight lang=rexx>/*REXX program converts CSV ───► HTML table representing the CSV data. */
arg header_ . /*obtain an uppercase version of args. */
wantsHdr= (header_=='HEADER') /*is the arg (low/upp/mix case)=HEADER?*/
Line 4,848 ⟶ 5,125:
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
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>
 
Line 4,957 ⟶ 5,234:
ruby csv2html.rb header
I/O is done through standard input/output.
<langsyntaxhighlight lang=ruby>require 'cgi'
 
puts '<table summary="csv2html program output">'
Line 4,973 ⟶ 5,250:
end
 
puts "</table>"</langsyntaxhighlight>
{{Out}}
<langsyntaxhighlight 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 4,982 ⟶ 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 5,009 ⟶ 5,286:
k = instr(strRep$,rep$)
WEND
END FUNCTION</langsyntaxhighlight>
{{Out}}
<table border=1><TR bgcolor=wheat align=center><th>Character</th><th>Speech</th></TR>
Line 5,020 ⟶ 5,297:
=={{header|Rust}}==
{{trans|C}}
<langsyntaxhighlight lang=rust>static INPUT : &'static str =
"Character,Speech
The multitude,The messiah! Show us the messiah!
Line 5,042 ⟶ 5,319:
println!("</td></tr>\n</table>");
}
</syntaxhighlight>
</lang>
{{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 5,051 ⟶ 5,328:
<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|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 5,096 ⟶ 5,373:
 
println(csv2html(csv, true))
}</langsyntaxhighlight>{{out}}<syntaxhighlight lang =html><html>
<head>
<title>CsvToHTML</title>
Line 5,145 ⟶ 5,422:
</table>
</body>
</html></langsyntaxhighlight>
 
=={{header|Sed}}==
 
File csv2html.sed
<langsyntaxhighlight lang=sed>#!/bin/sed -f
 
s|<|\&lt;|g
Line 5,158 ⟶ 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 5,187 ⟶ 5,464:
<td>Behold his mother! Behold his mother!</td>
</tr>
</table></langsyntaxhighlight>
 
=={{header|Seed7}}==
Line 5,193 ⟶ 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 5,222 ⟶ 5,499:
end for;
writeln("</table>");
end func;</langsyntaxhighlight>
 
{{Out}}
<langsyntaxhighlight lang=html5>
<table>
<tr><th>Character</th><th>Speech</th></tr>
Line 5,235 ⟶ 5,512:
<tr><td></td></tr>
</table>
</syntaxhighlight>
</lang>
 
{{Out}} viewed with a browser:
Line 5,249 ⟶ 5,526:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang=ruby>func escape(str) { str.trans(« & < > », « &amp; &lt; &gt; ») }
func tag(t, d) { "<#{t}>#{d}</#{t}>" }
 
Line 5,278 ⟶ 5,555:
EOT
 
print csv2html(str)</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang=html5><!DOCTYPE html>
<html>
<head><title>Some Text</title></head>
Line 5,290 ⟶ 5,567:
<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></langsyntaxhighlight>
 
=={{header|Tcl}}==
Line 5,296 ⟶ 5,573:
{{tcllib|html}}
{{tcllib|struct::queue}}
<langsyntaxhighlight lang=tcl>package require Tcl 8.5
package require csv
package require html
Line 5,319 ⟶ 5,596:
}]
[html::closeTag]
}]</langsyntaxhighlight>
 
Extra credit version:
<langsyntaxhighlight lang=tcl>package require Tcl 8.5
package require csv
package require html
Line 5,366 ⟶ 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 5,375 ⟶ 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 5,417 ⟶ 5,694:
WRITE html "</table></body></html>"
ENDACCESS/PRINT html
</syntaxhighlight>
</lang>
=== Output (source code) ===
<langsyntaxhighlight lang=html5><!DOCTYPE html system>
<html>
<head>
Line 5,434 ⟶ 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 ]]
Line 5,442 ⟶ 5,719:
====Simple====
 
<langsyntaxhighlight lang=txr>@(collect)
@char,@speech
@(end)
Line 5,454 ⟶ 5,731:
@ (end)
</table>
@(end)</langsyntaxhighlight>
 
{{Out}}
<pre>$ txr csv.txr csv.txt</pre>
 
<langsyntaxhighlight lang=html5><table>
<tr>
<td>Character</td>
Line 5,484 ⟶ 5,761:
<td>Behold his mother! Behold his mother!</td>
</tr>
</table></langsyntaxhighlight>
 
====With Styling====
 
<langsyntaxhighlight lang=txr>@(collect)
@char,@speech
@(end)
Line 5,516 ⟶ 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 5,556 ⟶ 5,833:
<td>Behold his mother! Behold his mother!</td>
</tr>
</table></langsyntaxhighlight>
 
=={{header|UNIX Shell}}==
{{works with|bash}}
<langsyntaxhighlight lang=bash>csv2html() {
IFS=,
echo "<table>"
Line 5,601 ⟶ 5,878:
Brians mother,I'm his mother; that's who!
The multitude,Behold his mother! Behold his mother!
END</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang=html5><table>
<thead>
<tr>
Line 5,633 ⟶ 5,910:
</tr>
</tbody>
</table></langsyntaxhighlight>
 
=={{header|VBA}}==
{{trans|Phix}}<langsyntaxhighlight lang=vb>Public Sub CSV_TO_HTML()
input_ = "Character,Speech\n" & _
"The multitude,The messiah! Show us the messiah!\n" & _
Line 5,663 ⟶ 5,940:
Next i
Debug.Print "</td></tr>" & vbCrLf & "</table>"
End Sub</langsyntaxhighlight>{{out}}
<langsyntaxhighlight lang=html5><table>
<tr><td>
Character</td><td>Speech</td></tr>
Line 5,672 ⟶ 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></langsyntaxhighlight>
 
=={{header|VBScript}}==
<syntaxhighlight lang=vb>
<lang vb>
Set objfso = CreateObject("Scripting.FileSystemObject")
 
Line 5,709 ⟶ 5,986:
replace_chars = Replace(Replace(s,"<","&lt;"),">","&gt;")
End Function
</syntaxhighlight>
</lang>
 
{{out}}
Line 5,720 ⟶ 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 5,746 ⟶ 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 5,756 ⟶ 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|Visual Basic .NET}}==
Line 5,767 ⟶ 6,044:
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.
 
<langsyntaxhighlight lang=vbnet>Imports Microsoft.VisualBasic.FileIO
 
Module Program
Line 5,809 ⟶ 6,086:
</table>
End Function
End Module</langsyntaxhighlight>
 
{{out|input=true}}
<langsyntaxhighlight 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>
Line 5,845 ⟶ 6,122:
<td>Behold his mother! Behold his mother!</td>
</tr>
</table></langsyntaxhighlight>
 
=={{header|Wren}}==
{{trans|Kotlin}}
<langsyntaxhighlight ecmascriptlang="wren">var csv =
"Character,Speech\n" +
"The multitude,The messiah! Show us the messiah!\n" +
Line 5,890 ⟶ 6,167:
}
sb = sb + "</td>\n%(ii)</tr>\n%(i)</tbody>\n</table>"
System.print(sb)</langsyntaxhighlight>
 
{{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>
 
Line 5,903 ⟶ 6,218:
# 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 5,956 ⟶ 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 5,997 ⟶ 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 6,012 ⟶ 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}}
<langsyntaxhighlight lang=html5><table>
<tr>
<td>Character</td>
Line 6,040 ⟶ 6,355:
<td>Behold his mother! Behold his mother!</td>
</tr>
</table></langsyntaxhighlight>
 
[[Category:CSV]]
[[Category:HTML]]
1,983

edits