Documention for: view-html.r Created by: btiffin on: 17-May-2007 Last updated by: btiffin on: 17-May-2007 Format: text/editable Downloaded on: 30-Apr-2025 [h1 Usage document for %view-html.r [contents [numbering-on [h2 Introduction to %view-html.r [p view-html.r is an introduction to the ease of gui creation with REBOL/View, plus the ease and simplicity of accessing internet URLs. [table/att/border="1px"/att/cellpadding="4px" [row [cell **gui** [cell Graphic User Interface, usually pronounced as gooey [row [cell **HTML** [cell Hyper Text Markup Language, usually pronounced [br H T M L [row [cell **URL** [cell Uniform Resource Locator, usually pronounced [br U R L [row [cell **widget** [cell Window Gadget, REBOL/View uses the term **face** for buttons, text areas and other windowing gadgets. table] [h2 view-html At a Glance [p Not setup is required, just **do** it. [asis >> do %view-html.r asis] [p [image http://www.rebol.org/library/arts/accessories/lf019t/view-html.png "view-html.png" [p The above image was created running REBOL/View 2.7.5.4.2 GNU/Linux 4.0, KDE Desktop. and has been cropped for space. [h2 Using %view-html.r [p Requires REBOL/View [h3 Running %view-html.r [p From the library with: [asis/style/font-size:75% >> do http://www.rebol.org/cgi-bin/cgiwrap/rebol/download-a-script.r?script-name=view-html.r asis] or locally with: [asis >> do %view-html.r asis] [h2 See also [p There is another rebol.org script, very similar to this one, [br [link http://www.rebol.org/cgi-bin/cgiwrap/rebol/view-script.r?script=webprint.r "%webprint.r" that is more like the sample D program listed below, in that there is no gui involved. [h2 What you can learn [p This script is an introduction to the **view** and **layout** functions. [br Please see the [link http://rebol.com/docs/view-guide.html "REBOL/View Developer's Guide" [h3 URLs [p **http://www.rebol.com** is actually a value with a special datatype. In REBOL this is a **url!**. Very powerful. No quotes needed. REBOL just **knows**. [h3 Web Server defaults [p Web Servers have default files that are returned. http://www.rebol.com is actually returned as http://www.rebol.com/index.html. This is not always the case. Some sites return default.htm, or index.php, or index.cgi. No need to worry, the REBOL **read** function and the web server will work that all out for you. If REBOL Technologies ever changes its web server setup, a different file may be returned and this script will still work. [h3 Changing what page is displayed [p Changing the website or page that is printed is as easy as changing the text after the http:// part following the **read** command. [h3 Getting REBOL [p Both REBOL/Core and REBOL/View are available [br **free of charge** from [link http://www.rebol.com "www.rebol.com" [h3 Super simple and powerful gui creation [p REBOL/View gui building is **easy**. [p **view layout** [...gui specification...] How cool is REBOL/View? [h3 Powerful builtin Internet Access [p REBOL has fantastically simple builtin procedures for accessing the internet. [br **read http://www.rebol.com** accesses and returns the HTML as text. How cool is that? [h3 Compare the complexity to the simplicity [p Please compare the **read http://www.rebol.com** 25 character sequence to this [link http://www.digitalmars.com/d "D language" program (which doesn't have a window by the way, just text output to a console). [h4 D Language sample for printing HTML as text [asis/style/font-size:75% /* HTMLget written by Christopher E. Miller This code is public domain. You may use it for any purpose. This code has no warranties and is provided 'as-is'. */ //debug = HTMLGET; import std.string, std.conv, std.stream; import std.socket, std.socketstream; int main(char[][] args) { if(args.length < 2) { printf("Usage:\n htmlget <web-page>\n"); return 0; } char[] url = args[1]; int i; i = std.string.find(url, "://"); if(i != -1) { if(icmp(url[0 .. i], "http")) throw new Exception("http:// expected"); } i = std.string.find(url, '#'); if(i != -1) // Remove anchor ref. url = url[0 .. i]; i = std.string.find(url, '/'); char[] domain; if(i == -1) { domain = url; url = "/"; } else { domain = url[0 .. i]; url = url[i .. url.length]; } uint port; i = std.string.find(domain, ':'); if(i == -1) { port = 80; // Default HTTP port. } else { port = std.conv.toUshort(domain[i + 1 .. domain.length]); domain = domain[0 .. i]; } debug(HTMLGET) printf("Connecting to " ~ domain ~ " on port " ~ std.string.toString(port) ~ "...\n"); auto Socket sock = new TcpSocket(new InternetAddress(domain, port)); Stream ss = new SocketStream(sock); debug(HTMLGET) printf("Connected!\nRequesting URL \"" ~ url ~ "\"...\n"); if(port != 80) domain = domain ~ ":" ~ std.string.toString(port); ss.writeString("GET " ~ url ~ " HTTP/1.1\r\n" "Host: " ~ domain ~ "\r\n" "\r\n"); // Skip HTTP header. char[] line; for(;;) { line = ss.readLine(); if(!line.length) break; const char[] CONTENT_TYPE_NAME = "Content-Type: "; if(line.length > CONTENT_TYPE_NAME.length && !icmp(CONTENT_TYPE_NAME, line[0 .. CONTENT_TYPE_NAME.length])) { char[] type; type = line[CONTENT_TYPE_NAME.length .. line.length]; if(type.length <= 5 || icmp("text/", type[0 .. 5])) throw new Exception("URL is not text"); } } print_lines: while(!ss.eof()) { line = ss.readLine(); printf("%.*s\n", line); //if(std.string.ifind(line, "</html>") != -1) // break; size_t iw; for(iw = 0; iw != line.length; iw++) { if(!icmp("</html>", line[iw .. line.length])) break print_lines; } } return 0; } asis] What would you rather type? The above or something like [br **print read join http:// ask "Web site? " ** What will be easier to remember 6 months from now? [h2 What can break [p This particular example does not have scroll bars. Not hard to add, but in this case, there is more text on the **rebol.com** home page than will fit in the window. [p You will need access to the internet and rebol.com will have to be up and running for this script to work. Don't worry, **rebol.com** is always up and running. [p REBOL/Core will not have the **view** or **layout** functions defined. You will need to start up REBOL/View. [h2 Credits [table/att/border="1px"/att/cellpadding="4px" [row [cell %view-html.r [cell Author: Unknown [row [cell %webprint.r [cell Author: Unknown [row [cell htmlget.d [cell Author Christopher E. Miller [row [cell D Programming Language [cell Walter Bright, Digital Mars [row [cell REBOL/View [cell Carl Sassenrath, REBOL Technologies table] [list [li The rebol.org Library Team [li Usage document by Brian Tiffin, Library Team Apprentice, [date list]