CSV to HTML translation: Difference between revisions

Content added Content deleted
(→‎{{header|Perl 6}}: added some comments)
(→‎{{header|Perl 6}}: restrucured the code and removed the optional part, since it didn't work anyway.)
Line 1,923: Line 1,923:


=={{header|Perl 6}}==
=={{header|Perl 6}}==

I'm not sure why this looks so messy, tried my best with comments, but this very funtional approach seems very unreadable.




Line 1,934: Line 1,932:
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!¦;

# 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 &escape = *.trans([ <& < \>>] => [<&amp; &lt; &gt;> ]); # a function with one argument that escapes the entities
my &tag = {"<$^t>"~$^s~"</$^t>"}; # a function that takes a string and a tagname, and tags it
my &tag = {"<$^tag>"~$^what~"</$^tag>"};



printf #printf statement
printf
'<!DOCTYPE html>
'<!DOCTYPE html>
<html>
<html>
Line 1,945: Line 1,947:
%s
%s
</table></body></html>
</table></body></html>
', [~] # concatenation reduction ('a', 'b', 'c') → 'abc'
', [~] escape($str).split(/\n/).map(-> $l { # concatenation reduction, escape the string, then split it at \n, then anonymous function
(escape($str).split(/\n/) # escape the string and split at newline
state $first //= 0; # state variable for considering if it is the first line (no ff implemented, yet)
#tag it with tr, split at ',' ==> tag those as td if $first++ (which means the first time it will be false, then true) alse as th
==> map -> $line {tag 'tr', # feed that into a map, that map function will tag as 'tr, and has an argument called $line
tag(([~] $l.split(/','/).map({tag($^a, $first++ ?? 'td' !!'th' )})),'tr')});
([~] $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
</lang>
</lang>