What I’m trying to do:
Escape some user-supplied text, for display as HTML.
What I’ve tried and what’s not working:
Code Sample:
import html # for std lib html.escape()
If there’s something handy, standard, pre-written and debugged, that would be the obvious choice. If not, I’ll roll my own.
Edit:
I threw this together, in case anyone else needs it:
def html_escape(s: str) -> str:
"""Convert a regular string to HTML-safe format,
just in case it contains special characters
"""
result = s.replace('&', '&').replace('<', '<').replace('>', '>')
result = result.replace('"', '"').replace("'", ''')
return result