Created
April 21, 2016 15:42
-
-
Save jennyq/c36437ef28b994b5e925aec2ce3a6a5b 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
""" 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