CSV to HTML translation: Difference between revisions

(Make Python image fit nicely with following section.)
Line 496:
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table></pre>
 
=={{header|Prolog}}==
Uses DCG. Works with SWI-Prolog.<br>
===Simple solution===
<lang Prolog>csv_html :-
L = "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!",
 
csv_html(L, Out, []),
string_to_list(Str, Out),
writeln(Str).
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% simple HTML
%
csv_html(L) -->
"<TABLE>\n",
csv_tr(L),
"</TABLE>".
 
csv_tr([]) --> [].
 
csv_tr(L) -->
"<TR>\n",
csv_td(L, S),
"\n</TR>\n",
csv_tr(S).
 
csv_td(L, S) -->
"<TD>",
csv_td_in(L, S),
"</TD>".
 
csv_td_in([], []) --> [].
 
csv_td_in([10|L], L) --> [].
 
csv_td_in([44|L], S) -->
"</TD><TD>",
csv_td_in(L,S).
 
csv_td_in([H|T], S) -->
[H],
csv_td_in(T, S).
 
</lang>
OutPut :
<pre><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><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</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>
</pre>
===Extra credit solution===
<lang Prolog>csv_html_plus :-
L =
"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!",
 
csv_html_plus(L, Out1, []),
string_to_list(Str1, Out1),
writeln(Str1).
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% HTML +
%
csv_html_plus(L) -->
"<TABLE>\n",
csv_head(L, R),
csv_body(R),
"</TABLE>".
 
 
csv_head(L, R) -->
"<THEAD>\n",
csv_head_tr(L, R),
"</THEAD>\n".
 
 
csv_head_tr(L, R) -->
"<TR>\n",
csv_head_th(L, R),
"\n</TR>\n".
 
 
csv_head_th(L, S) -->
"<TH style='color:#000; background:#FF0;'>",
csv_head_th_in(L, S),
"</TH>".
 
csv_head_th_in([], []) --> [].
 
csv_head_th_in([10|L], L) --> [].
 
csv_head_th_in([44|L], S) -->
"</TH><TH style='color:#000; background:#FF0;'>",
csv_head_th_in(L,S).
 
csv_head_th_in([H|T], S) -->
[H],
csv_head_th_in(T, S).
 
 
csv_body(L) -->
"<TBODY>\n",
csv_body_tr(L),
"</TBODY>\n".
 
csv_body_tr([]) --> [].
 
csv_body_tr(L) -->
"<TR>\n",
csv_body_td(L, S),
"\n</TR>\n",
csv_body_tr(S).
 
csv_body_td(L, S) -->
"<TD style='color:#000; background:#8FF; border:1px #000 solid; padding:0.6em;'>",
csv_body_td_in(L, S),
"</TD>".
 
csv_body_td_in([], []) --> [].
 
csv_body_td_in([10|L], L) --> [].
 
csv_body_td_in([44|L], S) -->
"</TD><TD style='color:#000; background:#8FF; border:1px #000 solid; padding:0.6em;'>",
csv_body_td_in(L,S).
 
csv_body_td_in([H|T], S) -->
[H],
csv_body_td_in(T, S).
</lang>
Output :
<pre><TABLE>
<THEAD>
<TR>
<TH style='color:#000; background:#FF0;'>Character</TH><TH style='color:#000; background:#FF0;'>Speech</TH>
</TR>
</THEAD>
<TBODY>
<TR>
<TD style='color:#000; background:#8FF; border:1px #000 solid; padding:0.6em;'>The multitude</TD><TD style='color:#000; background:#8FF; border:1px #000 solid; padding:0.6em;'>The messiah! Show us the messiah!</TD>
</TR>
<TR>
<TD style='color:#000; background:#8FF; border:1px #000 solid; padding:0.6em;'>Brians mother</TD><TD style='color:#000; background:#8FF; border:1px #000 solid; padding:0.6em;'><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry></TD>
</TR>
<TR>
<TD style='color:#000; background:#8FF; border:1px #000 solid; padding:0.6em;'>The multitude</TD><TD style='color:#000; background:#8FF; border:1px #000 solid; padding:0.6em;'>Who are you?</TD>
</TR>
<TR>
<TD style='color:#000; background:#8FF; border:1px #000 solid; padding:0.6em;'>Brians mother</TD><TD style='color:#000; background:#8FF; border:1px #000 solid; padding:0.6em;'>I'm his mother; that's who!</TD>
</TR>
<TR>
<TD style='color:#000; background:#8FF; border:1px #000 solid; padding:0.6em;'>The multitude</TD><TD style='color:#000; background:#8FF; border:1px #000 solid; padding:0.6em;'>Behold his mother! Behold his mother!</TD>
</TR>
</TBODY>
</TABLE>
</pre>
===HTML outputs rendered in firefox browser===
[[File:Prolog_csv_to_html.png|frame||none]]
 
 
=={{header|Python}}==
Anonymous user