Created
May 11, 2012 15:57
-
-
Save Sannis/2660605 to your computer and use it in GitHub Desktop.
WTF Wikipedia anchors encoding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Wikipedia link cleanup | |
(function (wpl) { | |
if (wpl) { | |
function decodeWikiURL(link) { | |
function deChar(ss) { | |
return decodeURIComponent(ss.replace(/\.([0-9A-F][0-9A-F])/g, '%$1')); | |
} | |
function deChar2(hhh) { | |
var ch = deChar(hhh); | |
return ('!"#$%&\'()*+,/;<=>?@\\^`~'.indexOf(ch) >= 0) | |
? ch | |
: hhh; | |
} | |
// simplify internal link: replace %20 and _ then decode anchor | |
link = link | |
.replace(/(_|%20)/g, ' ') | |
.replace(/^ +| +$/g, '') | |
.replace('http://', ''); | |
var parts = link.split('#'); | |
// no anchor | |
if (parts.length != 2) { | |
return deChar(link); | |
} | |
var anchor = parts[1] | |
.replace( | |
/\.F[0-4]\.[89AB][\dA-F]\.[89AB][\dA-F]\.[89AB][\dA-F]/g, | |
deChar) | |
.replace( | |
/\.E[\dA-F]\.[89AB][\dA-F]\.[89AB][\dA-F]/g, | |
deChar) | |
.replace( | |
/\.[CD][\dA-F]\.[89AB][\dA-F]/g, | |
deChar) | |
.replace( | |
/\.[2-7][0-9A-F]/g, | |
deChar2); | |
return (deChar(parts[0]) + ' : ' + anchor); | |
} | |
wpl.innerHTML = decodeWikiURL(wpl.href); | |
} | |
})(id('wikipedia-article')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment