public void AddChild(BBTreeNode ChildNode) { Children.Add(ChildNode); ChildNode.Parent = this; }
public static MvcHtmlString Parse(string PostText) { if (String.IsNullOrEmpty(PostText)) return MvcHtmlString.Empty; int TagStartPosition = 0; int TagEndPosition = 0; int ParsePosition = 0; BBTreeNode RootNode = new BBTreeNode("", null); BBTreeNode CurrentNode = RootNode; while ((TagStartPosition = PostText.IndexOf("[", TagEndPosition)) >= 0 && (TagEndPosition = PostText.IndexOf("]", TagStartPosition)) >= 0) { TagStartPosition = PostText.LastIndexOf("[", TagEndPosition); if (TagStartPosition > ParsePosition) // Current data. { CurrentNode.AddChild(new BBTreeNode(PostText.Substring(ParsePosition, TagStartPosition - ParsePosition), null)); } ParsePosition = TagEndPosition + 1; string Tag = PostText.Substring(TagStartPosition + 1, TagEndPosition - TagStartPosition - 1); if (Tag.Length > 1 && Tag.Substring(0, 1) == "/") // End tag { BBTreeNode SearchNode = CurrentNode; string TagName = Tag.Substring(1); while (SearchNode != RootNode) { if (String.Equals(SearchNode.TagType.Tag, TagName, StringComparison.InvariantCultureIgnoreCase)) { CurrentNode = SearchNode.Parent; break; } SearchNode = SearchNode.Parent; } if (SearchNode == RootNode && CurrentNode != RootNode && CurrentNode.TagType.TopMost) { CurrentNode.AddChild(new BBTreeNode(String.Format("[{0}]", Tag), null)); } continue; } BBTreeNode NewNode; if (CurrentNode.TagType != null && CurrentNode.TagType.TopMost) { NewNode = new BBTreeNode(String.Format("[{0}]", Tag), null); } else { NewNode = ParseNode(Tag); if (NewNode.TagType != null && !NewNode.TagType.AllowNesting) { BBTreeNode SearchNode = CurrentNode; while (SearchNode != RootNode) { if (SearchNode.TagType == NewNode.TagType) { NewNode = new BBTreeNode(String.Format("[{0}]", Tag), null); break; } SearchNode = RootNode; } } } CurrentNode.AddChild(NewNode); if (NewNode.TagType != null) CurrentNode = NewNode; } if (PostText.Length > ParsePosition) // Current data. { CurrentNode.AddChild(new BBTreeNode(PostText.Substring(ParsePosition), null)); } StringBuilder OutputBuilder = new StringBuilder(PostText.Length + 10); RootNode.WriteNode(OutputBuilder); return MvcHtmlString.Create(OutputBuilder.ToString()); }
static string UrlEncodeTagParser(BBCodeTag ToEncode, BBTreeNode NodeToParse) { string URL = NodeToParse.Data; if (String.IsNullOrEmpty(URL) && NodeToParse.Children.Count > 0) { URL = NodeToParse.Children[0].Data; } Regex Reg = new Regex(URLProtocolRegex); if (!Reg.IsMatch(URL, 0)) { URL = "http://" + URL; } return String.Format(ToEncode.HTML, HttpUtility.HtmlAttributeEncode(HttpUtility.UrlPathEncode(URL))); }
static string UrlEncodePartParser(BBCodeTag ToEncode, BBTreeNode NodeToParse) { string URL = NodeToParse.Data; if (String.IsNullOrEmpty(URL) && NodeToParse.Children.Count > 0) URL = NodeToParse.Children[0].Data; return String.Format(ToEncode.HTML, HttpUtility.HtmlAttributeEncode(HttpUtility.UrlPathEncode(URL))); }
static string QuoteParser(BBCodeTag ToEncode, BBTreeNode NodeToParse) { string Name = "Quote"; if (!String.IsNullOrWhiteSpace(NodeToParse.Data)) Name = HttpUtility.HtmlAttributeEncode(NodeToParse.Data); return String.Format(ToEncode.HTML, Name); }
static string HTMLEncodeTagParser(BBCodeTag ToEncode, BBTreeNode NodeToParse) { if (!ToEncode.AcceptParameters) return ToEncode.HTML; return String.Format(ToEncode.HTML, HttpUtility.HtmlAttributeEncode(NodeToParse.Data)); }
static string CodeParser(BBCodeTag ToEncode, BBTreeNode NodeToParse) { StringBuilder Test = new StringBuilder(); foreach (var Child in NodeToParse.Children) Test.Append(HttpUtility.HtmlEncode(Child.Data)); var data = Test.Replace("\n", "</li><li>").Replace("\r", "").ToString(); return String.Format(ToEncode.HTML, data); }