the BeanShell code

Here is a listing of the file testcode.bsh containing BeanShell code used in signatures and macro examples in this directory.

/***********

This file contains examples of BeanShell functions used in custom signature
definitions and macros. Some usages of them are in the *.xil files in this directory.

Xilize automatically finds and evaluates *.bsh files as it reads source files.  A
directory may contain more than one.

************/

/*
Signatures:

Before a custom signature's code is called the following variables are defined
and are available to functions with the signature's scope.  (Similar to the way
jEdit provides "view" and "buffer" to its macros.)

    sig, the Signature object
    task, the Task object
    block, the Block object
    text, the String returned by block.linesAsString()
*/

// These three helloWorld functions are the required trivial examples of
// functions used in custom signatures.

String helloWorldA() {

    // insertAttributes() will add any tag attributes generated by 
    // Xilize modifiers placed on the signature.  It's use is optional.
    
    // inlineMarkup() applies standard Xilize markup to the given string.
    return sig.insertAttributes("<p>")
        + task.markup("hello world " + text + " version A")
        +"</p>";

}

helloWorldB() {

    // note: helloWorldB() is not declared to return an explicit type
    // BeanShell will assumed type will be Object and Xilize will use
    // Object.toString() to add the text to the output file
    
    // the use of the keyword "return" is not necessary since this
    // statement evaluates to a String
    sig.insertAttributes("<p><strong>")
        + task.markup("hello world " + text + " version B")
        +"</strong></p>";

}

helloWorldC() {

    // markup() is a convenience method to inserts tag attributes and performs
    // standard Xilize inline markup. helloWorldC() is functionally equivalent
    // to helloWorldA() above but adds the toUpperCase() call.
    sig.markup(task, "<p>hello world " + text.toUpperCase() + " version C</p>");

}

// see also ../test.bsh for the exampleFile() function

/*

Macros:

While signatures are applied to blocks of text, macros are used within the block and are
marked with &{...}.

There are two forms of macro use: unrestricted and text operator.  Both marcos of 
either may access the "task" variable which is set to the current task object; in
addition, the text operator form  may access a "text" variable set to the string enclosed
in the markup.

*/


// a function for use as a text operator, note use of "text" variable

cap() {
    words = text.split(" ");
    result = "";
    for( s : words ) {
        result += s.substring(0,1).toUpperCase() + s.substring(1) + " ";
    }
    return result.substring(0,result.length() - 1);
}


// a regular function, uses the "task" variable if the debuging line is uncommented 

flag(string) {
    //task.report("    <debug> flag image used in "+task.getPath());
    string + "[!- flag.png!]";
}


// this becomes a global variable

String[] digitNames = "zero one two three four five six seven eight nine".split(" ");


// a utility function with explicit type declarations.

String toScores(int n) {

    if( n < 0 ) {
        throw new IllegalArgumentException("negative numbers not permitted");
    }

    int scores = n / 20;
    int rem = n % 20;
    if( scores > 20 )
        return "many, many";

    if( scores > 0 ) {
        if( rem > 0 )
                return digitNames[scores] + " score and "+ digitNames[rem];
        return digitNames[scores];
    }
    return digitNames[rem];
}


// a playful function used as an example, converts an integer to a string of the 
// form "four score and seven" (if n==87).

String oldFashioned(int n, boolean sentenceStart) {

    String result = toScores(n);
    if( sentenceStart ) {
        result = result.substring(0,1).toUpperCase() + result.substring(1);
    }
    return result;
}

source for Home/ByExample/BeanShell :: testcode.xil

the BeanShell code

Here is a listing of the file @testcode.bsh@ containing BeanShell code used in
signatures and macro examples in this directory.

bshListing. testcode.bsh

Long lines are wrapped in this listing.
Use your web browser's "View Page Source" feature to examine the HTML