Jump to content

User:Ais523/sandbox.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Ais523 (talk | contribs) at 11:25, 12 October 2007 (rv self; the script's done its job now). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// Signature optimizing script

//<pre><nowiki>
//<![CDATA[>

/* AJAX string rendering script; based on the public-domain [[User:Alex Smotrov/qpreview.js]] */

/* Written in Continuation Passing Style, because it's asynchronous */
function soAJAXRenderWikimarkup(markup, callback){
  var a = sajax_init_object();
  a.open('POST', wgServer+wgScriptPath+'/index.php?title=Wikipedia:Sandbox&action=submit&live',
         true);
  var Boundary = '--------p1415';
  a.setRequestHeader('Content-Type', 'multipart/form-data; boundary='+Boundary);
  var PostData = '--' + Boundary 
                      + '\nContent-Disposition: form-data; name="wpTextbox1"\n\n'
                      + markup + '\n--'+Boundary;
  if (a.overrideMimeType) a.overrideMimeType('text/html');
  a.send(PostData);
  a.onreadystatechange = function(){
    if (a.readyState != 4) return;
    var html = a.responseText;
    html = html.replace(/&gt;/g,'>').replace(/&lt;/g,'<')
               .replace(/&quot;/g,'"').replace(/&amp;/g,'&')
               .replace(/&apos;/g,"'").replace(/\n/g," ")
               .replace(/\r/g," ").replace(/\>\ \ +\</g,"> <");
    html = html.match(/\<\/strong>\ ?\<\/p\>\ ?\<\/div\>(.*)\<\/preview\>/)[1];
    (callback)(html);
  };
}

function htmlspecialchars(html)
{
  return html.replace(/\&/g,'((((ampersand))))amp;').replace(/\>/g,'&gt;')
             .replace(/\</g,'&lt;').replace(/\"/g,'&quot;')
             .replace(/\'/g,"&apos;").replace(/\(\(\(\(ampersand\)\)\)\)/g,"&");
}

function soAToWikilink(html)
{
  while(html.indexOf("<a ")!=-1)
  {
    var m=html.match
(/^(.*)\<a\ href\=\"(\/wiki\/|\/w\/index\.php\?title=)(.*?)(\&amp;action\=edit)?\"[^\>]*?\>(.*?)\<\/a\>(.*)$/);
    if(m[3].indexOf("Category:")==0||m[3].indexOf("Image:")==0) m[3]=":"+m[3];
    if(m==null)
    { //external link needed; false-negatives here for unadorned edit links to existing pages,
      //but that's unlikely to come up because comment links are more useful for Talk links in sigs
      m=html.match(/^(.*)\<a\ href=\"(.*?)\"[^\>]*\>(.*?)\<\/a\>(.*)$/);
      if(m!=null) html=m[1]+'<!--extstart-->['+m[2]+' '+m[3]+']<!--extend-->'+m[4]; else break;
      // the comments here are recognized when making the signature compliant with [[WP:SIG]];
      // they're removed later in the process
    }
    else html=m[1]+'[['+decodeURI(m[3])+'|'+m[5]+']]'+m[6];
  }
  return html;
}

var soAsyncComms;

function sigOptimize3(policised)
{
  document.getElementById('wlrender').innerHTML=policised;
  document.getElementById('wlsig').innerHTML=htmlspecialchars(soAsyncComms);
  // The next step is to optimize the markup.
  // To do this, the markup is split into individual properties, and then various combinations of
  // them can be tested.

  var props=new Array();
  // Properties with their own tags (replace logical markup with physical as it's shorter)
  props['tag-b']='b'; props['tag-strong']='b'; props['style-font-weight:bold']='b'; //bold
  props['tag-cite']='cite'; // cite style
  props['tag-code']='code'; // code style
  props['tag-i']='i'; props['tag-em']='i'; props['tag-var']='i';
  props['style-font-style:italic']='i'; //italic
  props['tag-s']='s'; props['tag-del']='s'; props['tag-strike']='s';
  props['style-text-decoration:line-through']='s'; //strike
  props['tag-small']='small'; //small
  props['tag-sub']='sub'; //subscript
  props['tag-sup']='sup'; //superscript
  props['tag-u']='u'; props['tag-ins']='u'; props['style-text-decoration:underline']='u'; //underline
}

function sigOptimize2(tidied)
{
  document.getElementById('wikisigrender').innerHTML=tidied;
  tidied=soAToWikilink(tidied);
  document.getElementById('tidysig').innerHTML=htmlspecialchars(tidied);
  // remove things against WP:SIG:
  tidied=tidied.replace(/\<img [^\>]*\/\>/g,''); // Images (replace with nothing)
  // Block level and other undesired elements with content (replace with spans)
  var cb="big blockquote caption center dd div dl dt h1 h2 h3 h4 h5 h6 li ol p table td th tr ul"
         .split(' ')
  for(e in cb)
  {
    tidied=tidied.split('<'+cb[e]+' ').join('<span ')
                 .split('<'+cb[e]+'>').join('<span>')
                 .split('<'+cb[e]+'/>').join('<span/>') // hopefully this never comes up
                 .split('</'+cb[e]+'>').join('</span>')
  }
  // Other undesired elements without content (only <br> and <hr> at the moment)
  tidied=tidied.replace(/\<br [^\>]*\/\>/g,'');
  tidied=tidied.replace(/\<hr [^\>]*\/\>/g,'');
  // External links:
  // not http (the only whitelisted protocol starting with h AFAIK):
  tidied=tidied.replace(/\<\!\-\-extstart\-\-\>\[[^h].*?<\!\-\-extend\-\-\>/g,'');
  // not to en.wikipedia (a link back to the Main Page without the /wiki/ also isn't allowed, but
  // who'd want that in a sig?)
  tidied=tidied.replace(
/\<\!\-\-extstart\-\-\>\[http\:\/\/en\.([^w]|w[^i]|wi[^k]|wik[^i]|wiki[^p]|wikip[^e]|wikipe[^d]|wikiped[^i]|wikipedi[^a]|wikipedia[^\.]|wikipedia\.[^o]|wikipedia\.o[^r]|wikipedia\.or[^g]|wikipedia\.org[^\/]).*?<\!\-\-extend\-\-\>/g
                       ,'');
  // If a link pipes to nothing, it must have been an image, so remove it
  tidied=tidied.replace(/\[\[.*?\|\]\]/g,'');
  // Leading spaces
  tidied=tidied.replace(/^\ +/,'');
  soAsyncComms=tidied;
  soAJAXRenderWikimarkup(tidied, sigOptimize3);
}

function sigOptimize1()
{
  soAJAXRenderWikimarkup(document.getElementById('wikisig').value, sigOptimize2);
}

addOnloadHook(function()
{
  if(wgPageName == "User:Ais523/sigoptimize")
  {
    var bodyContent=document.getElementById('bodyContent');
    bodyContent.innerHTML="Wikimarkup you want your sig to be equivalent to (doesn't have to "+
                          "be valid signature markup; use ~~~ for your existing sig):<br />"+
                          "<input type='text' id='wikisig' value='~~~' style='width:80%' />"+
                          "<a href='javascript:sigOptimize1()'>Optimize</a><br />"+
                          "Your signature currently renders as:<br/>"+
                          "<div id='wikisigrender'><p><br/></p></div>"+
                          "Step 1: Tidying markup (correcting misnested tags, substing templates, "+
                          "etc.): "+
                          "<small>(extra &lt;p&gt;s and &lt;br/&gt;s may be added that will "+
                          "be removed later)</small><br/>"+
                          "<div><code id='tidysig'></code></div><p><br/></p>"+
                          "Step 2: Removing images, block-level markup, and other things not "+
                          "within the signature guidelines:<br/>"+
                          "<div><code id='wlsig'></code></div><div id='wlrender'><p><br/></p></div>";
  }
});
//<]]>
//</nowiki></pre>