/// <summary> /// Constructor. /// </summary> /// <param name="parser">The parent parser.</param> /// <param name="level">The section nesting level (1=page level).</param> /// <param name="title">The section title.</param> /// <param name="wikiText">The partially converted section content.</param> public WikipediaSection(WikipediaParser parser, int level, string title, string wikiText) { this.parser = parser; this.Level = level; this.Title = title; wikiText = WikipediaParser.FormatText(wikiText); this.Blocks = parser.FormatBlocks(wikiText); // Process each of the blocks. foreach (var block in this.Blocks) { block.Format(parser); } //----------------------------------------------------------------- // Render the block and section HTML. var sbSection = new StringBuilder(Math.Min(2048, wikiText.Length)); var sbBlock = new StringBuilder(wikiText.Length); foreach (var block in this.Blocks) { sbBlock.Clear(); block.Append(sbBlock); block.Html = sbBlock.ToString(); sbSection.Append(block.Html); } this.Html = sbSection.ToString(); }
/// <summary> /// Used to construct a list item node. /// </summary> /// <param name="parser">The parent parser.</param> /// <param name="indentItemTypes">The list of item types parsed from the first few colums of the block text.</param> /// <param name="text">The block text (without any leading characters).</param> public WikipediaBlock(WikipediaParser parser, List <WikipediaBlockType> indentItemTypes, string text) { this.parser = parser; this.Type = indentItemTypes.Last(); this.Indent = indentItemTypes.Count; this.Text = text; this.Children = new List <WikipediaBlock>(); // Compute the actual node depth in the tree. This is somewhat tricky because // the indent item types passed may imply the creation of new parent list nodes // depending on what is above the node in the heirarchy. The algorithm below // walks the item node types passed and creates a list of the actual node types // that will need to exist below the root to host this node. var heirarchy = new List <WikipediaBlockType>(); foreach (var itemType in indentItemTypes) { heirarchy.Add(GetParentType(itemType)); heirarchy.Add(itemType); } this.NodeDepth = heirarchy.Count; // Create the heirarchy list by adding the virtual root and // removing the item type. this.ParentNodeTypes = new List <WikipediaBlockType>(heirarchy.Count); this.ParentNodeTypes.Add(WikipediaBlockType.RootList); for (int i = 0; i < heirarchy.Count - 1; i++) { this.ParentNodeTypes.Add(heirarchy[i]); } }
/// <summary> /// Used to construct a non-list item node. /// </summary> /// <param name="parser">The parent parser.</param> /// <param name="type">Identifies the block type.</param> /// <param name="text">The block text (without any leading wikitext characters).</param> public WikipediaBlock(WikipediaParser parser, WikipediaBlockType type, string text) { this.parser = parser; this.Type = type; this.Text = text; this.Indent = 0; this.NodeDepth = 0; this.Children = new List <WikipediaBlock>(); }
/// <summary> /// Recursively handles any additional wiki markup conversion and renders /// the block's HTML. /// </summary> /// <param name="parser">The parent parser.</param> internal void Format(WikipediaParser parser) { Text = parser.ProcessLinks(Text); Text = parser.RemoveReferences(Text); Text = parser.UnescapeHtmlChars(Text); Text = parser.RemoveArtifacts(Text); foreach (var child in Children) { child.Format(parser); } var sb = new StringBuilder(); Append(sb); this.Html = sb.ToString(); }