CSV to HTML translation: Difference between revisions

Added F# version
m (Added racket implementation. Fixed lang html -> html5)
(Added F# version)
Line 1,129:
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table></pre>
 
=={{header|F_Sharp|F#}}==
Use .NET XmlWriter.
Stylesheet styling is applied only when command line option <tt>-h</tt> ist given.
<lang fsharp>open System
open System.Text
open System.Xml
 
let data = """
Character,Speech
The multitude,The messiah! Show us the messiah!
Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>
The multitude,Who are you?
Brians mother,I'm his mother; that's who!
The multitude,Behold his mother! Behold his mother!
"""
 
let csv =
Array.map
(fun (line : string) -> line.Split(','))
(data.Trim().Split([|'\n';'\r'|],StringSplitOptions.RemoveEmptyEntries))
 
 
[<EntryPoint>]
let main argv =
let style = argv.Length > 0 && argv.[0] = "-h"
Console.OutputEncoding <- UTF8Encoding()
let xs = XmlWriterSettings()
xs.Indent <- true // be friendly to humans
use x = XmlWriter.Create(Console.Out, xs)
x.WriteStartDocument()
x.WriteDocType("HTML", null, null, null) // HTML5
x.WriteStartElement("html")
x.WriteStartElement("head")
x.WriteElementString("title", "Rosettacode - CSV to HTML translation")
if style then
x.WriteStartElement("style"); x.WriteAttributeString("type", "text/css")
x.WriteString("""
table { border-collapse: collapse; }
td, th { border: 1px solid black; padding: .25em}
th { background-color: #EEE; }
tbody th { font-weight: normal; font-size: 85%; }
""")
x.WriteEndElement() // style
x.WriteEndElement() // head
x.WriteStartElement("body")
x.WriteStartElement("table")
x.WriteStartElement("thead"); x.WriteStartElement("tr")
for part in csv.[0] do x.WriteElementString("th", part)
x.WriteEndElement(); x.WriteEndElement() // tr thead
x.WriteStartElement("tbody")
for line in csv.[1..] do
x.WriteStartElement("tr")
x.WriteElementString("th", line.[0])
x.WriteElementString("td", line.[1])
x.WriteEndElement() // tr
x.Close()
0</lang>
Output (stylesheet version)
<div style="font-size:70%">
<lang html><?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>&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>
<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></lang></div>
 
=={{header|Go}}==
Anonymous user