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

Content deleted Content added
pass more unit tests (publish.php)
pass more unit tests (publish.php)
Line 271:
// convert inline external links to references
inlineExternalLinksToRefs(wikicode) {
let externalLinksSectionInArticlesectionsToSkip = wikicode.match(/== ?['External link/i)', 'Further reading', 'Links'];
let regExString = '== ?(?:';
let furtherReadingSectionInArticle = wikicode.match(/== ?Further reading/i);
for ( let sectionToSkip of sectionsToSkip ) {
// let infoboxInArticle = wikicode.match(/{{Infobox/i);
regExString += sectionToSkip + '|';
}
regExString = regExString.slice(0, -1) + ')';
let hasSectionToSkip = wikicode.match(new RegExp(regExString, 'i'));
 
let sf = new StringFilter();
 
if ( ! boldInArticlehasSectionToSkip ) {
// skip external links and further reading sections
let regExToSplitArticle = new RegExp('((' + regExString + ').*$)', 'is');
if ( externalLinksSectionInArticle || furtherReadingSectionInArticle ) {
let topHalf = wikicode.replace(/((== ?Further reading|== ?External link).*$)/isregExToSplitArticle, '');
let bottomHalf = wikicode.match(/((== ?Further reading|== ?External link).*$)/isregExToSplitArticle)[1];
let buffer = sf.surgicalReplaceOutsideTags(/(?<!>|> )\[(http[^ \]]+) ?(.*?)\](?!<\/ref>| <\/ref>)/gm, '$2<ref>$1</ref>', topHalf, ['<ref', '{{'], ['</ref>', '/>', '}}']);
wikicode = buffer + bottomHalf;
} else {
wikicode = sf.surgicalReplaceOutsideTags(/(?<!>|> )\[(http[^ \]]+) ?(.*?)\](?!<\/ref>| <\/ref>)/gm, '$2<ref>$1</ref>', wikicode, ['<ref', '{{'], ['</ref>', '/>', '}}']);
}
return wikicode;
Line 324 ⟶ 328:
// WARNING: this is buggy sometimes
boldArticleTitle(wikicode, titleWithNamespaceAndSpaces) {
let titleToLookFor = titleWithNamespaceAndSpaces;
let boldInArticle = wikicode.includes("'''");
titleToLookFor = titleToLookFor.replace(/ \(.*?\)$^Draft:/, '');
if ( ! boldInArticle ) {
let titleToLookFor = titleWithNamespaceAndSpacestitleToLookFor.replace(/ \(.*?\)$/, '');
titleToLookFor = titleToLookForthis.replace_escapeRegEx(/^Draft:/, ''titleToLookFor);
// Be pretty strict, to avoid adding ''' to image paths and infoboxes, which messes up the image. Also, only replace first match.
titleToLookFor = titleToLookFor.replace(/ \(.*?\)$/, '');
titleToLookForlet regEx = this._escapeRegExnew RegExp("^(The )?("+titleToLookFor+")([ <,])", "mi");
wikicode = wikicode.replace(regEx, "$1'''$2'''$3");
// Be pretty strict, to avoid adding ''' to image paths and infoboxes, which messes up the image.
let regEx = new RegExp("^(The )?("+titleToLookFor+")([ <,])", "mi");
wikicode = wikicode.replace(regEx, "$1'''$2'''$3");
}
return wikicode;
}
Line 340 ⟶ 341:
// Careful of this string in URLs.
capitalizeCOVID19(wikicode) {
let sf = new StringFilter();
wikicode = wikicode.replace(/ covid-19/gmi, ' COVID-19');
wikicode = wikicodesf.replacesurgicalReplaceOutsideTags(/\ncovid covid-19/gmi, "\nCOVID' COVID-19"', wikicode, ['{{', '[['], ['}}', ']]']);
wikicode = sf.surgicalReplaceOutsideTags(/\ncovid-19/gmi, "\nCOVID-19", wikicode, ['{{', '[['], ['}}', ']]']);
return wikicode;
}
Line 421 ⟶ 423:
// convert smart quotes to regular quotes
convertSmartQuotesToRegularQuotes(wikicode) {
let sf = new StringFilter();
wikicode = wikicode.replace(/”/g, '"');
wikicode = wikicodesf.replacesurgicalReplaceOutsideTags(//g, '"', wikicode, ['[[File:'], [']]']);
wikicode = wikicodesf.replacesurgicalReplaceOutsideTags(//g, "'"', wikicode, ['[[File:'], [']]']);
wikicode = wikicodesf.replacesurgicalReplaceOutsideTags(//g, "'", wikicode, ['[[File:'], [']]']);
wikicode = wikicodesf.replacesurgicalReplaceOutsideTags(//g, "...'", wikicode, ['[[File:'], [']]']);
wikicode = sf.surgicalReplaceOutsideTags(/…/g, '...', wikicode, ['[[File:'], [']]']);
return wikicode;
}
Line 502 ⟶ 505:
for ( let line of lines ) {
i++;
// scan for first heading.
// empty lines, lines with templates, or lines with images do not count.
if ( line.startsWith('{') || line.length === 0 || line.startsWith('[[File:') ) {
output += line;
Line 657 ⟶ 661:
 
deleteEmptySections(wikicode) {
return wikicode.replace(/\n*== ?(?:See also|External links) ?==\n*$/, '');
// TODO:
return wikicode;
}