public static string Generate(IDocElement docElement) { string content = docElement.GetChildren() .Select(Generate) .Aggregate("", (acc, x) => acc + x); switch (docElement) { case SummaryDocElement summaryDocElement: return($"<summary>{content}</summary>\n"); case DescriptionDocElement descriptionDocElement: return($"<description>\n{content}\n</description>\n"); case CodeExampleDocElement codeExampleDocElement: return($"<code>\n{content}\n</code>\n"); case ParameterDocElement parameterDocElement: return($"<param name=\"{parameterDocElement.ParameterName}\">{content}</param>\n"); case RemarkDocElement remarkDocElement: return($"<remarks>{content}</remarks>\n"); case ReturnDocElement returnDocElement: return($"<returns>{content}</returns>\n"); case SeeDocElement seeDocElement: if (seeDocElement.Element.StartsWith("http")) { return($"<see href=\"{seeDocElement.Element}\" />\n"); } return($"<see cref=\"{seeDocElement.Element}\" />\n"); case TextDocElement textDocElement: return(textDocElement.Text); case NullDocElement nullDocElement: return(nullDocElement.Tag + nullDocElement.Text); case ParameterRefDocElement parameterRefDocElement: return($"<paramref name=\"{parameterRefDocElement.ParameterName}\" />"); case InGroupDocElement inGroupDocElement: // No equivalent return(""); case InternalDocElement internalDocElement: // No equivalent return(""); case CompoundDocElement compoundDocElement: return(content); default: throw new NotImplementedException(); } }
private static IEnumerable <string> GetDocLines(IDocElement element) { switch (element) { case Paragraph paragraph: yield return("<para>"); foreach (var child in paragraph.Children.SelectMany(GetDocLines)) { yield return(child); } yield return("</para>"); yield break; case InlineCode inlineCode: yield return ($"<c>${SecurityElement.Escape(inlineCode.ToString())}</c>"); yield break; case DocList list: yield return ("<list type=\"" + (list.ListType == DocListType.ItemizedList ? "bullet" : "number") + "\">"); foreach (var listItemLines in list.Children.Select(GetDocLines)) { yield return("<item><description>"); foreach (var line in listItemLines) { yield return(line); } yield return("</description></item>"); } yield return("</list>"); yield break; default: yield return(SecurityElement.Escape(element.ToString())); yield break; } }
public static IDocElement Parse(string docs) { DocTag[] docTags = Regex.Split(docs, "(?=@)") .Select(s => new DocTag(s)) .ToArray(); for (int i = 0; i < docTags.Count(); i++) { if (i == 0) { continue; } docTags[i - 1].Next = docTags[i]; docTags[i].Previous = docTags[i - 1]; } List <IDocElement> docElements = docTags.Select(docTag => { IDocElement element = ParseDocTag(docTag); if (element.IsLeaf()) { return(null); } if (docTag.Next == null || !ParseDocTag(docTag.Next).IsLeaf()) { return(element); } List <IDocElement> children = element.GetChildren(); switch (ParseDocTag(docTag.Next)) { case ParameterRefDocElement refDocElement: children.Add(refDocElement); children.Add(new TextDocElement { Text = refDocElement.Remainder }); element.SetChildren(children); return(element); case TextDocElement textDocElement: children.Add(textDocElement); element.SetChildren(children); return(element); case NullDocElement nullDocElement: return(element); case InternalDocElement internalDocElement: return(element); default: throw new NotImplementedException(); } }) .Where(x => x != null) .ToList(); return(new CompoundDocElement() { Children = docElements }); }