Last active
August 2, 2016 11:48
-
-
Save fotcorn/0228d397128e641cb24d406bfd0ccbc0 to your computer and use it in GitHub Desktop.
Add a HTML comment on every Django/Jinja2 {% block %} template tag to find out from which file a rendered output is coming from
This file contains hidden or 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
#!/usr/bin/python3.5 | |
import glob | |
import re | |
def replace_generator(fn): | |
def replace(match): | |
return '{}\n<!-- file: {} block: {} -->\n'.format(match.group(0), fn, match.group(2)) | |
return replace | |
for filename in glob.iglob('./**/*.html', recursive=True): | |
try: | |
f = open(filename) | |
data = f.read() | |
f.close() | |
data = re.sub('({%-? block ([a-zA-Z0-9_\-]+) -?%})', replace_generator(filename), data) | |
data = '<!-- {} -->\n'.format(filename) + data | |
f = open(filename, 'w') | |
f.write(data) | |
f.close() | |
except: | |
import traceback | |
traceback.print_exc() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment