CSV to HTML translation: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 141:
</tr>
</table></lang>
 
=={{header|ALGOL 68}}==
{{works with|ALGOL 68|Revision 1 - no extensions to language used.}}
Line 234 ⟶ 235:
</BODY>
</HTML></lang>
 
=={{header|ANTLR}}==
===Java===
<lang java>
// Create an HTML Table from comma seperated values
// Nigel Galloway - June 2nd., 2013
grammar csv2html;
dialog : {System.out.println("<HTML><Table>");}header body+{System.out.println("</Table></HTML>");} ;
header : {System.out.println("<THEAD align=\"center\"><TR bgcolor=\"blue\">");}row{System.out.println("</TR></THEAD");};
body : {System.out.println("<TBODY><TR>");}row{System.out.println("</TR></TBODY");};
row : field ',' field '\r'? '\n';
field : Field{System.out.println("<TD>" + $Field.text.replace("<","&lt;").replace(">","&gt;") + "</TD>");};
Field : ~[,\n\r]+;
</lang>
 
=={{header|AutoHotkey}}==
Line 333 ⟶ 348:
</tr>
</table></pre>
 
 
 
=={{header|ANTLR}}==
===Java===
<lang java>
// Create an HTML Table from comma seperated values
// Nigel Galloway - June 2nd., 2013
grammar csv2html;
dialog : {System.out.println("<HTML><Table>");}header body+{System.out.println("</Table></HTML>");} ;
header : {System.out.println("<THEAD align=\"center\"><TR bgcolor=\"blue\">");}row{System.out.println("</TR></THEAD");};
body : {System.out.println("<TBODY><TR>");}row{System.out.println("</TR></TBODY");};
row : field ',' field '\r'? '\n';
field : Field{System.out.println("<TD>" + $Field.text.replace("<","&lt;").replace(">","&gt;") + "</TD>");};
Field : ~[,\n\r]+;
</lang>
 
=={{header|AWK}}==
Line 544 ⟶ 543:
{{out|Rendered output}}
[[File:CSVHTML_BBC.gif]]
 
=={{header|Befunge}}==
The most practical solution for Befunge was to read the CSV from stdin, so this implementation works best with command line interpreters that can accept redirected input. That said, it is still possible test with many of the GUI and online interpretors just by entering the input manually.
 
Note that right angle brackets are deliberately not escaped, since that is not strictly necessary for the markup to be valid.
 
<lang Befunge><v_>#!,#:< "<table>" \0 +55
v >0>::65*1+`\"~"`!*#v_4-5v >
v>#^~^<v"<tr><td>" < \v-1/<>">elb"
<^ >:#,_$10 |!:<>\#v_ vv"ta"
v-",":\-"&":\-"<":\<>5#05#<v+ >"/"v
>#v_$$$0">dt<>dt/<"vv"tr>"+<5 v"<"<
>^>\#v_$$0";pma&" v>"/<>d"v5 v , <
$ > \#v_$0";tl&"v v"</t"<0 > : |
^_>#!,#:<>#<0#<\#<<< >:#,_$#^_v@ $<</lang>
{{out}}
<lang html5><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></lang>
 
=={{header|Bracmat}}==
Line 671 ⟶ 694:
<tr><td /></tr>
</tbody></table></lang>
 
=={{header|Befunge}}==
The most practical solution for Befunge was to read the CSV from stdin, so this implementation works best with command line interpreters that can accept redirected input. That said, it is still possible test with many of the GUI and online interpretors just by entering the input manually.
 
Note that right angle brackets are deliberately not escaped, since that is not strictly necessary for the markup to be valid.
 
<lang Befunge><v_>#!,#:< "<table>" \0 +55
v >0>::65*1+`\"~"`!*#v_4-5v >
v>#^~^<v"<tr><td>" < \v-1/<>">elb"
<^ >:#,_$10 |!:<>\#v_ vv"ta"
v-",":\-"&":\-"<":\<>5#05#<v+ >"/"v
>#v_$$$0">dt<>dt/<"vv"tr>"+<5 v"<"<
>^>\#v_$$0";pma&" v>"/<>d"v5 v , <
$ > \#v_$0";tl&"v v"</t"<0 > : |
^_>#!,#:<>#<0#<\#<<< >:#,_$#^_v@ $<</lang>
{{out}}
<lang html5><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></lang>
 
=={{header|C}}==
Line 741 ⟶ 740:
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table></lang>
 
=={{header|C++}}==
<lang cpp>#include <string>
#include <boost/regex.hpp>
#include <iostream>
 
std::string csvToHTML( const std::string & ) ;
 
int main( ) {
std::string text = "Character,Speech\n"
"The multitude,The messiah! Show us the messiah!\n"
"Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>\n"
"The multitude,Who are you?\n"
"Brians mother,I'm his mother; that's who!\n"
"The multitude,Behold his mother! Behold his mother!\n" ;
std::cout << csvToHTML( text ) ;
return 0 ;
}
 
