MediaWiki:Gadget-I18n-js.js: Difference between revisions
From Tardis Wiki, the free Doctor Who reference
add bold and italic formatting
m (add support for user-defined overrides) |
(add bold and italic formatting) |
||
Line 4: | Line 4: | ||
* | * | ||
* @author Cqm <https://dev.wikia.com/User:Cqm> | * @author Cqm <https://dev.wikia.com/User:Cqm> | ||
* @version 0.3. | * @version 0.3.5 | ||
* | * | ||
* @notes Also used by VSTF wiki for their reporting forms (with a non-dev i18n.json page) | * @notes Also used by VSTF wiki for their reporting forms (with a non-dev i18n.json page) | ||
Line 29: | Line 29: | ||
} | } | ||
/* | /** | ||
* Cache of mw config variables. | * Cache of mw config variables. | ||
*/ | */ | ||
Line 42: | Line 42: | ||
cache = {}, | cache = {}, | ||
/* | /** | ||
* | * Cache of overrides. | ||
*/ | */ | ||
overrides = {}, | overrides = {}, | ||
/* | /** | ||
* Language fallbacks for those that don't fallback to English. | * Language fallbacks for those that don't fallback to English. | ||
* Shouldn't need updating unless Wikia change theirs. | * Shouldn't need updating unless Wikia change theirs. | ||
Line 222: | Line 222: | ||
}; | }; | ||
/* | /** | ||
* Get a translation of a message from the messages object in the | * Get a translation of a message from the messages object in the | ||
* requested language. | * requested language. | ||
Line 251: | Line 251: | ||
} | } | ||
/* | /** | ||
* Substitute arguments into the string, where arguments are represented | * Substitute arguments into the string, where arguments are represented | ||
* as $n where n > 0. | * as $n where n > 0. | ||
Line 269: | Line 269: | ||
} | } | ||
/* | /** | ||
* Generate a HTML link using the supplied parameters. | * Generate a HTML link using the supplied parameters. | ||
* | * | ||
Line 280: | Line 280: | ||
*/ | */ | ||
function makeLink(href, text) { | function makeLink(href, text) { | ||
text = text || href; | text = text || href; | ||
href = href.indexOf('http') === 0 ? href : mw.util.getUrl(href); | href = href.indexOf('http') === 0 ? href : mw.util.getUrl(href); | ||
return $('<a>', { | |||
'href': mw.html.escape(href), | |||
'title': text, | |||
text: mw.html.escape(text) | |||
}).prop('outerHTML'); | |||
} | } | ||
/* | /** | ||
* Parse basic wikitext | * Generate a bold element using the supplied parameters. | ||
* | |||
* @param html The HTML of the element. | |||
* | |||
* @return The generated element. | |||
*/ | |||
function makeBold(html) { | |||
return $('<b>', { html: html }).prop('outerHTML') | |||
} | |||
/** | |||
* Generate an italic element using the supplied parameters. | |||
* | |||
* @param html The HTML of the element. | |||
* | |||
* @return The generated element. | |||
*/ | |||
function makeItalic(html) { | |||
return $('<i>', { html: html }).prop('outerHTML') | |||
} | |||
/** | |||
* Parse basic wikitext formatting into HTML. | |||
* | * | ||
* Will process: | * Will process: | ||
Line 296: | Line 321: | ||
* - [[pagename]] | * - [[pagename]] | ||
* - [[pagename|text]] | * - [[pagename|text]] | ||
* - '''text''' | |||
* - ''text'' | |||
* | * | ||
* @param message The message to process. | * @param message The message to process. | ||
Line 307: | Line 334: | ||
simplePageRgx = /\[\[([^|]*?)\]\]/g, | simplePageRgx = /\[\[([^|]*?)\]\]/g, | ||
// [[pagename|text]] -> [[$1|$2]] | // [[pagename|text]] -> [[$1|$2]] | ||
pageWithTextRgx = /\[\[(.+?)\|(.+?)\]\]/g; | pageWithTextRgx = /\[\[(.+?)\|(.+?)\]\]/g, | ||
// '''text''' -> <b>text</b> | |||
boldRgx = /'''([\s\S]+?)'''/g, | |||
// ''text'' -> <i>text</i> | |||
italicRgx = /''([\s\S]+?)''/g; | |||
return message | return message | ||
Line 318: | Line 349: | ||
.replace(pageWithTextRgx, function (_match, href, text) { | .replace(pageWithTextRgx, function (_match, href, text) { | ||
return makeLink(href, text); | return makeLink(href, text); | ||
}) | |||
.replace(boldRgx, function (_match, block, text) { | |||
return makeBold(html); | |||
}) | |||
.replace(italicRgx, function (_match, html) { | |||
return makeItalic(html); | |||
}); | }); | ||
} | } | ||
/* | /** | ||
* Create a new Message instance. | * Create a new Message instance. | ||
* | * | ||
Line 378: | Line 415: | ||
return { | return { | ||
/* | /** | ||
* Set the default language. | * Set the default language. | ||
* | * | ||
Line 387: | Line 424: | ||
}, | }, | ||
/* | /** | ||
* Set the default language to the content language. | * Set the default language to the content language. | ||
*/ | */ | ||
Line 401: | Line 438: | ||
}, | }, | ||
/* | /** | ||
* Create a new instance of Message. | * Create a new instance of Message. | ||
*/ | */ | ||
Line 409: | Line 446: | ||
}, | }, | ||
/* | /** | ||
* For accessing the raw messages. | * For accessing the raw messages. | ||
*/ | */ | ||
Line 416: | Line 453: | ||
} | } | ||
/* | /** | ||
* Strip comments from a JSON string which are illegal under the JSON spec. | * Strip comments from a JSON string which are illegal under the JSON spec. | ||
* | * |