User:Whiteknight/printversion2.js

var BookName, BookNameE; //contains the name of the book, if any var gml_xmlhttp;        //http client var PVtext;             //node element for the textarea var PVstat;             //node element for the status area var formnode; var xml; var PVformspan = "WKPVFormSpan"; var PVtextspan = "WKPVSpan"; var PVstatspan = "WKPVStatSpan"; var PVboxspan = "WKPVFormSpanSub"; var text = ""; var text2 = ""; function WKPVCreateForm { setSpanText(PVformspan,    "" +    makeTextBox("WKPVBookNameB") +    makeButton("WKPVSubmitB",  "Load",    "WKPVGet(this.form);") + " " +    makeButton("WKPVShowB",    "Show",    "WKPVShowBox;") +    makeButton("WKPVHideB",    "Hide",    "WKPVHideBox;") + " " +    makeButton("WKPVClearB",   "Clear",   "WKPVClear(this.form);") + " " +    makeButton("WKPVConvertB", "Convert", "WKPVConvert(this.form);") + " " +    makeButton("WKPVEditB",    "Edit",    "WKPVEdit(this.form);",    "true") +    makeButton("WKPVDisplayB", "Display", "WKPVDisplay(this.form);", "true") +    "" +    " " +    "  "); } function WKPVShowBox { var box = document.getElementById(PVboxspan); box.style.display = ""; } function WKPVHideBox { var box = document.getElementById(PVboxspan); box.style.display = "none"; } function WKPVClear(form) { ClearSpan(PVtextspan); ClearSpan(PVstatspan); WKPVHideBox; form.WKPVListB.value = ""; form.WKPVEditB.disabled = true; form.WKPVDisplayB.disabled = true; } function WKPVGet(form) { formnode = form; BookName = form.WKPVBookNameB.value; BookNameE = BookName.replace(" ", "_"); PVstat = ClearSpan(PVstatspan); PVtext = ClearSpan(PVtextspan); if(PVstat && PVtext) { if(BookName == "") { return; }   gml_xmlhttp = HTTPClient; if (!gml_xmlhttp) { setSpanText(PVstatspan, "xml thingy does not work"); return; }   gml_xmlhttp.open("GET", "http://en.wikibooks.org/w/index.php?title=" + BookNameE +                            "&action=raw", true); gml_xmlhttp.onreadystatechange = WKPVGet2; gml_xmlhttp.send(null); } else { alert("No " + PVstatspan + " or " + PVtextspan + " element!"); } } function WKPVGet2 { if (gml_xmlhttp.readyState != 4) { return; } formnode.WKPVListB.value = gml_xmlhttp.responseText; setSpanText(PVstatspan, "Got text from " + createWikiLink(BookName) + ". " +                         "Click Convert to prepare the print version. " +                          "Click Show to show and edit the current text manually."); } function WKPVSetChapter(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 ("\n\n"); } function WKPVFormat(xml, BookName) { 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, "" + BookName + "/$1"); //properly address relative links xml = xml.replace(/\[\[([^\]]+)\]\]/g, WKPVSetChapter); //prepare chapter entries 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(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; } function WKPVConvert(form) { form.WKPVEditB.disabled = false; form.WKPVDisplayB.disabled = false; BookName = form.WKPVBookNameB.value; WKPVHideBox; text = WKPVFormat(form.WKPVListB.value, BookName); text2 = text.replace(/&gt;/g, ">"); text2 = text2.replace(/&lt;/g, "<"); setSpanText(PVstatspan, "Successfully converted text. " +                         "Click Show to display and edit the current text. " +                          "Click Display to show the current text for copy+paste. " +                          "Click Edit to edit and save the current text."); ClearSpan(PVtextspan); } function WKPVDisplay(form) { BookName = form.WKPVBookNameB.value; BookNameE = BookName.replace(/ /g, "_"); PVstat   = document.getElementById(PVstatspan); WKPVHideBox; PVstat.innerHTML = "Print version text: "; if(BookName != "") { PVstat.innerHTML = PVstat.innerHTML + "Copy the text below into " + "" + 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. "; } setSpanText(PVtextspan, "" + text + " "); } function WKPVEdit(form) { formnode = form; BookName = form.WKPVBookNameB.value; if(BookName == "") { return; }   BookNameE = BookName.replace(" ", "_"); WKPVHideBox; gml_xmlhttp = HTTPClient; if (!gml_xmlhttp) { setSpanText(PVstatspan, "XML thingy doesnt work"); return; } editurl = "http://en.wikibooks.org/w/index.php?title=" + BookNameE + "&action=edit"; gml_xmlhttp.open('GET', editurl, true); gml_xmlhttp.onreadystatechange = WKPVEdit2; gml_xmlhttp.overrideMimeType("text/xml"); gml_xmlhttp.send(null); } function WKPVEdit2 { if (gml_xmlhttp.readyState != 4) return; var doc = gml_xmlhttp.responseXML; var form = doc.getElementById('editform'); var ta = doc.getElementById('wpTextbox1'); var sum = doc.getElementById('wpSummary'); sum.value = BookName + " Printable Version: Created by Whiteknight's Print Version Creator"; ta.value = text2; form.action = "/w/index.php?title=" + BookNameE + "/Print_Version&action=submit"; setSpanText(PVstatspan, "Editing text for " +                          createWikiLink(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."); PVtext = ClearSpan(PVtextspan); PVtext.appendChild(form); } if(typeof WhiteknightGadgets != 'function') { import_script('User:Whiteknight/gadgetscore.js'); } $(WKPVCreateForm);