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

RWT: Rebol Server Pages

 [1/1] from: maarten::koopmans::surfnet::nl at: 3-Mar-2003 19:39


#!c:\kgk\rebol.exe -cs REBOL [ Title: {Rebol Web Toolkit.} Author: {Ernie van der Meer, Maarten Koopmans} Version: 1.0.0.2 Date: 17-1-2003 Copyright: {2002, Ernie van der Meer, 2003 Maarten Koopmans.} License: {This software is released under the GNU public license.} ] if not value? 'advertise [ context [ symbols: [] advertised?: func [ {Determines whether a symbol is defined.} name [string! any-word!] ] [ return found? find symbols to string! name ] unadvertise: func [ {Removes a symbol from the world that shows something is included.} name [string! any-word!] /local pos ] [ if found? pos: find symbols to string! name [ remove symbols ] ] system/words/advertise: func [ {Advertises a symbol to the world to show something is included.} name [string! any-word!] /local statement ] [ if not advertised? name [ append symbols to string! name ] ] system/words/require: func [ {Search for an include and load it. If found, a symbol is automatically advertized for the include.} [catch] includes [any-word! string! any-block!] {What to include.} /search-path paths [file! any-block!] {Set the search path.} /local require-path found err local-includes ] [ if none? require-path [ ; Initialize the require-path local require-path: copy [ %./ ] ] if search-path [ ; Append the paths append require-path paths ; Make sure we don't have duplicates require-path: unique require-path ; Filter everything that's not of type file! for pos 1 length? require-path 1 [ ; Make sure the path entry ends with a '/'. if not #"/" = last to-string first require-path [ change require-path join first require-path "/" ] either not file? first require-path [ remove require-path] [ require-path: next require-path ] ] require-path: head require-path ] local-includes: either any [string? includes any-word? includes ] [ compose [ (includes) ] ] [ includes ] ; Now let's find all of our required includes foreach file local-includes [ found: no ; Is the symbol already advertised? ;if unset? get/any to-lit-word join file "__r" if not advertised? file [ ; Assume that we will be able to find the file and that do-ing it ; will not cause any errors. We need to do this to handle recursive ; requires, or two file mutually requiring eachother advertise file repeat dir require-path [ if exists? rejoin [ dir file ".r" ] [ ; Found the include. Now try to do the file, but watch for ; errors! if error? set/any 'err try [ do rejoin [ dir file ".r" ] ] [ ; Make sure we remove the advertized symbol and re-throw the ; original error. unadvertise file err ] found: yes break ] ] ; Signal an error message if we still haven't found the include. if not found [ unadvertise file throw make error! rejoin [ {*** include not found: } file ] ] ] ] ] ] ] process-rsp: func [ {Processes a text file. All sections of code between <% and %> tags are copied and executed as-is. Code between <%= and %> tags is evaluated and replaced by its literal value.} content [file! string!] {The content to process.} /local code-rule val-rule data code text g-result ] [ ; This would be a good place to consult our page cache in ; case the argument is a file. code: copy {} text: copy {} ; Save the global 'result value and replace it by our own value. set/any 'g-result get/any 'result result: copy {} code-rule: [ "<%" copy data to "%>" thru "%>" ] val-rule: [ "<%=" copy data to "%>" thru "%>" ] ; Parse input text, constructing a 'compiled' version of the text ; in 'code. Things not between "<%" and "%>" will be collected ; in a temporary buffer so we don't end up with a zillion append ; statements in our compiled page. parse/all either file? content [ read content ][ content ] [ any [ val-rule ( if not empty? text [ append code rejoin [ " append result " mold text " " ] clear text ] append code rejoin [ " append/only result " data " " ]) | code-rule ( if not empty? text [ append code rejoin [ " append result " mold text " " ] clear text ] append code rejoin [ " " data " " ]) | copy data skip (append text data) ] ] if not empty? text [ append code rejoin [ " append result " mold text " " ] clear text ] ; This would be a good place to store the code in a page cache. ; Execute the constructed code and remember the result. do code code: result ; Restore the global result value set/any 'result get/any 'g-result code ] ;Modify this to your paths/include libs require/search-path [ handle cgi cookies ] %lib/ cgi/init print "Content-type: text/html^/^/" handle/pass-to [ print process-rsp read either system/version/4 <> 3 [ to-file system/options/cgi/path-translated ] ;Handle windows [ to-file join "/" replace/all replace/all system/options/cgi/path-translated "\" "/" ":" "" ] ] func [ o ] [ ;if unset? get/any 'error-message [ error-message: {Een niet nader ;gespecificeerde fout is opgetreden} ] error-message: mold o if unset? get/any 'error-page [ error-page: %error.asp ] print process-rsp read join site-location error-page ]