Rewrote the html encoder to have slightly better performance

Also added a simple test to make sure encoding goes correctly with unicode in the template.