Template:TemplateSourceViewer

    Table of contents
    No headers
    <style type="text/css"> ".toggleme {display:none;}" </style>;
    
    //
    // Initialization
    //
    
    // If template name is passed as arg, automatically adopt dedicated 'embedded' mode
    var targ = $0 ?? $template;
    if (targ is str) let targ = [ targ ];
    if (targ is not list) let targ = nil;
    var embedded = (targ is not nil);
    if (!embedded && __request.args.templatename is not nil)
        let targ = [ __request.args.templatename ];
    
    // Other args
    var hide = embedded && ($1 ?? $hide ?? false);
    var install = embedded || (__request.args.install && #targ);
    var tlist = [ wiki.getpage('template:'..t) foreach var t in targ ];
    
    // Other stuff
    if (!embedded && #targ && tlist[0].title is nil) {
        <span style="color:red"> "ERROR: template '"..targ[0].."' not found" </span>;
        let tlist = [];
    }
    var tpage = tlist[0];
    
    // Source viewer mode
    if (!install) {
        if (tpage is nil) <p>
            "This page allows you to view quickly and easily the DekiScript blocks of a template with syntax highlighting, ";
            "which is very useful for debugging. ";
            "You can also access the template HTML source for easy installation; just click the \"(view template installation instructions)\" link.";
        </p>;
        var template_list = [ string.substr(p.path, 9)
            foreach var p in wiki.getsearch("namespace:template", 999) where (string.startswith(p.path..'','Template:') && #p.name) ];
        <p>
            "Select template: ";
            <select ctor="when($this.change) window.location.href={{page.uri}}+'&templatename='+escape($this.attr('value'));">    
                if (tpage is nil || !list.contains(template_list, tpage))
                    <option selected="selected" value=""> "(none selected)" </option>;
                foreach (var t in list.sort(template_list))
                    <option selected=(t==tpage.name ? "selected" : nil)> t </option>;
            </select>;
        </p>;
        if (tpage is not nil) {
            <p><a href=(page.uri & { templatename: tpage.name, install:true })> "(view template installation instructions)" </a></p>;
            if (xml.text(tpage.xml, ".//pre[@class='script']")) {
                <h2> "DekiScript source for "; web.link(tpage.uri & { action:"source" }, tpage.title); </h2>;
                <p> "Each block of style 'DekiScript' is shown (there may be additional content in the template source)" </p>;
                foreach (var x in xml.select(tpage.xml, ".//pre[@class='script']"))
                    syntax.dekiscript(xml.text(x));
            }
            else <p style="font-weight:bold">
                web.link(tpage.uri & { action:"source" }, tpage.title);; " has no DekiScript blocks";
            </p>;
        }
    }
    
    if (install && !embedded) {
        <p><a href=(page.uri & { templatename: tpage.name })> "(switch to DekiScript source view)" </a></p>;
        <p style="font-size:1.1em; font-weight:bold"> "Installation Instructions" </p>;
    }
    
    // Output installation instructions
    if (install) {
    
    if (!hide) {
        // Initialization for "visible" install mode
        var multiple = (#tlist > 1);
        var namelist;
        foreach (var t in tlist)
            let namelist ..= (__index ? (__index==#tlist-1 ? " and " : ", ") : "") .. "'" .. t.name .. "'";
        
        <p>
            "These instructions will help you install the template" .. (multiple ? "s" : "") .. namelist;
            " on your wiki quickly and correctly. ";
            if (!embedded) {
                "There might be additional installation requirements; be sure to see the template documentation for details. ";
                <em>"Note that this procedure is designed for single-page templates only." </em>;
            }
        </p>;
        <p> "Perform the following steps" .. (multiple ? <em> " for each template" </em> : "") .. ":" </p>;
        <ol>
        <li> <strong> 'Copy the template source code:' </strong>; <ol style="list-style-type:upper-alpha">
            <li> 'If the code in the "HTML Source" area below is not already visible and selected, click the "View" button to display and select it.' </li>;
            <li> 'Copy it to your clipboard (CTRL-C).' </li>;
        </ol></li>;
        <li> <strong> 'Create a new template on your wiki:' </strong>; <ol style="list-style-type:upper-alpha">
            <li> 'Select the Tools->Templates menu item on your wiki' </li>;
            <li> 'Click "New Page" button.  This will open the editor on a new page with the title "Template:Page Title".' </li>;
            <li> 'Replace "Page Title" with the name of the template.' </li>;
        </ol></li>;
        <li> <strong> 'Paste in the source code:' </strong>; <ol style="list-style-type:upper-alpha">
            <li> 'In the editor, click the "View->Source" menu item to view the HTML source of the page.' </li>;
            <li> 'Select all text (CTRL-A).' </li>;
            <li> 'Paste in the template source code from the clipboard (CTRL-V).' </li>;
        </ol></li>;
        <li> <strong> 'Click "Save" button.' </strong>; </li>;
        </ol>;
    } // if (!hide)
    
    foreach (var t in tlist) {
        var myId = @id .. "_" .. t.id;
        var ct = "when($this.click) { $(\'#" ..myId .."\').toggle(); if($(\'#" ..myId .."\').is(':visible')){$(\'#" ..myId .."\').select(); $this.val('Hide');}else{$this.val('View');}}";
        <p style="font-size:1.1em; font-weight:bold">
            <input type="button" value="View" ctor=(ct)/>;
            "HTML Source for "; web.link(t.uri, t.title);
        </p>;
        var source = list.splice(string.split(string.substr(xml.format(xml.select(t.xml,".//body")),0,-7),"\n"), 0,1);
        <textarea class="toggleme" id=(myId) style="max-width:100%; overflow-x:auto" wrap="off"
            rows=(#source) cols=(num.int(1.5*list.max([ #s foreach var s in source]))) readonly="readonly">
            foreach (var s in source)
                (__index ? "\n" : "") .. s;
        </textarea>;
    } // foreach
    
    } // if (install)
    
    Tag page
    You must login to post a comment.

    Copyright © 2011 MindTouch, Inc. Powered by