in reply to HTML document modification
Maybe if you read the file backwards and only replaced the last (first:) occurance of </body>.
If your html is well formed that should be fairly foolproof.
Perhaps, rather than reading backwards line by line, you could read the last couple of hundred bytes and then use the regex
s[(</body>)(?=(?:\s*</html>)>\s*\Z][$insert$1]i;
By only replacing the /body tag if there is only whitespace and an (optional) /html tag between it and the EOF, you'd be pretty certain of correctness assuming reasonably well-formed html. That wouldn't handle comments, but they are (probably) fairly rare at that point in the html?
If you raised an error in the event that the regex didn't match, any oddities could be fixed up manually.
|
|---|