std::string csvToHTML( const std::string & csvtext ) {
//the order of the regexes and the replacements is decisive!
std::string regexes[ 5 ] = { "<" , ">" , "^(.+?)\\b" , "," , "\n" } ;
const char* replacements [ 5 ] = { "&lt;" , "&gt;" , " <TR><TD>$1" , "</TD><TD>", "</TD></TR>\n" } ;
boost::regex e1( regexes[ 0 ] ) ;
std::string tabletext = boost::regex_replace( csvtext , e1 ,
replacements[ 0 ] , boost::match_default | boost::format_all ) ;
for ( int i = 1 ; i < 5 ; i++ ) {
e1.assign( regexes[ i ] ) ;
tabletext = boost::regex_replace( tabletext , e1 , replacements[ i ] , boost::match_default | boost::format_all ) ;
}
tabletext = std::string( "<TABLE>\n" ) + tabletext ;
tabletext.append( "</TABLE>\n" ) ;
return tabletext ;
}</lang>
{{out}}
<lang html5>
<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>
</lang>
 
=={{header|C sharp}}==
Line 915 ⟶ 869:
</tr>
</table></lang>
 
=={{header|C++}}==
<lang cpp>#include <string>
#include <boost/regex.hpp>
#include <iostream>
 
std::string csvToHTML( const std::string & ) ;
 
int main( ) {
std::string text = "Character,Speech\n"
"The multitude,The messiah! Show us the messiah!\n"
"Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>\n"
"The multitude,Who are you?\n"
"Brians mother,I'm his mother; that's who!\n"
"The multitude,Behold his mother! Behold his mother!\n" ;
std::cout << csvToHTML( text ) ;
return 0 ;
}
 
std::string csvToHTML( const std::string & csvtext ) {
//the order of the regexes and the replacements is decisive!
std::string regexes[ 5 ] = { "<" , ">" , "^(.+?)\\b" , "," , "\n" } ;
const char* replacements [ 5 ] = { "&lt;" , "&gt;" , " <TR><TD>$1" , "</TD><TD>", "</TD></TR>\n" } ;
boost::regex e1( regexes[ 0 ] ) ;
std::string tabletext = boost::regex_replace( csvtext , e1 ,
replacements[ 0 ] , boost::match_default | boost::format_all ) ;
for ( int i = 1 ; i < 5 ; i++ ) {
e1.assign( regexes[ i ] ) ;
tabletext = boost::regex_replace( tabletext , e1 , replacements[ i ] , boost::match_default | boost::format_all ) ;
}
tabletext = std::string( "<TABLE>\n" ) + tabletext ;
tabletext.append( "</TABLE>\n" ) ;
return tabletext ;
}</lang>
{{out}}
<lang html5>
<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>
</lang>
 
=={{header|Clojure}}==
 
We assume the presence of a file, but the input could come from anywhere.
 
<lang 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!
</lang>
 
