Major refactoring: use Jinja2, use docutils/markdown API, other changes.