Better header links and TOC

Header text contents are now sanitised to produce valid element ids,
TOC generates a nested structure to show header levels, and header ids
include parent elements to ensure uniqueness even if two lower-level
headers in different higher-level headers share the same name.