Created
October 8, 2015 07:01
-
-
Save 1e0ng/f9e2196a412402d2c96d 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 | |
FILE_PATH = 'nginx-rewrite.txt' | |
RR = 'RewriteRule' | |
def relate(r): | |
ans = re.findall(r'rewrite\s*([^\s]+?)\s*([^;\s]+?);', r) | |
assert len(ans) == 1 | |
return ans[0] | |
def solve(l, rs): | |
rs = re.findall(r'[^\n]+', rs, re.I|re.M) | |
if l.startswith('='): | |
l = l[1:].strip() | |
assert len(rs) == 1 | |
assert l[0] == '/' | |
f, t = relate(rs[0]) | |
print RR + ' ^' + l[1:] + '$ ' + t + ' [QSA, L]' | |
else: | |
for r in rs: | |
f, t = relate(r) | |
assert f != '^(.*)$' | |
f = re.sub(r'^\^/', r'^', f) | |
print RR + ' ' + f + ' ' + t + ' [QSA, L]' | |
def main(): | |
f = open(FILE_PATH).read() | |
for l in re.findall(r'location\s*([^{]+?)\s*{([^}]+)}', f, re.I|re.M): | |
solve(*l) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi