protected IEnumerable<Block> ParseBlocks(StringRange document)
{
int paragraphStart = -1;
int paragraphEnd = -1;
bool paragraphStarted = false;
// there should be one monster builder always in builders
ParagraphBuilder monsterBuilder = _builders.OfType<ParagraphBuilder>().Single();
for (int start = 0; start < document.Length; start++)
{
IBlockBuilder builder = GetBuilder(start, document);
// paragraph is special as it just eats
// everything else the others don't
// want. Special muncher!
if (builder is ParagraphBuilder)
{
if (!paragraphStarted)
{
paragraphStart = start;
paragraphStarted = true;
}
paragraphEnd = start;
continue;
}
if (builder is EmptyLineBuilder && paragraphStarted)
{
// include the emtpty line character into the end
paragraphEnd = start;
}
// have to kill the monster so others can
// feed too
if (paragraphStarted)
{
// yield it and then kill it!
yield return monsterBuilder.Build(paragraphStart, paragraphEnd, document);
paragraphStarted = false;
}
yield return BuildBlock(builder, start, document, out start);
}
// so one monster got away
// run after it as it's yielding
if (paragraphStarted)
{
yield return monsterBuilder.Build(paragraphStart, paragraphEnd, document);
}
}