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([ <& < \>>] => [<& < >> ]); # a function with one argument that escapes the entities |
my &escape = *.trans([ <& < \>>] => [<& < >> ]); # a function with one argument that escapes the entities |
||
my &tag = {"<$^ |
my &tag = {"<$^tag>"~$^what~"</$^tag>"}; |
||
printf |
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/) # escape the string and split at newline |
|||
state $first //= 0; # state variable for considering if it is the first line (no ff implemented, yet) |
|||
==> 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 |
|||
</lang> |
</lang> |
||