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(); } }
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 }); }