跳至内容

MediaWiki:Gadget-wkprintversion.js

来自维基教科书,为开放世界开放书籍
注意: 保存后,更改可能不会立即发生。单击此处了解如何绕过浏览器的缓存。
  • Mozilla/Firefox/Safari: 按住Shift键同时单击重新加载,或按下Ctrl-Shift-R(Apple Mac 上为Cmd-Shift-R);
  • Internet Explorer: 按住Ctrl键同时单击刷新,或按下Ctrl-F5
  • Konqueror: 直接单击重新加载按钮,或按下F5
  • Opera用户可能需要在工具→首选项中彻底清除其缓存。
//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 <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, "&lt;");
  xml = xml.replace(/>/g, "&gt;");
  var header = "{{Print version notice}}\n" +
               "__NOTOC__ __NOEDITSECTION__\n" +
               "&lt;br style=\"page-break-after: always\"&gt;\n";
  if(pv.bookName != "") {
    header = header + "{{:" + pv.bookName + "/Cover}}\n" +
                      "&lt;br style=\"page-break-after: always\"&gt;\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(/&gt;/g, ">");
  pv.text = pv.text.replace(/&lt;/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>.");
}
华夏公益教科书