User:Quarl/coor canonicalize.js
From Wikipedia, the free encyclopedia
If a message on your talk page led you here, please be wary of who left it. Code that you insert on this page could contain malicious content capable of compromising your account. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. If this is a .js page, the code will be executed when previewing the page.
Note: After saving, you have to bypass your browser's cache to see the changes. In Internet Explorer and Firefox, hold down the Ctrl key and click the Refresh or Reload button. Opera users have to clear their caches through Tools→Preferences, see the instructions for Opera. Konqueror and Safari users can just click the Reload button.
// [[User:Quarl/coor_canonicalize.js]] - canonicalizes geographical coordinates // Example: 59° 55′ N, 10° 44′ E becomes {{coor dm|59|55|N|10|44|E|}} // requires: wikipage.js, util.js, addlilink.js // quarl 2006-02-01 initial version //<pre><nowiki> coorz = new Object(); var coorz = new autoedit( 'coorz', 'CoorZ', 'ca-coorz', 'Canonicalize geographic coordinates', 'Coor canonicalization'); coorz.buildRegExp = function() { var sp = function(s) { return ' *' + s + ' *'; } var d = sp("(?:°|°)"); var m = sp("(?:′|'|′|′)"); var s = sp('(?:″|"|″|″)'); var C = '(-?[0-9.]+)'; var dms = C + d + '(?:' + C + m + '(?:' + C + s + ')?' + ')?'; var all = '\\b' + dms + '\\s*(N|S|[Nn]orth|[Ss]outh)[ \t,]*' + dms + '\\s*(E|W|[Ee]ast|[Ww]est)' + '\\b'; return new RegExp(all); } coorz.replaceRegExp = function(d, m) { var latitude = [m[1], m[2], m[3]]; var latitude_pole = m[4][0].toUpperCase(); var longitude = [m[5], m[6], m[7]]; var longitude_pole = m[8][0].toUpperCase(); if (latitude[2] || longitude[2]) { // dms return ('{{coor dms|' + latitude[0] + '|' + latitude[1] + '|' + latitude[2] + '|' + latitude_pole + '|' + longitude[0] + '|' + longitude[1] + '|' + longitude[2] + '|' + longitude_pole + '|}}'); } else if (latitude[1] || longitude[1]) { // dm return ('{{coor dm|' + latitude[0] + '|' + latitude[1] + '|' + latitude_pole + '|' + longitude[0] + '|' + longitude[1] + '|' + longitude_pole + '|}}'); } else if (latitude[0] || longitude[0]) { // d return ('{{coor d|' + latitude[0] + '|' + latitude_pole + '|' + longitude[0] + '|' + longitude_pole + '|}}'); } else { alert ("## internal error a86f430f-f362-4324-b8ce-df5f84e8f65b"); return null; } } coorz._load = function() { coorz.addTab(); } addOnloadHook(coorz._load); //</nowiki></pre>

