Created
May 22, 2014 01:26
-
-
Save acspike/0fef59f7f29ce9d75f1f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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