Skip to content

Instantly share code, notes, and snippets.

@Sannis
Created May 11, 2012 15:57
Show Gist options
  • Save Sannis/2660605 to your computer and use it in GitHub Desktop.
Save Sannis/2660605 to your computer and use it in GitHub Desktop.
WTF Wikipedia anchors encoding
// 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