CSV to HTML translation: Difference between revisions
Content added Content deleted
(→Extra credit solution: Marked incorrect (Prolog), No escaping of '<' in '<angry' and '</angry') |
(→{{header|Groovy}}: extra (builder) solution plus browser images) |
||
Line 116: | Line 116: | ||
=={{header|Groovy}}== |
=={{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 -> |
<lang groovy>def formatCell = { cell -> |
||
"<td>${cell.replaceAll('&','&').replaceAll('<','<')}</td>" |
"<td>${cell.replaceAll('&','&').replaceAll('<','<')}</td>" |
||
Line 157: | Line 159: | ||
Test: |
Test: |
||
<lang groovy>def csv = |
<lang groovy>def csv = '''Character,Speech |
||
The multitude,The messiah! Show us the messiah! |
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> |
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? |
The multitude,Who are you? |
||
Brians mother,I'm his mother; that's who! |
Brians mother,I'm his mother; that's who! |
||
The multitude,Behold his mother! Behold his mother! |
The multitude,Behold his mother! Behold his mother!''' |
||
println |
println 'Basic:' |
||
println |
println '-----------------------------------------' |
||
println (formatPage( |
println (formatPage('Basic', csv)) |
||
println |
println '-----------------------------------------' |
||
println() |
println() |
||
println() |
println() |
||
println |
println 'Extra Credit:' |
||
println |
println '-----------------------------------------' |
||
println (formatPage( |
println (formatPage('Extra Credit', csv, true)) |
||
println |
println '-----------------------------------------'</lang> |
||
Basic output: |
Basic output: |
||
<pre style="height:30ex;overflow:scroll;"><html> |
|||
<lang html><html> |
|||
<head> |
<head> |
||
<title>Basic</title> |
<title>Basic</title> |
||
Line 194: | Line 196: | ||
</table> |
</table> |
||
</body> |
</body> |
||
</html></ |
</html></pre> |
||
"Basic" HTML appearance as rendered in Google Chrome:<br/> |
|||
[[File:Groovy-csv-to-html-basic.jpg]] |
|||
Extra Credit output: |
Extra Credit output: |
||
<pre style="height:30ex;overflow:scroll;"><html> |
|||
<lang html><html> |
|||
<head> |
<head> |
||
<title>Extra Credit</title> |
<title>Extra Credit</title> |
||
Line 219: | Line 224: | ||
</table> |
</table> |
||
</body> |
</body> |
||
</html></ |
</html></pre> |
||
"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><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></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><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></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}}== |
=={{header|Icon}} and {{header|Unicon}}== |