double escaping entities
double escaping entities, add img alt attribute
use double escaped entities and single escape them at final stage. form of &amp;entity_name; will be &entity_name; in final output document.
replace custom math placeholder replacement with double escaped entities. use tidy (if available, not a dependency) to tidy up the html. will remove msg and make it fully valid html (hopefully).