Over Labor Day weekend I migrated my blog to use AMP but the first version was definitely a work in progress. One big item I needed to take care of was converting all my images to be AMP compatible by replacing <img> tag with <amp-img> along with the image width and height. I ended up writing a quick Python script to go through each of my posts, find each <img> tag, get the image’s dimensions, and then replace the original tag wit the AMP version. Unfortunately, I ran the script without too much testing and forgot to add closing tags which caused some of the content to go missing.
The solution was to write another script that once again went through every post but instead of replacing every img tag with an amp-img tag it found every amp-img referenced and added a closing tag in case it didn’t have one. These two scripts combined ended up fixing most of the AMP issues but I’m sure there are still a few posts that got warped so if you notice any please let me know.
In the spirit of constantly shipping the code is up on GitHub but is simple enough to not need a ton of polishing. Note that it’s not very robust and has some assumptions based on my blog structure so I would test it thoroughly before applying it to your posts.