public static void Write(Element elem, TextWriter w) { Paragraph para = elem as Paragraph; if (para != null) { Write(para, w); return; } Quote quot = elem as Quote; if (quot != null) { Write(quot, w); return; } Verse vers = elem as Verse; if (vers != null) { Write(vers, w); return; } Heading head = elem as Heading; if (head != null) { Write(head, w); return; } Footnote foot = elem as Footnote; if (foot != null) { Write(foot, w); return; } throw new Exception("Cannot render unknown element type " + elem.GetType().Name); }
public static void Write(Verse verse, TextWriter w) { bool first = true; foreach (Block block in verse.blocks) { if (first) { first = false; } else { w.WriteLine("> "); } int len = block.Lines.Count; for (int i = 0; i < len; i++) { string line = block.Lines[i]; Match m = initialSpace.Match(line); if (m.Success) { line = line.TrimStart(); string prefix = ""; for (int j = 0; j < m.Groups[0].Length; j++) { prefix = " " + prefix; } line = prefix + line; } w.Write("> "); Write(line, w); w.WriteLine(" "); } } w.WriteLine(); }
public static void Write(Verse verse, TextWriter w) { bool first = true; foreach (Block block in verse.blocks) { if (first) first = false; else w.WriteLine("> "); int len = block.Lines.Count; for (int i = 0; i < len; i++) { string line = block.Lines[i]; Match m = initialSpace.Match(line); if (m.Success) { line = line.TrimStart(); string prefix = ""; for (int j = 0; j < m.Groups[0].Length; j++) prefix = " " + prefix; line = prefix + line; } w.Write("> "); Write(line, w); w.WriteLine(" "); } } 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; }
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); }