Merge from trunk.
Also had to add a check for empty paragraphs to avoid writing out empty paragraphs between block templates - which would be tricky to detect in the grammar.