Jump to content

CSV to HTML translation: Difference between revisions

Add NetRexx implementation
(Add NetRexx implementation)
Line 2,133:
</body>
</html></lang>
 
=={{header|NetRexx}}==
Uses the [[NetRexx]] solution for [[Read_a_file_line_by_line#Using_Java_Scanner|Read a file line by line]] to read the CSV file into the program.
<lang NetRexx>/* NetRexx */
options replace format comments java crossref symbols nobinary
 
parse arg inFileName .
if inFileName = '' | inFileName = '.' then inFileName = './data/Brian.csv'
csv = RREadFileLineByLine01.scanFile(inFileName)
 
header = htmlHeader()
pre = htmlCsvText(csv, inFileName)
table = htmlCsvTable(csv, inFileName)
footer = htmlFooter()
 
say header
say pre
say table
say footer
 
return
 
method htmlHeader() public static returns Rexx
html = '<?xml version="1.0" encoding="UTF-8"?>\n' -
|| '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n' -
|| '<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">\n' -
|| '<head>\n' -
|| '<meta http-equiv="Content-type" content="text/html;charset=UTF-8"/>\n' -
|| '<title>RCsv2Html</title>\n' -
|| '<style type="text/css">\n' -
|| '<!--\n' -
|| '/* <![DATA[ */\n' -
|| 'body {\n' -
|| ' font-family: "Verdana", "Geneva", "Helvetica Neue", "Helvetica", "DejaVu Sans", "Arial", sans-serif;\n' -
|| '}\n' -
|| 'table, th, td {\n' -
|| ' border: 1px solid black;\n' -
|| ' border-collapse: collapse;\n' -
|| ' padding: 0.25em;\n' -
|| ' font-size: 85%;\n' -
|| '}\n' -
|| 'th {\n' -
|| ' color: white;\n' -
|| ' background-color: green;\n' -
|| '}\n' -
|| 'p.classname {\n' -
|| ' font-size: inherit;\n' -
|| '}\n' -
|| '/* ]] */\n' -
|| '//-->\n' -
|| '</style>\n' -
|| '</head>\n' -
|| '<body>\n' -
|| '<h1>Rosetta Code &ndash; NetRexx Sample Output</h2>\n' -
|| '<h2><a href="http://rosettacode.org/wiki/CSV_to_HTML_translation">CSV to HTML translation</a></h2>\n' -
|| ''
 
return html
 
method htmlFooter() public static returns Rexx
html = '</body>\n' -
|| '</html>\n' -
|| ''
return html
 
method htmlCsvText(csv, fileName = '.') public static returns Rexx
html = '<h3>Contents of CSV <code>'fileName'</code></h3>\n' -
|| '<pre>\n' -
|| ''
loop row = 1 to csv[0]
html = html || csv[row]'\n'
end row
html = html -
|| '</pre>\n' -
|| ''
return html
 
method htmlCsvTable(csv, fileName = '.') public static returns Rexx
html = '<table>\n' -
|| '<caption>Translation of CSV <code>'fileName'</code></caption>\n' -
|| '<thead>\n' -
|| ''
html = html -
|| htmlCsvTableRow(csv[1], 'th')'\n' -
|| '</thead>\n' -
|| '<tbody>\n' -
|| ''
loop r_ = 2 to csv[0]
html = html -
|| htmlCsvTableRow(csv[r_])'\n' -
|| ''
end r_
html = html -
|| '</tbody>\n' -
|| '</table>\n' -
|| ''
return html
 
method htmlCsvTableRow(row, tag = 'td') public static returns Rexx
row = row.strip('t')
row = row.changestr('&', '&amp;') -- need to do this one first to avoid double translation
row = row.changestr('"', '&quot;')
row = row.changestr("'", '&apos;')
row = row.changestr('<', '&lt;')
row = row.changestr('>', '&gt;')
elmts = ''
elmts[0] = 0
e_ = 0
loop while row.length() > 0
parse row elmt ',' row
e_ = e_ + 1; elmts[0] = e_; elmts[e_] = elmt
end
html = '<tr>\n' -
|| ''
loop e_ = 1 to elmts[0]
html = html -
|| '<'tag'>'elmts[e_]'</'tag'>\n' -
|| ''
end e_
html = html -
|| '</tr>\n' -
|| ''
return html
</lang>
'''Output:'''
<div style="height:30ex;overflow:scroll;">
<lang html><?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8"/>
<title>RCsv2Html</title>
<style type="text/css">
<!--
/* <![DATA[ */
body {
font-family: "Verdana", "Geneva", "Helvetica Neue", "Helvetica", "DejaVu Sans", "Arial", sans-serif;
}
table, th, td {
border: 1px solid black;
border-collapse: collapse;
padding: 0.25em;
font-size: 85%;
}
th {
color: white;
background-color: green;
}
p.classname {
font-size: inherit;
}
/* ]] */
//-->
</style>
</head>
<body>
<h1>Rosetta Code &ndash; NetRexx Sample Output</h2>
<h2><a href="http://rosettacode.org/wiki/CSV_to_HTML_translation">CSV to HTML translation</a></h2>
 
<h3>Contents of CSV <code>./data/Brian.csv</code></h3>
<pre>
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!
</pre>
 
<table>
<caption>Contents of CSV <code>./data/Brian.csv</code></caption>
<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>&lt;angry&gt;Now you listen here! He&apos;s not the messiah; he&apos;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&apos;m his mother; that&apos;s who!</td>
</tr>
 
<tr>
<td>The multitude</td>
<td>Behold his mother! Behold his mother!</td>
</tr>
 
</tbody>
</table>
 
</body>
</html>
</lang>
</div>
 
'''Rendered Output:'''
 
[[File:RCsv2HtmlNetRexx.png]]
 
=={{header|OCaml}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.