User:Whiteknight/printversion.js
From Wikibooks, open books for an open world
Note: After saving, changes may not occur immediately. Click here to learn how to bypass your browser's cache.
- Mozilla / Firefox / Safari: hold down Shift while clicking Reload, or press Ctrl-Shift-R (Cmd-Shift-R on Apple Mac);
- Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5;
- Konqueror: simply click the Reload button, or press F5;
- Opera users may need to completely clear their cache in Tools→Preferences.
| Code that you insert on this page could contain malicious content capable of compromising your account. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate reading room. The code will be executed when previewing this page under some skins, including Monobook. |
|
|
Documentation for this script can be added at User:Whiteknight/printversion. |
//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 = { 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(2.4)) { addOnloadHook(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 <b>Show</b> to see and edit the page text. Click <b>Convert</b> " + "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{{:" + page + "}}\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, "<"); xml = xml.replace(/>/g, ">"); var header = "{{Print version notice}}\n" + "__NOTOC__ __NOEDITSECTION__\n" + "<br style=\"page-break-after: always\">\n"; if(pv.bookName != "") { header = header + "{{:" + pv.bookName + "/Cover}}\n" + "<br style=\"page-break-after: always\">\n"; } xml = header + xml + "\n= GNU Free Documentation License =\n\n{{:GFDL|=}}\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(/>/g, ">"); pv.text = pv.text.replace(/</g, "<"); wk.spanText(pv.statspan, "Successfully converted text. " + "Click <b>Display</b> to show the current text for copy+paste. " + "Click <b>Edit</b> 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, "<b>Print version text:</b><br>"); if(pv.getBookName() != "") { wk.appendSpanText(pv.statspan, "Copy the text below into " + "[[<a href=\"/w/index.php?title=" + pv.bookName.replace(/ /g, "_") + "/Print_Version&action=edit\">" + pv.bookName + "/Print Version</a>]] " + "<br><small><font color=\"#FF0000\" align=\"center\">Warning</font>: " + "this text may need to be edited manually to remove unwanted " + "templates, categories, links or odd formatting that the tool " + "cannot account for.</small><br>"); } wk.spanText(pv.textspan, "<pre style=\"overflow: auto;\">" + pv.htmltext + "</pre>"); } 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 <b>Save Page</b>."); }