native-thumb2There are two types of functions in Rebol:

  • native
  • mezzanine

A native function is a compiled function which source code is not accessible whereas a mezzanine function is written in Rebol itself. For example the Parse function is native whereas the build-markup function is mezzanine. So try this in Rebol Console:


source build-markup

It will return in Console:


build-markup: func [
    {Return markup text replacing <%tags%> with their evaluated results.}
    content [string! file! url!]
    /quiet "Do not show errors in the output."
    /local out eval value
][
    content: either string? content [copy content] [read content]
    out: make string! 126
    eval: func [val /local tmp] [
        either error? set/any 'tmp try [do val] [
            if not quiet [
                tmp: disarm :tmp
                append out reform ["***ERROR" tmp/id "in:" val]
            ]
        ] [
            if not unset? get/any 'tmp [append out :tmp]
        ]
    ]
    parse/all content [
        any [
            end break
            | "<%" [copy value to "%>" 2 skip | copy value to end] (eval value)
            | copy value [to "<%" | to end] (append out value)
        ]
    ]
    out
]

If you want to get the source code into your notepad, you can use this instruction:


write clipboard:// mold :build-markup

You could for example inspire from the build-markup function to create this extract-template-vars function:


extract-template-vars: func [
    {Return list of variables in build-markup template}
    content [string! file! url!]
    /quiet "Do not show errors in the output."
    /local out eval value
][
    content: either string? content [copy content] [read content]
    out: copy []

    parse/all content [
        any [
            end break
            | "<%" [copy value to "%>" 2 skip | to end] (if none? find out value [append out value])
            | [to "<%" | to end]
        ]
    ]
    out
]

You can use it to extract all the templates vars from the C# winform templates for example (copy and paste them to the clipboard):


>> extract-template-vars read clipboard://
== ["Namespace" "Form-Name"]
>>

You could then create an utility function which will dynamically generate the ask questions for the user to fill the vars:


ask-template-vars: func[template][
  foreach var-name extract-template-vars template [
    set to-word var-name ask rejoin [var-name ": "]
  ]
]

To test it copy and paste the same C# winform templates into clipboard. Then type the code below in Console:


ask-template-vars read clipboard://

It should ask you for Namespace and Form-Name:
build-markup-ask-vars

You can then use the variables for build-markup:


write clipboard:// build-markup read clipboard://

you should then get this if you paste the clipboard into Notepad:


template: {
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace test
{
    public partial class test : Form
    {
        public test()
        {
            InitializeComponent();
        }
    }
}
}

template2: {
namespace test
{
    partial class test
    {
        ///
        /// Required designer variable.
        ///
        private System.ComponentModel.IContainer components = null;

        ///
        /// Clean up any resources being used.
        ///
        /// true if managed resources should be disposed; otherwise, false.
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        ///
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        ///
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Text = "test";
        }
        #endregion
    }
}
}

to-insert-template: {
      Form

      test.cs
    }

Bookmark and Share