MediaWiki:Gadget-wkprintversion.js

//Print Version Creator //Maintained by User:Whiteknight /*  This script operates a semi-automagical print version creator gadget. This gadget is  located at User:Whiteknight/Print Version Gadget. It uses Whiteknight's Javascript core library to help with some of the common tasks and the AJAX-related tasks. Documentation and the gadget itself is located at  User:Whiteknight/Print Version Gadget.

var pv = { require:  2.64, formspan: "WKPVFormSpan", textspan: "WKPVSpan", statspan: "WKPVStatSpan", boxspan: "WKPVFormSpanSub", form: null, text: "",      //raw converted text htmltext: ""  //html-formatted converted text }

pv.getForm = function { return pv._form; }

pv.createForm = function { var form = wk.spanText(pv.formspan, ""); if(form == null) return; pv._editB = wk.makeButton("WKPVEditB",   "Edit",    pv.edit); pv._displayB = wk.makeButton("WKPVDisplayB", "Display", pv.display); pv._booknameB = wk.makeInput("WKPVBookNameB", ""); pv._listB = wk.makeElement("Textarea", {name:"WKPVListB", rows:"15", cols:"70"}); pv._spanB = wk.makeElement("span", {id:pv.boxspan}, pv._listB); pv._form = wk.makeElement("form", {name:"WKPVFormB", method:"GET"}, [     pv._booknameB,      wk.makeButton("WKPVLoadB",    "Load",    pv.loadPage),      " ",      wk.makeButton("WKPVShowB",    "Show",    function{wk.toggleDisplay(pv.boxspan, "block")}),      wk.makeButton("WKPVHideB",    "Hide",    function{wk.toggleDisplay(pv.boxspan, "none")}),      " ",      wk.makeButton("WKPVClearB",   "Clear",   pv.clear),      " ",      wk.makeButton("WKPVConvertB", "Convert", pv.convert),      " ",      pv._editB,      pv._displayB,      pv._spanB,      wk.makeElement("br")  ]); wk.toggleDisplay(pv._spanB, "none"); wk.toggleDisable(pv._editB, true); wk.toggleDisable(pv._displayB, true); form.appendChild(pv._form); } if(typeof wk == 'object' && wk.testVersion(pv.require)) { $(pv.createForm); }

pv.clear = function { wk.spanText(pv.textspan, ""); wk.spanText(pv.statspan, ""); wk.toggleDisplay(pv.boxspan, "none"); pv._listB.value = ""; wk.toggleDisable(pv._editB, true); wk.toggleDisable(pv._displayB, true); }

pv.bookName = ""; pv.getBookName = function { pv.bookName = pv._booknameB.value; return pv.bookName; }

pv.loadPage = function { pv.getBookName; if(pv.bookName == "") { wk.spanText(pv.statspan, "Must enter a valid page name to load from"); return; } wk.loadWikiText(pv.bookName, function(text){pv._listB.value = text;}); wk.spanText(pv.statspan, "Successfully retrieved text from " +   wk.wikiLinkText(pv.bookName) +     ". Click Show to see and edit the page text. Click Convert " +     "to create the print version."  ); }

pv.setChapter = function (page, offset, xml) { page = page.replace("", ""); page = page.replace("", ""); var levels = page.split("/"); var subpage = levels[levels.length - 1]; if(subpage == "") {   subpage = levels[levels.length - 2]; } return ("{"+"{Print chapter heading|" + subpage + "}}\n\n"); }

pv.setSections = function (head, offset, xml) { head = head.replace(/=/g, ""); head = head.replace(/^\s*/g, ""); head = head.replace(/\s*$/g, ""); return("{"+"{Print unit page|" + head + "}}\n\n"); }

pv.xmlFormat = function(xml) { xml = xml.replace(/''+/g, ""); //kill text formatting xml = xml.replace(/\{\{[^\}]+\}\}/g, ""); //kill templates xml = xml.replace(/\[\[category:[^\]]+\]\]/ig, ""); //kill categories xml = xml.replace(/\|[^\]\}]+\]\]/g, "]]"); //kill piped links xml = xml.replace(/\[\[\/([^\]]+)\/\]\]/g, "" + pv.bookName + "/$1"); //properly address relative links xml = xml.replace(/\[\[([^\]]+)\]\]/g, pv.setChapter); //prepare chapter entries xml = xml.replace(/===([^=]+)===/g, pv.setSections); xml = xml.replace(/\* ?/g, ""); //kill bullets xml = xml.replace(/# ?/g, ""); //kill ordered lists xml = xml.replace(/==[^=]+==/g, ""); //kill headings xml = xml.replace(/\n\n\n+/g, "\n\n"); //kill extra whitespace xml = xml.replace(//g, "&gt;"); var header = "\n" + " \n" + "&lt;br style=\"page-break-after: always\"&gt;\n"; if(pv.bookName != "") { header = header + "\n" + "&lt;br style=\"page-break-after: always\"&gt;\n"; } xml = header + xml + "\n= GNU Free Documentation License =\n\n\n\n"; return xml; }

pv.convert = function { wk.toggleDisable(pv._editB, false); wk.toggleDisable(pv._displayB, false); wk.toggleDisplay(pv.boxspan, "none"); pv.htmltext = pv.xmlFormat(pv._listB.value); pv.text = pv.htmltext.replace(/&gt;/g, ">"); pv.text = pv.text.replace(/&lt;/g, "<"); wk.spanText(pv.statspan, "Successfully converted text. " +   "Click Display to show the current text for copy+paste. " +    "Click Edit to edit and save the current text."); wk.toggleDisable(pv._editB, false); wk.toggleDisable(pv._displayB, false); wk.spanText(pv.textspan, ""); }

pv.display = function { wk.toggleDisplay(pv.boxspan, "none"); wk.spanText(pv.statspan, "Print version text: "); if(pv.getBookName != "") { wk.appendSpanText(pv.statspan, "Copy the text below into " +     "" + pv.bookName + "/Print Version " +      " Warning : " +       "this text may need to be edited manually to remove unwanted " +       "templates, categories, links or odd formatting that the tool " +      "cannot account for. "); } wk.spanText(pv.textspan, "" + pv.htmltext + " "); }

pv.edit = function { if(pv.getBookName == "") { wk.spanText(pv.statspan, "Must enter a valid page name to edit"); return; }   wk.spanText(pv.statspan, "Loading..."); wk.toggleDisplay(pv.boxspan, "none"); wk.loadEditPage(pv.bookName + "/Print Version", pv._edit2); }

pv._edit2 = function(xml) { var edit = wk.showEditWindow(xml, pv.textspan); edit.summary.value = pv.bookName + " Printable Version: Created by Whiteknight's Print Version Creator"; edit.text.value = pv.text; wk.spanText(pv.statspan,    "Editing text for " +     wk.wikiLinkText(pv.bookName + "/Print Version") +     ". Edit the text below to remove any errors or artifacts " +    "of the conversion process. Once you have made your edits, " +    "click Save Page."); }