Skip to content

Instantly share code, notes, and snippets.

@zhuth
Created November 16, 2014 04:37
Show Gist options
  • Save zhuth/387fdd6ff3331bbfff77 to your computer and use it in GitHub Desktop.
Save zhuth/387fdd6ff3331bbfff77 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
#coding: utf-8
globals = {}
exec_snippet_counter = 0
def exec_snippet(ll):
global globals, exec_snippet_counter
x = ''
exec_snippet_counter += 1
argname = 'argname__%d' % exec_snippet_counter
for arg in ll['args']:
if arg['type'] == 'line':
x += arg['value'] + '\n'
globals[argname] = x
exec (ll['name'] + '(%s)' % argname) in globals
root = {'type': 'root', 'name': '', 'args': []}
cl = root
while 1:
try:
l = raw_input()
if l[:6] != '<!--py':
cl['args'].append({'type': 'line', 'value': l})
continue
if l[6] == '\\':
cl['args'].append({'type': 'command', 'name': l[7:-3].strip(), 'args': []})
cl = cl['args'][-1]
elif l[6] == '/':
cl = root
elif l[6] == ':':
defs = '#coding: utf-8\n'
while 1:
l = raw_input()
if l.strip() == '-->':
break
defs += l + '\n'
exec defs in globals
except EOFError:
break
for _ in root['args']:
if _['type'] == 'line':
print _['value']
else:
exec_snippet(_)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment