Home   Archive   Permalink

Accessing the REBOL header

In a script, in the REBOL header, one can have the line:
Title: 'Script title'
In the load/header function, it seems one can load a script into memory.    
In the rebol.org library, the script header is used, as I understand it, to build the index of sample scripts.
So, what is the syntax for getting one's hands on the script title?    
Thank you.

posted by:   Steven White     14-Sep-2016/16:03:20-7:00

s: load/header [R E B O L [title: "yes"] word1: does [print "hello world"]]
; == [R E B O L [title: "yes"] word1: does [print "hello world"]]
; creating an object is the easiest way, I guess
s-obj: make object! s/rebol
; == "yes"
s2: load/header {R E B O L [title: "yes"] word1: does [print "hello world"]}
; == [make object! [
;        Title: "yes"
;        Date: none
;        Name: none
;        Version: none
;        File: none
;        Home...
; == "yes"
Or if you are running it something like "rebol file.reb" you have system/script/header/ object and you can use it like (same object as s2's, I guess):
print system/script/header/title
print system/script/header/date

posted by:   Darek Nędza     14-Sep-2016/18:05:50-7:00

I haven't done this extensively, but I believe the basic approach is to do the script and then you can call the system object like so:
>> system/script/header/title
== "My Great, Short, Powerful Rebol XYZ Script"
>> system/script/header/description
== "This script slices, dices and makes a great dessert topping too."
As I said, I don't know all of the nuances, but that's the gist of it. This works in Rebol 2.x and Rebol 3 alpha. Don't think it works in Red, at least not in this syntax.

posted by:   Edoc     14-Sep-2016/18:08:47-7:00

As Ed says, SYSTEM/SCRIPT/HEADER will give you access to the header of the currently running script. LOAD/HEADER on a file/url/string (will call it a 'message') will LOAD the data and return a block with the header as an object (or if the message only contains a header, it'll return an object).
    probe load/header {R e b o l [Title: "Foo"] some data}
    probe load/header {R e b o l [Title: "Foo"]}
Note that the effective action converting a header block to an object is CONSTRUCT which unlike CONTEXT or MAKE OBJECT! does not evaluate the contents.
Note also that if your message is coming from an untrusted source, you should not use path notation to access header fields:
    >> data: load/header {R e b o l [Title: #[function! [][print "I am code!"]]]}    
    >> data/title
    I am code!
Rather use GET to read the field safely:
    >> title: get in data 'title
    == function!
Red will support header access at some point soon.

posted by:   Chris     14-Sep-2016/23:41:12-7:00

...as Ed and Darek say...

posted by:   Chris     15-Sep-2016/1:42:26-7:00

At the console [REBOL 2.7.8]:
>> ? system/script
SYSTEM/SCRIPT is an object of value:
     title         none!     none
     header         none!     none
     parent         none!     none
     path            file!     {REDACTED}
     args            none!     none
     words         none!     none
So using path notation system/script/title will get you the title to a script running.
For a running script to load the header of another script.
;; one-off way
R E B O L [
    Title: gets titles of other scripts
use [
    ;; load the files in the directory
    files: load %.
    ;; for all the the files
    forall files [
        ;; check if it is REBOL
        if equal? suffix? files/1 %.r [
            ;; load/header
            h: first load/header files/1
            print h/title
;; smarter way
R E B O L [
    Title: gets titles of other scripts
headhunter: func [
    dir [file!]
         ;; It is not a directory so splitsville
    if not dir? dir [exit]
    ;; load the files in the directory
    files: load dir
    ;; for all the the files
    foreach file files [
        ;; check if it is REBOL
        if equal? suffix? file %.r [
            ;; load/header
            h: first load/header file
            print h/title
;; do it at startup in user.r do headhunter.r to add the RVC Dictionary
;; assuming you are in the directory of your liking
;; call at the console or in a message
» headhunter what-dir
Because many get stuck in ways of thinking (paradigms), at times Carl spoke of scripts. Other times, though, Carl spoke of messages!
Carl said this about the header. Heed his words:
"At the beginning of the script there's a descriptive header that makes scripts self-identifying. THIS isn't just a string, but AN ACTUAL BLOCK of REBOL CODE/DATA that provides a standard method for identifying the purpose and attributes of a script.
"Because it's written in REBOL, other scripts can easily access this header, which means you can treat a group of scripts as a collective database to build cross-references or a script index."
Carl also said this:
"I call R E B O L [a virtual computer that has] a messaging language, because it's intended to be used in the same way as English: for communications, not just algorithms."
You ought to think of a %file.r as a message rather than a script precisely because it is a message. REBOL processes messages.
What you think of as a script is more like an RFC 2822 email message. Every %file.r message has a header and an optional body.
See my work on it: https://timeserieslord.github.io/rvc/#RVC%20Messages

posted by:   Time Series Lord     28-Sep-2016/18:08:28-7:00