User:PleaseStand/segregate-refs-dev.js: Difference between revisions

Content deleted Content added
m fix bug (thanks to JSLint)
added code to try to avoid unnecessary shifting of ref contents in complete search mode (includes a bug fix)
Line 87:
// Semi-global variables (private to this script)
var editForm; var refsDiv; var refsH2; var mainTextbox; var refsTextbox;
var randPrefix; var messages; var refsButton; var convertButton; var complete;
 
// Extend the string object with new methods
Line 186:
// 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. caseCues is whether or not to use
// capitalization to designate a preferred ref code ___location.
function segregateRefs(argWikiText, completeSearch, group, caseCues) {
// Create a random prefix for autogenerated ref names.
Line 197 ⟶ 198:
prefixChars.length)) + prefixChars.charAt(randNo % prefixChars.length) +
"-";
// Create the beginning of the code for a preferred ref ___location
var refPreferred = caseCues ? "<REF " : "<ref ";
// Variables for the main code
Line 265 ⟶ 269:
// Make a short code for the ref
if(!refGroup.length) {
refShort = "<refrefPreferred + "name=" +
refName.PsHTMLQuote() + "/>";
} else {
refShort = refPreferred + "<ref name=" + refName.PsHTMLQuote() + " " +
" " + "group=" + refGroup.PsHTMLQuote() + "/>";
}
// Otherwise, is the current longcode not empty?
// (only when in complete search mode), and when another non-empty,
}// elsesame-named ifref (completeSearchhas &&not !refEmpty)been {encountered)
} else if (completeSearch && !refEmpty && refNames[refName].empty) {
// Not empty: fill in the long code and make a short code
refCodes[refNames[refName].code] = refLong;
refNames[refName].empty = false;
if(!refGroup.length) {
refShort = "<refrefPreferred + "name=" +
refName.PsHTMLQuote() + "/>";
} else {
refShort = refPreferred + "<ref name=" + refName.PsHTMLQuote() + " " +
" " + "group=" + refGroup.PsHTMLQuote() + "/>";
}
} else {
// Leave the ref as-is in the original wikitext
refShort// =(except refLong;if caseCues apply)
if(caseCues) {
refShort = refLong.replace(/^<REF/, "<ref");
} else {
refShort = refLong;
}
}
// Replace the long code with the short code
Line 301 ⟶ 313:
 
// integrateRefs: Use for inserting ref contents back into text
function integrateRefs(argWikiText, argRefText, randPrefix, caseCues) {
// A function to remove an autogenerated ref name (if possible)
Line 311 ⟶ 323:
// Variables for the main code
var scanner; var ref; var parser; var attributes; var refCodes = {};
var usageFreq = {}; var preferredRef = {}; var refLong;
// First, we build an associative array of all the ref codes
Line 328 ⟶ 340:
// Next, we build an associative array that holds the usage frequency
// of every ref name used in text., and whether there is a preferred ref,
// if caseCues are selected.
scanner = new RefScanner(argWikiText);
while((ref = scanner.getRef())) {
Line 340 ⟶ 353:
// We already found this name
usageFreq[attributes.name]++;
}
if(caseCues) {
if(ref.slice(0, 4) == "<REF") {
preferredRef[attributes.name] = true;
}
}
}
Line 347 ⟶ 365:
// Finally, we go through the text again and this time we insert the
// ref codes where we need to, but only in the first place
// a ref name appears (or the first preferred ___location).
scanner = new RefScanner(argWikiText);
while((ref = scanner.getRef())) {
Line 355 ⟶ 373:
// Is this name on the replacement list?
if(refCodes.hasOwnProperty(attributes.name)) {
// If we are using caseCues, and this ___location is not
// preferred, skip to the next ref.
if(caseCues && ref.slice(0,4) != "<REF") {
continue;
}
// Is this name an autogenerated name?
if(attributes.name.slice(0, randPrefix.length) == randPrefix) {
Line 397 ⟶ 422:
// Do the actual integration work
result = integrateRefs(mainTextbox.value, refsTextbox.value,
randPrefix, complete);
// Find all unused ref names
Line 427 ⟶ 452:
editForm.onsubmit.apply(this, arguments);
}
return true;
}
 
function refsButtonHandler() { // Called when script activated by button click
// Local variables
var complete = false;
// Both buttons should disappear
Line 446 ⟶ 470:
SegregateRefsJsCompleteSearch) {
complete = true;
} else {
var complete = false;
}
// Do the actual segregation work and save the random prefix
var segFormat = segregateRefs(mainTextbox.value, complete, "", complete);
if(!segFormat) {
return false;
Line 513 ⟶ 539:
// Do the actual segregation work and save the random prefix
var segFormat = segregateRefs(mainTextbox.value, true, group, false);
if(!segFormat) {
return false;