I'm trying to write a large python/bash script which converts my html/css mockups to Shopify themes. One step in this process is changing out all the script sources. For instance:
<script type="text/javascript" src="./js/jquery.bxslider.min.js"></script>
becomes
<script type="text/javascript" src="{{ 'jquery.bxslider.min.js' | asset_url }}"></script>
Here is what I have so far:
import re
test = """
<script type="text/javascript" src="./js/jquery-1.8.3.min.js"></script>
<!--<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>-->
<script type="text/javascript" src="./js/ie-amendments.js"></script>
<script type="text/javascript" src="./js/jquery.bxslider.min.js"></script>
<script type="text/javascript" src="./js/jquery.colorbox-min.js"></script>
<script type="text/javascript" src="./js/main.js"></script>
"""
out = re.sub( 'src=\"(.+)\"', 'src="{{ \'\\1\' | asset_url }}"', test, flags=re.MULTILINE )
out
prints out
'\n <script type="text/javascript" src="{{ \'./js/jquery-1.8.3.min.js\' | asset_url }}"></script>\n <!--<script src="{{ \'http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript\' | asset_url }}"></script>-->\n <script type="text/javascript" src="{{ \'./js/ie-amendments.js\' | asset_url }}"></script>\n <script type="text/javascript" src="{{ \'./js/jquery.bxslider.min.js\' | asset_url }}"></script>\n <script type="text/javascript" src="{{ \'./js/jquery.colorbox-min.js\' | asset_url }}"></script>\n <script type="text/javascript" src="{{ \'./js/main.js\' | asset_url }}"></script>\n'
I have two problems so far:
Some of the backslash characters I'm using to escape the single quotes within my regexes are showing up in the output.
My capture group is capturing the entire original source string, but I just need what comes after the last "/"
ANSWER: Per Martijn Pieters helpful suggestion, I checked out the ? regular expression operator, and came up with this solution, which perfectly solved my problem. Also, for the replacement expression, I encapsulated it in double-quotes as opposed to singles, and escaped the doubles, which ended up removing the unnecessary backslashes. Thanks guys!
re.sub( r'src=".+?([^/]+?\.js)"', "src=\"{{ '\\1' | asset_url }}\"", test, flags=re.MULTILINE )