Program that transforms specially formatted template files into Rtfl functions
Find a file
2023-07-10 03:52:32 -04:00
examples initial commit 2023-07-10 02:32:04 -04:00
.gitignore add build script 2023-07-10 03:52:32 -04:00
build.sh add build script 2023-07-10 03:52:32 -04:00
BUILD_CONFIG add build script 2023-07-10 03:52:32 -04:00
LICENSE Initial commit 2023-07-10 06:05:03 +00:00
main.rtfl initial commit 2023-07-10 02:32:04 -04:00
project.json initial commit 2023-07-10 02:32:04 -04:00
README.md add build script 2023-07-10 03:52:32 -04:00

RtflTemp

Program that transforms specially formatted template files into Rtfl functions

Template Format

Templates start with metadata, each attribute starting with ##.

The following attributes are available:

  • ##name

    The name of the template.

    If not specified, the resulting template function's name will be derived from the template's filename.

  • ##arg

    A named argument that the resulting template function will have.

  • ##begin

    Used to tell the program that everything after this point is the template's content.

Template content may consist of any desired text format (HTML, markdown, etc.), but may also use Rtfl code within them.

Rtfl code blocks begin with <?rtfl and end with ?>. Anything inside an Rtfl code block will be inserted into the resulting template function verbatim, and therefore can be used for things like control flow, creating local variables, etc.

To insert a value into a template with Rtfl, you can use the <?rtfl= variant of the Rtfl code block opener. The content of the block will be treated as a value to insert into the template.

Template Example

##name list_viewer
##arg title
##arg description
##arg items
##begin
<h1><?rtfl= title ?></h1>
<pre><?rtfl= description ?></pre>
<ul>
    <?rtfl
    local i = 0
    local len = items.array_length
    while [i < len] {
    ?>
        <li><?rtfl= items[i] ?></li>
    <?rtfl
    inc("i")
    }
    ?>
<ul>

Resulting Template Functions

Template functions are prefixed with template_, followed by the template name. They are standard Rtfl functions that take in arguments specified by the template, and return the result of the template as a string.

Here is a formatted version of the function output for the template in Template Example:

func template_list_viewer(title, description, items) {
    local __tmp = ""
    __tmp = concat(__tmp, "<h1>")
    __tmp = concat(__tmp,  title)
    __tmp = concat(__tmp, "</h1>\n<pre>")
    __tmp = concat(__tmp,  description)
    __tmp = concat(__tmp, "</pre>\n<ul>\n    ")

    local i = 0
    local len = items.array_length
    while [i < len] {
        __tmp = concat(__tmp, "\n        <li>")
        __tmp = concat(__tmp,  items[i])
        __tmp = concat(__tmp, "</li>\n    ")

        inc("i")
    }
        
    __tmp = concat(__tmp, "\n<ul>\n")
    return __tmp
}

Additional Examples

Additional examples can be found in the examples directory.

Usage

rtfltemp <input template> <output Rtfl file>

Building

Check BUILD_CONFIG, tweaking as necessary, then run build.sh.

The compiled version will be available at rtfltemp.rtfc.

Note that building is not necessary if you are in the root of this repository; you can instead run main.rtfl directly without any build step.