MediaWiki:AddTag/code.js

/*jshint jquery:true, browser:true, es5:true, devel:true, camelcase:true, curly:false, undef:true, unused:true, bitwise:true, eqeqeq:true, forin:true, immed:true, latedef:true, newcap:true, noarg:true, regexp:false, strict:true, trailing:true, maxcomplexity:10 */ /*global mediaWiki:true*/ (function (module, mw, $) {   'use strict';    if (module.loadSource) return;    var translations = {        en: {            viewSource: 'View source',            viewArticle: 'View article'        },        de: {            viewSource: 'Quelltext Ansicht',            viewArticle: 'Artikel Ansicht'        },        hu: {            viewSource: 'Forráskód megtekintése',            viewArticle: 'Szócikk megtekintése'        },        pl: {            viewSource: 'Tekst źródłowy',            viewArticle: 'Pokaż artykuł'        },        es: {            viewSource: 'Ver código fuente',            viewArticle: 'Ver artículo'        },        ca: {            viewSource: 'Veure codi font',            viewArticle: 'Veure l\'article'        },        'pt-br': {            viewSource: 'Ver código',            viewArticle: 'Ver artigo'        }    },    parserFunctions = {        '#expr': 'Help:Extension:ParserFunctions#.23expr', '#if': 'Help:Extension:ParserFunctions#.23if', '#ifeq': 'Help:Extension:ParserFunctions#.23ifeq', '#iferror': 'Help:Extension:ParserFunctions#.23iferror', '#ifexpr': 'Help:Extension:ParserFunctions#.23ifexpr', '#ifexist': 'Help:Extension:ParserFunctions#.23ifexist', '#rel2abs': 'Help:Extension:ParserFunctions#.23rel2abs', '#switch': 'Help:Extension:ParserFunctions#.23switch', '#time': 'Help:Extension:ParserFunctions#.23time', '#timel': 'Help:Extension:ParserFunctions#.23timel', '#titleparts': 'Help:Extension:ParserFunctions#.23titleparts', 'subst': 'Help:Substitution', 'safesubst': 'Help:Substitution', '#len': 'Extension:StringFunctions#.23len:', '#pos': 'Extension:StringFunctions#.23pos:', '#rpos': 'Extension:StringFunctions#.23rpos:', '#sub': 'Extension:StringFunctions#.23sub:', '#pad': 'Extension:StringFunctions#.23pad:', '#replace': 'Extension:StringFunctions#.23replace:', '#explode': 'Extension:StringFunctions#.23explode:', '#urlencode': 'Extension:StringFunctions#.23urlencode:_and_.23urldecode:', '#urldecode': 'Extension:StringFunctions#.23urlencode:_and_.23urldecode:', // ->http://www.mediawiki.org/wiki/Help:Magic_words#Miscellaneous '#tag': 'Help:Magic_words#Miscellaneous' },   parserTags = { 'activityfeed': 'http://community.wikia.com/wiki/Help:Wiki_Activity', 'aoaudio': 'http://community.wikia.com/wiki/Help:Video', 'aovideo': 'http://community.wikia.com/wiki/Help:Video', 'bloglist': 'http://community.wikia.com/wiki/Help:Blog_article/Bloglist', 'categorytree': 'http://www.mediawiki.org/wiki/Categorytree', 'charinsert': 'http://www.mediawiki.org/wiki/Extension:CharInsert', 'choose': 'http://www.mediawiki.org/wiki/Extension:RandomSelection', 'createbox': 'http://www.mediawiki.org/wiki/Extension:CreateBox', 'gallery': 'http://www.mediawiki.org/wiki/Gallery#Rendering_a_gallery_of_images', 'ggtube': 'http://community.wikia.com/wiki/Help:Video', 'googlespreadsheet': 'http://www.mediawiki.org/wiki/Extension:GoogleDocs4MW', 'gtrailer': 'http://community.wikia.com/wiki/Help:Video', 'gvideo': 'http://community.wikia.com/wiki/Help:Video', 'hiero': 'http://www.mediawiki.org/wiki/Hiero', 'imagemap': 'http://www.mediawiki.org/wiki/Imagemap', 'includeonly': 'http://www.mediawiki.org/wiki/Templates', 'inputbox': 'http://www.mediawiki.org/wiki/Inputbox', 'mainpage-endcolumn': 'http://community.wikia.com/wiki/Help:Main_page_column_tags', 'mainpage-leftcolumn-start': 'http://community.wikia.com/wiki/Help:Main_page_column_tags', 'mainpage-rightcolumn-start': 'http://community.wikia.com/wiki/Help:Main_page_column_tags', 'math': 'http://www.mediawiki.org/wiki/Math', 'nicovideo': 'http://community.wikia.com/wiki/Help:Video', 'noinclude': 'http://www.mediawiki.org/wiki/Templates', 'nowiki': 'http://meta.wikimedia.org/wiki/Help:Wikitext_examples#Just_show_what_I_typed', 'onlyinclude': 'http://www.mediawiki.org/wiki/Templates', 'poem': 'http://www.mediawiki.org/wiki/Extension:Poem', 'poll': 'http://www.mediawiki.org/wiki/Extension:AJAXPoll', 'pre': 'http://meta.wikimedia.org/wiki/Help:Wikitext_examples#Just_show_what_I_typed', 'randomimage': 'http://www.mediawiki.org/wiki/Extension:RandomImage', 'ref': 'http://www.mediawiki.org/wiki/Ref', 'references': 'http://www.mediawiki.org/wiki/Ref', 'rss': 'http://community.wikia.com/wiki/Help:Feeds', 'section': 'http://www.mediawiki.org/wiki/Extension:Labeled_Section_Transclusion', 'source': 'http://www.mediawiki.org/wiki/Extension:SyntaxHighlight_GeSHi', 'syntaxhighlight': 'http://www.mediawiki.org/wiki/Extension:SyntaxHighlight_GeSHi', 'tabber': 'http://community.wikia.com/wiki/Help:Tabber', 'tabview': 'community.wikia.com/wiki/Help:Tab_view', 'tangler': 'http://community.wikia.com/wiki/Help:Video', 'timeline': 'http://www.mediawiki.org/wiki/Extension:Timeline', 'verbatim': 'http://community.wikia.com/wiki/Help:Verbatim_tags', 'videogallery': 'http://community.wikia.com/wiki/Help:Video', 'wegame': 'http://community.wikia.com/wiki/Help:Video', 'youtube': 'http://community.wikia.com/wiki/Help:Video' },   i18n = translations[ mw.config.get('wgContentLanguage') ] || translations.en, $content, $list, $source, $a, $toc, headers = []; function addButton { $a = $('' +           '' +                i18n.viewSource +            '' +        '') .appendTo($list) .find('a') .data('source', false) .click(function {            $a.closest('.wikia-menu-button')            .removeClass('active');            module[$a.data('source') ? 'hideSource' : 'loadSource'];        }); }   function addCSS  { $(document.head || 'head') .append('' +       '#source-code, #source-toc {' +            'font-family: "Courier New",Courier,monospace;' +            'background: white;' +            'color: black;' +            'padding: 10px;' +        '}' +        '#source-code {' +            'font-size: 13px;' +            'white-space: pre-wrap;' +        '}' +        '#source-toc {' +            'font-size: 12px;' +            'overflow-x: auto;' +            'white-space: nowrap;' +        '}' +        '#source-code a, #source-code a:link, #source-code a:visited, ' +        '#source-toc a, #source-toc a:link, #source-toc a:visited {' +            'color: black;' +            'text-decoration: none;' +        '}' +        '#source-code a, #source-code a:link, #source-code a:visited {' +            'border-bottom: 1px dotted #bbb;' +        '}' +        '#source-code a:hover, #source-toc a:hover {' + 'color: darkblue;' + 'border-bottom: 1px solid darkblue;' + 'text-decoration: none;' + '}' +       ' ');    }    function joinHrefParts (parts) {        for (var i = 0; i < parts.length; i++) {            parts[i] = encodeURIComponent(parts[i]);        }        return parts.join(':').replace(/ /g, '_');    }    function createHref (link) {        var parts, hash = '';        if (link.indexOf('#') !== -1) {            parts = link.split(/\#/);            link = parts.shift;            if (!link.length) link = mw.config.get('wgPageName');            hash = '#' + parts.pop;        }        if (link[0] === '/') link = mw.config.get('wgPageName') + link;        var interwikiMap = {w:'http://community.wikia.com/wiki/$1',community:'http://community.wikia.com/wiki/$1',bugzilla:'https://bugzilla.wikimedia.org/show_bug.cgi?id=$1',commons:'http://commons.wikimedia.org/wiki/$1',creativecommons:'http://creativecommons.org/licenses/$1',creativecommonswiki:'http://wiki.creativecommons.org/$1',dictionary:'http://www.dict.org/bin/Dict?Database=*&Form=Dict1&Strategy=*&Query=$1',dict:'http://www.dict.org/bin/Dict?Database=*&Form=Dict1&Strategy=*&Query=$1',docbook:'http://wiki.docbook.org/topic/$1',download:'http://download.wikimedia.org/$1',dbdump:'http://download.wikimedia.org/$1/latest/',dreamhost:'http://wiki.dreamhost.com/index.php/$1',finalfantasy:'http://finalfantasy.wikia.com/wiki/$1',flickruser:'http://www.flickr.com/people/$1',flickrphoto:'http://www.flickr.com/photo.gne?id=$1',foundation:'http://wikimediafoundation.org/wiki/$1',gerrit:'https://gerrit.wikimedia.org/r/$1',git:'https://gerrit.wikimedia.org/r/gitweb?p=mediawiki/$1;a=log;h=refs/heads/master',google:'http://www.google.com/search?q=$1',googledefine:'http://www.google.com/search?q=define:$1',googlegroups:'http://groups.google.com/groups?q=$1',guildwiki:'http://guildwars.wikia.com/wiki/$1',gutenberg:'http://www.gutenberg.org/etext/$1',gutenbergwiki:'http://www.gutenberg.org/wiki/$1',h2wiki:'http://halowiki.net/p/$1',imdbname:'http://www.imdb.com/name/nm$1/',imdbtitle:'http://www.imdb.com/title/tt$1/',imdbcompany:'http://www.imdb.com/company/co$1/',imdbcharacter:'http://www.imdb.com/character/ch$1/',incubator:'http://incubator.wikimedia.org/wiki/$1',infosecpedia:'http://infosecpedia.org/wiki/$1',irc:'irc://irc.freenode.net/$1',ircrc:'irc://irc.wikimedia.org/$1',rcirc:'irc://irc.wikimedia.org/$1','iso639-3':'http://www.sil.org/iso639-3/documentation.asp?id=$1',issn:'http://www.worldcat.org/issn/$1',javanet:'http://wiki.java.net/bin/view/Main/$1',javapedia:'http://wiki.java.net/bin/view/Javapedia/$1',lostpedia:'http://lostpedia.wikia.com/wiki/$1',mail:'https://lists.wikimedia.org/mailman/listinfo/$1',mailarchive:'http://lists.wikimedia.org/pipermail/$1',mariowiki:'http://www.mariowiki.com/$1',marveldatabase:'http://www.marveldatabase.com/wiki/index.php/$1',mediawikiwiki:'http://www.mediawiki.org/wiki/$1',mediazilla:'https://bugzilla.wikimedia.org/$1',memoryalpha:'http://memory-alpha.org/wiki/$1',metawiki:'http://sunir.org/apps/meta.pl?$1',metawikipedia:'http://meta.wikimedia.org/wiki/$1',mozcom:'http://mozilla.wikia.com/wiki/$1',mozillawiki:'https://wiki.mozilla.org/$1',mozillazinekb:'http://kb.mozillazine.org/$1',musicbrainz:'http://musicbrainz.org/doc/$1',mw:'http://www.mediawiki.org/wiki/$1',mwod:'http://www.merriam-webster.com/cgi-bin/dictionary?book=Dictionary&va=$1',mwot:'http://www.merriam-webster.com/cgi-bin/thesaurus?book=Thesaurus&va=$1',nost:'http://nostalgia.wikipedia.org/wiki/$1',nostalgia:'http://nostalgia.wikipedia.org/wiki/$1',openfacts:'http://openfacts.berlios.de/index-en.phtml?title=$1',openlibrary:'http://openlibrary.org/$1',openstreetmap:'http://wiki.openstreetmap.org/wiki/$1',openwetware:'http://openwetware.org/wiki/$1',openwiki:'http://openwiki.com/?$1',osmwiki:'http://wiki.openstreetmap.org/wiki/$1',otrs:'https://ticket.wikimedia.org/otrs/index.pl?Action=AgentTicketZoom&TicketID=$1',otrswiki:'http://otrs-wiki.wikimedia.org/wiki/$1',perlnet:'http://perl.net.au/wiki/$1',phpwiki:'http://phpwiki.sourceforge.net/phpwiki/index.php?$1',pyrev:'http://www.mediawiki.org/wiki/Special:Code/pywikipedia/$1',pythoninfo:'http://www.python.org/cgi-bin/moinmoin/$1',pythonwiki:'http://www.pythonwiki.de/$1',pywiki:'http://c2.com/cgi/wiki?$1',rev:'http://www.mediawiki.org/wiki/Special:Code/MediaWiki/$1',revo:'http://purl.org/NET/voko/revo/art/$1.html',rfc:'http://tools.ietf.org/html/rfc$1',robowiki:'http://robowiki.net/?$1',reuterswiki:'http://glossary.reuters.com/index.php/$1',slashdot:'http://slashdot.org/article.pl?sid=$1',sourceforge:'http://sourceforge.net/$1',species:'http://species.wikimedia.org/wiki/$1',strategy:'http://strategy.wikimedia.org/wiki/$1',strategywiki:'http://strategywiki.org/wiki/$1',sulutil:'http://toolserver.org/~quentinv57/sulinfo/$1',svn:'http://svn.wikimedia.org/viewvc/mediawiki/$1?view=log',svgwiki:'http://wiki.svg.org/index.php/$1',technorati:'http://www.technorati.com/search/$1',tenwiki:'http://ten.wikipedia.org/wiki/$1',testwiki:'http://test.wikipedia.org/wiki/$1',ticket:'https://ticket.wikimedia.org/otrs/index.pl?Action=AgentTicketZoom&TicketNumber=$1',tools:'http://toolserver.org/$1',tswiki:'https://wiki.toolserver.org/view/$1',translatewiki:'http://translatewiki.net/wiki/$1',tvtropes:'http://www.tvtropes.org/pmwiki/pmwiki.php/Main/$1',unreal:'http://wiki.beyondunreal.com/wiki/$1',urbandict:'http://www.urbandictionary.com/define.php?term=$1',usemod:'http://www.usemod.com/cgi-bin/wiki.pl?$1',usability:'http://usability.wikimedia.org/wiki/$1',webisodes:'http://www.webisodes.org/$1',wg:'http://wg.en.wikipedia.org/wiki/$1',wiki:'http://c2.com/cgi/wiki?$1',wikia:'http://www.wikia.com/wiki/c:$1',wikiasite:'http://www.wikia.com/wiki/c:$1',wikibooks:'http://en.wikibooks.org/wiki/$1',wikichat:'http://www.wikichat.org/$1',wikicities:'http://www.wikia.com/wiki/$1',wikicity:'http://www.wikia.com/wiki/c:$1',wikihow:'http://www.wikihow.com/$1',wikiindex:'http://wikiindex.org/$1',wikimedia:'http://wikimediafoundation.org/wiki/$1',wikinews:'http://en.wikinews.org/wiki/$1',wikipedia:'http://en.wikipedia.org/wiki/$1',wikipediawikipedia:'http://en.wikipedia.org/wiki/Wikipedia:$1',wikiquote:'http://en.wikiquote.org/wiki/$1',wikischool:'http://www.wikischool.de/wiki/$1',wikisource:'http://en.wikisource.org/wiki/$1',wikispecies:'http://species.wikimedia.org/wiki/$1',wikispot:'http://wikispot.org/?action=gotowikipage&v=$1',wikitech:'http://wikitech.wikimedia.org/view/$1',wikiversity:'http://en.wikiversity.org/wiki/$1',betawikiversity:'http://beta.wikiversity.org/wiki/$1',wiktionary:'http://en.wiktionary.org/wiki/$1',wmar:'http://www.wikimedia.org.ar/wiki/$1',wmau:'http://wikimedia.org.au/wiki/$1',wmbd:'http://bd.wikimedia.org/wiki/$1',wmbe:'http://be.wikimedia.org/wiki/$1',wmbr:'http://br.wikimedia.org/wiki/$1',wmca:'http://wikimedia.ca/wiki/$1',wmch:'http://www.wikimedia.ch/$1',wmcz:'http://meta.wikimedia.org/wiki/Wikimedia_Czech_Republic/$1',wmdc:'http://wikimediadc.org/wiki/$1',securewikidc:'https://secure.wikidc.org/$1',wmde:'http://wikimedia.de/wiki/$1',wmfi:'http://fi.wikimedia.org/wiki/$1',wmfr:'http://wikimedia.fr/$1',wmhk:'http://wikimedia.hk/index.php/$1',wmhu:'http://wiki.media.hu/wiki/$1',wmid:'http://www.wikimedia.or.id/wiki/$1',wmil:'http://www.wikimedia.org.il/$1',wmin:'http://wiki.wikimedia.in/$1',wmit:'http://wiki.wikimedia.it/wiki/$1',wmmx:'http://mx.wikimedia.org/wiki/$1',wmnl:'http://nl.wikimedia.org/wiki/$1',wmnyc:'http://nyc.wikimedia.org/wiki/$1',wmno:'http://no.wikimedia.org/wiki/$1',wmpl:'http://pl.wikimedia.org/wiki/$1',wmrs:'http://rs.wikimedia.org/wiki/$1',wmru:'http://ru.wikimedia.org/wiki/$1',wmse:'http://se.wikimedia.org/wiki/$1',wmtw:'http://wikimedia.tw/wiki/index.php5/$1',wmua:'http://ua.wikimedia.org/wiki/$1',wmuk:'http://uk.wikimedia.org/wiki/$1',wmf:'http://wikimediafoundation.org/wiki/$1',wmfblog:'http://blog.wikimedia.org/$1',wookieepedia:'http://starwars.wikia.com/wiki/$1',wowwiki:'http://www.wowwiki.com/$1'}; parts = link.split(/\:/); if ( parts.length > 2 && parts[0] === 'w' && parts[1] === 'c') { parts = parts.slice(2); return 'http://' + parts.shift + '.wikia.com/wiki/' + joinHrefParts(parts) + hash; } else if (parts.length > 1 && interwikiMap[parts[0].toLowerCase]) { return interwikiMap[parts.shift.toLowerCase].replace(/\$1/, joinHrefParts(parts) + hash); }       return '/wiki/' + joinHrefParts(parts) + hash; }   function replaceTag (all, delim, tag) { if (!parserTags[tag]) return '&lt;' + tag; return delim + '' + tag + ''; }   function replaceHeaders (m) { headers.push(m); return '' + m;   } function replaceWikiLink (all, link, title) { return '' + link + ''+ title + ''; }   function replaceTemplates (all, delim, name) { var href, m = name.match(/^(\#?)(\w+)(\:.*)/), fn = m && parserFunctions[m[1] + m[2]]; if (fn) { return delim + m[1] + '' + m[2] + '' + m[3]; }       m = name.match(/^(\s*)(.+)(\s*)$/); if (m[2][0] === ':') { href = m[2].substring(1); } else if (m[2].substring(0, 3) === 'w::') { href = 'w:' + m[2].substring(3); } else { href = 'Template:' + m[2]; }       return delim + m[1] + '' + m[2] + '' + m[3]; }   function replaceRegularLinks (all, link, title) { return '[' + link + '</a>'+ title + ']'; }   function createPseudoToc  { if (headers.length && $source.height > $(window).height) { var toc = '<ul>'; for (var i = 0; i < headers.length; i++) { toc += '<a href="#h' + i + '">' + headers[i] + '</a></li>'; }           toc += '</ul>'; $toc = $('<section id="source-toc" class="module">' + toc + ' ') .insertBefore($('#WikiaRail').find('.module').first); }   }    module.loadSource = function  { $a.text(i18n.viewArticle) .data('source', true); if ($source) { $source.css('display', 'block'); $content.css('display', 'none'); if ($toc) $toc.css('display', 'block'); } else { $.get('/wiki/' + mw.config.get('wgPageName') + '?action=raw&maxage=0&smaxage=0') .done(function (wikitext) {               $source = $(' ' + wikitext .replace(/&/g, '&amp;') .replace(/</g, '&lt;' ) .replace(/>/g, '&gt;' ) .replace(/(&lt;\/?)([\w\:\-]+)/g, replaceTag) .replace(/^((=+)[^\[\]\{\}]+?\2)/gm, replaceHeaders) .replace(/\[{2}([^\[\]\{\}\|]+)(\|[^\]]+)?\]{2}/g, replaceWikiLink) .replace(/\[(https?:\/\/[^ \]]+)([^\]]*)\]/g, replaceRegularLinks) .replace(/((?:^|[^\{])\{\{)([^\{\|\}]+)/g, replaceTemplates) .replace(/\r\n|\r|\n/g, ' ') + ' ')               .insertBefore($content.css('display', 'none'));                createPseudoToc;            }); }   };    module.hideSource = function  { if (!$source) return; $a.text(i18n.viewSource) .data('source', false); $source.css('display', 'none'); $content.css('display', 'block'); if ($toc) $toc.css('display', 'none'); };   if (mw.config.get('wgAction') === 'view') { $(function {            $content = $('#mw-content-text');            $list = $('#WikiaMainContent').find('nav.wikia-menu-button').first.find('ul');            if ($content.length && $list.length) {                addButton;                addCSS;                if ($.getUrlVar('view') === 'source') {                    module.loadSource;                }            }        }); } }((window.dev = window.dev || {}).viewSource = window.dev.viewSource || {}, mediaWiki, jQuery)); //