User:Whiteknight/printversion2.js
Appearance
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.
Warning: Malicious code can compromise your account. Page preview will cause your web browser to execute this page's content as code under some skins, including Monobook. If you have any questions about any code you plan to add, you can ask at the appropriate reading room. |
Documentation for this script can be added at User:Whiteknight/printversion2. |
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,
"<form Name=\"WKPVFormB\" Action=\"\" Method=\"GET\">" +
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") +
"<span id=\"" + PVboxspan + "\" style=\"display: none;\">" +
"<Textarea name=\"WKPVListB\" rows=\"15\" cols=\"70\"></textarea>" +
"<br></span></form>");
}
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 <b>Convert</b> to prepare the print version. " +
"Click <b>Show</b> 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 ("{{Print chapter heading|" + subpage + "}}\n{{:" + page + "}}\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, "<");
xml = xml.replace(/>/g, ">");
var header = "{{Print version notice}}\n" +
"__NOTOC__ __NOEDITSECTION__\n" +
"<br style=\"page-break-after: always\">\n";
if(BookName != "") {
header = header + "{{:" + 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;
}
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(/>/g, ">");
text2 = text2.replace(/</g, "<");
setSpanText(PVstatspan, "Successfully converted text. " +
"Click <b>Show</b> to display and edit the current text. " +
"Click <b>Display</b> to show the current text for copy+paste. " +
"Click <b>Edit</b> 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 = "<b>Print version text:</b><br>";
if(BookName != "") {
PVstat.innerHTML = PVstat.innerHTML +
"Copy the text below into " +
"[[<a href=\"/w/index.php?title=" + BookNameE +
"/Print_Version&action=edit\">" + 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>";
}
setSpanText(PVtextspan, "<pre style=\"overflow: auto;\">" + text + "</pre>");
}
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 <b>Save Page</b>.");
PVtext = ClearSpan(PVtextspan);
PVtext.appendChild(form);
}
if(typeof WhiteknightGadgets != 'function') {
import_script('User:Whiteknight/gadgetscore.js');
}
$(WKPVCreateForm);