CSV to HTML translation: Difference between revisions

m
mNo edit summary
 
(10 intermediate revisions by 6 users not shown)
Line 1,462:
</body>
</html></syntaxhighlight>
 
=={{header|EasyLang}}==
<syntaxhighlight>
h$ = "<table border=1>\n<tr><th>"
repeat
s$ = input
until s$ = ""
write h$
for c$ in strchars s$
if c$ = ","
if scnd = 0
c$ = "</th><th>"
else
c$ = "</td><td>"
.
elif c$ = "<"
c$ = "&lt;"
elif c$ = ">"
c$ = "&gt;"
elif c$ = "&"
c$ = "&amp;"
.
write c$
.
if scnd = 0
h$ = "</th></tr>\n<tr><td>"
scnd = 1
else
h$ = "</td></tr>\n<tr><td>"
.
.
print "</td></tr>\n</table>"
#
input_data
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!
</syntaxhighlight>
 
{{out}}
<table border=1>
<tr><th>Character</th><th>Speech</th></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>
 
=={{header|EchoLisp}}==
Line 1,980 ⟶ 2,031:
_window = 1
begin enum output 1
_webView
end enum
 
void local fn BuildWindow
CGRect r = fn CGRectMake( 0, 0, 600, 220 )
'~'1
window _window, @"Rosetta Code CSV to HTML", r, NSWindowStyleMaskTitled + NSWindowStyleMaskClosable
CGRect r = fn CGRectMake( 0, 0, 600, 220 )
window _window, @"Rosetta Code CSV to HTML", r, NSWindowStyleMaskTitled + NSWindowStyleMaskClosable
r = fn CGRectMake( 20, 20, 560, 180 )
 
wkwebview _webView, r,, _window
r = fn CGRectMake( 20, 20, 560, 180 )
wkwebview _webView, r,, _window
end fn
 
local fn CSV2HTML as CFStringRef
NSUInteger i, count
'~'1
NSUInteger i, count
CFStringRef csvStr = @"Character,Speech\n¬
 
The multitude,The messiah! Show us the messiah!\n¬
CFStringRef csvStr = @"Character,Speech\n¬
The multitude Brians mother,TheNow messiahyou listen here! ShowHe's usnot the messiah; he's a very naughty boy! Now go away!\n¬
The multitude,Who are you\n¬
Brians mother,Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!\n¬
Brians mother,I'm his mother; that's who!\n¬
The multitude,Who are you\n¬
Brians mother The multitude,I'mBehold his mother;! that'sBehold whohis mother!\n¬"
The multitude,Behold his mother! Behold his mother!"
CFArrayRef linesArray = fn StringComponentsSeparatedByString( csvStr, @"\n" )
 
CFMutableStringRef htmlStr = fn MutableStringWithCapacity(0)
CFArrayRef linesArray = fn StringComponentsSeparatedByString( csvStr, @"\n" )
MutableStringAppendString( htmlStr, @"<table style=\"background:#eee;\">\n" )
CFMutableStringRef htmlStr = fn MutableStringWithCapacity(0)
MutableStringAppendString( htmlStr, @"<tabletr stylebgcolor=\"background:#eee;\"wheat><th>Character</th><th>Speech</th></tr>\n" )
MutableStringAppendString( htmlStr, @"<trcaption>From bgcolor=wheat><th>Character</th><th>Speech</th>Monty Python's \"The Life of Brian\"</trcaption>\n" )
count = len( linesArray )
MutableStringAppendString( htmlStr, @"<caption>From Monty Python's \"The Life of Brian\"</caption>\n" )
for i = 1 to count - 1
count = len( linesArray )
CFStringRef tempStr = linesArray[i]
for i = 1 to count - 1
CFArrayRef tempArr = fn StringComponentsSeparatedByString( tempStr, @"," )
CFStringRef tempStr = linesArray[i]
MutableStringAppendString( htmlStr, @"<tr>\n" )
CFArrayRef tempArr = fn StringComponentsSeparatedByString( tempStr, @"," )
MutableStringAppendString( htmlStr, fn StringWithFormat( @"<trtd style=\"width:120px;\"><b>%@</b></td>>\n", tempArr[0] ) )
MutableStringAppendString( htmlStr, fn StringWithFormat( @"<td style=\"width:120px;\"><bi>%@</bi></td>>\n", tempArr[01] ) )
MutableStringAppendString( htmlStr, fn StringWithFormat( @"<td><i>%@</i></tdtr>\n", tempArr[1] ) )
next
MutableStringAppendString( htmlStr, @"</tr>\n" )
MutableStringAppendString( htmlStr, @"</table><br></br>" )
next
MutableStringAppendString( htmlStr, @"</table><br></br>" )
end fn = fn StringWithString( htmlStr )
 
