User:Novem Linguae/Scripts/DraftCleaner.js: Difference between revisions

Content deleted Content added
removeBorderFromImagesInInfoboxes() -> album cover (publish.php)
moveAFCSubmissionTemplatesToTop(), deleteNonAFCDraftTags(), refactor (publish.php)
Line 1:
// <nowiki>
 
// === Compiled with Novem Linguae's publish.php script ======================
 
$(async function() {
 
// === main.js ======================================================
/* THIS SCRIPT IS STILL IN BETA AND IS BUGGY ABOUT 25% OF THE TIME. Be sure to check the diff that pops up before submitting.
 
/* THIS SCRIPT IS STILL IN BETA AND IS BUGGY ABOUT 2510% OF THE TIME. Be sure to check the diff that pops up before submitting.
 
- Adds "Run DraftCleaner" link to the left sidebar
Line 176 ⟶ 180:
}
});
 
// === modules/DraftCleaner.js ======================================================
 
 
Line 182 ⟶ 188:
// run before other stuff
wikicode = this.deleteSomeHTMLTags(wikicode);
wikicode = this.deleteBigTagdeleteNonAFCDraftTags(wikicode);
 
wikicode = this.fixWikilinksContainingURL(wikicode);
Line 218 ⟶ 224:
wikicode = this.removeBorderFromImagesInInfoboxes(wikicode);
wikicode = this.removeExtraAFCSubmissionTemplates(wikicode);
wikicode = this.moveAFCSubmissionTemplatesToTop(wikicode);
 
// all ==sections== should start with a capital letter
Line 640 ⟶ 647:
wikicode = wikicode.replace(/<\/?nowiki( [^>]*)?\/?>/g, '');
wikicode = wikicode.replace(/<\/?u( [^>]*)?\/?>/g, '');
returnwikicode = wikicode.replace(/(?:<big>|<\/big>)/g, '');
return wikicode;
}
 
deleteNonAFCDraftTags(wikicode) {
wikicode = wikicode.replace(/{{Preloaddraft submit}}\n{0,2}/gi, '');
wikicode = wikicode.replace(/<!-- When you move this draft into article space, please link it to the Wikidata entry and remove the QID in the infobox code\. -->\n{0,2}/gi, '');
wikicode = wikicode.replace(/{{Draft}}\n{0,2}/gi, '');
return wikicode;
}
Line 664 ⟶ 679:
 
return wikicode;
}
 
deleteBigTag(wikicode) {
return wikicode.replace(/(?:<big>|<\/big>)/g, '');
}
 
Line 710 ⟶ 721:
if ( hasSubmittedTemplate && hasUnsubmittedTemplate ) {
wikicode = wikicode.replace(/{{AfC submission\|t\|[^\}\}]*\}\}\n?/gm, '');
}
return wikicode;
}
 
moveAFCSubmissionTemplatesToTop(wikicode) {
let hasTemplateAtBottom = wikicode.match(/\n[^\n]+\n*({{AfC submission[^\}]*}})\s*$/i);
if ( hasTemplateAtBottom ) {
// delete all submission templates
wikicode = wikicode.replace(/{{AfC submission[^\}\}]*\}\}\n?/gm, '');
 
// insert template at top
wikicode = hasTemplateAtBottom[1] + "\n\n" + wikicode;
}
return wikicode;
Line 733 ⟶ 756:
}
 
// === modules/StringFilter.js ======================================================
/** Lets you use regex to specify what parts of a very long string you want to specify as "off limits", then you can do additional regex's and search/replace to the remaining parts of the string. */
 
/**
/* * Lets you use regex to specify what parts of a very long string you want to specify as "off limits", then you can do additional regex's and search/replace to the remaining parts of the string. */
*/
class StringFilter {
/**
/* * Does a replace, but specifies areas of the file that should NOT be replaced. Those areas are specified by providing an openingTag and a closingTag, and those areas are marked as off limits. */
*/
surgicalReplaceOutsideTags(regex, replacement, haystack, openingTags, closingTags) {
let allTags = [...openingTags, ...closingTags];
Line 756 ⟶ 785:
}
 
/**
/* * Does a replace, but specifies areas of the file that SHOULD be replaced, then skips the rest of the file. The area that should be replaced is specified by providing an openingTag and a closingTag. */
*/
surgicalReplaceInsideTags(regex, replacement, haystack, openingTags, closingTags) {
let allTags = [...openingTags, ...closingTags];
Line 773 ⟶ 804:
/**
* Also keeps the pattern in the result, unlike string.prototype.split. Algorithm isn't perfect, will fail with this pattern: <ref>Test/>Test</ref>. But should be good enough for DraftCleaner stuff.
* @param {Arraystring[]} patterns
* @returns {string[]}
@param {Array} patterns
*/
_splitStringUsingMultiplePatterns(string, patterns) {
let length = string.length;