Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

(long) RE: Re: help me parse this?

 [1/3] from: pierce::athenasecurity::com at: 5-Nov-2000 16:04


:: whoa, nelly, that's way more complex than I :: need. I'm going to read through your message a :: couple more times to see if I can "get" what :: you're doing. I chose that part of the script because it used 'parse' in several different ways. The main goal was to find the broadcast address of an interface and ping it. The broadcast address will send the request to every machine on that part of a network. This is normally a bad idea, but I wanted to find all of the machines on that part of the network, since it should be restricted to just a few known hosts. Then I compare the machines found against the ones that should be there to find any un-authorized machines placed on the network. :: What I need is only this: I have a local directory :: full of html with something like, :: <select> :: <option> blah blah </option> :: <option> blah blah </option> :: <option> blah blah </option> :: </select> :: in each page. I have a seperate file, with just :: <option> blah blah </option> :: <option> blah blah </option> :: <option> blah blah </option> :: I'd like to replace the options on all the pages :: with the options from the other file. :: That's alot less complicated than what you're doing! :: :-) I have a gift for making things more complicated than they need to be... ;-) Okay, will each page have their own list of <option>s? Is this a permanent replacement, or do you want to have it dynamically generate the list based upon what's currently in the list? Here's one pass at building the list dynamically, based upon what's in the file at that time. This probably isn't the most direct way of doing this, but it should provide some flexibility: If you want to create the page entirely in REBOL, here's one method (if you re-type the code below ignore the numbers in parentheses): ---start REBOL file--- REBOL[] (1) page: { <HTML> <HEAD> <TITLE></TITLE> </HEAD> <BODY> <SELECT>} (2) options: read %options.txt (3) options: parse options none (4) body: [] (5) disarm try[ while [length? options][ (6) append body join "<OPTION>'" join first options join "'</OPTION>" join second options #"^/" (7) remove head options (7) remove head options ]] (8) append page body (9) append page {</SELECT></BODY></HTML>} ---end REBOL file--- This does the following: (1) The variable 'page' is setup with the default page up through the opening <SELECT> tag. Because this is in braces it's a string. (2) This sets the options variable with the values in the options.txt file. This file needs to be in the same directory as the script. The format for the options.txt file, in this case is that the first column should be the item between the <OPTION> and </OPTION> tags. The second item, separated by any type of whitespace, is the text that goes after the </OPTION> tag. (3) Here we parse the contents of the options variable splitting the string on any whitespace (spaces, tabs, newlines, etc). If any of your entries have spaces in them this will treat them as separate items. You will need to put quotes around any items that have spaces in them. (4) This sets up the variable for the part of the page where we dynamically create the <OPTION> tags. (5) We want to loop through the options variable until there is nothing left. (6) This is probably most confusing part, and I'm sure there are easier ways to do this. All we're doing is joining a lot of variables and text together to form the HTML and appending it to the end of the body block. Join works from right to left, which in this case is doing the following (let's presume the first two items in the options.txt file were 'first' and 'First'): join second options #"^/" This will take the second item in the options variable and append a new line to it. Our string looks like: {First#"^/"} join "'</OPTION> join second options #"^/" This takes the result from the join above and pre-pends '</OPTION> to it. I put a ' before the </OPTION> tag in the event that there was a space in the text between option tags. Now our string looks like: {'</OPTION>First#"^/"} join first options join "'</OPTION>" join second options #"^/" Here we take the first item in the options block and pre-pend it to the growing list. At this point, our string looks like: {first'</OPTION>First#"^/"} Next it'll do the final join. This puts the beginning <OPTION> tag in place. Our final string looks like: {<OPTION>'first'</OPTION>First#"^/"} Now that we have completed all of the joins, we append the string to the block 'body'. (7) Since we don't want to loop forever we remove the first two items in the body block. This will eventually empty the block and cause the while loop to end. Just make sure you have an even number of items in the options.txt file. (8) Once we have looped through the contents of the options block, we append the body block to the page. This takes the items from the options.txt file, which have been HTMLized and puts them at the end of the page. (9) Finally we append the necessary HTML to close off the document. At this point if you wanted to have static HTML you could write the page variable to a file with .html extension and it would load fine. Here is another way to do the same thing, but using a static HTML document, a REBOL script and a text file of menu options: ---start html file--- <HTML> ... <SELECT> *REPLACE-WITH-OPTIONS* </SELECT> </BODY> </HEAD> ---end html file--- ---start REBOL script--- REBOL[] (1) page: read %index.html (2) parse page [copy page-top to "*REPLACE-WITH-OPTIONS*" thru *REPLACE-WITH-OPTIONS* copy page-bottom to end] (3) options: read %options.txt (4) options: parse options none (5) body: [] (6) disarm try[ while [length? options][ (7) append body join "<OPTION>'" join first options join "'</OPTION>" join second options #"^/" (8) remove head options (8) remove head options ]] (9) append page-top body (10) append page-top page-bottom ---end REBOL script--- This script is very similar to the one above. Here is the break out: (1) The first thing we'll do is read the index.html page, presumably this will be the template. In this case I put *REPLACE-WITH-OPTIONS* in the HTML page and use this as the place to insert the <OPTION> tags. If you change this change both instances in line #2. (2) This line parses the variable page, which contains the index.html file. [copy page-top to "*REPLACE-WITH-OPTIONS*" thru "*REPLACE-WITH-OPTIONS*" copy page-bottom to end] The first part of this: copy page-top to "*REPLACE-WITH-OPTIONS*" Copies everything up, but not including *REPLACE-WITH-OPTIONS*, into the variable page-top. We don't have to define page-top before setting it here, the parse command will define it for us. Next we copy the remaining part of the document with: thru "*REPLACE-WITH-OPTIONS*" copy page-bottom to end This reads the page variable and goes through the first instance of *REPLACE-WITH-OPTIONS* then copies the remaining into the variable page-bottom to the end of the document. By going through *REPLACE-WITH-OPTIONS* neither of the two variables will have that part of the document in it. All we have to do now is create the <OPTION> tags and insert them in the middle. (3) This sets the options variable with the values in the options.txt file. This file needs to be in the same directory as the script. The format for the options.txt file, in this case is that the first column should be the item between the <OPTION> and </OPTION> tags. The second item, separated by any type of whitespace, is the text that goes after the </OPTION> tag. (4) Here we parse the contents of the options variable splitting the string on any whitespace (spaces, tabs, newlines, etc). If any of your entries have spaces in them this will treat them as separate items. You will need to put quotes around any items that have spaces in them. (5) This sets up the variable for the part of the page where we dynamically create the <OPTION> tags. (6) We want to loop through the options variable until there is nothing left. (7) This is probably most confusing part, and I'm sure there are easier ways to do this. All we're doing is joining a lot of variables and text together to form the HTML and appending it to the end of the body block. Join works from right to left, which in this case is doing the following (let's presume the first two items in the options.txt file were 'first' and 'First'): join second options #"^/" This will take the second item in the options variable and append a new line to it. Our string looks like: {First#"^/"} join "'</OPTION> join second options #"^/" This takes the result from the join above and pre-pends '</OPTION> to it. I put a ' before the </OPTION> tag in the event that there was a space in the text between option tags. Now our string looks like: {'</OPTION>First#"^/"} join first options join "'</OPTION>" join second options #"^/" Here we take the first item in the options block and pre-pend it to the growing list. At this point, our string looks like: {first'</OPTION>First#"^/"} Next it'll do the final join. This puts the beginning <OPTION> tag in place. Our final string looks like: {<OPTION>'first'</OPTION>First#"^/"} Now that we have completed all of the joins, we append the string to the block 'body'. (8) Since we don't want to loop forever we remove the first two items in the body block. This will eventually empty the block and cause the while loop to end. Just make sure you have an even number of items in the options.txt file. (9) Once we have looped through the contents of the options block, we append the body block to the page. This takes the items from the options.txt file, which have been HTMLized and puts them at the end of the page. (10) Finally we append the necessary HTML to close off the document. At this point if you wanted to have static HTML you could write the page variable to a file with .html extension and it would load fine. Hope this helps, Wayne

 [2/3] from: al:bri:xtra at: 6-Nov-2000 16:58


Wayne Pierce wrote:
> REBOL[] > (1) page: {
<<quoted lines omitted: 15>>
> (8) append page body > (9) append page {</SELECT></BODY></HTML>}
Better might be: [ Rebol [] Page: make block! 0 insert head Page [ <html> <head> <title> "Insert your Page title here" </title> <body> <select> ] foreach [Value Text] parse read %Options.txt none [ append Page reduce [ <option> Value </option> Text newline ] ] append Page [ </select> </body> </html> ] write %YourFileNameHere.html join "" Page ] Or using my HTML dialect: [ Rebol [] do %HTML.r write %YourFileNameHere.html HTML [ title "Insert your Page title here" body h1 "Insert your Page title here" select load %Options.txt ] ] I hope that helps! Andrew Martin ICQ: 26227169 http://members.nbci.com/AndrewMartin/

 [3/3] from: al:bri:xtra at: 6-Nov-2000 17:24


I wrote:
> foreach [Value Text] parse read %Options.txt none [ > append Page reduce [ > <option> Value </option> Text newline > ] > ]
Just a slight correction: foreach [Value Text] parse read %Options.txt none [ append Page reduce [ rejoin <option> [{ value="} Value {"}] </option> Text newline ] ] Andrew Martin 900 km/h fingers, brain in idle... ICQ: 26227169 http://members.nbci.com/AndrewMartin/

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted