View script | License | Download documentation as: HTML or editable | Download script | History |
[0.055] 38.521k
Documentation for: weblinks.rUsage document for the %weblinks.r1. Using weblinks.rThis script will scan a web page and display each clickable link found. As a learning aid, it includes an example of the power and simplicity of the REBOL parse dialect. 1.1. %weblinks.r At a GlanceExecuted from the library of 21-May-2007. Your results will most definitely vary. >> do %weblinks.r connecting to: www.rebol.org Script: "Web Page Link Displayer" (20-May-1999) connecting to: www.rebol.com http://www.rebol.com /index-lang.html /download.html /docs.html http://www.rebol.net http://www.rebol.org /license.html /index-ios.html /ios-advantage.html /reblets.html /mission.html /support.html /feedback.html /contacts.html http://devcon2007.rebdocproj.org http://www.rebol.com/article/0333.html http://www.rebol.com/article/0324.html http://www.rebol.com/article/0324.html /docs3/architecture.html priorities.html docs/quick-start.html docs/quick-start5.html news/cheyenne.html web-plugin.html http://www.rebol.com/notes/rebol3roadmap.html index-ios.html help-wanted.html what-rebol.html tutorials.html http://www.rebol.com/cgi-bin/blog.r http://rebolweek.blogspot.com/ http://www.rebol.net/cgi-bin/r3blog.r news/rt5610.html news/rt5610.html http://www.rebol.net docs/services/ sdk.html /cgi-bin/wip.r?r=edit+%25web/index.html& == none 1.2. Setup optional%weblinks.r can be customized by changing the url! read into the page variable. Copy the library script to a local directory with the command listed below. It is listed below to keep the floating table of contents from obscuring the command. Edit the local %weblinks.r. Change the web page you wish to check. If you are using REBOL/View, just use the builtin editor function. For REBOL/Core, you will need to run an external editor, such as notepad. Getting a local copy using REBOL: >> write %weblinks.r read http://www.rebol.org/cgi-bin/cgiwrap/rebol/download-a-script.r?script-name=weblinks.ror you use your browser and the rebol.org download script options. 1.3. Standard REBOL network settingsThere is a standard utility that holds some basic REBOL configuration information. set-net takes a block of information so that REBOL knows how to route mail and for other internet connections. The REBOL/View Viewtop main User menu allows access to these settings or you can edit the default %user.r file and change the set-net information. >> help set-net USAGE: SET-NET settings DESCRIPTION: Network setup. All values after default are optional. Words OK for server names. SET-NET is a function value. ARGUMENTS: settings -- [email-addr default-server pop-server proxy-server proxy-port-id proxy-type esmtp-user esmtp-pass] (Type: block) The first two setting are for sending mail, the third is for reading mail, then there are proxy connection settings and then two settings for authenticated mail username and password. For %weblinks.r, you will only need to worry about this if there is a proxy internet connection that needs to be configured. 1.4. Running %weblinks.rAfter the configuration is all set up, using weblinks is simple. Just do it. >> do %weblinks.ror directly out of the library, using http://www.rebol.com as the page to scan. >> do http://www.rebol.org/cgi-bin/cgiwrap/rebol/download-a-script.r?script-name=weblinks.r This will read the web page, and parse out any text found between <a href= and the > hypertext anchor tags. 2. What you can learnThis script has a few learning goodies in it. 2.1. makeREBOL is a multi-paradigm programming environment. One of those paradigms is Object Orientation. In support of this paradigm and for other reasons, REBOL supports a make feature. make creates a new data item from a specification. It doesn't necessarily have to be objects that are created, and this case make is used to preallocate a string! and a block!. The expression tags: make block! 100 allocates empty space for 100 block entries. This is for performance reasons. REBOL expands and shrinks data structures without programmer intervention, but allows such intervention for performance or explicit data type initialization. In this case, tags needed to be set to a block! type for a later append operation to complete successfully, but it could have been set to any number from zero to very large, bounded only by available computer memory. The expression text: make string! 8000 allocated space for a string up to 8000 characters long. Again, purely for performance. These sizings are not required, but setting the datatype may be. 2.2. parse rulesThe variable html-code is now set to what might be termed a design pattern. It is a general purpose parsing rule that extract tags and text from a string of markup entries. In the case of %weblinks.r the textual portion is not used, but as a design pattern, this is a reusable sequence of code. In a large parse, where performance was critical, such reusable sequences may be detrimental, but for this sample script, REBOL is more than fast enough that you will be hard pressed to notice the effect of this "extra" work. Don't remove the code that looks extraneous, just yet. It has the effect of also moving the parse scanner to the next tag. You will see this html-code sequence in another web related rebol.org library script, %websplit.r. 2.2.1. parse dialect...copy, to and thruOne of the main features of REBOL (built into the name actually) is that expressions are relative. The copy in the html-code parse rule block is not the same function as copy in the global REBOL namespace. The REBOL copy creates a copy of the value following. The parse dialect copy captures the next substring match to a variable. Completely different. So copy tag ["<" thru ">"] will capture the string between angle brackets (including the brackets) to the tag variable. The keyword thru has no meaning in the REBOL global context but in the parse dialect causes the scanner to proceed searching for the pattern that follows and include the pattern. The keyword to, which means to convert and return value according to a given spec, in the REBOL context, causes the parse scanner to proceed to search up to but not including the pattern that follows. 2.3. In parse evaluationIn html-code there is a copy into the variable tag followed by a paren! expression that appends the captured tag into the tags block. paren! expressions inside the parse dialect instructs the scanner to execute the REBOL code, using the REBOL global context if a match succeeded. In this case, if a tag is matched, the tag is appended to the tags block! that was created with the make block! 100 at the top of the script. 2.4. Alternative matchingNow we get to some of the magic of pattern matching in parse. After the paren! expression there is a vertical bar (|). This informs parse to attempt an alternative pattern match if the last one failed. It is kind of like saying or to the parser. If you find a tag, fine, but if not try the next pattern. So find a tag or the next pattern. In this case, scan (and copy the resulting match to the variable txt) up to the next opening angle bracket "<". This is a really powerful feature, referred to as alternative matching. In the case of html-code, this alternative match also includes a paren! expression, which appends the captured txt to the text string! created with the make string! 8000. This append is extraneous to the task at hand, as the text string is never used, but it will occur so fast that it doesn't hurt too much here. 2.5. Reading web pagesNow we get to one of the really powerful but simple features of REBOL, the ease of reading a web page. The expression page: read http://www.rebol.com places the HTML of the rebol.com home page into the variable page. 2.5.1. Changing the url!You can change the site you pull links out of by changing the url! that is read into page.2.6. parse with someAnd finally the parse. A fine example of REBOL's concise and expressive power. The short expression parse page [to < some html-code] does a lot of things. First the parser is instructed to find the first opening angle bracket "<" in the page variable. Up to but not including the bracket. Then a some keyword is encountered. some instructs the scanner to match the pattern, in this case html-code, one or more times. So it will keep trying to match the patterns in html-code as many times as it can, but at least once.So, reading back to the html-code rules, it starts out by capturing into the variable tag an opening angle bracket "<", which should work, because the very first thing the scanner did was to "<". Then all the text thru the closing angle bracket ">" and appends any found tag to the tags block. The html-code rule has an alternate if no tag is found. The assumption is that it did find a tag, so the alternate will not need to be tried, the first time. Back to some. So the scanner found a tag, appended it to tags and then hit the bottom of the html-code rule. And then parse, inside a some directive tries to find as many matches as it can. So it internally loops back to the start of the html-code rule. It may or may not find a tag this time. (This is an over simplification but) many web sites start out with <html><head><title>The website title</title></head>and that means parse will probably will find another tag <head> and then another <title> and then it won't, no "<" this time. So the alternate will be tried, copying anything it scans into the variable txt and up to the next "<". Ready for the some to call the html-code rule again, which will match a tag. And so on until html-code can't find either a tag or any text. 2.6.1. parse match failureWhen parse is operating, it tries it's hardest to succeed. When it can't it returns false, meaning the match didn't work. In the case of %weblinks.r, we don't care if this parse actually succeeds, because all the tags have been appended to the tags block. 2.7. foreach loopNow %weblinks.r starts a foreach loop. This handy little expression evaluates a block, setting temporary variables (in this case just one, tag, a completely different variable than the tag that was used in the html-code rule) for every element of a series! (in this case, each tag in the tags block). 2.8. parse match success or failureThis time, parse is called and the code logic cares if it succeeds or fails. It is wrapped in a if expression. If the current tag starts with an "<A" which is HTML speak for anchor followed by "HREF=", meaning a Hypertext reference anchor, we have an HTML link. So, copy the text of the link into the variable link. This is done with an alternate, to include both links that are surrounded by quotes, or not. The quotes are not included in the first alternate case. 2.8.1. to endThis match pattern ends with to end. end is a special pattern inside a parse rule that matches the end of the input. It lets the parse expression return true, since we care this time. If the to end was not included, parse would return false as the final ">" would still be left over and parse wants to match everything before it informs REBOL of a positive pattern match. The if expression will either print out the text captured in link or if the current tag is not a hyper link will see false come out of parse and do nothing. 3. Further studyThere are some good tutorials on the use of parse. Brett Handley has an awesome tutorial site, as well as the REBOL/Core manual Parsing chapter, and a REBOL WikiBook entry. 4. Some Definitions
5. Also worth a lookThere is a full suite of scripts that demonstrate how easy it is to use the HTTP url! (or "the web" ) features in REBOL. These features are one of the central design goals of the REBOL scripting environment. These sample scripts highlight the ease of using internet resources with REBOL. 6. List of tutorial scripts in the web category
7. A script you have to check out
8. More in-depth scripts
8.1. Other web related scriptsdealing with web related REBOL programming that you can find in the rebol.org library. There are also complete suites for FTP, HTML, CGI, and many others.9. Credits
|