Starting a web browser: Difference between revisions
Thundergnat (talk | contribs) (→{{header|Perl 6}}: Add Perl 6 example) |
Thundergnat (talk | contribs) m (→{{header|Perl 6}}: minor cleanup, get rid of centering; looked better in my head) |
||
Line 209: | Line 209: | ||
# Use a temporary file name and file handle |
# Use a temporary file name and file handle |
||
my ($fn,$fh) = tempfile :suffix('.html'); |
my ($fn, $fh) = tempfile :suffix('.html'); |
||
# dump the content to the file |
# dump the content to the file |
||
$fh.spurt: content; |
$fh.spurt: content; |
||
# use appropriate command for Windows or X11 |
# use appropriate command for Windows or X11 |
||
# other OSs/WMs may need different invocation |
# other OSs/WMs may need different invocation |
||
my $command = $*DISTRO.is-win ?? "start $fn" !! "xdg-open $fn"; |
my $command = $*DISTRO.is-win ?? "start $fn" !! "xdg-open $fn"; |
||
Line 225: | Line 225: | ||
</lang> |
</lang> |
||
{{out}} Will start the default browser (or open a new tab/window in a running one) and display this table. |
{{out}} Will start the default browser (or open a new tab/window in a running one) and display this table. |
||
<div align="center"> |
|||
<table border="2"> <tr bgcolor="#02ccfe"><th>Address</th><th>Street</th><th>House Number</th> |
<table border="2"> <tr bgcolor="#02ccfe"><th>Address</th><th>Street</th><th>House Number</th> |
||
<tr bgcolor="#d7fffe"><td>Plataanstraat 5</td><td>Plataanstraat</td><td>5</td></tr> |
<tr bgcolor="#d7fffe"><td>Plataanstraat 5</td><td>Plataanstraat</td><td>5</td></tr> |
||
Line 261: | Line 260: | ||
<tr bgcolor="#d7fffe"><td>Karl-Weysser-Str. 6</td><td>Karl-Weysser-Str.</td><td>6</td></tr> |
<tr bgcolor="#d7fffe"><td>Karl-Weysser-Str. 6</td><td>Karl-Weysser-Str.</td><td>6</td></tr> |
||
</table> |
</table> |
||
</div> |
|||
=={{header|Racket}}== |
=={{header|Racket}}== |
Revision as of 21:11, 22 October 2017
Build and show a web page displaying the data from the task separate the house number from the street name in a formatted and colored table.
- Task
Write the code which automatically opens a web page in a browser showing the addresses. No static data must be shown, only processed data.
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. <lang scheme>
- table from "Separate_the_house_number_from_the_street_name"
(print-html (table->html adresses)) </lang>
- Output:
Plataanstraat | 5 |
Straat | 12 |
Straat | 12 II |
Straat 1940 | II |
Dr. J. Straat | 40 |
Dr. J. Straat | 12 a |
Dr. J. Straat | 12-14 |
Laan 1940 – 1945 | 37 |
Plein 1940 | 2 |
1213-laan | 11 |
16 april | 1944 Pad 1 |
1e Kruisweg | 36 |
Laan 1940-'45 | 66 |
Laan '40-'45 | ❓❓❓ |
Langeloërduinen | 3 46 |
Marienwaerdt | 2e Dreef 2 |
Provincialeweg N205 | 1 |
Rivium | 2e Straat 59. |
Nieuwe gracht | 20rd |
Nieuwe gracht | 20rd 2 |
Nieuwe gracht | 20zw /2 |
Nieuwe gracht | 20zw/3 |
Nieuwe gracht | 20 zw/4 |
Bahnhofstr. | 4 |
Wertstr. | 10 |
Lindenhof | 1 |
Nordesch | 20 |
Weilstr. | 6 |
Harthauer Weg | 2 |
Mainaustr. | 49 |
August-Horch-Str. | 3 |
Marktplatz | 31 |
Schmidener Weg | 3 |
Karl-Weysser-Str. | 6 |
Perl 6
Uses the code from the Separate the house number from the street name task almost verbatim. Included here to make a complete, runnable example.
<lang perl6>use File::Temp;
my $addresses = qq :to /END/;
Plataanstraat 5 Straat 12 Straat 12 II Dr. J. Straat 12 Dr. J. Straat 12 a Dr. J. Straat 12-14 Laan 1940 – 1945 37 Plein 1940 2 1213-laan 11 16 april 1944 Pad 1 1e Kruisweg 36 Laan 1940-’45 66 Laan ’40-’45 Langeloërduinen 3 46 Marienwaerdt 2e Dreef 2 Provincialeweg N205 1 Rivium 2e Straat 59. Nieuwe gracht 20rd Nieuwe gracht 20rd 2 Nieuwe gracht 20zw /2 Nieuwe gracht 20zw/3 Nieuwe gracht 20 zw/4 Bahnhofstr. 4 Wertstr. 10 Lindenhof 1 Nordesch 20 Weilstr. 6 Harthauer Weg 2 Mainaustr. 49 August-Horch-Str. 3 Marktplatz 31 Schmidener Weg 3 Karl-Weysser-Str. 6 END
my @row-color = '#d7fffe', '#9dbcd4';
- build the table
sub genTable () {
my $table = '
' ~ qq|\n|;my $i = 0; for $addresses.lines -> $addr {$table ~= qq||;
$addr ~~ m[ ( .*? ) [ \s+ ( | \d+ [ \- | \/ ] \d+ | <!before 1940 | 1945> \d+ <[ a..z I . / \x20 ]>* \d* ) ]? $ ];quietly $table ~= qq|\n|;
}$table ~ '
Address | Street | House Number |
---|---|---|
{$addr} | {$0.Str} | {$1.Str||} |
';
}
- generate the page content
sub content {
qq :to /END/; <html> <head> <title>Rosetta Code - Start a Web Browser</title> <meta charset="UTF-8"> </head> <body bgcolor="#d8dcd6">
Split the house number from the street name
{ genTable }
</body> </html> END
}
- Use a temporary file name and file handle
my ($fn, $fh) = tempfile :suffix('.html');
- dump the content to the file
$fh.spurt: content;
- use appropriate command for Windows or X11
- other OSs/WMs may need different invocation
my $command = $*DISTRO.is-win ?? "start $fn" !! "xdg-open $fn";
- start the browser
shell $command;
- wait for a bit to give browser time to load before destroying temp file
sleep 5; </lang>
- Output:
Will start the default browser (or open a new tab/window in a running one) and display this table.
Address | Street | House Number |
---|---|---|
Plataanstraat 5 | Plataanstraat | 5 |
Straat 12 | Straat | 12 |
Straat 12 II | Straat | 12 II |
Dr. J. Straat 12 | Dr. J. Straat | 12 |
Dr. J. Straat 12 a | Dr. J. Straat | 12 a |
Dr. J. Straat 12-14 | Dr. J. Straat | 12-14 |
Laan 1940 – 1945 37 | Laan 1940 – 1945 | 37 |
Plein 1940 2 | Plein 1940 | 2 |
1213-laan 11 | 1213-laan | 11 |
16 april 1944 Pad 1 | 16 april 1944 Pad | 1 |
1e Kruisweg 36 | 1e Kruisweg | 36 |
Laan 1940-’45 66 | Laan 1940-’45 | 66 |
Laan ’40-’45 | Laan ’40-’45 | |
Langeloërduinen 3 46 | Langeloërduinen | 3 46 |
Marienwaerdt 2e Dreef 2 | Marienwaerdt 2e Dreef | 2 |
Provincialeweg N205 1 | Provincialeweg N205 | 1 |
Rivium 2e Straat 59. | Rivium 2e Straat | 59. |
Nieuwe gracht 20rd | Nieuwe gracht | 20rd |
Nieuwe gracht 20rd 2 | Nieuwe gracht | 20rd 2 |
Nieuwe gracht 20zw /2 | Nieuwe gracht | 20zw /2 |
Nieuwe gracht 20zw/3 | Nieuwe gracht | 20zw/3 |
Nieuwe gracht 20 zw/4 | Nieuwe gracht | 20 zw/4 |
Bahnhofstr. 4 | Bahnhofstr. | 4 |
Wertstr. 10 | Wertstr. | 10 |
Lindenhof 1 | Lindenhof | 1 |
Nordesch 20 | Nordesch | 20 |
Weilstr. 6 | Weilstr. | 6 |
Harthauer Weg 2 | Harthauer Weg | 2 |
Mainaustr. 49 | Mainaustr. | 49 |
August-Horch-Str. 3 | August-Horch-Str. | 3 |
Marktplatz 31 | Marktplatz | 31 |
Schmidener Weg 3 | Schmidener Weg | 3 |
Karl-Weysser-Str. 6 | Karl-Weysser-Str. | 6 |
Racket
<lang racket>
- lang at-exp racket
... same code as "Separate_the_house_number_from_the_street_name" ...
(require net/sendurl scribble/html)
(define (render-table)
(for/list ([str (in-list (string-split adressen #rx" *\r?\n *"))] [i (in-naturals)]) (tr bgcolor: (if (even? i) "#fcf" "#cff") (td str) (map td (cond [(splits-adressen str) => cdr] [else '(??? ???)])))))
@(compose1 send-url/contents xml->string){
@html{@head{@title{Splitting Results}} @body{@h1{Splitting Results} @table{@render-table}}}
} </lang>
Scala
Uses nothing but the Standard Library: <lang scala>import java.awt.Desktop import java.io.{IOException, PrintWriter} import java.net.{URI, ServerSocket} import scala.xml.Elem
class WebServer(port: Int, soleDocument: Elem) extends Thread {
this.setName(s"Server at $port")
override def run() { val listener = try { new ServerSocket(port) } catch { case e: java.net.BindException => throw new IllegalStateException(s"Port $port already taken!") } println(s"Listening on port ${listener.getLocalPort}")
while (!Thread.interrupted()) { try { //print(".") val socket = listener.accept new PrintWriter(socket.getOutputStream, true).println(soleDocument) socket.close() } catch { case ioe: IOException => println(ioe) } } }
} // class WebServer
object HtmlServer extends App {
val PORT = 64507 // Main val thread = new WebServer(PORT, HtmlBuilder) val uri = URI.create(s"http://localhost:$PORT/") thread.start()
def HtmlBuilder: Elem = { def adressen: Iterator[String] = """Plataanstraat 5 |Straat 12 |Straat 12 II |Straat 1940 II |Dr. J. Straat 40 |Dr. J. Straat 12 a |Dr. J. Straat 12-14 |Laan 1940 – 1945 37 |Plein 1940 2 |1213-laan 11 |16 april 1944 Pad 1 |1e Kruisweg 36 |Laan 1940-’45 66 |Laan ’40-’45 |Langeloërduinen 3 46 |Marienwaerdt 2e Dreef 2 |Provincialeweg N205 1 |Rivium 2e Straat 59. |Nieuwe gracht 20rd |Nieuwe gracht 20rd 2 |Nieuwe gracht 20zw /2 |Nieuwe gracht 20zw/3 |Nieuwe gracht 20 zw/4 |Bahnhofstr. 4 |Wertstr. 10 |Lindenhof 1 |Nordesch 20 |Weilstr. 6 |Harthauer Weg 2 |Mainaustr. 49 |August-Horch-Str. 3 |Marktplatz 31 |Schmidener Weg 3 |Karl-Weysser-Str. 6""".stripMargin.lines
def getSplittedAddresses(addresses: Iterator[String]) = { val extractor = new scala.util.matching.Regex( """(\s\d+[-/]\d+)|(\s(?!1940|1945)\d+[a-zI. /]*\d*)$|\d+\['][40|45]$""")
def splitsAdres(input: String): (String, String) = (extractor.split(input).mkString, extractor.findFirstIn(input).getOrElse(""))
addresses.map(org => { val temp = splitsAdres(org) List(org, temp._1, temp._2) }) }
def generateTable: Elem = {
def coloring(rownum: Any): String = { rownum match { case Nil => "#9bbb59" case n: Int => if (n % 2 == 0) "#ebf1de" else "#d8e4bc" } }{(List(List("Given Address", "Street", "House Number")) ++ getSplittedAddresses(adressen)). zipWithIndex.map { case (row, rownum) => (if (rownum == 0) Nil else rownum) +: row}.map(row => {row.map(cell => if (row.head == Nil) else )} )}
{cell} |
{cell} |
---|
} // def generateTable
<html> <head> <title>Rosetta.org Task solution</title> </head> <body lang="en-US" bgcolor="#e6e6ff" dir="LTR">
Split the house number from the street name
{generateTable}
</body> </html> } // def content if (Desktop.isDesktopSupported && Desktop.getDesktop.isSupported(Desktop.Action.BROWSE)) Desktop.getDesktop.browse(uri) else println(s"Automatic start of Web browser not possible.\nWeb browser must be started manually, use $uri.")
if (!thread.isAlive) sys.exit(-1) println("Web server started.") do print("Do you want to shutdown this server? <Y(es)/N>: ") while (!scala.io.StdIn.readBoolean) sys.exit()
}</lang>
- Output:
Split the house number from the street name
Given Address | Street | House Number | |
---|---|---|---|
1 | Plataanstraat 5 | Plataanstraat | 5 |
2 | Straat 12 | Straat | 12 |
3 | Straat 12 II | Straat | 12 II |
4 | Straat 1940 II | Straat 1940 II | |
5 | Dr. J. Straat40 | Dr. J. Straat | 40 |
6 | Dr. J. Straat 12 a | Dr. J. Straat | 12 a |
7 | Dr. J. Straat 12-14 | Dr. J. Straat | 12-14 |
8 | Laan 1940 – 1945 37 | Laan 1940 – 1945 | 37 |
9 | Plein 1940 2 | Plein 1940 | 2 |
10 | 1213-laan 11 | 1213-laan | 11 |
11 | 16 april 1944 Pad 1 | 16 april 1944 Pad | 1 |
12 | 1e Kruisweg 36 | 1e Kruisweg | 36 |
13 | Laan 1940-’45 66 | Laan 1940-’45 | 66 |
14 | Laan ’40-’45 | Laan ’40-’45 | |
15 | Langeloërduinen 3 46 | Langeloërduinen | 3 46 |
16 | Marienwaerdt 2e Dreef 2 | Marienwaerdt 2e Dreef | 2 |
17 | Provincialeweg N205 1 | Provincialeweg N205 | 1 |
18 | Rivium 2e Straat 59. | Rivium 2e Straat | 59. |
19 | Nieuwe gracht 20rd | Nieuwe gracht | 20rd |
20 | Nieuwe gracht 20rd 2 | Nieuwe gracht | 20rd 2 |
21 | Nieuwe gracht 20zw /2 | Nieuwe gracht | 20zw /2 |
22 | Nieuwe gracht 20zw/3 | Nieuwe gracht | 20zw/3 |
23 | Nieuwe gracht 20 zw/4 | Nieuwe gracht | 20 zw/4 |
24 | Bahnhofstr. 4 | Bahnhofstr. | 4 |
25 | Wertstr. 10 | Wertstr. | 10 |
26 | Lindenhof 1 | Lindenhof | 1 |
27 | Nordesch 20 | Nordesch | 20 |
28 | Weilstr. 6 | Weilstr. | 6 |
29 | Harthauer Weg 2 | Harthauer Weg | 2 |
30 | Mainaustr. 49 | Mainaustr. | 49 |
31 | August-Horch-Str. 3 | August-Horch-Str. | 3 |
32 | Marktplatz 31 | Marktplatz | 31 |
33 | Schmidener Weg 3 | Schmidener Weg | 3 |
34 | Karl-Weysser-Str. 6 | Karl-Weysser-Str. | 6 |
Tcl
<lang tcl>package require Tcl 8.6
- This is identical to the address task. Skip forward to the next section...
proc split_DE_NL_address {streetAddress} {
set RE {(?x)
^ (.*?) ( (?:\s \d+ [-/] \d+) | (?:\s (?!1940|1945)\d+ [a-zI. /]* \d*) )? $
} regexp $RE $streetAddress -> str num return [list [string trim $str] [string trim $num]]
}
set data {
Plataanstraat 5 Straat 12 Straat 12 II Dr. J. Straat 12 Dr. J. Straat 12 a Dr. J. Straat 12-14 Laan 1940 – 1945 37 Plein 1940 2 1213-laan 11 16 april 1944 Pad 1 1e Kruisweg 36 Laan 1940-’45 66 Laan ’40-’45 Langeloërduinen 3 46 Marienwaerdt 2e Dreef 2 Provincialeweg N205 1 Rivium 2e Straat 59. Nieuwe gracht 20rd Nieuwe gracht 20rd 2 Nieuwe gracht 20zw /2 Nieuwe gracht 20zw/3 Nieuwe gracht 20 zw/4 Bahnhofstr. 4 Wertstr. 10 Lindenhof 1 Nordesch 20 Weilstr. 6 Harthauer Weg 2 Mainaustr. 49 August-Horch-Str. 3 Marktplatz 31 Schmidener Weg 3 Karl-Weysser-Str. 6
}
- Construct the HTML to show
set html "<html><head><meta charset=\"UTF-8\"> <title>split_DE_NL_address</title></head><body>
"foreach streetAddress [split $data "\n"] {
set streetAddress [string trim $streetAddress] if {$streetAddress eq ""} continue lassign [split_DE_NL_address $streetAddress] str numappend html ""
}
append html "Address | Street | Number |
---|---|---|
$streetAddress | $str | $num |
</body></html>"
- Pick a unique filename with .html extension (important!)
set f [file tempfile filename street.html] fconfigure $f -encoding utf-8 puts $f $html close $f
- THE WEB BROWSER LAUNCH CODE MAGIC #####
- Relies on the default registration of browsers for .html files
- This is all very platform specific; Android requires another incantation again
if {$tcl_platform(platform) eq "windows"} {
exec {*}[auto_execok start] "" [file nativename $filename]
} elseif {$tcl_platform(os) eq "Darwin"} {
exec open $filename
} else {
exec xdg_open $filename
}</lang>
- Output:
Address | Street | Number |
---|---|---|
Plataanstraat 5 | Plataanstraat | 5 |
Straat 12 | Straat | 12 |
Straat 12 II | Straat | 12 II |
Dr. J. Straat 12 | Dr. J. Straat | 12 |
Dr. J. Straat 12 a | Dr. J. Straat | 12 a |
Dr. J. Straat 12-14 | Dr. J. Straat | 12-14 |
Laan 1940 – 1945 37 | Laan 1940 – 1945 | 37 |
Plein 1940 2 | Plein 1940 | 2 |
1213-laan 11 | 1213-laan | 11 |
16 april 1944 Pad 1 | 16 april 1944 Pad | 1 |
1e Kruisweg 36 | 1e Kruisweg | 36 |
Laan 1940-’45 66 | Laan 1940-’45 | 66 |
Laan ’40-’45 | Laan ’40-’45 | |
Langeloërduinen 3 46 | Langeloërduinen | 3 46 |
Marienwaerdt 2e Dreef 2 | Marienwaerdt 2e Dreef | 2 |
Provincialeweg N205 1 | Provincialeweg N205 | 1 |
Rivium 2e Straat 59. | Rivium 2e Straat | 59. |
Nieuwe gracht 20rd | Nieuwe gracht | 20rd |
Nieuwe gracht 20rd 2 | Nieuwe gracht | 20rd 2 |
Nieuwe gracht 20zw /2 | Nieuwe gracht | 20zw /2 |
Nieuwe gracht 20zw/3 | Nieuwe gracht | 20zw/3 |
Nieuwe gracht 20 zw/4 | Nieuwe gracht | 20 zw/4 |
Bahnhofstr. 4 | Bahnhofstr. | 4 |
Wertstr. 10 | Wertstr. | 10 |
Lindenhof 1 | Lindenhof | 1 |
Nordesch 20 | Nordesch | 20 |
Weilstr. 6 | Weilstr. | 6 |
Harthauer Weg 2 | Harthauer Weg | 2 |
Mainaustr. 49 | Mainaustr. | 49 |
August-Horch-Str. 3 | August-Horch-Str. | 3 |
Marktplatz 31 | Marktplatz | 31 |
Schmidener Weg 3 | Schmidener Weg | 3 |
Karl-Weysser-Str. 6 | Karl-Weysser-Str. | 6 |