#!/usr/bin/env python # encoding: utf-8 import urllib import re from xml.etree import ElementTree from xml.parsers.expat import ExpatError import htmlentitydefs import os import glob _UNESCAPE = re.compile(ur'&\w+?;', re.UNICODE) def unescape(s): if s is None: return '' def fixup(m): ent = m.group(0)[1:-1] return unichr(htmlentitydefs.name2codepoint[ent]) try: return _UNESCAPE.sub(fixup, s) except: print 'unescape failed: %s' % repr(s) raise class UnknownVariable(Exception): pass class UnsupportedVariableExpression(Exception): pass def replace_vars(m): """Replace vars in 'content' portion. :m: match object :returns: string """ var = m.group(1) default = m.group(2) if not re.match(r'\w+$', var): raise UnsupportedVariableExpression(var) translate_vars = { 'TM_PHP_OPEN_TAG_WITH_ECHO': 'g:UltiSnipsOpenTagWithEcho', 'TM_PHP_OPEN_TAG': 'g:UltiSnipsOpenTag', 'PHPDOC_AUTHOR': 'g:snips_author', } # TODO: TM_SELECTED_TEXT/([\t ]*).*/$1/m if var in translate_vars: newvar = translate_vars[var] else: # TODO: this could be autogenerated raise UnknownVariable(var) return "`!v exists('%s') ? %s : '%s'`" % (newvar, newvar, default) def parse_content(c): try: data = ElementTree.fromstring(c)[0] rv = {} for k, v in zip(data[::2], data[1::2]): rv[k.text] = unescape(v.text) if re.search(r'\$\{\D', rv['content']): rv['content'] = re.sub( r'\$\{([^\d}][^}:]*)(?::([^}]*))?\}', replace_vars, rv['content']) return rv except (ExpatError, ElementTree.ParseError) as detail: print ' Syntax Error: %s' % (detail,) print c return None except UnknownVariable as detail: print ' Unknown variable: %s' % (detail,) return None except UnsupportedVariableExpression as detail: print ' Unsupported variable expression: %s' % (detail,) return None def fetch_snippets_from_svn(name): base_url = 'http://svn.textmate.org/trunk/Bundles/' + name + '.tmbundle/' snippet_idx = base_url + 'Snippets/' idx_list = urllib.urlopen(snippet_idx).read() rv = [] for link in re.findall('