<lang clojure>
(require 'clojure.string)
 
(def escapes
{\< "&lt;", \> "&gt;", \& "&amp;"})
 
(defn escape
[content]
(clojure.string/escape content escapes))
 
(defn tr
[cells]
(format "<tr>%s</tr>"
(apply str (map #(str "<td>" (escape %) "</td>") cells))))
 
;; turn a seq of seq of cells into a string.
(defn to-html
[tbl]
(format "<table><tbody>%s</tbody></thead>"
(apply str (map tr tbl))))
 
;; Read from a string to a seq of seq of cells.
(defn from-csv
[text]
(map #(clojure.string/split % #",")
(clojure.string/split-lines text)))
 
(defn -main
[]
(let [lines (line-seq (java.io.BufferedReader. *in*))
tbl (map #(clojure.string/split % #",") lines)]
(println (to-html tbl)))
</lang>
 
{{out}}
<lang html>
<table><tbody><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></tbody></thead>
</lang>
 
=={{header|CoffeeScript}}==
Line 979 ⟶ 1,030:
</tbody>
</table></lang>
 
 
=={{header|Clojure}}==
 
We assume the presence of a file, but the input could come from anywhere.
 
<lang 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!
</lang>
 
<lang clojure>
(require 'clojure.string)
 
(def escapes
{\< "&lt;", \> "&gt;", \& "&amp;"})
 
(defn escape
[content]
(clojure.string/escape content escapes))
 
(defn tr
[cells]
(format "<tr>%s</tr>"
(apply str (map #(str "<td>" (escape %) "</td>") cells))))
 
;; turn a seq of seq of cells into a string.
(defn to-html
[tbl]
(format "<table><tbody>%s</tbody></thead>"
(apply str (map tr tbl))))
 
;; Read from a string to a seq of seq of cells.
(defn from-csv
[text]
(map #(clojure.string/split % #",")
(clojure.string/split-lines text)))
 
(defn -main
[]
(let [lines (line-seq (java.io.BufferedReader. *in*))
tbl (map #(clojure.string/split % #",") lines)]
(println (to-html tbl)))
</lang>
 
{{out}}
<lang html>
<table><tbody><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></tbody></thead>
</lang>
 
=={{header|Common Lisp}}==
Line 1,455 ⟶ 1,453:
</lang>
 
<table style='border-spacing: 10px;border:28px ridge orange'> <tr> <th style='color:blue;'> Character </th> <th style='color:blue;'> Speech </th> </tr> <tr> <td style='text-align:left'> The multitude </td> <td style='text-align:left'> The messiah! Show us the messiah! </td> </tr> <tr> <td style='text-align:left'> Brians mother </td> <td style='text-align:left'> <b>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</b> </td> </tr> <tr> <td style='text-align:left'> The multitude </td> <td style='text-align:left'> Who are you? </td> </tr> <tr> <td style='text-align:left'> Brians mother </td> <td style='text-align:left'> I'm his mother; that's who! </td> </tr> <tr> <td style='text-align:left'> The multitude </td> <td style='text-align:left'> Behold his mother! Behold his mother! </td> </tr> </table>
 
 
=={{header|Erlang}}==
Line 2,982 ⟶ 2,979:
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table></lang>
 
 
=={{header|Maple}}==
Line 3,025 ⟶ 3,021:
</table>
</pre>
 
 
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
Line 3,950 ⟶ 3,944:
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table></lang>
 
=={{header|Perl 6}}==
{{works with|rakudo|2015.09}}
A very lispy solution:
<lang perl6>my $str = "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!";
 
# comment the next line out, if you want to read from standard input instead of the hard-coded $str above
# my $str = $*IN.slurp;
 
my &escape = *.trans(« & < > » => « &amp; &lt; &gt; »); # a function with one argument that escapes the entities
my &tag = {"<$^tag>"~$^what~"</$^tag>"};
 
printf
'<!DOCTYPE html>
<html>
<head><title>Some Text</title></head>
<body><table>
%s
</table></body></html>
', [~] # concatenation reduction ('a', 'b', 'c') → 'abc'
(escape($str).split(/\n/) # escape the string and split at newline
==> map -> $line {tag 'tr', # feed that into a map, that map function will tag as 'tr, and has an argument called $line
([~] $line.split(/','/)\ # split $line at ',',
# that / at the end is just an unspace, you can omit it, but then you have to delete
# all whitespace and comments between split(…) and .map
.map({tag 'td', $^cell}))})\ # map those cells as td
.join("\n"); # append a newline for nicer output</lang>
 
{{Out}}
<lang html><!DOCTYPE html>
<html>
<head><title>Some Text</title></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></lang>
 
=={{header|Phix}}==
Line 4,574 ⟶ 4,523:
</tr><tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td>
</tr></tbody></table></lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|rakudo|2015.09}}
A very lispy solution:
<lang perl6>my $str = "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!";
 
# comment the next line out, if you want to read from standard input instead of the hard-coded $str above
# my $str = $*IN.slurp;
 
my &escape = *.trans(« & < > » => « &amp; &lt; &gt; »); # a function with one argument that escapes the entities
my &tag = {"<$^tag>"~$^what~"</$^tag>"};
 
printf
'<!DOCTYPE html>
<html>
<head><title>Some Text</title></head>
<body><table>
%s
</table></body></html>
', [~] # concatenation reduction ('a', 'b', 'c') → 'abc'
(escape($str).split(/\n/) # escape the string and split at newline
==> map -> $line {tag 'tr', # feed that into a map, that map function will tag as 'tr, and has an argument called $line
([~] $line.split(/','/)\ # split $line at ',',
# that / at the end is just an unspace, you can omit it, but then you have to delete
# all whitespace and comments between split(…) and .map
.map({tag 'td', $^cell}))})\ # map those cells as td
.join("\n"); # append a newline for nicer output</lang>
 
{{Out}}
<lang html><!DOCTYPE html>
<html>
<head><title>Some Text</title></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></lang>
 
=={{header|Red}}==
<lang Red>Red []
Line 4,635 ⟶ 4,631:
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table>
 
=={{header|Retro}}==
<lang Retro>remapping off
Line 5,298 ⟶ 5,295:
=== Output (rendered) ===
[[file:tuscript_csv2html.png|500px|thumb|none|rendered by browser ]]
 
 
=={{header|TXR}}==
Line 5,518 ⟶ 5,514:
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table></lang>
 
=={{header|VBScript}}==
<lang vb>
10,327

edits