Created
January 10, 2015 00:17
-
-
Save datamafia/baba53f5de148fd90c0f to your computer and use it in GitHub Desktop.
Group match and replace to use liquid and similar template systems to document syntax.
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/python | |
# Problem : Need to present {{, }}, {%, and %} in html document without | |
# triggering parser | |
# Solution : Simple python script to open a file and replace {{,}},{& and %} with | |
# HTML escaped characters inside pre tags. Key to documenting liquid and other | |
# template systems using that very template system for rendering | |
# Handles newline as a character / able to operate w/line breaks | |
# datamafia.com likes Python | |
import re | |
file_loc = 'theme.liquid' # where is the target file? | |
f = open(file_loc, 'r+') | |
s = f.read() | |
p = re.compile(r"(<pre)(.*?)(</pre)", re.DOTALL) | |
# extra verbose to allow for easy tweaks | |
s = p.sub(lambda match: match.group(0).replace('{{','{{') ,s) | |
s = p.sub(lambda match: match.group(0).replace('}}','}}') ,s) | |
s = p.sub(lambda match: match.group(0).replace('{%','{%') ,s) | |
s = p.sub(lambda match: match.group(0).replace('%}','%}') ,s) | |
f.seek(0) | |
f.write(s) | |
f.truncate() | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment