Template:TemplateDoc

    Documentation

    This box includes basic usage information for this template.  When calling the template, this documentation will not appear.  Functional template code should be placed outside the dotted box.

    Summary

    Template Description

    This template generates the core part of a documentation page for a template, much like ScriptDoc() does for extensions and scripts.  For this to work, the template page must have a self-documentation block in it, much like this template.  The self-documentation block may be found in the TemplateStarter template; use that as a skeleton when creating your own new template.

    Current version of this template only supports documentation of a single stand-alone template.  In the future we hope to extend this to multi-template packages, packages that include templates + other server code, etc.  One thing at a time.

    Requirements MindTouch 9.08 or later
    Documentation URL http://developer.mindtouch.com/User:neilw/Template_Self-Documentation_Project/TemplateDoc%3a_automatically_generate_basic_template_documentation
    Discussion URL http://forums.developer.mindtouch.co...ad.php?p=38702

    Version History

    Place newest version at the top of the table.

    Version Date Author Description
    0.3 12-May-2011 neilw Tweaked version table operation
    0.2b 10-March-2010 neilw

    Usable beta

    • re-ordered summary table
    • added Discussion URL field
    0.1b 10-March-2010 neilw first test version

    Template Parameters

    Name Type Default Description
    template string   Name of template to document.  This should just be the name, not including the "Template:" prefix.  So, for example, this template is documented by calling {{ TemplateDoc("TemplateDoc") }}.
    install bool? true Include template installation instructions
    source bool? true (NOT YET IMPLEMENTED) Include syntax-highlighted source code display

    Template Code

    // Fetch template name and make sure the page exists
    var name = $0 ?? $template;
    var template_page;
    var x_self_doc;
    if (!#name)
        <div style="color:red"> "TEMPLATEDOC ERROR: Must specify valid template name" </div>;
    else {
        let template_page = wiki.getpage("Template:" .. name);
        if (template_page.id is nil) {
            <div style="color:red"> "TEMPLATEDOC ERROR: template '" .. name .. "' not found" </div>;
            let template_page = nil;
        }
        else {
            let x_self_doc = template_page.xml["//div[@id='templateSelfDocumentation']"];
            if (!#x_self_doc)
                <div style="color:red"> "TEMPLATEDOC ERROR: couldn't find self-documentation in template '" .. name .."'" </div>;
        }
    }
    
    // Fetch other params
    var show_install = $1 ?? $install ?? true;
    var show_source  = $2 ?? $source  ?? true;
    
    // Only continue if we found the documentation
    if (#x_self_doc) {
        var x_summary = x_self_doc[".//table[@class='summary']"] ?? <p/>;
    // Validate documentation URI
        var doc_url = string.trim(xml.text(x_summary[".//tr[@class='docPage']/td[2]"] ?? <p/>));
        if (doc_url != page.uri)
            <div style="color:red">
                "TEMPLATEDOC WARNING: documentation URL in template '" .. name .. "' does not point to this page (should be '" .. 
                page.uri .. "')";
            </div>;
    // Output discussion note if applicable
        var discussion_url = string.trim(xml.text(x_summary[".//tr[@class='discussionPage']/td[2]"] ?? <p/>));
    
        if (#discussion_url) {
            if (string.startswith(discussion_url, "http://"))
                <p> "Discuss this template "; <a href=(discussion_url)> "here" </a>; </p>;
            else
                <div style="color:red"> "TEMPLATEDOC ERROR: invalid Discussion URL (must start with 'http://')" </div>;
        }
    // Extract other stuff
        var x_description  = x_summary[".//tr[@class='description']/td[2]"];
        var x_requirements = x_summary[".//tr[@class='requirements']/td[2]"];
        var x_versions   = x_self_doc[".//table[@class='versions']"];
        var x_parameters = x_self_doc[".//table[@class='parameters']"];
    // Insert critical metadata onto the page in an easily-findable way.  This will enable remote auto-version
    // checking and maybe even auto-installation!
        <div id="templateMetadata" style="display:none">
            if (#x_versions) {
                var current_version = xml.text(x_versions[".//tbody/tr[1]/td[1]"] ?? <p/>);
                if (#current_version)
                    <span class="currentVersion"> current_version </span>;
            }
            <span class="sourceCodeURI"> (template_page.api & "contents" & { format:"xhtml", mode:"edit" }) </span>;
        </div>;
    
    // Output template description
        <h2> "Description" </h2>;
        if (#x_description) xml.html(x_description);
        else "(description not found)";
    
    // Output template requirements
        <h2> "Requirements" </h2>;
        if (#x_requirements && #xml.text(x_requirements)) xml.html(x_requirements);
        else "(none)";
    
    // Output version history
        <h2> "Version" </h2>;
        var num_versions = #x_versions ? #xml.list(x_versions, ".//tbody/tr") : 0;
        if (num_versions) {
            if (num_versions > 1)
                collapseItem(@id, "Show full version history", "Show latest version only");
            <table border="1" width="100%" style="border-style:solid; border-width:1px">
                foreach (var c in x_versions["./col"]) c;
                <tbody>
                    x_versions[".//thead/tr[1]"];
                    x_versions[".//tbody/tr[1]"];
                </tbody>;
                <tbody id=(@id) style="display:none;">
                    foreach (var r in x_versions[".//tbody/tr"])
                        if (__index) r;
                </tbody>;
            </table>;
        }
        else "(no version info found)";
    
    // Installation instructions
        if (show_install) {
            <h2> "How to Install" </h2>;
            templateSourceViewer(name);
        }
    
    // Output parameter summary
        <h2> "Parameters" </h2>;
        if (#x_parameters) x_parameters;
        else "(no parameter info found)";
    }
    
    Tag page
    You must login to post a comment.

    Copyright © 2011 MindTouch, Inc. Powered by