pythonhtmlmustachemandrill

Find & Replace python script


I have a python script to change the syntax of an html file. To be specific, I want to convert the html file from mandrill syntax to mustache syntax. The script I currently have works well with changing all the syntax except one.

I have several lines of code with syntax similar to this

<tr mc:edit="MerchantCustomHeader"></tr>

the syntax above is mandrill and the equivalent mustache syntax would be.

{{MerchantCustomHeader}}

The issue I'm having is making this change in the entire html file. Here is an example

original_text = 'font mc:edit="OrderReceiveDate" blah blah this is a test so i do not know what to "really" do bbbecause i am just writing this "OrderReceiveDate" mc:edit="OrderReceiveDate"'
replacement_text = original_text.replace('mc:edit="', '{{').replace('"', '}}', 1)
print(replacement_text)

With the code above, this is the output i get:

font {{OrderReceiveDate}} blah blah this is a test so i do not know what to "really" do bbbecause i am just writing this "OrderReceiveDate" {{OrderReceiveDate"

The changes are only applied to the first instance of mc:edit, in the second instance, mc:edit is rightfully replaced with the double curly braces '{{' but the double quotation is not replaced with the closing curly braces. I only want the quotation to be replaced with the closing curly braces if it is part of the mc:edit tag, like this mc:edit="OrderReceiveDate" not if it is just in double quotation like this "OrderReceiveDate" Is there a mistake I am making in my script that the. replace function is not being applied to all instances of mc:edit?

I have placed what I tried and expected to happen above.


Solution

  • Try RegEx:

    pattern = r'mc:edit="([^"]+)"'
    replacement_text = re.sub(pattern, r'{{\1}}', original_text)
    

    Output:

    font {{OrderReceiveDate}} blah blah this is a test so i do not know what to "really" do bbbecause i am just writing this "OrderReceiveDate" {{OrderReceiveDate}}