GROFF_NO_SGR=1 man -l -Tutf8 $manpage | sed 's/.\b//g' > $manpage.txt
(echo '<html><head><meta charset="UTF-8"></head><body><pre>'
GROFF_NO_SGR=1 man -l -Tutf8 $manpage | sed '
-s/&/&/g
-s/</</g
-s/>/>/g
-s,\(.\)\b\1,<b>\1</b>,g
-s,_\b\(.\),<u>\1</u>,g'
+# Change bold and underline via backspacing into bracketing with control
+# characters. We cannot directly translate them to HTML because <> need
+# to be escaped later. (We cannot escape <> first because bold or
+# underlined escaped characters would be mis-processed.)
+s,\(.\)\b\1,\ 1\1\ 2,g
+s,_\b\(.\),\ 3\1\ 4,g
+
+# Drop redundant font changes, to keep from having every character have
+# a separate tag pair.
+s,\ 2\ 1,,g
+s,\ 4\ 3,,g
+
+# Escape special characters.
+s,&,\&,g
+s,<,\<,g
+s,>,\>,g
+
+# Translate control characters to HTML.
+s,\ 1,<b>,g
+s,\ 2,</b>,g
+s,\ 3,<u>,g
+s,\ 4,</u>,g
+'
echo '</pre></body></html>'
) > $manpage.html