မီႇတီႇယႃႇဝီႇၶီႇ:Gadget-wkprintversion.js

မၢႆတွင်း: ဝၢႆးသေသိမ်းပၼ်ယဝ်ႉ၊ တွၼ်ႈတႃႇ ၸဝ်ႈၵဝ်ႇ တေႁၼ်လႆႈ လွင်ႈလႅၵ်ႈလၢႆႈၼၼ်ႉ ၸဝ်ႈၵဝ်ႇတေၸၢင်ႈလႆႈလတ်းၶၢမ်ႈ ၶႅတ်ႉၶျ် တူဝ်ပိုတ်ႇဝႅပ်ႉၸဝ်ႈၵဝ်ႇယဝ်ႉ။
  • ၽွင်းမိူဝ်ႈတိုၵ်ႉၼဵၵ်း Reload တီႈ Firefox / Safari: ၼၼ်ႉ ၼဵၵ်းဝႆႉပႃး Shift ၊ဢမ်ႇၼၼ် ၼဵၵ်းပၼ် Ctrl-F5 ဢမ်ႇၼၼ် Ctrl-R (တီႈၼႂ်း Mac ၼႆ ၼဵၵ်းပၼ်⌘-R)
  • တီႈၼႂ်း Google Chrome: ၼဵၵ်းပၼ် Ctrl-Shift-R (တီႈၼႂ်း Mac ၼႆႉ ၼဵၵ်းပၼ်⌘-Shift-R )
  • ၽွင်းမိူဝ်ႈ တိုၵ်ႉၼဵၵ်း Refreshတီႈ Internet Explorer/ Edge: ၼဵၵ်းဝႆႉပၼ် Ctrl ဢမ်ႇၼၼ် ၼဵၵ်းပၼ် Ctrl-F5
  • တီႈၼႂ်း Opera: ၵႂႃႇၸူးတီႈ Menu → Settings (ပေႃးပဵၼ်တီႈၼႂ်း Mac ၸိုင် Opera → Preferences ) သေ သိုပ်ႇၵႂႃႇ Privacy & security → Clear browsing data → Cached images and files ၼၼ်ႉလႄႈ။
//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>.");
}