Starting a web browser: Difference between revisions

m
(→‎{{header|Wren}}: Removed a duplicated line from the C code.)
m (→‎{{header|Wren}}: Minor tidy)
 
(7 intermediate revisions by 6 users not shown)
Line 8:
=={{header|EchoLisp}}==
Since we are already in a browser window, the browser is started. '''print-hatml''' sends an html string to the standard output area. The table is styled with the "htable" css class, which is included in EchoLisp.
<langsyntaxhighlight lang="scheme">
;; table from "Separate_the_house_number_from_the_street_name"
;;
(print-html (table->html adresses))
</syntaxhighlight>
</lang>
{{out}}
 
Line 122:
=={{header|Go}}==
This uses the same format and color scheme for the table as the Raku entry.
<langsyntaxhighlight lang="go">package main
 
import (
Line 249:
err = os.Remove(fileName)
check(err)
}</langsyntaxhighlight>
 
{{out}}
Line 255:
Similar to the Raku entry.
</pre>
 
=={{header|J}}==
 
Here, we assume that <code>browser</code> refers to a browser in a format which is usable by a command shell, and <code>din5008</code> and <code>sampledata</code> have been defined according to the [[Separate_the_house_number_from_the_street_name#J|J house number street name]] task entry:
 
<syntaxhighlight lang="j"> wrap=: {{'<',m,'>',y,'</',(;{.;:m),'>' }}(L:0)("1)
'table.htm'fwrite~'<style>tr:nth-child(even){color:blue}</style>','table' wrap ,'tr' wrap ;"1 'td' wrap din5008"1 sampledata
1660
launch browser,' table.htm'</syntaxhighlight>
 
(Here, 1660 was the length of the generated file.)
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Tables, BrowseTables
 
function testbrowsertable(addresstext)
Line 324 ⟶ 335:
 
testbrowsertable(adressen)
</syntaxhighlight>
</lang>
 
=={{header|Nim}}==
Based on Phix solution. Using same colors as Scala program.
 
<langsyntaxhighlight Nimlang="nim">import os, strutils
 
func separateHouseNumber(address: string): tuple[street, house: string] =
Line 415 ⟶ 426:
discard execShellCmd("firefox test.html")
os.sleep(5000)
removeFile("test.html")</langsyntaxhighlight>
 
{{out}}
Line 423 ⟶ 434:
Borrowing code from the [[Separate_the_house_number_from_the_street_name|Separate the house number from the street name]] task.
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use File::Temp qw(tempfile);
 
my @addresses = (
Line 498 ⟶ 509:
# wait for a bit to give browser time to load before destroying temp file
sleep 5;
</syntaxhighlight>
</lang>
{{out}}
<table border="2"> <tr bgcolor="#02ccfe"><th>Address</th><th>Street</th><th>House Number</th>
Line 536 ⟶ 547:
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(notonline)-->
<lang Phix>constant addresses = {"Plataanstraat 5",
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (file i/o, system)</span>
"Straat 12",
<span style="color: #008080;">constant</span> <span style="color: #000000;">addresses</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"Plataanstraat 5"</span><span style="color: #0000FF;">,</span>
"Straat 12 II",
"Dr. J.<span style="color: #008000;">"Straat 12"</span><span style="color: 12#0000FF;">,</span>
"Dr. J.<span style="color: #008000;">"Straat 12 aII"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"Dr. J. Straat 12-14"</span><span style="color: #0000FF;">,</span>
<span style="Laancolor: 1940#008000;">"Dr. -J. 1945Straat 12 a"</span><span style="color: 37#0000FF;">,</span>
<span style="Pleincolor: 1940#008000;">"Dr. J. Straat 12-14"</span><span style="color: 2#0000FF;">,</span>
<span style="1213color: #008000;">"Laan 1940 -laan 111945 37"</span><span style="color: #0000FF;">,</span>
<span style="16color: april#008000;">"Plein 19441940 Pad2"</span><span style="color: 1#0000FF;">,</span>
<span style="1ecolor: Kruisweg#008000;">"1213-laan 11"</span><span style="color: 36#0000FF;">,</span>
<span style="Laancolor: 1940-'45#008000;">"16 april 1944 Pad 1"</span><span style="color: 66#0000FF;">,</span>
<span style="color: #008000;">"1e Kruisweg 36"</span><span style="Laancolor: '40-'45#0000FF;">,</span>
<span style="Langeloërduinencolor: 3#008000;">"Laan 1940-'45 66"</span><span style="color: 46#0000FF;">,</span>
<span style="Marienwaerdtcolor: 2e#008000;">"Laan Dreef'40-'45"</span><span style="color: 2#0000FF;">,</span>
<span style="Provincialewegcolor: N205#008000;">"Langeloërduinen 3 46"</span><span style="color: 1#0000FF;">,</span>
<span style="Riviumcolor: #008000;">"Marienwaerdt 2e StraatDreef 2"</span><span style="color: 59.#0000FF;">,</span>
<span style="Nieuwecolor: gracht#008000;">"Provincialeweg N205 1"</span><span style="color: 20rd#0000FF;">,</span>
<span style="Nieuwecolor: gracht#008000;">"Rivium 20rd2e Straat 59."</span><span style="color: 2#0000FF;">,</span>
<span style="color: #008000;">"Nieuwe gracht 20zw 20rd"</2span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"Nieuwe gracht 20zw20rd 2"</3span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"Nieuwe gracht 2020zw zw/42"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"Nieuwe gracht 20zw/3"</span><span style="Bahnhofstr.color: 4#0000FF;">,</span>
<span style="color: #008000;">"Nieuwe gracht 20 zw/4"</span><span style="Wertstr.color: 10#0000FF;">,</span>
<span style="color: #008000;">"Bahnhofstr. 4"</span><span style="Lindenhofcolor: 1#0000FF;">,</span>
<span style="color: #008000;">"Wertstr. 10"</span><span style="Nordeschcolor: 20#0000FF;">,</span>
<span style="color: #008000;">"Lindenhof 1"</span><span style="Weilstr.color: 6#0000FF;">,</span>
<span style="Harthauercolor: Weg#008000;">"Nordesch 20"</span><span style="color: 2#0000FF;">,</span>
<span style="Mainaustrcolor: #008000;">"Weilstr. 496"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"Harthauer Weg 2"</span><span style="August-Horch-Str.color: 3#0000FF;">,</span>
<span style="color: #008000;">"Mainaustr. 49"</span><span style="Marktplatzcolor: 31#0000FF;">,</span>
<span style="Schmidenercolor: Weg#008000;">"August-Horch-Str. 3"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"Marktplatz 31"</span><span style="Karl-Weysser-Str.color: 6#0000FF;"}>,</span>
<span style="color: #008000;">"Schmidener Weg 3"</span><span style="color: #0000FF;">,</span>
 
<span style="color: #008000;">"Karl-Weysser-Str. 6"</span><span style="color: #0000FF;">}</span>
function isDigit(integer ch)
return ch>='0' and ch<='9'
<span style="color: #008080;">function</span> <span style="color: #000000;">isDigit</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;">)</span>
end function
<span style="color: #008080;">return</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;">>=</span><span style="color: #008000;">'0'</span> <span style="color: #008080;">and</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;"><=</span><span style="color: #008000;">'9'</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
function separateHouseNumber(integer i)
string address = addresses[i]
<span style="color: #008080;">function</span> <span style="color: #000000;">separateHouseNumber</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
sequence parts = split(address)
<span style="color: #004080;">string</span> <span style="color: #000000;">address</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">addresses</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
string street, house
<span style="color: #004080;">sequence</span> <span style="color: #000000;">parts</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #000000;">address</span><span style="color: #0000FF;">)</span>
integer h = 0
<span style="color: #004080;">string</span> <span style="color: #000000;">street</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">house</span>
if length(parts)>1 then
<span style="color: #004080;">integer</span> <span style="color: #000000;">h</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
string last = parts[$]
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">parts</span><span style="color: #0000FF;">)></span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
if isDigit(last[1]) then
<span style="color: #004080;">string</span> <span style="color: #000000;">last</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">parts</span><span style="color: #0000FF;">[$]</span>
h = 1
<span style="color: #008080;">if</span> <span style="color: #000000;">isDigit</span><span style="color: #0000FF;">(</span><span style="color: #000000;">last</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])</span> <span style="color: #008080;">then</span>
string penult = parts[$-1]
<span style="color: #000000;">h</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
if length(parts)>2
<span style="color: #004080;">string</span> <span style="color: #000000;">penult</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">parts</span><span style="color: #0000FF;">[$-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
and isDigit(penult[1])
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">parts</span><span style="color: #0000FF;">)></span><span style="color: #000000;">2</span>
and match("194",penult)!=1 then
<span style="color: #008080;">and</span> <span style="color: #000000;">isDigit</span><span style="color: #0000FF;">(</span><span style="color: #000000;">penult</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])</span>
h = 2
<span style="color: #008080;">and</span> <span style="color: #7060A8;">match</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"194"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">penult</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
end if
<span style="color: #000000;">h</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span>
elsif length(parts)>2 then
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
h = 2
<span style="color: #008080;">elsif</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">parts</span><span style="color: #0000FF;">)></span><span style="color: #000000;">2</span> <span style="color: #008080;">then</span>
end if
<span style="color: #000000;">h</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if h then
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
street = join(parts[1..$-h])
<span style="color: #008080;">if</span> <span style="color: #000000;">h</span> <span style="color: #008080;">then</span>
house = join(parts[$-h+1..$])
<span style="color: #000000;">street</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">parts</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..$-</span><span style="color: #000000;">h</span><span style="color: #0000FF;">])</span>
else
<span style="color: #000000;">house</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">parts</span><span style="color: #0000FF;">[$-</span><span style="color: #000000;">h</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..$])</span>
street = join(parts)
<span style="color: #008080;">else</span>
house = "(none)"
<span style="color: #000000;">street</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">parts</span><span style="color: #0000FF;">)</span>
end if
<span style="color: #000000;">house</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"(none)"</span>
string colour = iff(mod(i,2)=0?"#d7fffe":"#9dbcd4")
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
return {colour,address,street,house}
<span style="color: #004080;">string</span> <span style="color: #000000;">colour</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">?</span><span style="color: #008000;">"#d7fffe"</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"#9dbcd4"</span><span style="color: #0000FF;">)</span>
end function
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">colour</span><span style="color: #0000FF;">,</span><span style="color: #000000;">address</span><span style="color: #0000FF;">,</span><span style="color: #000000;">street</span><span style="color: #0000FF;">,</span><span style="color: #000000;">house</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
constant html_hdr = """
<html>
<span style="color: #008080;">constant</span> <span style="color: #000000;">html_hdr</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
<head>
&lt;html&gt;
<title>Rosetta Code - Start a Web Browser</title>
&lt;head&gt;
<meta charset="UTF-8">
&lt;title&gt;Rosetta Code - Start a Web Browser&lt;/title&gt;
</head>
&lt;meta charset="UTF-8"&gt;
<body bgcolor="#d8dcd6">
&lt;/head&gt;
<p align="center">
&lt;body bgcolor="#d8dcd6"&gt;
<font face="Arial, sans-serif" size="5">Split the house number from the street name</font>
&lt;p align="center"&gt;
</p>
&lt;font face="Arial, sans-serif" size="5"&gt;Split the house number from the street name&lt;/font&gt;
<p align="center">
&lt;/p&gt;
<table border="2"> <tr bgcolor="#02ccfe">
&lt;p align="center"&gt;
<th>Address</th><th>Street</th><th>House Number</th>
&lt;table border="2"&gt; &lt;tr bgcolor="#02ccfe"&gt;
""",
&lt;th&gt;Address&lt;/th&gt;&lt;th&gt;Street&lt;/th&gt;&lt;th&gt;House Number&lt;/th&gt;
html_line = """
"""</span><span style="color: #0000FF;">,</span>
<tr bgcolor=%s><td>%s</td><td>%s</td><td>%s</td></tr>
<span style="color: #000000;">html_line</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
""",
&lt;tr bgcolor=%s&gt;&lt;td&gt;%s&lt;/td&gt;&lt;td&gt;%s&lt;/td&gt;&lt;td&gt;%s&lt;/td&gt;&lt;/tr&gt;
html_ftr = """
"""</span><span style="color: #0000FF;">,</span>
</table>
<span style="color: #000000;">html_ftr</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
</p>
&lt;/table&gt;
</body>
&lt;/p&gt;
</html>
&lt;/body&gt;
"""
&lt;/html&gt;
 
"""</span>
procedure main()
integer fn = open("test.html","w")
<span style="color: #008080;">procedure</span> <span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
printf(fn,html_hdr)
<span style="color: #004080;">integer</span> <span style="color: #000000;">fn</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">open</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"test.html"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"w"</span><span style="color: #0000FF;">)</span>
for i=1 to length(addresses) do
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">,</span><span style="color: #000000;">html_hdr</span><span style="color: #0000FF;">)</span>
printf(fn,html_line,separateHouseNumber(i))
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">addresses</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
end for
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">,</span><span style="color: #000000;">html_line</span><span style="color: #0000FF;">,</span><span style="color: #000000;">separateHouseNumber</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">))</span>
printf(fn,html_ftr)
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
close(fn)
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">,</span><span style="color: #000000;">html_ftr</span><span style="color: #0000FF;">)</span>
system("test.html")
<span style="color: #7060A8;">close</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">)</span>
end procedure
<span style="color: #7060A8;">system</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"test.html"</span><span style="color: #0000FF;">)</span>
main()</lang>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</syntaxhighlight>-->
output as perl
 
=={{header|Racket}}==
 
<langsyntaxhighlight lang="racket">
#lang at-exp racket
 
Line 662 ⟶ 676:
@table{@render-table}}}
}
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
Line 669 ⟶ 683:
Uses the code from the [[Separate_the_house_number_from_the_street_name|Separate the house number from the street name]] task almost verbatim. Included here to make a complete, runnable example.
 
<syntaxhighlight lang="raku" perl6line>use File::Temp;
 
my $addresses = qq :to /END/;
Line 767 ⟶ 781:
# wait for a bit to give browser time to load before destroying temp file
sleep 5;
</syntaxhighlight>
</lang>
{{out}} Will start the default browser (or open a new tab/window in a running one) and display this table.
<table border="2"> <tr bgcolor="#02ccfe"><th>Address</th><th>Street</th><th>House Number</th>
Line 808 ⟶ 822:
<!-- ==={{libheader|Scala}}=== -->
Uses nothing but the Standard Library:
<langsyntaxhighlight lang="scala">import java.awt.Desktop
import java.io.{IOException, PrintWriter}
import java.net.{URI, ServerSocket}
Line 942 ⟶ 956:
do print("Do you want to shutdown this server? <Y(es)/N>: ") while (!scala.io.StdIn.readBoolean)
sys.exit()
}</langsyntaxhighlight>
{{out}}
<font face="Arial, sans-serif" size="5">Split the house number from the street name</font>
Line 949 ⟶ 963:
=={{header|Tcl}}==
{{works with|Tcl|8.6}}<!--required for [file tempfile]-->
<langsyntaxhighlight lang="tcl">package require Tcl 8.6
 
# This is identical to the address task. Skip forward to the next section...
Line 1,027 ⟶ 1,041:
} else {
exec xdg_open $filename
}</langsyntaxhighlight>
{{out}}
<!-- Strictly, this is just the contents of the <body> inside a <div>… --><div style="margin-left: 50px">
Line 1,036 ⟶ 1,050:
{{libheader|Wren-pattern}}
{{libheader|Wren-fmt}}
{{libheader|Wren-traititerate}}
An embedded program so we can ask the C host to open the browser for us after we've created the html file.
 
The Wren code is based in part on the Nim example and uses the same color scheme.
<langsyntaxhighlight ecmascriptlang="wren">/* starting_web_browserStarting_a_web_browser.wren */
 
import "./pattern" for Pattern
import "./fmt" for Fmt
import "./traititerate" for Indexed
 
foreign class File {
Line 1,162 ⟶ 1,176:
File.flushAll() // flush all streams
C.sleep(5) // wait five seconds
File.remove("test.html") // remove the file</langsyntaxhighlight>
<br>
We now embed this in the following C program, compile and run it.
<langsyntaxhighlight lang="c">/* gcc starting_web_browserStarting_a_web_browser.c -o starting_web_browserStarting_a_web_browser -lwren -lm */
 
#include <stdio.h>
Line 1,300 ⟶ 1,314:
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "starting_web_browserStarting_a_web_browser.wren";
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
Line 1,316 ⟶ 1,330:
free(script);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 1,322 ⟶ 1,336:
Same as Nim.
</pre>
 
{{omit from|EasyLang}}
9,476

edits