local fn LoadHTML2WebView
CFStringRef htmlStr = fn CSV2HTML
'~'1
fn WKWebViewLoadHTMLString( _webView, htmlStr, NULL )
CFStringRef htmlStr = fn CSV2HTML
fn WKWebViewLoadHTMLString( _webView, htmlStr, NULL )
end fn
 
void local fn DoDialog( ev as long, tag as long, wnd as long, obj as CFTypeRef )
select (ev)
'~'1
case _windowWillClose : end
select (ev)
end select
case _windowWillClose : end
end select
end fn
 
Line 2,042 ⟶ 2,089:
{{output}}
[[File:CSV to HTML.png]]
 
 
 
 
=={{header|Go}}==
Line 3,232 ⟶ 3,276:
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table></syntaxhighlight>
 
=={{header|M2000 Interpreter}}==
 
<syntaxhighlight lang="m2000 interpreter">
module csv2html {
nl$={
}
Repl$=lambda$ (a$) ->{
a$=replace$("&", "&amp;",a$)
a$=replace$(">", "&gt;",a$)
a$=replace$("""", "&quot;",a$)
// add any other replacement here
=replace$("<", "&lt;",a$)
}
Tag$=lambda$ nl$, repl$ (a$, b$, n=4)->{
if n>0 then
a$=rtrim$(replace$(nl$, nl$+string$(" ", n), nl$+a$))
if right$(a$,2)<>nl$ then a$+=nl$
else
if right$(a$, 2)=nl$ then if left$(a$,2)<>nl$ then a$=nl$+a$
end if
prop=(,) : Read ? prop // forth parameter optional (we have to initalize first with an empty array)
p=each(prop) : prop$="" // p is an iteration object.
while p
prop$+=" "+repl$(prop#val$(p^))+"="+quote$(repl$(prop#val$(p^+1)))
p=each(prop,p^+2) // start new from p^+2 (p^ is the internal counter)
end while
="<"+b$+prop$+">"+a$+"</"+b$+">"+nl$
}
// Prepare FILE csv
tofile$={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!
}
open "forHtml.csv" for wide output as #f
Print #f, tofile$;
close #f
// prepare the input style for Input from file statement
// "," - we use comma between fields
// "." - for numbers we use dot for decimal separator
// false - we didn't read json style strings to normal strings (so \n convert to code 13, \t to code 9)
// true - we read strings unquote
input with ",",".",false, true
// Read csv file
i=1
document export$=""
Open "forHtml.csv" for wide input as #f
while not eof(#f)
input #f, a$, b$
a$=repl$(a$)
b$=repl$(b$)
if i=1 then
export$+=tag$(tag$(a$, "th", 0)+tag$(b$,"th", 0), "tr", 4)
else
export$+=tag$(tag$(a$, "td", 0)+tag$(b$,"td", 0), "tr", 4)
end if
i++
end while
close #f
style$=tag$({TD {background-color:#ddddff; }
thead TD {background-color:#ddffdd; text-align:center; }
},"style",4,("type", "text/css"))
title$= tag$("CSV to HTML translation - Extra Credit","title",0)
Head$= tag$(title$+ style$,"head")
html$={<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">}+nl$+tag$(head$+tag$(tag$(export$, "table"), "body"), "html")
clipboard html$
Report html$
Print "Press Esc to exit browser"
browser "about: "+html$
}
csv2html
</syntaxhighlight>
{{out}}
<pre>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>CSV to HTML translation - Extra Credit</title>
<style type="text/css">
TD {background-color:#ddddff; }
thead TD {background-color:#ddffdd; text-align:center; }
</style>
</head>
<body>
<table>
<tr>
<th>Character</th>
<th>Speech</th>
</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>
</pre>
 
=={{header|Maple}}==
Line 5,964 ⟶ 6,126:
=={{header|Wren}}==
{{trans|Kotlin}}
<syntaxhighlight lang=ecmascript"wren">var csv =
"Character,Speech\n" +
"The multitude,The messiah! Show us the messiah!\n" +
Line 6,010 ⟶ 6,172:
<pre>
Same as Kotlin entry.
</pre>
 
=={{header|XPL0}}==
{{trans|C}}
<syntaxhighlight lang "XPL0">string 0;
char Input, S;
[Input:=
"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!";
 
Text(0, "<table>^m^j<tr><td>");
S:= Input;
while S(0) do
[case S(0) of
$0A: Text(0, "</td></tr>^m^j<tr><td>");
^,: Text(0, "</td><td>");
^<: Text(0, "&lt;");
^>: Text(0, "&gt;");
^&: Text(0, "&amp;")
other ChOut(0, S(0));
S:= S+1;
];
Text(0, "</td></tr>^m^j</table>");
]</syntaxhighlight>
{{out}}
<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>&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>
</pre>
 
Line 6,156 ⟶ 6,356:
</tr>
</table></syntaxhighlight>
 
[[Category:CSV]]
[[Category:HTML]]
1,983

edits