MindTouch Developer Center > Deki > FAQ > Extensions > How do I...Write a parameterized extension in .Net?

How do I...Write a parameterized extension in .Net?

This walkthrough explains how to write an extension that takes parameters.  The following data types are supported: nil, bool, num, string, uri, list, map, and xml.

Download source code

Creating an Extension Library

1.  Open VS.NET and select File->New->Project. From the New Project Dialog Box, choose the Windows Class Library template project and name it ParameterizedExtService.

2.  To access the Deki Wiki extension framework, select Project->Reference and add a reference to the assemblies below.  These binaries can be found within the bin directory of your Deki Wiki web root (Deki Wiki VM:  /var/www/deki-hayes/bin).

  • log4net.dll
  • mindtouch.deki.ext.dll
  • mindtouch.dream.dll
  • SgmlReaderDll.dll

3.  Create a new class containing the code below.  You will add new parameterized extensions into this "samples" library. 

using System;
using System.Collections;
using MindTouch.Dream;
using MindTouch.Deki;

namespace ParameterizedExtService {

    [DreamService("Parameterized Extension Service",
                  "Copyright (c) 2007 MindTouch, Inc.",
                  SID = new string[] { "http://services.mindtouch.com/deki/draft/2007/06/parameterized" }
    )]
    [DekiExtLibrary(Namespace = "samples", Description = "Samples Library")]
    public class ParameterizedExtService : DekiExtService {
    }
}

Adding Parameterized Extensions

This section describes each data type and how to create a function that takes it as a parameter.  Note that these parameters are indicated by the DekiExtParam attribute. 

Bool Data Type:

Bool refers to a boolean value (i.e. true, false).  The following function takes a bool parameter as input and displays its value as a string:

[DekiExtFunction(Description = "Function with a bool parameter")]
public string BoolParam(
    [DekiExtParam("Sample bool parameter")] bool boolValue
) {
    return String.Format("BoolParam value is '{0}'", boolValue.ToString());
}

To invoke with a value of 'true': 

{{ samples.boolparam(true) }} 

Output:  BoolParam value is 'True'

Num Data Type:

Num refers to an integer or floating-point number (e.g. 1, -5, 0.6).  The following function takes a double parameter as input and rounds to the nearest integer:

[DekiExtFunction(Description = "Rounds a float to the nearest integer value")]
public int Round(
    [DekiExtParam("Number to round")] double floatValue
) {
    return (int)Math.Round(floatValue);
}

To invoke with a value of '2.7': 

{{ samples.round(2.7) }} 

Output:  3

String Data Type:

String refers to a sequence of characters (e.g. "Hello World").  The following function takes a string as input and displayes it in bold:

[DekiExtFunction(Description = "Displays a string in subscript")]
public XDoc Bold(
    [DekiExtParam("String to format")] string stringValue
) {
    return new XDoc("html").Start("body").Elem("b", stringValue).End();
}

To invoke with a value of 'Hello World!': 

{{ samples.bold("Hello World!") }} 

Output:  Hello World!

Uri Data Type:

Uri refers to a uniform resource identifier (e.g. "http://wiki.opengarden.org").  The following function takes a uri as input and creates a link to it:

[DekiExtFunction(Description = "Creates a link to a given uri")]
public XDoc Link(
    [DekiExtParam("Uri for link")] XUri uriValue
) {
    return new XDoc("html").Start("body").Start("a").Attr("href", uriValue).Value(uriValue).End().End();
}

To invoke with a value of 'http://wiki.opengarden.org': 

{{ samples.link("http://wiki.opengarden.org") }} 

Output:  http://wiki.opengarden.org

List Data Type:

List refers to an indexed array (e.g. [ 1, 2, "a" ]).  The following function takes a list as input and creates an html list:

[DekiExtFunction(Description = "Creates a bulleted list of items")]
public XDoc List(
    [DekiExtParam("Items for list")] ArrayList inputs
) {
    XDoc result = new XDoc("html").Start("body").Start("ul");
    foreach (string input in inputs) {
        result.Elem("li", input);
    }
    return result.End();
}

To invoke with a list of three items: 

{{ samples.list(["item1", "item2", "item3"]) }} 

Output:

  • item1
  • item2
  • item3
Map Data Type:

Map refers to an an associative array (e.g. { a : 1, b : 2 }).  The following function takes a map as input and creates an html list of name/value pairs:

[DekiExtFunction(Description = "Creates a list of names and values")]
public XDoc Map(
    [DekiExtParam("Items for list")] Hashtable inputs
) {
    XDoc result = new XDoc("html").Start("body").Start("ul");
    foreach (String key in inputs.Keys) {
        result.Start("li").Elem("b", key).Value(":  " + inputs[key]).End();
    }
    return result.End();
}

To invoke with a map of three items: 

{{ samples.map({label1: "value1", label2: "value2", label3: "value3"}) }} 

Output:

  • label3: value3
  • label2: value2
  • label1: value1
XDoc Data Type:

XDoc refers to xml data.  The following function takes an XDoc as input and makes the contents of its body element bold:

[DekiExtFunction(Description = "Bolds an html block")]
public XDoc BoldDoc(
    [DekiExtParam("XDoc to format")] XDoc doc
) {
    return new XDoc("html").Start("body").Start("b").AddNodes(doc["body"]).End().End();
}

To invoke with the output from the previous List parameter sample: 

{{ samples.bolddoc(samples.list(["item1", "item2", "item3"])) }} 

Output:

  • item1
  • item2
  • item3
Optional Parameters:

The DekiExtParam attribute can be used to specify optional parameters.  Optional parameters accept the empty value ( _, nil, or null).  The following function modifies the previous Num sample to  make the input parameter optional:

[DekiExtFunction(Description = "Rounds a float to the nearest integer value")]
public int Round2(
    [DekiExtParam("Number to round (default: 0)", true)] double? floatValue
) {
    if (floatValue.HasValue) {
        return (int)Math.Round(floatValue.Value);
    } else {
        return 0;
    }
}

To invoke with the default value: 

{{ samples.round2() }} 

Output:  0

Testing the Extension

1.  Compile and copy the ParameterizedExtService.dll assembly into the bin/services directory of your Deki Wiki web root (Deki Wiki VM:  /var/www/deki-hayes/bin/services).  Deki Wiki will automatically load new extension libraries from this location.

2.  Restart Deki Wiki (Deki Wiki VM:  /etc/init.d/dekihost restart).

3.  Register the service by opening your wiki and selecting Tools->Control Panel->Service Management (administrator access is required).  Add a new Local service with SID=http://services.mindtouch.com/deki/d.../parameterized

4.  You can now invoke the new extensions from your wiki pages.


Tag page

Files 1

FileSizeDateAttached by 
ParameterizedExtService.zip
No description
6.58 kB22:21, 20 Sep 2007BrigetteKActions
You must login to post a comment.
Powered by MindTouch Deki v.8.08.2