Consider a simplified CSV format where all rows are separated by a newline
and all columns are separated by commas.

No commas are allowed as field data, but the data may contain
other characters and character sequences that would
normally be   escaped   when converted to HTML



Task

Create a function that takes a string representation of the CSV data
and returns a text string of an HTML table representing the CSV data.

Use the following data as the CSV text to convert, and show your output.

 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!


Extra credit

Optionally allow special formatting for the first row of the table as if it is the tables header row
(via <thead> preferably; CSS if you must).




/* 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
 