//Pages class, to contain information about listed pages.
function BookPage(name) {
this.pagename = name;
this.fullname = "";
this.headings = new Array();
this.subpages = new Array();
}
BookPage.prototype.addHeading = function(heading){this.headings[this.headings.length] = heading;}
BookPage.prototype.addSubpage = function(subpage){this.subpages[this.subpages.length] = subpage;}
BookPage.prototype.makeNode = function() {
var box = wk.makeElement('div', null, [
wk.makeElement('b', null, [
document.createTextNode("- " + this.pagename)
]),
wk.makeElement('br')
]);
box.style.borderLeft = "1px solid #AAAAAA";
box.style.padding = "5px";
box.style.marginBottom = "1em";
var container = wk.makeElement('div', {style:"margin-left:2em"});
for(var i = 0; i < this.subpages.length; i++) {
var node = this.subpages[i].makeNode();
container.appendChild(node);
}
for(var i = 0; i < this.headings.length; i++) {
container.appendChild(document.createTextNode("== " + this.headings[i] + " =="));
container.appendChild(wk.makeElement('br'));
}
box.appendChild(container);
return(box);
}
//Book designer (bd) class
var bd = {
_require: 2.45,
text: null,
stat: null,
_form: null,
box: null,
formspan: "WKBDFormSpan",
boxspan: "WKBDFormSpanSub",
textspan: "WKBDSpan",
statspan: "WKBDStatSpan",
bookName: "",
links: "",
text:"",
templates: new Array(
["Simple Header", 0, 0],
["Page Nav Header", 1, 0],
["Page Nav Header2", 1, 0],
["Page List Header", 0, 1],
["Page List Nav", 1, 1]
),
pageTree: null
};
bd.createForm = function () {
var div = wk.spanText(bd.formspan, "");
if(div == null) return;
bd._editB = wk.makeButton("WKBDEditB", "Edit", bd.edit);
bd._displayB = wk.makeButton("WKBDDisplayB", "Display", bd.display);
bd._visualB = wk.makeButton("WKBDVisualB", "Visual", bd.visual);
bd._booknameB = wk.makeInput("WKBDBookNameB", "");
bd._form = wk.makeElement("Form", {name:"WKBDFormB", method:"GET"}, [
"Title: ",
bd._booknameB,
" ",
wk.makeButton("WKBDDesignB", "Design", bd.design),
" ",
bd._displayB,
bd._editB,
wk.makeButton("WKBDClearB", "Clear", bd.clear),
bd._visualB,
wk.makeElement("br"),
wk.makeButton("WKBDShowB", "Show", function(){wk.toggleDisplay(bd.boxspan, 'block');}),
wk.makeButton("WKBDHideB", "Hide", function(){wk.toggleDisplay(bd.boxspan, 'none');}),
" "
]);
wk.toggleDisable(bd._editB, true);
wk.toggleDisable(bd._displayB, true);
wk.toggleDisable(bd._visualB, true);
bd._tempTypeB = wk.makeElement("select", {id:"WKBDTempTypeB", name:"WKTempTypeB", size:"1"});
for(var i = 0; i < bd.templates.length; i++) {
bd._tempTypeB.appendChild(wk.makeElement("option", {value:i}, bd.templates[i][0]));
}
bd._automateB = wk.makeButton("WKBDAutomateB", "Automate", bd.automate);
bd._sourceB = wk.makeInput("WKBDSourceB", "");
bd._listB = wk.makeElement("Textarea", {name:"WKBDListB", rows:"15", cols:"50"});
bd._boxspanB = wk.makeElement("span", {id:bd.boxspan}, [
bd._listB,
wk.makeElement("br"),
"Load From: ",
bd._sourceB,
wk.makeButton("WKBDLoadB", "Load", bd.load),
wk.makeButton("WKBDStripB", "Strip", bd.strip),
wk.makeButton("WKBDSaveB", "Save", bd.save)
]);
wk.appendChildren(bd._form, [
bd._tempTypeB,
wk.makeButton("WKBDTemplateB", "Template", bd.template),
wk.makeButton("WKBDEditTemplateB", "Edit Template", bd.editTemplate),
bd._automateB,
wk.makeElement("br"),
bd._boxspanB
]);
wk.toggleDisable(bd._automateB, true);
wk.toggleDisplay(bd._boxspanB, "none");
div.appendChild(bd._form);
}
if(typeof wk == 'object' && wk.testVersion(bd._require)) {
addOnloadHook(bd.createForm);
bd.status = wk.spanEditor(bd.statspan);
}
bd.getForm = function() { return bd._form; }
bd.getBookName = function () {
bd.bookName = bd._booknameB.value
if(bd.bookName == "") return "";
var bn = bd.bookName.split(" ");
for(var i = 0; i < bn.length; i++) {
if(bn[i].length > 3)
bn[i] = bn[i].charAt(0).toUpperCase() + bn[i].slice(1);
}
bd.bookName = bn.join(" ");
bd._booknameB.value = bd.bookName;
return bd.bookName;
}
bd.getLoadPage = function() { return bd._sourceB.value; }
bd.clear = function() {
wk.spanText(bd.textspan, "");
bd.status("");
wk.toggleDisplay(bd.boxspan, "none");
bd._listB.value = "";
bd.links = "";
bd.text = "";
wk.toggleDisable(bd._displayB, true);
wk.toggleDisable(bd._editB, true);
wk.toggleDisable(bd._visualB, true);
}
bd.design = function() {
wk.toggleDisplay(bd.boxspan, "none");
wk.spanText(bd.textspan, "");
if(bd.getBookName() == "") {
wk.spanText(bd.statspan, "Must enter a valid book title to <b>Design</b>.");
return;
}
bd.pageTree = new BookPage(bd.bookName);
wk.toggleDisable(bd._displayB, false);
wk.toggleDisable(bd._editB, false);
wk.toggleDisable(bd._visualB, false);
if(wgUserName == "Whiteknight") wk.toggleDisable(bd._automateB, false);
bd.links = "<b>Pages to create:</b><ul>" +
"<li>[[" + wk.wikiLinkText(bd.bookName) + "]]" +
"<li>{"+"{" +
wk.wikiLinkText("Template:" + bd.bookName + "/Page", bd.bookName + "/Page") + "}}" +
"<li>[[" + wk.wikiLinkText(bd.bookName + "/Introduction") + "]]";
bd.text = "";
var pages = bd._listB.value.split("\n");
var preface = "";
var prereqs = new Array();
var tags = new Array();
var subjects = new Array();
var lastn = 0;
var last = new Array();
bd._pages = "";
for(i = 0; i < pages.length; i++) {
if(pages[i] == "") continue;
pages[i] = pages[i].replace(/^[ \t]+/, "");
pages[i] = pages[i].replace(/[ \t]+$/, "");
pages[i] = pages[i].replace(/\r\n/g, "");
if(pages[i].match(/^%/)) {
preface += pages[i].substr(1) + "\n\n";
} else if(pages[i].match(/^=/)) {
bd._pages += "\n=== " + pages[i].substr(1) + " ===\n\n";
} else if(pages[i].match(/^\/\//)) {
continue;
} else if(pages[i].match(/^&/)) {
prereqs.push(pages[i].substr(1));
} else if(pages[i].match(/^\+/)) {
tags.push(pages[i].substr(1));
} else if(pages[i].match(/^-/)) {
subjects.push(pages[i].substr(1));
} else if(pages[i].match(/^\*+/)) {
var stars = pages[i].match(/^\*+/);
var n = stars[0].length;
var k = n - 1;
if(last[k] == null) { k = 0; }
last[n] = new BookPage(pages[i].substr(n));
last[n].fullname = last[k].fullname + "/" + last[n].pagename;
last[k].addSubpage(last[n]);
lastn = n;
bd._pages += "*" + stars[0] + "[[" + last[n].fullname + "|" + last[n].pagename + "]]\n";
bd.links += "<li>[[" + wk.wikiLinkText(last[n].fullname, last[n].fullname) + "]]";
} else if(pages[i].match(/^@/)) {
last[lastn].addHeading(pages[i].substr(1));
} else {
bd._pages += "*[[" + bd.bookName + "/" + pages[i] + "|" + pages[i] + "]]\n";
bd.links += "<li>[[" +
wk.wikiLinkText(bd.bookName + "/" + pages[i], bd.bookName + "/" + pages[i]) + "]]";
last[0] = new BookPage(pages[i]);
last[0].fullname = bd.pageTree.pagename + "/" + last[0].pagename;
lastn = 0;
bd.pageTree.addSubpage(last[0]);
}
}
bd.text = "{"+"{New book}}\n" +
"{"+"{" + bd.bookName + "/Page}}";
for(i = 0; i < tags.length; i++) {
bd.text += "\n{"+"{" + tags[i] + "}}";
}
for(i = 0; i < prereqs.length; i++) {
bd.text += "\n{"+"{Prerequisite|" + prereqs[i] + "}}";
}
bd.text += "\n\n== Preface ==\n\n" +
preface +
"== Table of Contents ==\n\n" +
"*[[" + bd.bookName + "/Introduction|Introduction]]\n" +
bd._pages;
bd.text += "\n== Resources ==\n\n" +
"*[[" + bd.bookName + "/Resources|Resources]]\n" +
"*[[" + bd.bookName + "/Licensing|Licensing]]\n\n" +
"{"+"{Subject";
for(i = 0; i < subjects.length; i++) {
bd.text += "|" + subjects[i];
}
bd.text += "}}\n{"+"{Alphabetical|" + bd.bookName.substr(0,1) + "}}\n" +
"[[" + "Category:" + bd.bookName + "]]";
bd.links += "<li>[[" + wk.wikiLinkText(bd.bookName + "/Resources") + "]]" +
"<li>[[" + wk.wikiLinkText(bd.bookName + "/Licensing") + "]]" +
"<li>[[" + wk.wikiLinkText("Category:" + bd.bookName) + "]]" +
"\n</ul>";
bd.status("Successfully designed new book [[" +
wk.wikiLinkText(bd.bookName) + "]]. " +
"Click <b>Display</b> to view the new book text. " +
"Click <b>Edit</b> to display an edit window to create the page. " +
"Click <b>Template</b> to display the code for a navigation template. ");
}
bd.display = function() {
wk.toggleDisplay(bd.boxspan, "none");
wk.spanText(bd.textspan,
"<table width=\"100%\"><tr><td width=\"75%\" valign=\"top\">" +
"<pre style=\"overflow: auto;\">" +
bd.text + "</pre></td><td valign=\"top\">" +
bd.links + "</td></tr></table>");
}
bd.edit = function() {
if(bd.getBookName() == "") {
wk.spanText(bd.statspan, "Must enter a valid book name to <b>Edit</b>");
return;
}
wk.toggleDisplay(bd.boxspan, "none");
bd.status("Loading edit form...");
wk.loadEditPage(bd.bookName, bd._edit2);
}
bd._edit2 = function(xml) {
if(typeof cg == 'object') {
var div = wk.spanText(bd.textspan,
"<span id=\"" + cg.editspan + "\">" +
bd.bookName + "</span>");
cg.createForm2();
} else {
var div = wk.spanText(bd.textspan);
}
var editform = wk.showEditWindow(xml, bd.textspan);
editform.summary.value = bd.bookName + ": Created by Whiteknight's Book Designer";
editform.text.value = bd.text;
bd.status("Editing text for [[" + wk.wikiLinkText(bd.bookName) +
"]]. Edit the text below to remove any errors or artifacts " +
"of the design process. Once you have made your edits, " +
"click <b>Save Page</b>.");
}
bd.makeTemplate = function() {
var pages = bd._listB.value.split("\n");
var select = bd._tempTypeB;
var template = bd.templates[select.selectedIndex];
var temp = "{" + "{subst:User:Whiteknight/" + template[0] + "|" +
((template[1] == 1)?("book="):("")) + bd.bookName;
if(template[2] == 1) { //pagelist
temp += "|" + ((template[1] == 1)?("list="):("")) + "\n" +
"[[" + bd.bookName + "/Introduction|Introduction]] —\n";
for(i = 0; i < pages.length; i++) {
if(pages[i] == "") continue;
if(pages[i].match(/^[%=]/)) continue;
if(pages[i].match(/^\/\//)) continue;
if(pages[i].match(/^[-+]/)) {
continue; //used for setting options eventually.
}
pages[i] = pages[i].replace(/[ \t]+$/, "");
temp = temp + "[[" + bd.bookName + "/" + pages[i] + "|" + pages[i] + "]] —\n";
}
temp += "[[" + bd.bookName + "/Resources|Resources]] —\n" +
"[[" + bd.bookName + "/Licensing|Licensing]] —\n" +
"[[Talk:" + bd.bookName + "|Discuss]]";
}
return temp + "}}";
}
bd.template = function() {
if(bd.getBookName() == "") {
bd.status("Must enter a valid book name to <b>Template</b>.");
return;
}
wk.toggleDisplay(bd.boxspan, "none");
var temp = bd.makeTemplate();
var select = bd.getForm().WKBDTempTypeB;
var template = bd.templates[select.selectedIndex];
bd.status("This is the text of an example inter-page navigation template " +
"for the book " + bd.bookName +
". Forward and Back Links: <b>" + ((template[1])?("true"):("false")) +
"</b>. Page list: <b>" + ((template[2])?("true"):("false")) +
"</b>. Copy and Paste this code into {"+"{" +
wk.wikiLinkText("Template:" + bd.bookName + "/Page", bd.bookName + "/Page") +
"}} or {"+"{" +
wk.wikiLinkText("Template:" + bd.bookName, bd.bookName) + "}}."
);
wk.spanText(bd.textspan, "<pre style=\"overflow: auto;\">" + temp + "</pre>");
}
bd.load = function() {
if(bd.getLoadPage() == "") {
wk.spanText(bd.statspan, "Must enter valid page name to <b>Load</b>.");
return;
}
bd.status("Loading text from TOC...");
wk.loadWikiText(bd.getLoadPage(), function(text) {
bd._listB.value = text;
bd.status("Loaded text from TOC");
});
}
bd.strip = function() {
var pages = bd._listB.value;
pages = pages.replace(/<\/?nowiki>/g, "");
pages = pages.replace(/(===+)([^=\n]+)\1/g, "=$2");
pages = pages.replace(/\[\[\/([^\]]+)\/\]\]/g, "$1");
pages = pages.replace(/(\*|#)\W*/g, "");
pages = pages.replace(/\([^\)]*\)/g, "");
pages = pages.replace(/\{\{[^\}]+\}\}/g, "");
bd._listB.value = pages;
}
bd.editTemplate = function() {
if(bd.getBookName() == "") {
bd.status("Must enter a valid book name to <b>Edit Template</b>.");
return;
}
wk.toggleDisplay(bd.boxspan, "none");
bd.status("Loading template edit box...");
wk.loadEditPage("Template:" + bd.bookName + "/Page", bd._editTemplate2)
}
bd._editTemplate2 = function(xml) {
var editform = wk.showEditWindow(xml, bd.textspan);
editform.summary.value = "Template:" + bd.bookName +
"/Page: Creating nav template using Whiteknight's Book Designer";
editform.text.value = bd.makeTemplate();
bd.status("Editing text for {"+"{" +
wk.wikiLinkText("Template:" + bd.bookName + "/Page", bd.bookName + "/Page") +
"}}. Edit the text below to ensure the template is created correctly. " +
"Click <b>Save Page</b> when you are finished.");
}
bd.save = function() {
if(wgUserName == null) {
bd.status("You must be logged in to save an outline");
return;
}
if(bd.getBookName() == "") {
bd.status("Must enter a valid book name to <b>Save</b>.");
return;
}
wk.toggleDisplay(bd.boxspan, "none");
bd.status("Loading userpage edit box...");
wk.loadEditPage("User:" + wgUserName + "/" + bd.bookName, bd._save2);
}
bd._save2 = function(xml) {
var editform = wk.showEditWindow(xml, bd.textspan);
var time = editform.form.wpEdittime.value;
editform.summary.value =
"Saving incomplete book outline using Whiteknight's Book Designer Gadget";
editform.text.value = editform.text.value +
"\n== Outline (" + time + ") ==\n\n" +
":<small>This outline is used with [[User:Whiteknight|Whiteknight's]] " +
"[[User:Whiteknight/Book Creator|Book Creator]]</small>\n" +
" <nowiki>\n" +
bd._listB.value + "\n</nowiki>\n";
bd.status("Saving text to your userspace. Click <b>Save Page</b> when you are finished.");
}
bd.automate = function() {
//test function, will automatically create all necessary pages with default text.
//HIGHLY EXPERIMENTAL!
if(bd.getBookName() == "") {
bd.status("must enter a valid bookname to automate!");
return;
}
wk.toggleDisplay(bd.boxspan, "none");
bd.status("Automating book design process...");
wk.postEdit(bd.bookName, bd.text, "Creating book TOC");
wk.appendSpanText(bd.statspan, "<br>Created TOC");
wk.postEdit("Template:" + bd.bookName + "/Page", bd.makeTemplate(), "Automate: Creating book Template");
wk.appendSpanText(bd.statspan, "<br>Created Nav Template");
wk.postEdit(bd.bookName + "/Licensing",
"{"+"{" + bd.bookName + "/Page}}\n\n" +
"== License ==\n\n" +
"The text of this book is released under the following license:\n\n" +
"{"+"{GFDL}}",
"Automate: Adding Licensing Information");
wk.appendSpanText(bd.statspan, "<br>Created Licensing Page");
wk.postEdit(bd.bookName + "/Resources",
"{"+"{" + bd.bookName + "/Page}}\n\n" +
"== Wikimedia Resources ==\n\n" +
"=== Wikibooks ===\n\n=== Wikiversity Courses ===\n\n=== Wikipedia Articles ===\n\n" +
"== External Sources ==\n",
"Automate: Creating Resource Page");
wk.appendSpanText(bd.statspan, "<br>Created Resources Page");
wk.postEdit(bd.bookName + "/Introduction",
"{"+"{" + bd.bookName + "/Page}}\n\n" +
"== About This Book ==\n\n" +
"== Target Audience ==\n\n" +
"== Prerequisites ==\n\n" +
"== Corequisites ==\n\n",
"Automate: Creating Introduction Page");
wk.appendSpanText(bd.statspan, "<br>Created Introduction<br>Done.");
}
bd.visual = function() {
var box = wk.spanText(bd.textspan, "");
box.appendChild(bd.pageTree.makeNode());
}