Jump to content

User:Ocee/wikilinks.js

From Wikipedia, the free encyclopedia
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
//***********************************************
// IceKarma's WikiLinks script for ChatZilla
// Version 1.2
//   1.2 by James Ross: fix the normal links by shunting the
//       word-hyphenator as well.
// Version 2.0
//   2.0 By Glen Mailer:
//        - Converted to new plugin API
//        - Ripped out a whole load of unused stuff
//        - Also Made to fit chatzilla coding pedantics
//   2.1 By Alphax:
//        - Added basic template linking functionality
//   2.2 By Alphax:
//        - subst: and pipes now handled correctly in templates
// This file is hereby placed by the author into the public domain.

plugin.id = "WikiLinks";

plugin.prefary = [
    ["class", "wiki-link", ""],
];

//
// Plugin management
//

plugin.init = 
function init(glob) {
    plugin.major = 2;
    plugin.minor = 2;
    plugin.version = plugin.major + "." + plugin.minor;
    plugin.description =
    "Munges wiki-links to be clickable in the output window";
    plugin.prefary = plugin.prefary.concat(plugin.prefary);
}

plugin.disable = 
function disable()
{
    client.munger.delRule("wiki-link");
    client.munger.delRule("wiki-template-link");
    client.commandManager.removeCommands(plugin.commands);

    display( plugin.id + " v" + plugin.version + " disabled.");
    
    return true;
}

plugin.enable = 
function enable()
{

    client.munger.addRule("wiki-link", /(\[\[.+?\]\])/, insertWikiLink);
    client.munger.addRule("wiki-template-link", /(\{\{.+?\}\})/, insertWikiTemplateLink);

    // ugly hack to cause the munger to evaluate wiki-link before these rules
    var tmp1 = client.munger.entries["link"];
    var tmp2 = client.munger.entries["channel-link"];
    var tmp3 = client.munger.entries["word-hyphenator"];
    delete client.munger.entries["link"];
    delete client.munger.entries["channel-link"];
    delete client.munger.entries["word-hyphenator"];
    client.munger.entries["link"] = tmp1;
    client.munger.entries["channel-link"] = tmp2;
    client.munger.entries["word-hyphenator"] = tmp3;

    var cmdary = [
        [ "wiki-links-class", cmdClass, CMD_CONSOLE, "[<className>]" ],
    ];

    plugin.commands = client.commandManager.defineCommands(cmdary);

    display( plugin.id + " v" + plugin.version + " enabled.");
    
    return true;

}


//
// Mungers
//

function insertWikiLink(matchText,containerTag) {
    var wikiLink = matchText;
    var linkTitle;

    wikiLink  = matchText.replace(/^\[\[/, "");
    wikiLink  = wikiLink.replace(/\]\]$/, "");
    linkTitle = wikiLink;
    if (linkTitle.match(/\|/)) {
        var ary = linkTitle.match(/^(.*?)\|(.*)$/);
        wikiLink = ary[1];
        linkTitle = ary[2];
    }
    wikiLink = escape(wikiLink.replace(/ /g, "_"));

    var anchor = document.createElementNS( "http://www.w3.org/1999/xhtml",
                                           "html:a");
    anchor.setAttribute("href", "http://en.wikipedia.org/wiki/" + wikiLink);
    anchor.setAttribute("class", "chatzilla-link "+plugin.prefs["class"]);

    insertHyphenatedWord(linkTitle, anchor);
    containerTag.appendChild(document.createTextNode("[["));
    containerTag.appendChild(anchor);
    containerTag.appendChild(document.createTextNode("]]"));
}

function insertWikiTemplateLink(matchText,containerTag) {
    var wikiLink = matchText;
    var linkTitle;

    wikiLink  = matchText.replace(/^\{\{/, "");
    wikiLink  = wikiLink.replace(/\}\}$/, "");
    linkTitle = wikiLink;
    if (linkTitle.match(/^subst:/))
    {
        var ary = linkTitle.match(/^(subst:)(.*)$/);
        wikiLink = ary[2];
    }
    if (linkTitle.match(/\|/))
    {
        if(linkTitle.match(/^subst:/))
        {
            var ary = linkTitle.match(/^(subst:)(.*?)\|(.*)$/);
            wikiLink = ary[2];
        }
        else
        {
            var ary = linkTitle.match(/^(.*?)\|(.*)$/);
            wikiLink = ary[1];
        }
    }
    wikiLink = escape(wikiLink.replace(/ /g, "_"));

    var anchor = document.createElementNS( "http://www.w3.org/1999/xhtml",
                                           "html:a");
    anchor.setAttribute("href", "http://en.wikipedia.org/wiki/Template:" + wikiLink);
    anchor.setAttribute("class", "chatzilla-link "+plugin.prefs["class"]);

    insertHyphenatedWord(linkTitle, anchor);
    containerTag.appendChild(document.createTextNode("{{"));
    containerTag.appendChild(anchor);
    containerTag.appendChild(document.createTextNode("}}"));
}

//
// Commands
//

function cmdClass(e) {
    if ( null != e.linkclass )
        plugin.prefs["class"] = e.linkclass;
    display( "Current value: " + plugin.prefs["class"] );
}