Generalize entity encoding/decoding code

So that it can be used with custom set of entities.
Handle nonentities at end of string

We were passing through things like ”&copy ” (notice the space) but not
“&copy” (no space) which was completely dropped from output.
Don’t touch existing entities in entityencode()
Use quote.entityencode()/entitydecode()

They provide more sophisticated handling of entities than blind replace,
will allow us to handle the ampersand properly.
Support &amp;

The roundtrip test for “&amp;nbsp;” is failing, need to find out how to
fix it or whether it is a valid input or not.