from genshi.builder import Element, tag from genshi.core import Markup from trac.core import * from trac.wiki.api import IWikiMacroProvider, WikiSystem, parse_args from trac.wiki.model import WikiPage import inspect import re import wikiaddress, geocode __all__ = ['AddressMacro'] _API_KEY = 'PUT YOUR KEY HERE' class AddressMacro(Component): """ Creates a Google map for the supplied address and optional location. This map is stored in the trac database and associated with the page. The rendered map shows other addresses in proximity. {{{ [[Address("123 Any Street, Anytown, NY 14450","Some Location")]] [[Address("123 Any Street, Anytown, NY 14450")]] }}} """ implements(IWikiMacroProvider) def get_macros(self): yield 'Address' def get_macro_description(self, name): return inspect.getdoc(AddressMacro) def apiURL(self,api_key): ''' Create the API key for Google Maps ''' return "\n"%(api_key) def basemap(self): ''' Render JavaScript that is common to every map ''' out = '''\ ''' return out def singlemap(self,name,place,loctag,location,nearby): ''' Create a string containig javascript for google map place = place object of center of map loctag = location tag name for this point location = location name for this point nearby = dictionary of nearby places ''' out = '''\ '''%(loctag) return out def expand_macro(self, formatter, name, txt): r1 = re.compile(r'^\s*\"(.+)\"\s*,\s*\"(.+)\"\s*$') if r1.search(txt): (address,location) = r1.search(txt).groups() else: address = txt.strip('"') location = None req = formatter.req author = req.authname ipnr = req.remote_addr resource = formatter.resource page = WikiPage(self.env,resource) name = page.name db = self.env.get_db_cnx() place = wikiaddress.wikiaddress(db,name,_API_KEY,author, ipnr,address,location) if place.lat is None: return address if location is None: loctag = 'l' else: loctag = 'l_'+location.replace(' ','') nearby = place.getNearby() # use the req object to store whether the base javascript has # been rendered. don't do it twice out = '' if not hasattr(req,'addressmacro'): req.addressmacro = True out += self.apiURL(_API_KEY) out += self.basemap() out += self.singlemap(name,place,loctag,location,nearby) out += address+" "+" [View Map]"+"" out += "
" return out