/// <summary> /// Reconstructs the original EggsML that is represented by this node.</summary> /// <remarks> /// This does not necessarily return the same EggsML that was originally parsed. For example, redundant uses of /// the <c>`</c> character are removed.</remarks> public override string ToString() { if (_children.Count == 0) { return(Tag == null ? "" : EggsML.alwaysOpens(Tag) ? Tag.ToString() + EggsML.opposite(Tag) : Tag + "`" + Tag); } var childrenStr = stringify(_children, Tag); return(Tag == null ? childrenStr : childrenStr.StartsWith(Tag) ? childrenStr.EndsWith(EggsML.opposite(Tag)) ? Tag + "`" + childrenStr + "`" + EggsML.opposite(Tag) : Tag + "`" + childrenStr + EggsML.opposite(Tag) : childrenStr.EndsWith(EggsML.opposite(Tag)) ? Tag + childrenStr + "`" + EggsML.opposite(Tag) : Tag + childrenStr + EggsML.opposite(Tag)); }
/// <summary> /// Turns a list of child nodes into EggsML mark-up.</summary> /// <param name="children"> /// List of children to turn into mark-up.</param> /// <param name="tag"> /// If non-null, assumes we are directly inside a tag with the specified character, causing necessary escaping to /// be performed.</param> /// <returns> /// EggsML mark-up representing the same tree structure as this node.</returns> protected static string stringify(List <EggsNode> children, char?tag) { if (children == null || children.Count == 0) { return(""); } var sb = new StringBuilder(); for (int i = 0; i < children.Count; i++) { var childStr = children[i].ToString(); if (string.IsNullOrEmpty(childStr)) { continue; } // If the item is a tag, and it is the same tag character as the current one, we need to escape it by tripling it if (sb.Length > 0 && childStr.Length > 0 && childStr[0] == sb[sb.Length - 1]) { sb.Append('`'); } if (tag != null && children[i] is EggsTag && ((EggsTag)children[i]).Tag == tag && !EggsML.alwaysOpens(tag)) { sb.Append(new string(tag.Value, 2)); } sb.Append(childStr); } return(sb.ToString()); }