templates: handle a document without any tree items correctly
Created by: LawnGnome
When generating the page index (table of contents), we have a special case where a document with a single top level element will have the indexing move down a single level into that element's children, presumably to catch documents with subtly malformed element trees. This is implemented in the templating logic in document.html
.
Unfortunately, the check that is intended to prevent this behaviour kicking in when there is not one top level element dereferences .Doc.Tree
's first element unconditionally, because the and
function provided by Go templates does not short
circuit and the second branch expression calls (index .Doc.Tree 0)
.
The fix here is to split out the logic to calculate the tree to index into a set of nested if
statements, and use that to populate a template variable. Goodbye, nice one liner, and hello spaghetti, but it does work.