Skip to content

Instantly share code, notes, and snippets.

@jennyq
Created April 21, 2016 15:42
Show Gist options
  • Save jennyq/c36437ef28b994b5e925aec2ce3a6a5b to your computer and use it in GitHub Desktop.
Save jennyq/c36437ef28b994b5e925aec2ce3a6a5b to your computer and use it in GitHub Desktop.
""" url_converter.py
Convert url tags to have "'" included.
Usage: Copy the script to your project root,
specify your theme name, then run
python url_converter.py
"""
import os
import re
# specify your theme name here. example: theme_name = 'mytheme'
theme_name = ""
PROJECT_ROOT = os.path.abspath(os.path.split(os.path.split(__file__)[0])[0])
theme_dir = os.path.join(PROJECT_ROOT, 'themes/%s/templates' % theme_name)
print theme_dir
def convert_urls(directory):
# Update {% url xxx to {% url 'xxx'
for root, dirs, files in os.walk(directory):
for name in files:
if name.endswith('.txt') or name.endswith('.html'):
path = os.path.join(root, name)
with open(path) as f:
s = f.read()
s = re.sub(r'{%\s+url\s+([\w_\.]+)\s+', r"{% url '\1' ", s)
with open(path, "w") as f:
f.write(s)
for dname in dirs:
convert_urls(dname)
convert_urls(theme_dir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment