Skip to content

Instantly share code, notes, and snippets.

@datamafia
Created January 10, 2015 00:17
Show Gist options
  • Save datamafia/baba53f5de148fd90c0f to your computer and use it in GitHub Desktop.
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.
#!/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('{{','&#123;&#123;') ,s)
s = p.sub(lambda match: match.group(0).replace('}}','&#125;&#125;') ,s)
s = p.sub(lambda match: match.group(0).replace('{%','&#123;&#37;') ,s)
s = p.sub(lambda match: match.group(0).replace('%}','&#37;&#125;') ,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