Home   Archive   Permalink



Demo runner for Nick's Short Code Examples

Being too lazy to copy and paste, I wrote a program to read Nick's file of short examples, put them into a menu, and run them when selected from the menu. It's not a terribly long program even with my un-REBOL-ish style, so I will paste it in below in case anyone finds it useful. Some demos are not friendly to this kind of running, but most work.
    
R E B O L [
     title: 'Run the short demos of Nick Antonaccio'
]
    
DEMOLIST: copy [] ;; Demo file, reassembled
DEMOCODE: copy '' ;; Holding area for one block of demo code
DEMOTITLE: copy '' ;; Holding area for one program title
DEMOID: %demo.r     ;; Temporary file for launching demo
    
IS-HEADER?: func [
     'Check if a line of code is a REBOL header'
     SOURCELINE
] [
     either equal? 'REBOL' copy/part trim/head SOURCELINE 5 [
         return true                    
     ] [
         return false    
     ]
]
    
GET-TITLE: func [
     'Extract any data between quotes (which will be the script title)'
     SOURCELINE
] [
     IN-TITLE: false
     TITLELIT: copy ''
     foreach CHARACTER SOURCELINE [
         either equal? CHARACTER #'^'' [
             either IN-TITLE [
                 IN-TITLE: false
             ] [
                 IN-TITLE: true
             ]
         ] [
             if IN-TITLE [
                 if not-equal? CHARACTER #'^'' [
                     append TITLELIT CHARACTER
                 ]
             ]
         ]
     ]
     return TITLELIT
]
    
;; -- Bring the whole file into memory and remove the comment lines.
DEMOFILE: read/lines http://re-bol.com/short_rebol_examples.r
foreach LINE DEMOFILE [
     if equal? ';' copy/part at trim/head LINE 1 1 [
         remove DEMOFILE
     ]
]
    
;; -- Build a block of titles and the associeated REBOL code.
foreach LINE DEMOFILE [
     either IS-HEADER? LINE [
         if not-equal? '' DEMOTITLE [
             append DEMOLIST DEMOTITLE
             append DEMOLIST DEMOCODE
         ]
         DEMOTITLE: copy ''
         DEMOTITLE: GET-TITLE LINE
         DEMOCODE: copy ''
         append DEMOCODE rejoin [LINE newline]
     ] [
         append DEMOCODE rejoin [LINE newline]
     ]
]
;; -- Save the last one if we hit end-of-file.
if not-equal? '' DEMOTITLE [
     append DEMOLIST DEMOTITLE
     append DEMOLIST DEMOCODE
]
    
;; -- Run the block of code for a selected title.
RUN-DEMO: func [
     CODE
] [
     write/lines DEMOID CODE ;; Save to disk
     CODE-WINDOW/text: CODE ;; Show the code
     show CODE-WINDOW
     launch DEMOID            ;; Use launch so we can close demo separately
]
    
MAIN-WINDOW: layout [
     across
     banner 'Select a demo to run it'
     return
     PROGRAM-LIST: text-list 400x700 data (extract DEMOLIST 2)
         [RUN-DEMO select DEMOLIST value]
     CODE-WINDOW: info 500x700
     return
     button 'Quit' [quit]
]
    
view center-face MAIN-WINDOW
    
    
    
    


posted by:   Steven White     23-Mar-2016/16:01:45-7:00



Still a beginner am I. If you want the indenting preserved in the code window, go to those two lines with the "trim" function and replace it with "trim copy" because "trim" does not make a copy, and the trimming was removing leading spaces.

posted by:   Steven White     23-Mar-2016/16:50:59-7:00