Skip to content

Instantly share code, notes, and snippets.

@jleclanche
Last active August 29, 2015 14:21
Show Gist options
  • Save jleclanche/56ee788efe699578252a to your computer and use it in GitHub Desktop.
Save jleclanche/56ee788efe699578252a to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import re
import sys
REGEX = re.compile(r"^RewriteRule ([^ ]+) (.+?) \[([^\]]+)]")
def process_line(line):
if not line:
return
sre = REGEX.match(line)
assert sre, line
source, target, flags = sre.groups()
flags = flags.split(",")
return source, target, flags
def main():
with open(sys.argv[1], "r") as f:
for line in f.readlines():
if line.startswith("RewriteRule "):
source, target, flags = process_line(line.strip())
print(source, target, flags)
is_regex = ".+" in source
if not is_regex:
if source.startswith("^"):
source = source[1:]
if source.endswith("$"):
source = source[:-1]
source = source.replace("\\", "")
if source.startswith("(?i)"):
# case insensitive
source = source[4:]
if source.endswith("(?-i)"):
source = source[:-5]
print("Source is now %r" % (source))
if target.startswith("{{ url }}"):
...
else:
... # good luck
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment