⚠️ Warning: This is a draft ⚠️
This means it might contain formatting issues, incorrect code, conceptual problems, or other severe issues.
If you want to help to improve and eventually enable this page, please fork RosettaGit's repository and open a merge request on GitHub.
{{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
;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,
;Extra credit:
''Optionally'' allow special formatting for the first row of the table as if it is the tables header row
(via {{trans|C}} {{out}} {{works with|Ada 2005}}
{{libheader|AWS}}
csv2html.adb: table.tmplt: {{out}} {{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''.}} {{out}} Very basic implementation {{out}} (note the output has been modified slightly since this webpage is html.) {{out}} {{works with|Gawk}} Includes extra credit. Extra credit: {{out}} {{works with|BBC BASIC for Windows}} {{out}} {{out|Rendered output}}
[[File:CSVHTML_BBC.gif]] 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 Output: Newer versions of Bracmat have the built in function In the code below, we use Output: 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. {{out}} {{out}} {{out}} {{out}} when using the text suggested: {{out|Sample HTML Output}} {{works with|node.js}} {{out}} We assume the presence of a file, but the input could come from anywhere. {{out}} {{out}} {{trans|C}} {{out}} This solution solves both the basic and extra credit tasks. {{out|Basic output}} {{out|Extra credit output}} {{out}} Using functions from [[Create_an_HTML_table]] {{out}} {{trans|C}}
{{works with|Euphoria|4.*}} {{out}} =={{header|F_Sharp|F#}}==
Use .NET XmlWriter.
Stylesheet styling is applied only when command line option -h ist given. {{out}} (stylesheet version) {{out}} {{works with|gforth}}
{{trans|C}} {{out}} 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 Thus, the entire line for a table row can be written in one go, starting with the tr then many repetitions of (WOT,''text'', WOT), with each starting and ending WOT appropriately encased by < and > and / characters supplied by the FORMAT. This repeated pattern is unsuitable for starting the line (so the tr is provided by the FORMAT, with indentation) because then it would be out of step, but happily, after the last text is rolled, the required /tr can be sent to use the format codes that would normally be used for the start of the next text. Humm. If there ''were'' to be 666 texts to roll, this will exhaust the FORMAT statement and it will write the current line out and start a fresh one, resuming at the rightmost ( in the FORMAT specification. This rule does not always prove convenient so I'd prefer the vertical bar usage of musical notation to mark the resumption location. Interpreters of html do not care about layout but humans do, so, just in case, the repeat count should be 667 (or <NCOLS + 1>), or, if The check for a comma is not {{Output}} And interpreted, first with no special heading: Extra credit version accepts -h command line option to do the special formatting for the heading line. {{out}}
Extra credit version with -h Basic version, or extra credit version without -h Brute force solution using nested GStrings. It solves both the basic and extra credit tasks. '''Test:''' {{out|Basic output}}11l
V input_csv = ‘Character,Speech
The multitude,The messiah! Show us the messiah!
Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>
The multitude,Who are you?
Brians mother,I'm his mother; that's who!
The multitude,Behold his mother! Behold his mother!’
print("<table>\n<tr><td>", end' ‘’)
L(c) input_csv
print(S c {
"\n"{"</td></tr>\n<tr><td>"}
‘,’ {‘</td><td>’}
‘<’ {‘<’}
‘>’ {‘>’}
‘&’ {‘&’}
E {c}
}, end' ‘’)
print("</td></tr>\n</table>")
<tr><td>Character</td><td>Speech</td></tr>
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
<tr><td>Brians mother</td><td><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry></td></tr>
<tr><td>The multitude</td><td>Who are you?</td></tr>
<tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table>
Ada
with Ada.Strings.Fixed;
with Ada.Text_IO;
with Templates_Parser;
procedure Csv2Html is
use type Templates_Parser.Vector_Tag;
Chars : Templates_Parser.Vector_Tag;
Speeches : Templates_Parser.Vector_Tag;
CSV_File : Ada.Text_IO.File_Type;
begin
-- read the csv data
Ada.Text_IO.Open (File => CSV_File,
Mode => Ada.Text_IO.In_File,
Name => "data.csv");
-- fill the tags
while not Ada.Text_IO.End_Of_File (CSV_File) loop
declare
Whole_Line : String := Ada.Text_IO.Get_Line (CSV_File);
Comma_Pos : Natural := Ada.Strings.Fixed.Index (Whole_Line, ",");
begin
Chars := Chars & Whole_Line (Whole_Line'First .. Comma_Pos - 1);
Speeches := Speeches & Whole_Line (Comma_Pos + 1 .. Whole_Line'Last);
end;
end loop;
Ada.Text_IO.Close (CSV_File);
-- build translation table and output html
declare
Translations : constant Templates_Parser.Translate_Table :=
(1 => Templates_Parser.Assoc ("CHAR", Chars),
2 => Templates_Parser.Assoc ("SPEECH", Speeches));
begin
Ada.Text_IO.Put_Line
(Templates_Parser.Parse ("table.tmplt", Translations));
end;
end Csv2Html;
@@TABLE@@
<tr>
<td>@_WEB_ESCAPE:CHAR_@</td>
<td>@_WEB_ESCAPE:SPEECH_@</td>
</tr>
@@END_TABLE@@
</table>
<tr>
<td>Character</td>
<td>Speech</td>
</tr>
<tr>
<td>The multitude</td>
<td>The messiah! Show us the messiah!</td>
</tr>
<tr>
<td>Brians mother</td>
<td><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry></td>
</tr>
<tr>
<td>The multitude</td>
<td>Who are you?</td>
</tr>
<tr>
<td>Brians mother</td>
<td>I'm his mother; that's who!</td>
</tr>
<tr>
<td>The multitude</td>
<td>Behold his mother! Behold his mother!</td>
</tr>
</table>
ALGOL 68
#!/usr/local/bin/a68g --script #
[6]STRING rows := []STRING(
"Character,Speech",
"The multitude,The messiah! Show us the messiah!",
"Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>",
"The multitude,Who are you?",
"Brians mother,I'm his mother; that's who!",
"The multitude,Behold his mother! Behold his mother!"
);
[max abs char]STRING encoded; FOR i TO UPB encoded DO encoded[i]:=REPR i OD;
# encoded[ABS""""] := """; optional #
encoded[ABS "&"] := "&";
encoded[ABS "<"] := "<";
# encoded[ABS ">"] := ">"; optional #
OP ENCODE = (STRING s)STRING: (
STRING out := "";
FOR i TO UPB s DO out+:= encoded[ABS s[i]] OD;
out
);
PROC head = (STRING title)VOID: (
printf((
$"<HEAD>"l$,
$"<TITLE>"g"</TITLE>"l$, title,
$"<STYLE type=""text/css"">"l$,
$"TD {background-color:#ddddff; }"l$,
$"thead TD {background-color:#ddffdd; text-align:center; }"l$,
$"</STYLE>"l$,
$"</HEAD>"l$
))
);
# define HTML tags using Algol68's "reverent" block structuring #
PROC html = VOID: print(("<HTML>", new line)),
body = VOID: print(("<BODY>", new line)),
table = VOID: print(("<TABLE>", new line)),
table row = VOID: print(("<TR>")),
th = (STRING s)VOID: printf(($"<TH>"g"</TH>"$, s)),
td = (STRING s)VOID: printf(($"<TD>"g"</TD>"$, s)),
elbat row = VOID: print(("</TR>", new line)),
elbat = VOID: print(("</TABLE>", new line)),
ydob = VOID: print(("</BODY>", new line)),
lmth = VOID: print(("</HTML>", new line));
FILE row input; STRING row; CHAR ifs = ",";
associate(row input, row); make term(row input, ifs);
html;
head("CSV to HTML translation - Extra Credit");
body;
table;
FOR nr TO UPB rows DO
row := rows[nr];
table row;
on logical file end(row input, (REF FILE row input)BOOL: row end);
FOR nf DO
STRING field; get(row input,field);
(nr=1|th|td)(ENCODE field);
get(row input, space)
OD;
row end: reset(row input);
elbat row
OD;
elbat;
ydob;
lmth
<HEAD>
<TITLE>CSV to HTML translation - Extra Credit</TITLE>
<STYLE type="text/css">
TD {background-color:#ddddff; }
thead TD {background-color:#ddffdd; text-align:center; }
</STYLE>
</HEAD>
<BODY>
<TABLE>
<TR><TH>Character</TH><TH>Speech</TH></TR>
<TR><TD>The multitude</TD><TD>The messiah! Show us the messiah!</TD></TR>
<TR><TD>Brians mother</TD><TD><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry></TD></TR>
<TR><TD>The multitude</TD><TD>Who are you?</TD></TR>
<TR><TD>Brians mother</TD><TD>I'm his mother; that's who!</TD></TR>
<TR><TD>The multitude</TD><TD>Behold his mother! Behold his mother!</TD></TR>
</TABLE>
</BODY>
</HTML>
AutoHotkey
CSVData =
(
Character,Speech
The multitude,The messiah! Show us the messiah!
Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>
The multitude,Who are you?
Brians mother,I'm his mother; that's who!
The multitude,Behold his mother! Behold his mother!
)
TableData := "<table>"
Loop Parse, CSVData,`n
{
TableData .= "`n <tr>"
Loop Parse, A_LoopField, CSV
TableData .= "<td>" HTMLEncode(A_LoopField) "</td>"
TableData .= "</tr>"
}
TableData .= "`n</table>"
HTMLEncode(str){
static rep := "&<lt;>gt;""quot"
Loop Parse, rep,;
StringReplace, str, str, % SubStr(A_LoopField, 1, 1), % "&" . SubStr(A_LoopField, 2) . ";", All
return str
}
MsgBox % clipboard := TableData
<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>
AutoIt
Local $ascarray[4] = [34,38,60,62]
$String = "Character,Speech" & @CRLF
$String &= "The multitude,The messiah! Show us the messiah!" & @CRLF
$String &= "Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>" & @CRLF
$String &= "The multitude,Who are you?" & @CRLF
$String &= "Brians mother,I'm his mother; that's who!" & @CRLF
$String &= "The multitude,Behold his mother! Behold his mother!"
For $i = 0 To UBound($ascarray) -1
$String = Stringreplace($String, chr($ascarray[$i]), "&#"&$ascarray[$i]&";")
Next
$newstring = "<table>" & @CRLF
$crlfsplit = StringSplit($String, @CRLF, 1)
For $i = 1 To $crlfsplit[0]
If $i = 1 Then $newstring &= "<thead>" & @CRLF
$newstring &= "<tr>" & @CRLF
$komsplit = StringSplit($crlfsplit[$i], ",")
For $k = 1 To $komsplit[0]
If $i = 1 Then
$newstring &= "<th>" &$komsplit[$k] & "</th>" & @CRLF
Else
$newstring &= "<td>" &$komsplit[$k] & "</td>" & @CRLF
EndIf
Next
$newstring &= "</tr>" & @CRLF
If $i = 1 Then $newstring &= "</thead>" & @CRLF
Next
$newstring &= "</table>"
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $newstring = ' & $newstring & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
<table>
<thead>
<tr>
<th>Character</th>
<th>Speech</th>
</tr>
</thead>
<tr>
<td>The multitude</td>
<td>The messiah! Show us the messiah!</td>
</tr>
<tr>
<td>Brians mother</td>
<td><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry></td>
</tr>
<tr>
<td>The multitude</td>
<td>Who are you?</td>
</tr>
<tr>
<td>Brians mother</td>
<td>I'm his mother; that's who!</td>
</tr>
<tr>
<td>The multitude</td>
<td>Behold his mother! Behold his mother!</td>
</tr>
</table>
ANTLR
Java
// Create an HTML Table from comma seperated values
// Nigel Galloway - June 2nd., 2013
grammar csv2html;
dialog : {System.out.println("<HTML><Table>");}header body+{System.out.println("</Table></HTML>");} ;
header : {System.out.println("<THEAD align=\"center\"><TR bgcolor=\"blue\">");}row{System.out.println("</TR></THEAD");};
body : {System.out.println("<TBODY><TR>");}row{System.out.println("</TR></TBODY");};
row : field ',' field '\r'? '\n';
field : Field{System.out.println("<TD>" + $Field.text.replace("<","<").replace(">",">") + "</TD>");};
Field : ~[,\n\r]+;
AWK
File csv2html.awk#!/usr/bin/awk -f
BEGIN {
FS=","
print "<table>"
}
{
gsub(/</, "\\<")
gsub(/>/, "\\>")
gsub(/&/, "\\>")
print "\t<tr>"
for(f = 1; f <= NF; f++) {
if(NR == 1 && header) {
printf "\t\t<th>%s</th>\n", $f
}
else printf "\t\t<td>%s</td>\n", $f
}
print "\t</tr>"
}
END {
print "</table>"
}
$ awk -f csv2html.awk input.csv
<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>
$ awk -v header=1 -f csv2html.awk input.csv
<tr>
<th>Character</th>
<th>Speech</th>
</tr>
<tr>
<td>The multitude</td>
<td>The messiah! Show us the messiah!</td>
</tr>
<tr>
<td>Brians mother</td>
<td>>lt;angry>gt;Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!>lt;/angry>gt;</td>
</tr>
<tr>
<td>The multitude</td>
<td>Who are you?</td>
</tr>
<tr>
<td>Brians mother</td>
<td>I'm his mother; that's who!</td>
</tr>
<tr>
<td>The multitude</td>
<td>Behold his mother! Behold his mother!</td>
</tr>
</table>
Batch File
::Batch Files are terrifying when it comes to string processing.
::But well, a decent implementation!
@echo off
REM Below is the CSV data to be converted.
REM Exactly three colons must be put before the actual line.
:::Character,Speech
:::The multitude,The messiah! Show us the messiah!
:::Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>
:::The multitude,Who are you?
:::Brians mother,I'm his mother; that's who!
:::The multitude,Behold his mother! Behold his mother!
setlocal disabledelayedexpansion
echo ^<table^>
for /f "delims=" %%A in ('findstr "^:::" "%~f0"') do (
set "var=%%A"
setlocal enabledelayedexpansion
REM The next command removes the three colons...
set "var=!var:~3!"
REM The following commands to the substitions per line...
set "var=!var:&=&!"
set "var=!var:<=<!"
set "var=!var:>=>!"
set "var=!var:,=</td><td>!"
echo ^<tr^>^<td^>!var!^</td^>^</tr^>
endlocal
)
echo ^</table^>
<tr><td>Character</td><td>Speech</td></tr>
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
<tr><td>Brians mother</td><td><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry></td></tr>
<tr><td>The multitude</td><td>Who are you?</td></tr>
<tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table>
BBC BASIC
DATA "Character,Speech"
DATA "The multitude,The messiah! Show us the messiah!"
DATA "Brian's mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>"
DATA "The multitude,Who are you?"
DATA "Brian's mother,I'm his mother; that's who!"
DATA "The multitude,Behold his mother! Behold his mother!"
DATA "***"
*SPOOL CSVtoHTML.htm
PRINT "<HTML>"
PRINT "<HEAD>"
PRINT "</HEAD>"
PRINT "<BODY>"
PRINT "<table border=1 cellpadding =10 cellspacing=0>"
header% = TRUE
REPEAT
READ csv$
IF csv$ = "***" THEN EXIT REPEAT
IF header% PRINT "<tr><th>"; ELSE PRINT "<tr><td>";
FOR i% = 1 TO LEN(csv$)
c$ = MID$(csv$, i%, 1)
CASE c$ OF
WHEN ",": IF header% PRINT "</th><th>"; ELSE PRINT "</td><td>";
WHEN "<": PRINT "<";
WHEN ">": PRINT ">";
WHEN "&": PRINT "&";
OTHERWISE: PRINT c$;
ENDCASE
NEXT i%
IF header% PRINT "</th></tr>" ELSE PRINT "</td></tr>"
header% = FALSE
UNTIL FALSE
PRINT "</table>"
PRINT "</BODY>"
PRINT "</HTML>"
*spool
SYS "ShellExecute", @hwnd%, 0, "CSVtoHTML.htm", 0, 0, 1
<HTML>
<HEAD>
</HEAD>
<BODY>
<table border=1 cellpadding =10 cellspacing=0>
<tr><th>Character</th><th>Speech</th></tr>
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
<tr><td>Brian's mother</td><td>&lt;angry&gt;Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!&lt;/angry&gt;</td></tr>
<tr><td>The multitude</td><td>Who are you?</td></tr>
<tr><td>Brian's mother</td><td>I'm his mother; that's who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table>
</BODY>
</HTML>
Bracmat
Extra credit solution using pattern matching
[!p
. Each time a line is matched, p
is updated, the two found elements are collected and the pattern is forced to fail, so the pattern matcher finds the next line. The found rows are collected in reverse order, because prepending to a list is faster than appending. When all lines are read, the collected lines are reversed, interspersed with newline characters. Finally the predefined function toML is used to create HTML.( ( CSVtoHTML
= p q Character Speech swor rows row
. 0:?p
& :?swor:?rows
& ( @( !arg
: ?
( [!p ?Character "," ?Speech \n [?q ?
& !q:?p
& (tr.,(th.,!Character) (th.,!Speech))
!swor
: ?swor
& ~
)
)
| whl
' ( !swor:%?row %?swor
& !row \n !rows:?rows
)
& toML
$ (table.,(thead.,!swor) \n (tbody.,!rows))
)
)
& CSVtoHTML
$ "Character,Speech
The multitude,The messiah! Show us the messiah!
Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>
The multitude,Who are you?
Brians mother,I'm his mother; that's who!
The multitude,Behold his mother! Behold his mother!
"
)
<tbody><tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
<tr><td>Brians mother</td><td><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry></td></tr>
<tr><td>The multitude</td><td>Who are you?</td></tr>
<tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</tbody></table>
Simple solution NOT using pattern matching
vap
that splits an input string in
single characters or that splits it everywhere where a given separator character occurs. Each single character or slab of characters is
passed to a function. The values returned from this function become the elements in a list that is returned from the vap
function. For example, vap$(upp.Википедию)
"vaporizes" the word Википедию into characters and produces the list of
uppercased characters В И К И П Е Д И Ю. Instead of the name of a function we can choose to just give vap
the definition of an anonymous function, like so: vap$((=.!arg:~и|).Википедию)
. This returns a list of the characters in the word Википедию, except for the и character: В к п е д ю.vap
with a third argument, a splitting separator character. The outer call to vap
splits a text into rows. An embedded call to vap
splits each row into cell elements. This code is very efficient.( ( Csv2Html
=
. toML
$ ( table
.
, vap
$ ( (
= .tr.,vap$((=.td.,!arg).!arg.",")
)
. !arg
. \n
)
)
)
& Csv2Html
$ "Character,Speech
The multitude,The messiah! Show us the messiah!
Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>
The multitude,Who are you?
Brians mother,I'm his mother; that's who!
The multitude,Behold his mother! Behold his mother!
"
)
Extra credit solution
( ( Csv2Html
=
. toML
$ ( table
.
, vap
$ ( (=..vap$((=.,!arg).!arg.","))
. !arg
. \n
)
: (.%?header) ?body
& ( thead
.
, (tr.,map$((=.th.!arg).!header))
\n
)
( tbody
.
, map
$ ( (
=
. !arg:(.?arg)
& (tr.,map$((=.td.!arg).!arg))
\n
)
. !body
)
)
)
)
& Csv2Html
$ "Character,Speech
The multitude,The messiah! Show us the messiah!
Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>
The multitude,Who are you?
Brians mother,I'm his mother; that's who!
The multitude,Behold his mother! Behold his mother!
"
)
<table><thead><tr><th>Character</th><th>Speech</th></tr>
</thead><tbody><tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
<tr><td>Brians mother</td><td><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry></td></tr>
<tr><td>The multitude</td><td>Who are you?</td></tr>
<tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
<tr><td /></tr>
</tbody></table>
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@ $<
<tr><td>Character</td><td>Speech</td></tr>
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
<tr><td>Brians mother</td><td><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry></td></tr>
<tr><td>The multitude</td><td>Who are you?</td></tr>
<tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table>
C
#include <stdio.h>
const char *input =
"Character,Speech\n"
"The multitude,The messiah! Show us the messiah!\n"
"Brians mother,<angry>Now you listen here! He's not the messiah; "
"he's a very naughty boy! Now go away!</angry>\n"
"The multitude,Who are you?\n"
"Brians mother,I'm his mother; that's who!\n"
"The multitude,Behold his mother! Behold his mother!";
int main()
{
const char *s;
printf("<table>\n<tr><td>");
for (s = input; *s; s++) {
switch(*s) {
case '\n': printf("</td></tr>\n<tr><td>"); break;
case ',': printf("</td><td>"); break;
case '<': printf("<"); break;
case '>': printf(">"); break;
case '&': printf("&"); break;
default: putchar(*s);
}
}
puts("</td></tr>\n</table>");
return 0;
}
$ gcc -Wall -W -ansi -pedantic csv.c -o csv
$ ./csv
<tr><td>Character</td><td>Speech</td></tr>
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
<tr><td>Brians mother</td><td><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry></td></tr>
<tr><td>The multitude</td><td>Who are you?</td></tr>
<tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table>
C++
#include <string>
#include <boost/regex.hpp>
#include <iostream>
std::string csvToHTML( const std::string & ) ;
int main( ) {
std::string text = "Character,Speech\n"
"The multitude,The messiah! Show us the messiah!\n"
"Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>\n"
"The multitude,Who are you?\n"
"Brians mother,I'm his mother; that's who!\n"
"The multitude,Behold his mother! Behold his mother!\n" ;
std::cout << csvToHTML( text ) ;
return 0 ;
}
std::string csvToHTML( const std::string & csvtext ) {
//the order of the regexes and the replacements is decisive!
std::string regexes[ 5 ] = { "<" , ">" , "^(.+?)\\b" , "," , "\n" } ;
const char* replacements [ 5 ] = { "<" , ">" , " <TR><TD>$1" , "</TD><TD>", "</TD></TR>\n" } ;
boost::regex e1( regexes[ 0 ] ) ;
std::string tabletext = boost::regex_replace( csvtext , e1 ,
replacements[ 0 ] , boost::match_default | boost::format_all ) ;
for ( int i = 1 ; i < 5 ; i++ ) {
e1.assign( regexes[ i ] ) ;
tabletext = boost::regex_replace( tabletext , e1 , replacements[ i ] , boost::match_default | boost::format_all ) ;
}
tabletext = std::string( "<TABLE>\n" ) + tabletext ;
tabletext.append( "</TABLE>\n" ) ;
return tabletext ;
}
<TABLE>
<TR><TD>Character</TD><TD>Speech</TD></TR>
<TR><TD>The multitude</TD><TD>The messiah! Show us the messiah!</TD></TR>
<TR><TD>Brians mother</TD><TD><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry></TD></TR>
<TR><TD>The multitude</TD><TD>Who are you?</TD></TR>
<TR><TD>Brians mother</TD><TD>I'm his mother; that's who!</TD></TR>
<TR><TD>The multitude</TD><TD>Behold his mother! Behold his mother!</TD></TR>
</TABLE>
C#
Simple Solution
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
class Program
{
private static string ConvertCsvToHtmlTable(string csvText)
{
//split the CSV, assume no commas or line breaks in text
List<List<string>> splitString = new List<List<string>>();
List<string> lineSplit = csvText.Split('\n').ToList();
foreach (string line in lineSplit)
{
splitString.Add(line.Split(',').ToList());
}
//encode text safely, and create table
string tableResult = "<table>";
foreach(List<string> splitLine in splitString)
{
tableResult += "<tr>";
foreach(string splitText in splitLine)
{
tableResult += "<td>" + WebUtility.HtmlEncode(splitText) + "</td>";
}
tableResult += "</tr>";
}
tableResult += "</table>";
return tableResult;
}
}
<table><tr><td>Character</td><td>Speech</td></tr><tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr><tr><td>Brians mother</td><td><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry></td></tr><tr><td>The multitude</td><td>Who are you?</td></tr><tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr><tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr></table>
Extra Credit Solution
using System;
using System.Linq;
using System.Net;
namespace CsvToHtml
{
class Program
{
static void Main(string[] args)
{
string csv =
@"Character,Speech
The multitude,The messiah! Show us the messiah!
Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>
The multitude,Who are you?
Brians mother,I'm his mother; that's who!
The multitude,Behold his mother! Behold his mother!";
Console.Write(ConvertCsvToHtmlTable(csv, true));
}
private static string ConvertCsvToHtmlTable(string csvText, bool formatHeaders)
{
var rows =
(from text in csvText.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries) /* Split the string by newline,
* removing any empty rows. */
select text.Split(',')).ToArray(); // Split each row by comma.
string output = "<table>"; // Initialize the output with the value of "<table>".
for (int index = 0; index < rows.Length; index++) // Iterate through each row.
{
var row = rows[index];
var tag = (index == 0 && formatHeaders) ? "th" : "td"; /* Check if this is the first row, and if to format headers.
* If so, then set the tags as table headers.
* Otherwise, set the tags as table data. */
output += "\r\n\t<tr>"; // Add table row tag to output string.
// Add escaped cell data with proper tags to output string for each cell in row.
output = row.Aggregate(output,
(current, cell) =>
current +
string.Format("\r\n\t\t<{0}>{1}</{0}>", tag, WebUtility.HtmlEncode(cell)));
output += "\r\n\t</tr>"; // Add closing table row tag to output string.
}
output += "\r\n</table>"; // Add closing table tag to output string.
return output;
}
}
}
<tr>
<th>Character</th>
<th>Speech</th>
</tr>
<tr>
<td>The multitude</td>
<td>The messiah! Show us the messiah!</td>
</tr>
<tr>
<td>Brians mother</td>
<td><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry></td>
</tr>
<tr>
<td>The multitude</td>
<td>Who are you?</td>
</tr>
<tr>
<td>Brians mother</td>
<td>I'm his mother; that's who!</td>
</tr>
<tr>
<td>The multitude</td>
<td>Behold his mother! Behold his mother!</td>
</tr>
</table>
CoffeeScript
String::__defineGetter__ 'escaped', () ->
this.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"') // rosettacode doesn't like "
text = '''
Character,Speech
The multitude,The messiah! Show us the messiah!
Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>
The multitude,Who are you?
Brians mother,I'm his mother; that's who!
The multitude,Behold his mother! Behold his mother!
'''
lines = (line.split ',' for line in text.split /[\n\r]+/g)
header = lines.shift()
console.log """
<table cellspacing="0">
<thead>
<th scope="col">#{header[0]}</th>
<th scope="col">#{header[1]}</th>
</thead>
<tbody>
"""
for line in lines
[character, speech] = line
console.log """
<th scope="row">#{character}</th>
<td>#{speech.escaped}</td>
"""
console.log """
</tbody>
</table>
"""
<table cellspacing="0">
<thead>
<th scope="col">Character</th>
<th scope="col">Speech</th>
</thead>
<tbody>
<th scope="row">The multitude</th>
<td>The messiah! Show us the messiah!</td>
<th scope="row">Brians mother</th>
<td><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry></td>
<th scope="row">The multitude</th>
<td>Who are you?</td>
<th scope="row">Brians mother</th>
<td>I'm his mother; that's who!</td>
<th scope="row">The multitude</th>
<td>Behold his mother! Behold his mother!</td>
</tbody>
</table>
Clojure
Character,Speech
The multitude,The messiah! Show us the messiah!
Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>
The multitude,Who are you?
Brians mother,I'm his mother; that's who!
The multitude,Behold his mother! Behold his mother!
(require 'clojure.string)
(def escapes
{\< "<", \> ">", \& "&"})
(defn escape
[content]
(clojure.string/escape content escapes))
(defn tr
[cells]
(format "<tr>%s</tr>"
(apply str (map #(str "<td>" (escape %) "</td>") cells))))
;; turn a seq of seq of cells into a string.
(defn to-html
[tbl]
(format "<table><tbody>%s</tbody></thead>"
(apply str (map tr tbl))))
;; Read from a string to a seq of seq of cells.
(defn from-csv
[text]
(map #(clojure.string/split % #",")
(clojure.string/split-lines text)))
(defn -main
[]
(let [lines (line-seq (java.io.BufferedReader. *in*))
tbl (map #(clojure.string/split % #",") lines)]
(println (to-html tbl)))
<table><tbody><tr><td>Character</td><td>Speech</td></tr><tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr><tr><td>Brians mother</td><td><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry></td></tr><tr><td>The multitude</td><td>Who are you?</td></tr><tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr><tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr></tbody></thead>
Common Lisp
(defvar *csv* "Character,Speech
The multitude,The messiah! Show us the messiah!
Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>
The multitude,Who are you?
Brians mother,I'm his mother; that's who!
The multitude,Behold his mother! Behold his mother!")
(defun split-string (string delim-char)
(let ((result '()))
(do* ((start 0 (1+ end))
(end (position delim-char string)
(position delim-char string :start start)))
((not end) (reverse (cons (subseq string start) result)))
(push (subseq string start end) result))))
;;; HTML escape code modified from:
;;; http://www.gigamonkeys.com/book/practical-an-html-generation-library-the-interpreter.html
(defun escape-char (char)
(case char
(#\& "&")
(#\< "<")
(#\> ">")
(t (format nil "&#~d;" (char-code char)))))
(defun escape (in)
(let ((to-escape "<>&"))
(flet ((needs-escape-p (char) (find char to-escape)))
(with-output-to-string (out)
(loop for start = 0 then (1+ pos)
for pos = (position-if #'needs-escape-p in :start start)
do (write-sequence in out :start start :end pos)
when pos do (write-sequence (escape-char (char in pos)) out)
while pos)))))
(defun html-row (values headerp)
(let ((tag (if headerp "th" "td")))
(with-output-to-string (out)
(write-string "<tr>" out)
(dolist (val values)
(format out "<~A>~A</~A>" tag (escape val) tag))
(write-string "</tr>" out))))
(defun csv->html (csv)
(let* ((lines (split-string csv #\Newline))
(cols (split-string (first lines) #\,))
(rows (mapcar (lambda (row) (split-string row #\,)) (rest lines))))
(with-output-to-string (html)
(format html "<table>~C" #\Newline)
(format html "~C~A~C" #\Tab (html-row cols t) #\Newline)
(dolist (row rows)
(format html "~C~A~C" #\Tab (html-row row nil) #\Newline))
(write-string "</table>" html))))
CL-USER> (csv->html *csv*)
<tr><th>Character</th><th>Speech</th></tr>
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
<tr><td>Brians mother</td><td><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry></td></tr>
<tr><td>The multitude</td><td>Who are you?</td></tr>
<tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table>
D
void main() {
import std.stdio;
immutable input =
"Character,Speech\n" ~
"The multitude,The messiah! Show us the messiah!\n" ~
"Brians mother,<angry>Now you listen here! He's not the messiah; " ~
"he's a very naughty boy! Now go away!</angry>\n" ~
"The multitude,Who are you?\n" ~
"Brians mother,I'm his mother; that's who!\n" ~
"The multitude,Behold his mother! Behold his mother!";
"<html>\n<head><meta charset=\"utf-8\"></head>\n<body>\n\n".write;
"<table border=\"1\" cellpadding=\"5\" cellspacing=\"0\">\n<thead>\n <tr><td>".write;
bool theadDone = false;
foreach (immutable c; input) {
switch(c) {
case '\n':
if (theadDone) {
"</td></tr>\n <tr><td>".write;
} else {
"</td></tr>\n</thead>\n<tbody>\n <tr><td>".write;
theadDone = true;
}
break;
case ',': "</td><td>".write; break;
case '<': "<".write; break;
case '>': ">".write; break;
case '&': "&".write; break;
default: c.write; break;
}
}
"</td></tr>\n</tbody>\n</table>\n\n</body></html>".write;
}
<head><meta charset="utf-8"></head>
<body>
<table border="1" cellpadding="5" cellspacing="0">
<thead>
<tr><td>Character</td><td>Speech</td></tr>
</thead>
<tbody>
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
<tr><td>Brians mother</td><td><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry></td></tr>
<tr><td>The multitude</td><td>Who are you?</td></tr>
<tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</tbody>
</table>
</body>
</html>
Delphi
program csv2html;
{$APPTYPE CONSOLE}
uses
SysUtils,
Classes;
const
// Carriage Return/Line Feed
CRLF = #13#10;
// The CSV data
csvData =
'Character,Speech'+CRLF+
'The multitude,The messiah! Show us the messiah!'+CRLF+
'Brians mother,<angry>Now you listen here! He''s not the messiah; he''s a very naughty boy! Now go away!</angry>'+CRLF+
'The multitude,Who are you?'+CRLF+
'Brians mother,I''m his mother; that''s who!'+CRLF+
'The multitude,Behold his mother! Behold his mother!';
// HTML header
htmlHead =
'<!DOCTYPE html'+CRLF+
'PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"'+CRLF+
'"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'+CRLF+
'<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">'+CRLF+
'<head>'+CRLF+
'<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />'+CRLF+
'<title>CSV-to-HTML Conversion</title>'+CRLF+
'<style type="text/css">'+CRLF+
'body {font-family:verdana,helvetica,sans-serif;font-size:100%}'+CRLF+
'table {width:70%;border:0;font-size:80%;margin:auto}'+CRLF+
'th,td {padding:4px}'+CRLF+
'th {text-align:left;background-color:#eee}'+CRLF+
'th.c {width:15%}'+CRLF+
'td.c {width:15%}'+CRLF+
'</style>'+CRLF+
'</head>'+CRLF+
'<body>'+CRLF;
// HTML footer
htmlFoot =
'</body>'+CRLF+
'</html>';
{ Function to split a string into a list using a given delimiter }
procedure SplitString(S, Delim: string; Rslt: TStrings);
var
i: integer;
fld: string;
begin
fld := '';
for i := Length(S) downto 1 do
begin
if S[i] = Delim then
begin
Rslt.Insert(0,fld);
fld := '';
end
else
fld := S[i]+fld;
end;
if (fld <> '') then
Rslt.Insert(0,fld);
end;
{ Simple CSV parser with option to specify that the first row is a header row }
procedure ParseCSV(const csvIn: string; htmlOut: TStrings; FirstRowIsHeader: Boolean = True);
const
rowstart = '<tr><td class="c">';
rowend = '</td></tr>';
cellendstart = '</td><td class="s">';
hcellendstart = '</th><th class="s">';
hrowstart = '<tr><th class="c">';
hrowend = '</th></tr>';
var
tmp,pieces: TStrings;
i: Integer;
begin
// HTML header
htmlOut.Text := htmlHead + CRLF + CRLF;
// Start the HTML table
htmlOut.Text := htmlOut.Text + '<table summary="csv2table conversion">' + CRLF;
// Create stringlist
tmp := TStringList.Create;
try
// Assign CSV data to stringlist and fix occurences of '<' and '>'
tmp.Text := StringReplace(csvIn,'<','<',[rfReplaceAll]);
tmp.Text := StringReplace(tmp.Text,'>','>',[rfReplaceAll]);
// Create stringlist to hold the parts of the split data
pieces := TStringList.Create;
try
// Loop through the CSV rows
for i := 0 to Pred(tmp.Count) do
begin
// Split the current row
SplitString(tmp[i],',',pieces);
// Check if first row and FirstRowIsHeader flag set
if (i = 0) and FirstRowIsHeader then
// Render HTML
htmlOut.Text := htmlOut.Text + hrowstart + pieces[0] + hcellendstart + pieces[1] + hrowend + CRLF
else
htmlOut.Text := htmlOut.Text + rowstart + pieces[0] + cellendstart + pieces[1] + rowend + CRLF;
end;
// Finish the HTML table and end the HTML page
htmlOut.Text := htmlOut.Text + '</table>' + CRLF + htmlFoot;
finally
pieces.Free;
end;
finally
tmp.Free;
end;
end;
var
HTML: TStrings;
begin
// Create stringlist to hold HTML output
HTML := TStringList.Create;
try
Writeln('Basic:');
Writeln('');
// Load and parse the CSV data
ParseCSV(csvData,HTML,False);
// Output the HTML to the console
Writeln(HTML.Text);
// Save the HTML to a file (in application's folder)
HTML.SaveToFile('csv2html_basic.html');
Writeln('');
Writeln('
### ===============================
');
Writeln('');
HTML.Clear;
Writeln('Extra Credit:');
Writeln('');
// Load and parse the CSV data
ParseCSV(csvData,HTML,True);
// Output the HTML to the console
Writeln(HTML.Text);
// Save the HTML to a file (in application's folder)
HTML.SaveToFile('csv2html_extra.html');
Writeln('');
Writeln('
### ===============================
');
finally
HTML.Free;
end;
// Keep console window open
Readln;
end.
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
<title>CSV-to-HTML Conversion</title>
<style type="text/css">
body {font-family:verdana,helvetica,sans-serif;font-size:100%}
table {width:70%;border:0;font-size:80%;margin:auto}
th,td {padding:4px}
th {text-align:left;background-color:#eee}
th.c {width:15%}
td.c {width:15%}
</style>
</head>
<body>
<table summary="csv2table conversion">
<tr><td class="c">Character</td><td class="s">Speech</td></tr>
<tr><td class="c">The multitude</td><td class="s">The messiah! Show us the messiah!</td></tr>
<tr><td class="c">Brians mother</td><td class="s"><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry></td></tr>
<tr><td class="c">The multitude</td><td class="s">Who are you?</td></tr>
<tr><td class="c">Brians mother</td><td class="s">I'm his mother; that's who!</td></tr>
<tr><td class="c">The multitude</td><td class="s">Behold his mother! Behold his mother!</td></tr>
</table>
</body>
</html>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
<title>CSV-to-HTML Conversion</title>
<style type="text/css">
body {font-family:verdana,helvetica,sans-serif;font-size:100%}
table {width:70%;border:0;font-size:80%;margin:auto}
th,td {padding:4px}
th {text-align:left;background-color:#eee}
th.c {width:15%}
td.c {width:15%}
</style>
</head>
<body>
<table summary="csv2table conversion">
<tr><th class="c">Character</th><th class="s">Speech</th></tr>
<tr><td class="c">The multitude</td><td class="s">The messiah! Show us the messiah!</td></tr>
<tr><td class="c">Brians mother</td><td class="s"><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry></td></tr>
<tr><td class="c">The multitude</td><td class="s">Who are you?</td></tr>
<tr><td class="c">Brians mother</td><td class="s">I'm his mother; that's who!</td></tr>
<tr><td class="c">The multitude</td><td class="s">Behold his mother! Behold his mother!</td></tr>
</table>
</body>
</html>
EchoLisp
;; CSV -> LISTS
(define (csv->row line) (string-split line ","))
(define (csv->table csv) (map csv->row (string-split csv "\n")))
;; LISTS->HTML
(define html 'html)
(define (emit-tag tag html-proc content )
(if (style tag)
(push html (format "<%s style='%a'>" tag (style tag)))
(push html (format "<%s>" tag )))
(html-proc content)
(push html (format "</%s> " tag )))
;; html procs : 1 tag, 1 proc
(define (h-raw content)
(push html (format "%s" content)))
(define (h-header headers)
(for ((h headers)) (emit-tag 'th h-raw h)))
(define (h-row row)
(for ((item row)) (emit-tag 'td h-raw item)))
(define (h-table table )
(emit-tag 'tr h-header (first table))
(for ((row (rest table))) (emit-tag 'tr h-row row)))
(define (html-dump) (string-join (stack->list html) " "))
;; STYLES
(style 'td "text-align:left")
(style 'table "border-spacing: 10px;border:28px ridge orange") ;; special biblical border
(style 'th "color:blue;")
;; changed <angry> to <b> to show that html tags inside text are correctly transmitted.
(define MontyPython #<<
Character,Speech
The multitude,The messiah! Show us the messiah!
Brians mother,<b>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</b>
The multitude,Who are you?
Brians mother,I'm his mother; that's who!
The multitude,Behold his mother! Behold his mother!
>>#)
(define (task speech)
(define table (csv->table speech))
(stack html)
(emit-tag 'table h-table table)
(html-dump))
(task MontyPython)
Character Speech The multitude The messiah! Show us the messiah! Brians mother Now you listen here! He's not the messiah; he's a very naughty boy! Now go away! The multitude Who are you? Brians mother I'm his mother; that's who! The multitude Behold his mother! Behold his mother! Erlang
-module( csv_to_html ).
-export( [table_translation/1, task/0] ).
table_translation( CSV ) ->
[Headers | Contents] = [string:tokens(X, ",") || X <- string:tokens( CSV, "\n")],
Table = create_html_table:html_table( [{border, "1"}, {cellpadding, "10"}], Headers, Contents ),
create_html_table:external_format( Table ).
task() -> table_translation( csv() ).
csv() ->
"Character,Speech
The multitude,The messiah! Show us the messiah!
Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>
The multitude,Who are you?
Brians mother,I'm his mother; that's who!
The multitude,Behold his mother! Behold his mother!".
Character Speech The multitude The messiah! Show us the messiah! Brians mother <angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry> The multitude Who are you? Brians mother I'm his mother; that's who! The multitude Behold his mother! Behold his mother! Euphoria
constant input = "Character,Speech\n" &
"The multitude,The messiah! Show us the messiah!\n" &
"Brians mother,<angry>Now you listen here! He's not the messiah; " &
"he's a very naughty boy! Now go away!</angry>\n" &
"The multitude,Who are you?\n" &
"Brians mother,I'm his mother; that's who!\n" &
"The multitude,Behold his mother! Behold his mother!"
puts(1,"<table>\n<tr><td>")
for i = 1 to length(input) do
switch input[i] do
case '\n' then puts(1,"</td></tr>\n<tr><td>")
case ',' then puts(1,"</td><td>")
case '<' then puts(1,"<")
case '>' then puts(1,">")
case '&' then puts(1,"&")
case else puts(1,input[i])
end switch
end for
puts(1,"</td></tr>\n</table>")
<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>
open System
open System.Text
open System.Xml
let data = """
Character,Speech
The multitude,The messiah! Show us the messiah!
Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>
The multitude,Who are you?
Brians mother,I'm his mother; that's who!
The multitude,Behold his mother! Behold his mother!
"""
let csv =
Array.map
(fun (line : string) -> line.Split(','))
(data.Trim().Split([|'\n';'\r'|],StringSplitOptions.RemoveEmptyEntries))
[<EntryPoint>]
let main argv =
let style = argv.Length > 0 && argv.[0] = "-h"
Console.OutputEncoding <- UTF8Encoding()
let xs = XmlWriterSettings()
xs.Indent <- true // be friendly to humans
use x = XmlWriter.Create(Console.Out, xs)
x.WriteStartDocument()
x.WriteDocType("HTML", null, null, null) // HTML5
x.WriteStartElement("html")
x.WriteStartElement("head")
x.WriteElementString("title", "Rosettacode - CSV to HTML translation")
if style then
x.WriteStartElement("style"); x.WriteAttributeString("type", "text/css")
x.WriteString("""
table { border-collapse: collapse; }
td, th { border: 1px solid black; padding: .25em}
th { background-color: #EEE; }
tbody th { font-weight: normal; font-size: 85%; }
""")
x.WriteEndElement() // style
x.WriteEndElement() // head
x.WriteStartElement("body")
x.WriteStartElement("table")
x.WriteStartElement("thead"); x.WriteStartElement("tr")
for part in csv.[0] do x.WriteElementString("th", part)
x.WriteEndElement(); x.WriteEndElement() // tr thead
x.WriteStartElement("tbody")
for line in csv.[1..] do
x.WriteStartElement("tr")
x.WriteElementString("th", line.[0])
x.WriteElementString("td", line.[1])
x.WriteEndElement() // tr
x.Close()
0
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE HTML >
<html>
<head>
<title>Rosettacode - CSV to HTML translation</title>
<style type="text/css">
table { border-collapse: collapse; }
td, th { border: 1px solid black; padding: .25em}
th { background-color: #EEE; }
tbody th { font-weight: normal; font-size: 85%; }
</style>
</head>
<body>
<table>
<thead>
<tr>
<th>Character</th>
<th>Speech</th>
</tr>
</thead>
<tbody>
<tr>
<th>The multitude</th>
<td>The messiah! Show us the messiah!</td>
</tr>
<tr>
<th>Brians mother</th>
<td><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry></td>
</tr>
<tr>
<th>The multitude</th>
<td>Who are you?</td>
</tr>
<tr>
<th>Brians mother</th>
<td>I'm his mother; that's who!</td>
</tr>
<tr>
<th>The multitude</th>
<td>Behold his mother! Behold his mother!</td>
</tr>
</tbody>
</table>
</body>
</html>
Factor
USING: combinators csv io kernel sequences strings ;
IN: rosetta-code.csv-to-html
CONSTANT: input
"Character,Speech
The multitude,The messiah! Show us the messiah!
Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>
The multitude,Who are you?
Brians mother,I'm his mother; that's who!
The multitude,Behold his mother! Behold his mother!"
: escape-chars ( seq -- seq' )
[
{
{ CHAR: & [ "&" ] }
{ CHAR: ' [ "'" ] }
{ CHAR: < [ "<" ] }
{ CHAR: > [ ">" ] }
[ 1string ]
} case
] { } map-as concat ;
: tag ( str tag -- <tag>str</tag> )
[ "<" ">" surround ] [ "</" ">" surround ] bi surround ;
: csv>table ( seq -- str )
[ [ "td" tag ] map concat "tr" tag " " prepend ] map
{ "<table>" } prepend { "</table>" } append "\n" join ;
input escape-chars string>csv csv>table print
<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>
Forth
: BEGIN-COLUMN ." <td>" ;
: END-COLUMN ." </td>" ;
: BEGIN-ROW ." <tr>" BEGIN-COLUMN ;
: END-ROW END-COLUMN ." </tr>" CR ;
: CSV2HTML
." <table>" CR BEGIN-ROW
BEGIN KEY DUP #EOF <> WHILE
CASE
10 OF END-ROW BEGIN-ROW ENDOF
[CHAR] , OF END-COLUMN BEGIN-COLUMN ENDOF
[CHAR] < OF ." <" ENDOF
[CHAR] > OF ." >" ENDOF
[CHAR] & OF ." &" ENDOF
DUP EMIT
ENDCASE
REPEAT
END-ROW ." </table>" CR
;
CSV2HTML BYE
<tr><td>Character</td><td>Speech</td></tr>
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
<tr><td>Brians mother</td><td><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry></td></tr>
<tr><td>The multitude</td><td>Who are you?</td></tr>
<tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table>
Fortran
(a,b,c, I = 1,N)
where ''a'', ''b'' and ''c'' constitute a three-element list of items - any of which could themselves be an implied DO-list if nested lists were desired. Thus, by one means or another, the WRITE statement produces a sequence of values (of various types) to be sent forth. In association with the list of entries in the nominated FORMAT statement: processing proceeds in parallel through both lists in the manner of coroutines, not sequentially. It is possible with a READ statement that a value just read adjusts a part of a FORMAT statement not yet encountered during the processing of the READ statement. For instance FORMAT 13 could have ,A
in the FORMAT, or, the "/tr" could be removed from the WRITE and appended to end the FORMAT just as at its start, but enough.ALINE(I:I).EQ.","
because in other work this usage has been found to evoke astoundingly bad code, notably that ''both'' appearances of "I" are checked as being within bounds, and, the length is calculated by subtracting the "first" (I) from the "last" (also I) at run time! At least by the COMPAQ F90/95 compiler. By contrast, the ICHAR usage, which can only be for a single character, lacks this madness and far superior speed results. Not important in this example, but it explains why this puzzling usage appeared in a prog. at the Culham Science Centre in source from an IBM mainframe.
SUBROUTINE CSVTEXT2HTML(FNAME,HEADED) !Does not recognise quoted strings.
Converts without checking field counts, or noting special characters.
CHARACTER*(*) FNAME !Names the input file.
LOGICAL HEADED !Perhaps its first line is to be a heading.
INTEGER MANY !How long is a piece of string?
PARAMETER (MANY=666) !This should suffice.
CHARACTER*(MANY) ALINE !A scratchpad for the input.
INTEGER MARK(0:MANY + 1) !Fingers the commas on a line.
INTEGER I,L,N !Assistants.
CHARACTER*2 WOT(2) !I don't see why a "table datum" could not be for either.
PARAMETER (WOT = (/"th","td"/)) !A table heding or a table datum
INTEGER IT !But, one must select appropriately.
INTEGER KBD,MSG,IN !A selection.
COMMON /IOUNITS/ KBD,MSG,IN !The caller thus avoids collisions.
OPEN(IN,FILE=FNAME,STATUS="OLD",ACTION="READ",ERR=661) !Go for the file.
WRITE (MSG,1) !Start the blather.
1 FORMAT ("<Table border=1>") !By stating that a table follows.
MARK(0) = 0 !Syncopation for the comma fingers.
N = 0 !No records read.
10 READ (IN,11,END = 20) L,ALINE(1:MIN(L,MANY)) !Carefully acquire some text.
11 FORMAT (Q,A) !Q = number of characters yet to read, A = characters.
N = N + 1 !So, a record has been read.
IF (L.GT.MANY) THEN !Perhaps it is rather long?
WRITE (MSG,12) N,L,MANY !Alas!
12 FORMAT ("Line ",I0," has length ",I0,"! My limit is ",I0) !Squawk/
L = MANY !The limit actually read.
END IF !So much for paranoia.
IF (N.EQ.1 .AND. HEADED) THEN !Is the first line to be treated specially?
WRITE (MSG,*) "<tHead>" !Yep. Nominate a heading.
IT = 1 !And select "th" rather than "td".
ELSE !But mostly,
IT = 2 !Just another row for the table.
END IF !So much for the first line.
NCOLS = 0 !No commas have been seen.
DO I = 1,L !So scan the text for them.
IF (ICHAR(ALINE(I:I)).EQ.ICHAR(",")) THEN !Here?
NCOLS = NCOLS + 1 !Yes!
MARK(NCOLS) = I !The texts are between commas.
END IF !So much for that character.
END DO !On to the next.
NCOLS = NCOLS + 1 !This is why the + 1 for the size of MARK.
MARK(NCOLS) = L + 1 !End-of-line is as if a comma was one further along.
WRITE (MSG,13) !Now roll all the texts.
1 (WOT(IT), !This starting a cell,
2 ALINE(MARK(I - 1) + 1:MARK(I) - 1), !This being the text between the commas,
3 WOT(IT), !And this ending each cell.
4 I = 1,NCOLS), !For this number of columns.
5 "/tr" !And this ends the row.
13 FORMAT (" <tr>",666("<",A,">",A,"</",A,">")) !How long is a piece of string?
IF (N.EQ.1 .AND. HEADED) WRITE (MSG,*) "</tHead>" !Finish the possible header.
GO TO 10 !And try for another record.
20 CLOSE (IN) !Finished with input.
WRITE (MSG,21) !And finished with output.
21 FORMAT ("</Table>") !This writes starting at column one.
RETURN !Done!
Confusions.
661 WRITE (MSG,*) "Can't open file ",FNAME !Alas.
END !So much for the conversion.
INTEGER KBD,MSG,IN
COMMON /IOUNITS/ KBD,MSG,IN
KBD = 5 !Standard input.
MSG = 6 !Standard output.
IN = 10 !Some unspecial number.
CALL CSVTEXT2HTML("Text.csv",.FALSE.) !The first line is not special.
WRITE (MSG,*)
CALL CSVTEXT2HTML("Text.csv",.TRUE.) !The first line is a heading.
END
<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>
Character Speech The multitude The messiah! Show us the messiah! Brian's mother The multitude Who are you? Brian's mother I'm his mother; that's who! The multitude Behold his mother! Behold his mother!
Character Speech The multitude The messiah! Show us the messiah! Brian's mother The multitude Who are you? Brian's mother I'm his mother; that's who! The multitude Behold his mother! Behold his mother! Go
package main
import (
"bytes"
"encoding/csv"
"fmt"
"html/template"
"strings"
)
var c = `Character,Speech
The multitude,The messiah! Show us the messiah!
Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>
The multitude,Who are you?
Brians mother,I'm his mother; that's who!
The multitude,Behold his mother! Behold his mother!`
func main() {
if h, err := csvToHtml(c); err != nil {
fmt.Println(err)
} else {
fmt.Print(h)
}
}
func csvToHtml(c string) (string, error) {
data, err := csv.NewReader(bytes.NewBufferString(c)).ReadAll()
if err != nil {
return "", err
}
var b strings.Builder
err = template.Must(template.New("").Parse(`<table>
{{range .}} <tr>{{range .}}<td>{{.}}</td>{{end}}</tr>
{{end}}</table>
`)).Execute(&b, data)
return b.String(), err
}
package main
import (
"bytes"
"encoding/csv"
"flag"
"fmt"
"html/template"
"strings"
)
var csvStr = `Character,Speech
The multitude,The messiah! Show us the messiah!
Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>
The multitude,Who are you?
Brians mother,I'm his mother; that's who!
The multitude,Behold his mother! Behold his mother!`
func main() {
headings := flag.Bool("h", false, "format first row as column headings")
flag.Parse()
if html, err := csvToHtml(csvStr, *headings); err != nil {
fmt.Println(err)
} else {
fmt.Print(html)
}
}
func csvToHtml(csvStr string, headings bool) (string, error) {
data, err := csv.NewReader(bytes.NewBufferString(csvStr)).ReadAll()
if err != nil {
return "", err
}
tStr := tPlain
if headings {
tStr = tHeadings
}
var b strings.Builder
err = template.Must(template.New("").Parse(tStr)).Execute(&b, data)
return b.String(), err
}
const (
tPlain = `<table>
{{range .}} <tr>{{range .}}<td>{{.}}</td>{{end}}</tr>
{{end}}</table>
`
tHeadings = `<table>{{if .}}
{{range $x, $e := .}}{{if $x}}
<tr>{{range .}}<td>{{.}}</td>{{end}}</tr>{{else}} <thead>
<tr>{{range .}}<th>{{.}}</th>{{end}}</tr>
</thead>
<tbody>{{end}}{{end}}
</tbody>{{end}}
</table>
`
)
<thead>
<tr><th>Character</th><th>Speech</th></tr>
</thead>
<tbody>
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
<tr><td>Brians mother</td><td><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry></td></tr>
<tr><td>The multitude</td><td>Who are you?</td></tr>
<tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</tbody>
</table>
<tr><td>Character</td><td>Speech</td></tr>
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
<tr><td>Brians mother</td><td><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry></td></tr>
<tr><td>The multitude</td><td>Who are you?</td></tr>
<tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table>
Groovy
Solution #1: Nested GStrings
def formatCell = { cell ->
"<td>${cell.replaceAll('&','&').replaceAll('<','<')}</td>"
}
def formatRow = { row ->
"""<tr>${row.split(',').collect { cell -> formatCell(cell) }.join('')}</tr>
"""
}
def formatTable = { csv, header=false ->
def rows = csv.split('\n').collect { row -> formatRow(row) }
header \
? """
<table>
<thead>
${rows[0]}</thead>
<tbody>
${rows[1..-1].join('')}</tbody>
</table>
""" \
: """
<table>
${rows.join('')}</table>
"""
}
def formatPage = { title, csv, header=false ->
"""<html>
<head>
<title>${title}</title>
<style type="text/css">
td {background-color:#ddddff; }
thead td {background-color:#ddffdd; text-align:center; }
</style>
</head>
<body>${formatTable(csv, header)}</body>
</html>"""
}
def csv = '''Character,Speech
The multitude,The messiah! Show us the messiah!
Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>
The multitude,Who are you?
Brians mother,I'm his mother; that's who!
The multitude,Behold his mother! Behold his mother!'''
println 'Basic:'
println '-----------------------------------------'
println (formatPage('Basic', csv))
println '-----------------------------------------'
println()
println()
println 'Extra Credit:'
println '-----------------------------------------'
println (formatPage('Extra Credit', csv, true))
println '-----------------------------------------'
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!