View script | License | Download documentation as: HTML or editable | Download script | History |
[0.058] 37.863k
Documentation for: websplit.rUsage document for the %websplit.r1. Using websplit.rThis script will scan a web page and seperate all the tags, from the text. As a learning aid, it includes an example of the power and simplicity of the REBOL parse dialect. 1.1. %websplit.r At a GlanceExecuted on 25-May-2007. Your results will most definitely vary. >> do %websplit.r Script: "Web HTML Tag Extractor" (20-May-1999) connecting to: www.rebol.com <html> <head> <META NAME="Description" CONTENT="Lightweight distributed computing, collaboration, and programming systems for the X Internet. Site includes products, downloads, documentation, and support."> <META NAME="Keywords" CONTENT="REBOL, programming, Internet, software, domain specific language, distributed computing, collaboration, operating systems, development, rebel"> <meta http-equiv="content-type" content="text/html;charset=iso-8859-1"> <title> </title> <style type="text/css"> </style> </head> <body background="/graphics/backlined.gif" bgcolor="#80887a" LINK="maroon" ALINK="#9F9466" VLINK="#506027"stuff removed, a few carriage returns added, and some data hidden behind the table of contents. REBOL Technologies body, p, td, ol, ul {font-family: arial, sans-serif, helvetica; font-size: 10pt;} h1 {font-size: 14pt;} h2 {font-size: 12pt; color: #2030a0; width: 100%; border-bottom: 1px solid #c09060;} h3 {font-size: 11pt; color: #2030a0;} h4 {font-size: 10pt;} tt {font-family: "courier new", monospace, courier; font-size: 9pt;} pre { font: bold 10pt "courier new", monospace, console; overflow: auto; background-color: #f0f0f0; padding: 16px; border: solid #a0a0a0 1px; } li {margin-bottom: 10px} .title {font-size: 16pt; font-weight: bold;}more stuff removed 1.2. Setup optional%websplit.r can be customized by changing the url! read into the page variable. Copy the library script to a local directory with >> write %websplit.r read http://www.rebol.org/cgi-bin/cgiwrap/rebol/download-a-script.r?script-name=websplit.ror you use your browser and the rebol.org download script options. Edit the local %websplit.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. 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 %websplit.r, you will only need to worry about this if there is a proxy internet connection that needs to be configured. 1.4. Running %websplit.rAfter the configuration is all set up, using websplit is simple. Just do it. >> do %websplit.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=websplit.r This will read the web page, parse out all the HTML tags and seperate all the normal text. 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. As a case in point, there are far more than 100 tags when reading the home page at www.rebol.com. But no worries, REBOL will take care of any and all memory allocations as this script runs. Thanks REBOL. This feature removes seriously complex and error prone programming that lesser languages may force on the programmer. 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.1.1. Advocacy versus disrespectPlease note, the use of lesser is in jest. The "REBOL is better than language X" debate is entirely arbitrary and mostly moot. It is like saying Italian is better than Polish. Emotional perhaps, but not a valid argument. REBOL is just the best and we'll leave it at that. 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. You will see this html-code sequence in another web related rebol.org library script, %weblinks.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 a 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. Same word, different context, different meaning. Just like Polish or Italian. 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 try 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. 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 %websplit.r, we don't care if this parse actually succeeds, because all the tags have been appended to the tags block. And all the text into the text string. We don't really care if it returns true or false, this time. 2.7. foreach loopNow %websplit.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). And finally print out all the text that was accumulated using the html-code rule. 3. Further studyThere are some good tutorials on the use of parse. Brett Handley has an awesome tutorial site. There is also 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 scriptsThere are many scripts dealing 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
|