User:SD0001/parseTemplate.js: Difference between revisions

Content deleted Content added
No edit summary
m Fix requested on WP:IANB
 
(8 intermediate revisions by 2 users not shown)
Line 1:
// <nowiki>
/**
* Returns an array of objects representing the invocationsusages of `template`a given set of in
* `templates in the given wikitext`. The object key-value pairs are the template
* |parameter=value pairs.
*
* Piped links, nested templates, nowiki tags and even HTML comments in parameter values
* values are adequately accounted for.
*
* @paramIf {boolean} [resolveRedirects] is set Alsoas checktrue, forany transclusions of the redirects toof the specified `template`?
* templates would also be found. But since the API call needed to achieve this
* makes the function asynchronous, you need to provide a callback function to
* execute upon completion. The function takes the result array as the argument.
*
* Usage: Can be executed from the browser console or within another script.
*
* @param {string} wikitext TextWikitext in which to search for templatethe templates
*
* @param {(string[]|string)} templatetemplates ArrayName of the template namespage, withoutor Template:array of prefix
* @param {boolean} [resolveRedirects] Also check for transclusions of redirects to the specified `template`?
* template page names. Need not necessarily be pages in template namespace,
* @param {function} callback Callback function to execute after processing of the redirect
* any page can be used. If no namespace is provided, it is assumed as Template:.
* templates. The result array is passed as a parameter to it. Compulsory if `resolveRedirects` is set true.
*
* @returns {Object[]} Returns only if `resolveRedirects` is set false
* @param {boolean} [resolveRedirects=false] Also check for transclusions of
* redirects of the specified `templates`?
*
* @param {function} callback Callback function to execute after processing, ofif the redirect
* templatesresolveRedirects is set true. The result array is passed as a parameter to it. Compulsory if `resolveRedirects` is set true.
*
* @returns {Object[]} Returns only if `resolveRedirects` is set falseunset
*
* @throws if (i) API call is unsuccessful (resolveRedirects mode only)
* (ii) The end of template is not found in the wikitext
* (iii) resolveRedirects is set true but no callback is provided
*
* ISSUES:
* 1. It is possible that a template usage found be entirely within a comment or
* nowiki tags.
* 2. BizarreVery rare situations where, within a parameter value, there are nowiki tags inside
* inside a comment, or vice-versa, will cause problems.
*
* Found any other bug? Report at [[User talk:SD0001]] or via email.
Line 104 ⟶ 125:
}
}
console.log("With i at", wikitext[i], "inComment is", inCommentOrNowiki);
 
}
 
if (numUnclosed !== 0) {
console.error('Error:[parseTemplate] failedFailed to find closing }} of ' + t);
}
 
Line 117 ⟶ 137:
// swap out pipe in links with \1 control character
text = text.replace(/(\[\[[^\]]*?)\|(.*?\]\])/g, '$1\1$2');
 
console.log(text);
 
var chunks = text.split('|');
Line 146 ⟶ 164:
 
if(resolveRedirects && callback === undefined) {
console.error('[parseTemplate] No callback function provided to execute after resolving of redirects');
}
 
templates.forEach(function (template, idx) {
if (template.indexOf(':') === -1) {
template = "Template:" + template;
Line 163 ⟶ 181:
"lhshow": "redirect",
"lhlimit": "max"
}, ).done(function (response) {
$(response).find('lh').each(function(idx,tag) {
var template_redir = $(tag).attr('title');
Line 174 ⟶ 192:
callback(result);
}
}).fail(function (response) {
console.error('[parseTemplate] API call for getting redirects to {{'
+ template + '}} failed');
});
}
Line 185 ⟶ 206:
 
window.parseTemplate = parseTemplate;
// </nowiki>