/**
* @title 住所をリンク
* @description Add link to google maps japan on address-like texts
* @include http://*
* @license MIT License
* @version 20070304
* @see http://nonn-et-twk.net/twk/node/51
* @see http://d.hatena.ne.jp/shinichitomita/20060713/1152800000
*/
function AddressLink(node) {
if (!node) node = document.body;
if (isNaN(arguments.callee.count)) arguments.callee.count = 0;
var NODE_TYPE = {TAG:1, TEXT:3};
var splitNodeNum = 0;
if (node.nodeType == NODE_TYPE.TAG && node.childNodes)
{
var childNodes = node.childNodes;
for (var i = 0; i < childNodes.length; ++i)
i += arguments.callee(childNodes[i]);
}
else if (node.nodeType == NODE_TYPE.TEXT)
{
var text = node.data;
var re = arguments.callee.re;
var matched = text.match(re.general);
if (matched)
{
var m = matched[0];
var ws = m.match(re.prews);
if (ws)
m = ws[1];
// remove building names since google maps rarely recognize them
var building = m.match(re.building1);
if (building)
m = building[0];
var building2 = m.match(re.building2);
if (building2)
m = building2[1];
// split at first matched place
var matchedNode = node.splitText(text.indexOf(m));
MakeLink(matchedNode, m);
++splitNodeNum;
++arguments.callee.count;
}
}
return splitNodeNum;
function MakeLink(matchedNode, matchedText)
{
matchedNode.splitText(matchedText.length);
var anchorNode = document.createElement('A');
anchorNode.title = 'Googleマップで開く';
anchorNode.href = 'http://maps.google.com/?hl=ja&q='
+ encodeURI(matchedNode.data.replace(/[\s ::、。]/, ''));
anchorNode.appendChild(matchedNode.cloneNode(true));
matchedNode.parentNode.replaceChild(anchorNode, matchedNode);
}
} // AddressLink
// initialize
(function (){
var WHITESPACE_ex = /[\s ::()\[\][]「」()、。]/.source;
var NO_WHITESPACE_ex = /[^\s ::()\[\][]「」()、。]/.source;
var TODOFUKEN_ex = /(?:東京都|北海道|(?:大阪|京都)府|(?:神奈川|和歌山|鹿児島|.{2})県)(?![\d0-9])/.source;
var CITY_SEPARATE_CHAR_ex = new RegExp('(?:^|' + WHITESPACE_ex + ')').source;
var SHICHOSON_ex = new RegExp(NO_WHITESPACE_ex + '{1,4}[郡市区]').source;
var AZA_ex = NO_WHITESPACE_ex + '{1,20}';
var BANCHI_ex = /[\d0-9]/.source + '{1,8}'; // 一二三四五六七八九十東西南北無ABC
var KYOTO_ex = /京都[府市][^\s]{6,20}(?:[上下][るル])?(?:(?:東入|西入)?[るル]?|入中)/.source;
var PRE_WHITESPACES_re = new RegExp('^' + WHITESPACE_ex + '+(.*)$');
var BUILDING1_re = /^[^\d0-9]+[\d0-9]+(?:(?:[-―-ー]|丁目|番地?|号)[\d0-9]+)+/;
var BUILDING2_re = /^(.*(?:丁目|番地?|号))[^地\d0-9]+(?:ビル|$)/;
AddressLink.re = {
general: new RegExp(
'(?:' + TODOFUKEN_ex + '|' + CITY_SEPARATE_CHAR_ex + SHICHOSON_ex + ')' + '[ \t ]*' +
'(?:' + AZA_ex + '[ \t ]*' +
'(?:' + BANCHI_ex + '(?:丁目|番地?|号)?[-―-ー]?' + '){1,8}' +
')' +
'|' + KYOTO_ex
)
, prews: PRE_WHITESPACES_re
, building1: BUILDING1_re
, building2: BUILDING2_re
};
// prompt('',AddressLink.re.prews.source);
})();
AddressLink();