#!c:/rebol/rebol.exe -cs
REBOL [
    Title: "RBML Template Processing Engine"
    Version: 0.1.3
    Date: 10-Jun-2001
    File: %rbml.r
    Author: "John Schuhr"
    Email: john@schuhr.com
    Purpose: "Execute embedded rebol code"
    References: {
        Maarten Koopmans - erebol.r
        Jeff Kries - rebweb.r
        Tyler Booth - rebweb.r
        Bohdan Lechnowsky - rebweb.r
        Graham Chiu - rebweb.r
    }
]

; Nifty function from rebweb.r
printerror: func [
    error [object!]
    /local arg1 arg2 arg3 message out
][
    out: make string! 100
    set [arg1 arg2 arg3][error/arg1 error/arg2 error/arg3]
    message: get in get in system/error error/type error/id
    if block? message [bind message 'arg1]
    append out reform reduce message
    append out reform [newline "Near: " mold error/near newline]
    append out reform ["Where: " mold error/where newline]
    replace/all replace/all out "<" "&lt;" ">" "&gt;"
]

; Modified from erebol.r
rbml-proc: func [ rbml-content [file! string!] /local rbml-text ] [
  rbml-text: read rbml-content

  ; removes any comment between <!--- and --->, useful for debugging
  rbml-comment: [to "<!---" cs: thru "--->" ce:  (remove/part cs ((index? ce) - (index? cs)))]
  parse rbml-text [any rbml-comment]
  
  ; seeks out any tag that starts with <reb and copies the command within as well
  ; as any text between the open and end tags
  tag-terminator: complement charset "/>"
  reb-rule: [
    copy text-start to "<reb" code-start: (prin text-start)
    thru "<reb"
    copy reb-command [some tag-terminator]
    [
        #"/" thru ">"
        |
        #">"
        copy reb-text to "</reb"
        thru ">"
    ]
    copy rest to end
  ]

    reb-process: func [mu-text [series!]] [
        until [
            unset 'reb-command
            parse/all mu-text [reb-rule]
                
            if value? 'reb-command [
                ; rebml looping construct
                if ((copy/part reb-command 4) == "loop") [
                    ; found loop tag
                    parse (to-block reb-command) [
                        set rbml-foo word! 
                        some ['count set loopcount integer!]
                    ]
                    loop loopcount [
                        do reb-text
                    ]
                ]
            
                ; output vars
                if (reb-command == "out") [
                    until [
                        unset 'somevar
                        parse reb-text [
                            copy out-start to "~" (prin out-start) 
                            thru "~" 
                            copy somevar to "~" 
                            thru "~" 
                            copy rest-text to end
                        ]
                        if (value? 'somevar) [
                            prin get to-word somevar
                        ]
                        reb-text: rest-text
                        not value? 'somevar
                    ]
                    prin rest-text
                ]       

                ; set var
                if ((copy/part reb-command 3) == "set") [
                    unset 'rbml-var
                    unset 'rbml-val
                    parse (parse reb-command none) [
                        set rbml-foo string! 
                        set rbml-var string! 
                        set rbml-val any-type! (set to-word rbml-var rbml-val)
                    ]
                ]
                
                ; include directive
                if ((copy/part reb-command 7) == "include") [
                    unset 'my-include
                    parse (parse reb-command none) [set rbml-foo string! set rbml-include-file string!]
                    rest: join (read to-file rejoin [rbml-base-path "/" rbml-include-file]) rest
                ]
            
                ; generic rebol script
                if (reb-command == "script") [
                    ; found script tag
                    do reb-text
                ]
                mu-text: copy rest
            ]
            not value? 'reb-command
        ]
        prin rest
    ]
    
    ; Process de-commented code
    reb-process rbml-text 
]

print "Content-Type: text/html^/"
rbml-template: to-file replace/all (replace system/options/cgi/path-translated "c:\" "/c/") "\" "/"
rbml-path-info: system/options/cgi/path-info
parse system/options/cgi/path-translated [copy rbml-base-path to rbml-path-info to end]
rbml-proc rbml-template