Skip to content

Instantly share code, notes, and snippets.

@mvasilkov
Created November 6, 2013 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mvasilkov/7341556 to your computer and use it in GitHub Desktop.
Save mvasilkov/7341556 to your computer and use it in GitHub Desktop.
Helper tool for Box2D-HTML5 conversion. Fucking kill Google, one Closure Library at a time.
import re
import sys
def replacer_pro(match):
print match.group(1)
parts = match.group(1).split('.')
init_code = ''
cur = ''
for part in parts:
if cur:
cur += '.' + part
else:
cur = part
init_code += """if (typeof NAME === 'undefined')
NAME = {}""".replace('NAME', cur) + '\n'
if init_code:
init_code = '\n' + init_code
return '// -removed- ' + match.group(0) + init_code
def replacer_req(match):
print match.group(1)
name = match.group(1)
init_code = """if (typeof NAME === 'undefined')
throw Error('I can haz NAME?')""".replace('NAME', name) + '\n'
init_code = '\n' + init_code
return '// -removed- ' + match.group(0) + init_code
def wrap(filename):
print '- %s' % filename
with open(filename) as f:
text = f.read()
text = re.sub(r"^goog\.provide\('(.+?)'\);?",
replacer_pro, text, flags=re.M)
text = re.sub(r"^goog\.require\('(.+?)'\);?",
replacer_req, text, flags=re.M)
with open(filename, 'wb') as f:
f.write(text)
if __name__ == '__main__':
for a in sys.argv[1:]:
wrap(a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment