Skip to content

Instantly share code, notes, and snippets.

@acspike
Created May 22, 2014 01:26
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 acspike/0fef59f7f29ce9d75f1f to your computer and use it in GitHub Desktop.
Save acspike/0fef59f7f29ce9d75f1f to your computer and use it in GitHub Desktop.
import re
tag = re.compile(r'\{% ?([^ ]+(?: [^ ]+)*) ?%\}')
rules = {
'static': lambda x: 'statistically',
'page': lambda x: 'paging '+x[0]+' up to',
}
def replace(m):
args = m.groups()[0].split(' ')
cmd = args.pop(0)
try:
return rules[cmd](args)
except:
return ''
if __name__ == '__main__':
corpus ='''
lary
This is text and {% static %} is right here.
stuffy
url = "{% page what %}">/></>
stuff {% lastly one two three %}
'''
tests = [
'{% static %}',
'{% page what %}',
'{%static %}',
'{% static%}',
'{%static%}',
'{% static one two %}',
'{%static one two %}',
'{% static one two%}',
'{%static one two%}',
'{ static one two %}',
]
for x in tests:
print 'pass:' if tag.match(x) else 'fail:', x
print tag.sub(replace, corpus)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment