internal List <string> Build() { var allTypes = Assembly.GetTypes(); var buildFiles = new List <string>(); foreach (var type in allTypes) { Program.Process($"Generating from type: {type.FullName}"); var fileName = BuildType(type); if (string.IsNullOrEmpty(fileName)) { Program.Process($"Target type ({type.Name}) refused to generate content.", true); continue; } buildFiles.Add(fileName); Program.Process($"Content successfully generated from: {type.FullName}"); } Program.Process($"Generating Sidebar from {buildFiles.Count} files."); // TODO: Support multiple assemblies var str = new StringBuilder(); if (File.Exists(AppConfig.Loaded.SidebarBefore)) { str.AppendLine(File.ReadAllText(AppConfig.Loaded.SidebarBefore)); str.AppendLine(); } str.AppendLine($"- {AppConfig.Loaded.TreeTitleName}"); foreach (var f in buildFiles) { str.AppendLine($" - [{f}]({DocSyntax.GetMarkdownFile($"{AppConfig.Loaded.DeploySidebarPath}{f}")})"); } if (File.Exists(AppConfig.Loaded.SidebarEnd)) { str.AppendLine(); str.AppendLine(File.ReadAllText(AppConfig.Loaded.SidebarEnd)); } WriteAllText($"{DeployDir}{JEMVar.DirectorySeparatorChar}{AppConfig.Loaded.SidebarName}", str.ToString()); if (File.Exists(AppConfig.Loaded.ReadMe)) { File.Copy(AppConfig.Loaded.ReadMe, $"{DeployDir}{JEMVar.DirectorySeparatorChar}README.md"); } return(buildFiles); }
/// <summary> /// Gets markdown string of given type. /// It includes type reference. /// </summary> internal static string GetTypeMarkdown(Type t, bool quote = true, string fixedName = null) { var name = string.Empty; var typeName = DocSyntax.FixVarName(t.Name); // fix type name if (t.IsByRef) { typeName = typeName.Remove(typeName.Length - 1, 1); typeName = $"ref {typeName}"; } if (!string.IsNullOrEmpty(fixedName)) { typeName += $" {fixedName}"; } if (!CanDefineTypeReference(t)) { if (quote) { name += "`"; } name += typeName; if (quote) { name += "`"; } name += " "; } else { name += "["; if (quote) { name += "`"; } name += typeName; if (quote) { name += "`"; } name += $"]({DocSyntax.CollectMarkDownReference(t)})"; } return(name); }
internal static bool CollectSummaryFromNode(XmlNodeList nodes, string targetName, out string summary) { foreach (XmlNode node in nodes) { var nodeName = node.Attributes?[XmlName]?.Value ?? throw new NullReferenceException(); var typeName = nodeName.Remove(0, 2); if (typeName != targetName) { continue; } summary = node.SelectSingleNode(XmlSummary)?.InnerText; summary = DocSyntax.RemoveSpaces(summary); return(true); } summary = XmlNotAvailable; return(false); }
private void BuildExample(Type t, StringBuilder str) { var exampleFile = FindExampleFile(t); if (string.IsNullOrEmpty(exampleFile)) { return; } str.AppendLine(); str.AppendLine("## Examples"); str.AppendLine(); var exampleStr = File.ReadAllText(exampleFile); var allTypes = t.Assembly.GetTypes(); foreach (var a in allTypes) { var s = a.Name; exampleStr = exampleStr.Replace($"(#{s})", $"({DocSyntax.CollectMarkDownReference(a)})"); } str.AppendLine(exampleStr); }
private string BuildType(Type t) { if (!t.IsVisible) { Program.Process($"Target type ({t.Name}) is not visible!", true); return(null); } if (DocBuilderHelper.IsTypeArrayContainsNotAllowed(t)) { Program.Process($"Target type ({t.Name}) is not allowed to be generated!", true); return(null); } var fileName = DocSyntax.CollectTypeName(t); var filePath = DocSyntax.GetMarkdownFile($"{DeployDir}{JEMVar.DirectorySeparatorChar}{fileName}"); if (DocSyntax.FixVarName(ref filePath)) { Program.Process($"Target type ({t.Name}) has invalid filePath ({filePath}).", true); return(null); } var str = new StringBuilder(); var subStr = new StringBuilder(); if (t.IsClass) { Program.Process($"New class: {t.Name}"); // HEADER str.AppendLine($"# {t.Name}"); if (t.BaseType != null && t.BaseType != typeof(object)) { str.AppendLine($"<small>class in `{Path.GetFileNameWithoutExtension(new Uri(t.Assembly.CodeBase).AbsolutePath)}` " + $"/ inherits from {DocClassUtil.GetTypeMarkdown(t.BaseType, false)}</small>"); } else { str.AppendLine($"<small>{(t.IsAbstract && t.IsSealed ? "static " : string.Empty)}class in `{Path.GetFileNameWithoutExtension(new Uri(t.Assembly.CodeBase).AbsolutePath)}`</small>"); } str.AppendLine(string.Empty); // SUMMARY DocXmlUtil.CollectSummaryFromNode(_nodes, t.FullName, out var classSummary); str.AppendLine("### Description"); str.AppendLine(classSummary); // EVENTS DocBuilderHelper.BuildEvents(t, str, _nodes, TypeContent.Target, false); // FIELDS DocBuilderHelper.BuildFields(t, str, _nodes, TypeContent.Target, false); // PROPERTIES DocBuilderHelper.BuildProperties(t, str, _nodes, TypeContent.Target, false); // METHODS DocBuilderHelper.BuildMethods(t, str, _nodes, TypeContent.Target, false); // STATIC EVENTS DocBuilderHelper.BuildEvents(t, str, _nodes, TypeContent.Target, true); // STATIC FIELDS DocBuilderHelper.BuildFields(t, str, _nodes, TypeContent.Target, true); // STATIC PROPERTIES DocBuilderHelper.BuildProperties(t, str, _nodes, TypeContent.Target, true); // STATIC METHODS DocBuilderHelper.BuildMethods(t, str, _nodes, TypeContent.Target, true); subStr.Clear(); // INHERITED MEMBERS // EVENTS DocBuilderHelper.BuildEvents(t, str, _nodes, TypeContent.Inherited, false); // INHERITED MEMBERS // FIELDS DocBuilderHelper.BuildFields(t, subStr, _nodes, TypeContent.Inherited, false); // INHERITED MEMBERS // PROPERTIES DocBuilderHelper.BuildProperties(t, subStr, _nodes, TypeContent.Inherited, false); // INHERITED MEMBERS // METHODS DocBuilderHelper.BuildMethods(t, subStr, _nodes, TypeContent.Inherited, false); // INHERITED MEMBERS // STATIC EVENTS DocBuilderHelper.BuildEvents(t, str, _nodes, TypeContent.Inherited, true); // INHERITED MEMBERS // STATIC FIELDS DocBuilderHelper.BuildFields(t, subStr, _nodes, TypeContent.Inherited, true); // INHERITED MEMBERS // STATIC PROPERTIES DocBuilderHelper.BuildProperties(t, subStr, _nodes, TypeContent.Inherited, true); // INHERITED MEMBERS // STATIC METHODS DocBuilderHelper.BuildMethods(t, subStr, _nodes, TypeContent.Inherited, true); if (!string.IsNullOrEmpty(subStr.ToString()) && !string.IsNullOrWhiteSpace(subStr.ToString())) { str.AppendLine(); str.AppendLine(); str.AppendLine("## Inherited Members"); str.AppendLine(subStr.ToString()); } // EXAMPLES BuildExample(t, str); } else if (t.IsEnum) { Program.Process($"New Enum: {t.Name}"); // HEADER str.AppendLine($"# {t.Name}"); if (t.BaseType != null && t.BaseType != typeof(Object)) { str.AppendLine($"<small>enumeration in `{Path.GetFileNameWithoutExtension(new Uri(t.Assembly.CodeBase).AbsolutePath)}`</small>"); } str.AppendLine(string.Empty); // SUMMARY DocXmlUtil.CollectSummaryFromNode(_nodes, t.FullName, out var classSummary); str.AppendLine("### Description"); str.AppendLine(classSummary); // PROPERTIES DocBuilderHelper.BuildEnum(t, str, _nodes); // EXAMPLES BuildExample(t, str); } if (File.Exists(AppConfig.Loaded.FileEnd)) { str.AppendLine(); str.AppendLine(File.ReadAllText(AppConfig.Loaded.FileEnd)); } WriteAllText(filePath, str.ToString()); return(fileName); }