// Linearizes the tree structure of a content node into single words with // font style attached, making it easier to output. public IEnumerable <WordAndFont> Linearize() { Stack <LinearizerState> stack = new Stack <ContentNode.LinearizerState>(); LinearizerState current = new ContentNode.LinearizerState(null, this, 0, FontStyle.Plain); // Descend first stack.Push(current); while (current.NextKidReady) { current = current.Child(); stack.Push(current); } do { // Grab item current = stack.Pop(); // Any value to output? if (current.Node.Value != null) { FontStyle style = current.FindStyle(); foreach (string t in current.Node.Value.Split(new char[] { ' ', '\t', '\r', '\n' })) { string trimmed = t.Trim(); if (trimmed.Length == 0) { continue; } yield return(new WordAndFont(style, trimmed)); } } // Do we have kids, and a next kid to go to? if (current.NextKidReady) { // Put myself back on the stack stack.Push(current = current.Next()); // The the depth of the kid's kids. while (current.NextKidReady) { current = current.Child(); stack.Push(current); } } } while(stack.Count > 0); }
// Linearizes the tree structure of a content node into single words with // font style attached, making it easier to output. public IEnumerable<WordAndFont> Linearize() { Stack<LinearizerState> stack = new Stack<ContentNode.LinearizerState>(); LinearizerState current = new ContentNode.LinearizerState(null, this, 0, FontStyle.Plain); // Descend first stack.Push(current); while(current.NextKidReady) { current = current.Child(); stack.Push(current); } do { // Grab item current = stack.Pop(); // Any value to output? if (current.Node.Value != null) { FontStyle style = current.FindStyle(); foreach (string t in current.Node.Value.Split(new char[] { ' ', '\t', '\r', '\n'})) { string trimmed = t.Trim(); if (trimmed.Length == 0) continue; yield return new WordAndFont(style, trimmed); } } // Do we have kids, and a next kid to go to? if (current.NextKidReady) { // Put myself back on the stack stack.Push(current = current.Next()); // The the depth of the kid's kids. while(current.NextKidReady) { current = current.Child(); stack.Push(current); } } } while(stack.Count > 0); }