Content deleted Content added
PleaseStand (talk | contribs) adding code to turn off when section editing |
PleaseStand (talk | contribs) updating with capability to convert to LDR |
||
Line 32:
wikEdUseWikEd, WikEdUpdateTextarea, WikEdUpdateFrame*/
// Translate the right-hand side of these if necessary.
// Put translations in a separate file, changing the first line to:
// var SegregateRefsJsL10n = {
var SegregateRefsJsMsgs = {
version: 1.1,
buttonText: "Segregate refs for editing",
buttonStyle: "background: #dfd;",
buttonConvertText: "Migrate article to LDR",
buttonConvertStyle: "background: #fdd;",
autoWord: "Auto",
emptyRefsWarning: "IMPORTANT: This page includes one or more named " +
Line 50 ⟶ 53:
"\n\nDo you acknowledge this limitation of the script? DO NOT CLICK OK " +
"UNTIL YOU HAVE READ THE ABOVE INFORMATION.",
convertRefsWarning: "WARNING: You need consensus to migrate an article " +
"to list-defined references format (LDR) BEFORE you do so.\n\nClick " +
"Cancel now if consensus has not been established in favor of this " +
"migration. If there is consensus to make the conversion, click OK to " +
"do so.",
refsHeader: "Inline footnotes",
convertHeader: "Generated refs list",
refsCommentIncomplete: "<!-- This list of footnotes may be incomplete. " +
"Do not use for conversion purposes. -->\n\n",
refsCommentComplete: "<!-- Converted to LDR format\n" +
" using [[User:PleaseStand/References segregator]] -->\n\n"
};
Line 58 ⟶ 71:
// Semi-global variables (private to this script)
var editForm; var refsDiv; var refsH2; var mainTextbox; var refsTextbox;
var randPrefix; var messages;
// Extend the string object with new methods
Line 155 ⟶ 164:
// segregateRefs: Use for segregating refs from content.
// If completeSearch == true, the function looks in all occurrences for ref
// contents. If completeSearch == false, the function only checks the first.
// group is the reference group to limit the operation to. The empty string
// refers to all ungrouped refs.
function segregateRefs(argWikiText, completeSearch, group) {
// Create a random prefix for autogenerated ref names.
// in theory this has a 1/1296 probability of collision - extremely low
Line 167 ⟶ 181:
// Variables for the main code
var scanner = new RefScanner(argWikiText);
var unnamedRefs = 0; var refNames = {}; var ref; var refStored;
var parser; var attributes; var refName; var refLong; var refShort;
var refCodes = []; var refEmpty; var emptyRefsWarningGiven = false;
var refGroup;
// Disable the empty refs warning (see below) if the user has disabled it
Line 180 ⟶ 195:
parser = new RefParser(ref);
attributes = parser.getAttributes();
// First make sure that the ref is in the right group.
refGroup = attributes.hasOwnProperty("group") ? attributes.group : "";
if(group != refGroup) {
continue;
}
// Does the ref have a name?
// (Note: No matter how incorrect it seems, the empty string is
Line 186 ⟶ 208:
if(!attributes.hasOwnProperty("name")) {
// Bad: it doesn't have one - create a name for it
refStored = false;
refEmpty = false;
refName = randPrefix + (++unnamedRefs).toString(10);
// Change the corresponding ref code
Line 194 ⟶ 218:
refName = attributes.name;
refLong = ref;
//
refStored = refNames.hasOwnProperty(refName);
// Since this script, when not set in complete search mode, checks
// only the first
// of this limitation if it may pose a problem.
if(!completeSearch && !emptyRefsWarningGiven && !refStored) {
if(refEmpty) {
if(!window.confirm(messages.emptyRefsWarning)) {
return false;
}
emptyRefsWarningGiven = true;
}
}
}
// Is the ref's name unique?
if(!
//
refNames[refName] =
empty: refEmpty
};
refCodes[refNames[refName].code] = refLong;
// Make a short code for the ref
if(group.length) {
refShort = "<ref name=" + refName.PsHTMLQuote() + "/>"; } else {
refShort = "<ref name=" + refName.PsHTMLQuote() + " " +
"group=" + group.PsHTMLQuote() + "/>";
}
// Otherwise, is the stored longcode empty?
// (only when in complete search mode)
} else if (completeSearch && refNames[refName].empty) {
// Empty: fill in the long code and make a short code
refCodes[refNames[refName].code] = refLong;
refNames[refName].empty = false;
if(group.length) {
refShort = "<ref name=" + refName.PsHTMLQuote() + "/>";
} else {
refShort = "<ref name=" + refName.PsHTMLQuote() + " " +
"group=" + group.PsHTMLQuote() + "/>";
}
} else {
//
refShort = refLong;
}
Line 344 ⟶ 391:
function refsButtonHandler() { // Called when script activated by button click
//
this.parentNode.removeChild(this);
this.parentNode.removeChild(this.nextSibling);
// Do the actual segregation work and save the random prefix
var segFormat = segregateRefs(mainTextbox.value, false, "");
if(!segFormat) {
return false;
Line 374 ⟶ 422:
refsTextbox = window.document.createElement("textarea");
refsTextbox.id = "PsRefsTextbox";
refsTextbox.value =
segFormat.refCodes.join("\n\n");
refsTextbox.rows = Math.floor(mainTextbox.rows / 2);
refsTextbox.cols = mainTextbox.cols;
Line 391 ⟶ 440:
}
function
// Display warning
if(!window.confirm(messages.convertRefsWarning)) {
return false;
}
// The first button should disappear
this.parentNode.removeChild(this.previousSibling);
// Do the actual segregation work and save the random prefix
var segFormat = segregateRefs(mainTextbox.value, true, "");
if(!segFormat) {
return false;
}
randPrefix = segFormat.randPrefix;
//
if(typeof wikEdUseWikEd != "undefined" && wikEdUseWikEd) {
WikEdUpdateTextarea();
}
//
mainTextbox.value = segFormat.wikiText;
//
if(typeof wikEdUseWikEd != "undefined" && wikEdUseWikEd) {
WikEdUpdateFrame();
}
// Inline refs header
refsH2 = window.document.createElement("h2");
refsH2.appendChild(window.document.createTextNode(messages.convertHeader));
// Inline refs textbox
if(!refsTextbox) {
// Does not exist; creating
refsTextbox = window.document.createElement("textarea");
refsTextbox.id = "PsRefsTextbox";
refsTextbox.value = messages.refsCommentComplete +
segFormat.refCodes.join("\n\n");
refsTextbox.rows = Math.floor(mainTextbox.rows / 2);
refsTextbox.cols = mainTextbox.cols;
// Add to document
refsDiv.appendChild(refsH2);
refsDiv.appendChild(refsTextbox);
} else {
// Already exists
refsTextbox.value += messages.refsCommentComplete +
segFormat.refCodes.join("\n\n");
}
// Don't submit form
return false;
}
function loadHandler() { // This function is called on page load
try {
// Restore the previous load handler;
window.onload = window.PsOnload;
// Handle message translations
messages = (typeof SegregateRefsJsL10n == "object" &&
SegregateRefsJsL10n.version == 1.1 ? SegregateRefsJsL10n :
SegregateRefsJsMsgs);
// Only activate on edit pages (that are not section edit pages)
if(!window.document.getElementById("editform") ||
(window.___location.search.indexOf("section=") >= 0)) {
return;
}
// Get the edit form
editForm = window.document.getElementById("editform");
// Get the edit box
mainTextbox = window.document.getElementById("wpTextbox1");
// Make the "segregate" button
var refsButton = window.document.createElement("input");
refsButton.type = "button";
refsButton.value = messages.buttonText;
refsButton.setAttribute("style", messages.buttonStyle);
refsButton.onclick = refsButtonHandler;
// Make the "convert" button
var convertButton = window.document.createElement("input");
convertButton.type = "button";
convertButton.value = messages.buttonConvertText;
convertButton.setAttribute("style", messages.buttonConvertStyle);
convertButton.onclick = convertButtonHandler;
// Add the refs div
refsDiv = window.document.createElement("div");
refsDiv.appendChild(refsButton);
editForm.insertBefore(refsDiv,
window.document.getElementById("editpage-copywarn"));
} catch(e) {
}
// Run any previously registered load handler
if(this.onload) {
|