CSV to HTML translation: Difference between revisions

(→‎{{header|Groovy}}: new solution)
Line 113:
return EXIT_SUCCESS;
}</lang>
 
=={{header|Groovy}}==
This is a brute force solution using GStrings. It solves both the basic and extra credit versions.
<lang groovy>def formatCell = { cell ->
"<td>${cell.replaceAll('&','&amp;').replaceAll('<','&lt;')}</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>"""
}</lang>
 
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:
<lang html><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>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!&lt;/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></lang>
 
Extra Credit output:
<lang html><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>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!&lt;/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></lang>
 
=={{header|Icon}} and {{header|Unicon}}==
Anonymous user