public void Identify(Block previous, bool convertQuotes, bool allVerse) { if (Lines.Count == 0) return; if (allVerse) { Verse = Tied = true; } else if (previous != null && previous.Tied) { Verse = previous.Verse; Tied = true; } if (Lines[0].Trim() == "Footnotes:" || Lines[0].Trim() == "<contents>") { Lines.RemoveAt(0); if (Lines.Count == 0) return; } int trimLength = 0; Match m = verseTagRe.Match(Lines[0]); if (m.Success) { Lines.RemoveAt(0); if (Lines.Count == 0) return; Verse = true; Tied = true; trimLength = m.Groups[1].Length; if (trimLength > 0) Quoted = true; } if (Tied && Lines[Lines.Count - 1].Trim() == "</verse>") { Tied = false; Lines.RemoveAt(Lines.Count - 1); } m = footnoteRe.Match(Lines[0]); if (m.Success) { Footnote = Int32.Parse(m.Groups[1].Value); Lines[0] = Lines[0].Substring(m.Groups[0].Length); trimLength = -1; } if (! m.Success && ! Verse) { m = headingRe.Match(Lines[0]); if (m.Success) { Heading = m.Groups[1].Length; trimLength = m.Groups[0].Length; } } if (! m.Success && ! Verse) { m = listItemRe.Match(Lines[0]); if (m.Success) { ListItem = true; if (m.Groups[1].Value != "-") Numbered = true; trimLength = m.Groups[0].Length; } } if (! m.Success && ! Verse) { m = verseRe.Match(Lines[0]); if (m.Success) { Verse = true; if (m.Groups[1].Success) Quoted = true; trimLength = m.Groups[0].Length; } } if (! m.Success && ! Verse) { m = quoteRe.Match(Lines[0]); if (m.Success) { Quoted = true; trimLength = m.Groups[0].Length; } } // Is this an initial paragraph? if ((previous == null || previous.Heading.HasValue) && (! Quoted && ! Verse && ! ListItem && ! Footnote.HasValue && ! Heading.HasValue)) Initial = true; // Check whether this is a continuation of a single quote if (Quoted && previous != null && previous.Quoted) { // Right now the only real way I can tell if a quote is broken is whether // the previous paragraph ends in a footnote reference -- jww (2006-02-12) string prevLastLine = previous.Lines[previous.Lines.Count - 1]; if (! endingFnRe.IsMatch(prevLastLine)) previous.Tied = true; } for (int i = 0; i < Lines.Count; i++) { if (trimLength > 0) Lines[i] = Lines[i].Substring(trimLength).TrimEnd(null); else if (trimLength == -1) Lines[i] = Lines[i].Trim(null); else Lines[i] = Lines[i].TrimEnd(null); } if (! Verse) { string merged = String.Join(" ", Lines.ToArray()); Lines.Clear(); Lines.Add(merged); } for (int i = 0; i < Lines.Count; i++) { Lines[i] = Regex.Replace(Lines[i], "<br>", " \n"); StringBuilder newLine = new StringBuilder(); Stack<InlineStyle> currentStyle = new Stack<InlineStyle>(); currentStyle.Push(InlineStyle.Plain); int length = Lines[i].Length; int bracketDepth = 0; for (int j = 0; j < length; j++) { switch (Lines[i][j]) { case '[': newLine.Append(Lines[i][j]); bracketDepth++; break; case ']': newLine.Append(Lines[i][j]); bracketDepth--; break; case '=': if (bracketDepth > 0) { newLine.Append(Lines[i][j]); break; } if (currentStyle.Peek() == InlineStyle.Literal) { newLine.Append("`"); currentStyle.Pop(); } else { newLine.Append("`"); currentStyle.Push(InlineStyle.Literal); } break; default: newLine.Append(Lines[i][j]); break; } } Lines[i] = newLine.ToString(); } // If this is not a quote, we are interested in breaking down sentences. if (! Quoted) { if (Verse) { Sentences = Lines; } else { Sentences = new List<string>(); int startPos = 0; m = sentEndRe.Match(Lines[0]); while (m.Success) { if (! m.Groups[1].Success) Sentences.Add(Lines[0].Substring(startPos, (m.Groups[0].Index + 1) - startPos)); startPos = m.Groups[0].Index + (m.Groups[0].Length - 1); m = sentEndRe.Match(Lines[0], startPos); } Sentences.Add(Lines[0].Substring(startPos)); } } }
public static List<Block> Parse(TextReader reader, bool convertQuotes, bool allVerse) { List<Block> blocks = new List<Block>(); Block prevBlock = null; Block block = new Block(); bool first = true; string line = reader.ReadLine(); while (line != null) { if (line.Length == 0) { if (block.Lines.Count > 0) { if (! (first == true && block.Lines[0][0] == '#')) block.Identify(prevBlock, convertQuotes, allVerse); first = false; if (block.Lines.Count > 0) { blocks.Add(block); prevBlock = block; } block = new Block(); } } else { block.InputLines.Add(line); block.Lines.Add(line); } line = reader.ReadLine(); } if (block.Lines.Count > 0) { block.Identify(prevBlock, convertQuotes, allVerse); if (block.Lines.Count > 0) blocks.Add(block); } return blocks; }