I haven't looked at your code in detail, but it looks like you're using both bs4
and regular expressions, which, IMHO, isn't a good idea.
The invalid code that Sigil is reporting is caused by the following lines in LSH.xhtml
before:
Code:
<div class="illus">
<img alt="" class="img065" src="../Images/lsh-23-065.png"/></div>
after:
Code:
<div class="illus">
<img alt="" class="mobionly" height="441px" src="../Images/lsh-23-065.png" width="630px"/>
<img alt="" class="kf8only" src="../Images/lsh-23-065.png" style="width: 100%;height: auto;"/>
For some reason your code removed the closing </div> tag.
I don't know what the exact cause is, but since you seem to be using regular expressions, it might help to normalize the HTML input using the Sigil bs4 prettyprint_xhtml function. When I ran the following plugin prior to running yours, I didn't get any error messages: