Here's a useful little script for those of you who are
diligently working on REBOL this Memorial Day weekend.

-Carl

REBOL [
    Title: "REBOL Script Cleaner"
    Author: "Carl Sassenrath"
    File:  %clean-script.r
    Date:  27-May-2000
    Email: [EMAIL PROTECTED]
    Purpose: {
        Cleans REBOL scripts by parsing the REBOL code
        and supplying standard indentation and spacing.
    }
    Note: {
        This script produces STANDARD script indentation and
        spacing.  No doubt you will want to modify it to use
        your own rules.  Send your enhancements and I will
        consider adding them to the distribution... but keep
        this header intact and keep the code clean.  No hacks.
    }
    Category: [script util text 3]
    History: [
        "Carl Sassenrath" 1.0.0 27-May-2000 "Original program."
    ]
]

script-cleaner: make object! [

    out: none   ; output text
    spaced: off ; add extra bracket spacing
    indent: ""  ; holds indentation tabs

    emit-line: func [] [append out newline]

    emit-space: func [pos] [
        append out either newline = last out [indent][
            pick [#" " ""] found? any [
                spaced
                not any [find "[(" last out find ")]" first pos]
            ]
        ]
    ]

    emit: func [p1 p2] [emit-space p1  append out copy/part p1 p2]

    set 'clean-script func [
        "Returns new script text with standard spacing."
        script "Original Script text"
        /spacey "Optional spaces near brackets and parens"
        /local str new
    ][
        spaced: found? spacey
        out: append clear copy script newline
        parse script blk-rule: [
            some [
                str:
                newline (emit-line) |
                #";" thru newline new: (emit str new) |
                [#"[" | #"("] (emit str 1 append indent tab) blk-rule |
                [#"]" | #")"] (remove indent emit str 1) |
                skip (set [value new] load/next str  emit str new) :new
            ]
        ]
        remove out  ; remove first char
    ]
]

;Example:  print clean-script read %clean-script.r


Reply via email to