Wikipedia:AutoEd/htmltowikitext.js: Difference between revisions

Content deleted Content added
Add
Fixes
 
(9 intermediate revisions by 3 users not shown)
Line 1:
//<source lang=javascript>
 
//Convert HTML to wikitext
function autoEdHTMLtoWikitext(str) {
// <b>, <strong>,and <i>, and <em> tags
str = str.replace(/<(B|STRONG)[ ]*>((?:[^<>]|<[a-z][^<>]*\/>|<([a-z]+)(?:| [^<>]*)>[^<>]*<\/\32>)*?)<\/\1B[ ]*>/gi, "'''$21'''");
str = str.replace(/<(I|EM)[ ]*>((?:[^<>]|<[a-z][^<>]*\/>|<([a-z]+)(?:| [^<>]*)>[^<>]*<\/\32>)*?)<\/\1I[ ]*>/gi, "''$21''");
// </br>, <\br>, <br\>, <BR />, ...
str = str.replace(/<?<[\\\/]+BR[\\\/\s]*>>?/gim, '<br />');
str = str.replace(/<?<[\\\/\s]*BR[\s]*[\\\/]+[\s]*>>?/gim, '<br />');
// <.br>, <br.>, <Br>, ...
str = str.replace(/<?<[\s\.]*BR[\s\.]*>>?/gim, '<br>');
// <br>>, <<br />, <<br >> ...
str = str.replace(/<[\\\/\s]*REFERENCES(<br[\\s\/\s]*>)/gim, '<references />$1');
str = str.replace(/(<br[\s\/]*>)[\s]*>/gim, '$1');
// <hr>
str = str.replace(/([\r\n])[\t ]*<[\\\/\. ]*HR[\\\/\. ]*>/gi, '$1----');
// str = str.replace(/(.)<[\\\/\. ]*HR[\\\/\. ]*>/gi, '$1\n----'); // Breaks wikitables
// Not really an HTML-to-wikitext fix, but close enough
str = str.replace(/<[\\\/\s]*REFERENCES[\\\/\s]*>/gim, '<references />');
// Repeated references tag
str = str.replace(/(<references \/>)[\s]*\1/gim, '$1');
Line 38 ⟶ 37:
return str;
}
 
//</source>