Thus, before scripting the above approach, I looked for other options. Two seem promising: Pygments and highlight. Both provide shell filters for converting program text into HTML. For now, I've settled on highlight, since Pygments lacks support for highlighting Standard ML.
Several command line options are appropriate. For example, I can format an SML file with
cat real-ord.sml | highlight --inline-css -f --enclose-pre -S sml -s seashell | pbcopy
Pasting that into MarsEdit nicely formats the contents of
structure RealOrdKey : ORD_KEY where type ord_key = Real.real = struct
type ord_key = real
I'm sure I'll need to refine the approach a bit as I go, but this already seems easier than the approach I'd been using.