CSV to HTML translation: Difference between revisions

→‎{{header|Groovy}}: extra (builder) solution plus browser images
(→‎Extra credit solution: Marked incorrect (Prolog), No escaping of '<' in '<angry' and '</angry')
(→‎{{header|Groovy}}: extra (builder) solution plus browser images)
Line 116:
 
=={{header|Groovy}}==
 
This is a brute force solution using GStrings. It solves both the basic and extra credit versions.
===Solution #1: Nested GStrings===
Brute force solution using nested GStrings. It solves both the basic and extra credit tasks.
<lang groovy>def formatCell = { cell ->
"<td>${cell.replaceAll('&','&amp;').replaceAll('<','&lt;')}</td>"
Line 157 ⟶ 159:
 
Test:
<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>
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 "'-----------------------------------------"'</lang>
 
Basic output:
<pre style="height:30ex;overflow:scroll;"><html>
<lang html><html>
<head>
<title>Basic</title>
Line 194 ⟶ 196:
</table>
</body>
</html></langpre>
 
"Basic" HTML appearance as rendered in Google Chrome:<br/>
[[File:Groovy-csv-to-html-basic.jpg]]
 
Extra Credit output:
<pre style="height:30ex;overflow:scroll;"><html>
<lang html><html>
<head>
<title>Extra Credit</title>
Line 219 ⟶ 224:
</table>
</body>
</html></langpre>
 
"Extra Credit" HTML appearance as rendered in Google Chrome:<br/>
[[File:Groovy-csv-to-html-extra.jpg]]
 
===Solution #2: MarkupBuilder===
A much cleaner solution using the Groovy XML MarkupBuilder class. It solves both the basic and extra credit tasks.
<lang groovy>import groovy.xml.MarkupBuilder
 
def formatRow = { doc, row ->
doc.tr { row.each { cell -> td { mkp.yield(cell) } } }
}
 
def formatPage = { titleString, csv, header=false ->
def writer = new StringWriter()
def doc = new MarkupBuilder(writer)
def rows = csv.split('\n').collect { row -> row.split(',') }
doc.html {
head {
title (titleString)
style (type:"text/css") {
mkp.yield('''
td {background-color:#ddddff; }
thead td {background-color:#ddffdd; text-align:center; }
''')
}
}
body {
table {
header && thead { formatRow(doc, rows[0]) }
header && tbody { rows[1..-1].each { formatRow(doc, it) } }
header || rows.each { formatRow(doc, it) }
}
}
}
writer.toString()
}</lang>
 
Test:<br/>
The interface is the same for both solutions, so we just reuse the same test as before.
 
Basic output:
<pre style="height:30ex;overflow:scroll;"><html>
<head>
<title>Basic</title>
<style type='text/css'>
td {background-color:#ddddff; }
thead td {background-color:#ddffdd; text-align:center; }
</style>
</head>
<body>
<table>
<tr>
<td>Character</td>
<td>Speech</td>
</tr>
<tr>
<td>The multitude</td>
<td>The messiah! Show us the messiah!</td>
</tr>
<tr>
<td>Brians mother</td>
<td>&lt;angry&gt;Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!&lt;/angry&gt;</td>
</tr>
<tr>
<td>The multitude</td>
<td>Who are you?</td>
</tr>
<tr>
<td>Brians mother</td>
<td>I'm his mother; that's who!</td>
</tr>
<tr>
<td>The multitude</td>
<td>Behold his mother! Behold his mother!</td>
</tr>
</table>
</body>
</html></pre>
 
The "Basic" 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:
<pre style="height:30ex;overflow:scroll;"><html>
<head>
<title>Extra Credit</title>
<style type='text/css'>
td {background-color:#ddddff; }
thead td {background-color:#ddffdd; text-align:center; }
</style>
</head>
<body>
<table>
<thead>
<tr>
<td>Character</td>
<td>Speech</td>
</tr>
</thead>
<tbody>
<tr>
<td>The multitude</td>
<td>The messiah! Show us the messiah!</td>
</tr>
<tr>
<td>Brians mother</td>
<td>&lt;angry&gt;Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!&lt;/angry&gt;</td>
</tr>
<tr>
<td>The multitude</td>
<td>Who are you?</td>
</tr>
<tr>
<td>Brians mother</td>
<td>I'm his mother; that's who!</td>
</tr>
<tr>
<td>The multitude</td>
<td>Behold his mother! Behold his mother!</td>
</tr>
</tbody>
</table>
</body>
</html></pre>
 
The "Extra Credit" HTML for this solution looks superficially different than that from the GString solution, but the appearance as rendered in Google Chrome is identical.
 
=={{header|Icon}} and {{header|Unicon}}==
Anonymous user