public static void Write(Quote quote, TextWriter w) { // This is done literally because I can't stop TextWriter from indenting the // inner text. bool first = true; foreach (Element element in quote.elements) { if (first) first = false; else w.WriteLine("> "); Paragraph para = element as Paragraph; if (para != null) { w.Write("> "); Write(para.block.Lines[0], w); w.WriteLine(); } else { Write(element, w); } } w.WriteLine(); }
public static List<Element> Assemble(List<Block> blocks) { Block previous = null; Verse previousVerse = null; Quote previousQuote = null; List<Element> elements = new List<Element>(); foreach (Block block in blocks) { Element element = null; if (block.Verse) { if (previous != null && previous.Tied && previous.Verse) { Debug.Assert(previousVerse != null); previousVerse.blocks.Add(block); } else { previousVerse = new Verse(); previousVerse.blocks.Add(block); element = previousVerse; } } else if (block.Heading.HasValue) { Heading head = new Heading(); Debug.Assert(block.Lines.Count == 1); head.Title = block.Lines[0]; head.Depth = block.Heading.Value; element = head; } else if (block.Footnote.HasValue) { Footnote fn = new Footnote(); fn.Text = block; fn.Index = block.Footnote.Value; element = fn; } else { Paragraph para = new Paragraph(); para.block = block; element = para; } bool added = false; if (block.Quoted && element != null) { if (previous != null && previous.Tied && previous.Quoted) { Debug.Assert(previousQuote != null); previousQuote.elements.Add(element); added = true; } else { previousQuote = new Quote(); previousQuote.elements.Add(element); element = previousQuote; } } if (! added && element != null) elements.Add(element); previous = block; } return elements; }