public static void BuildNamespaceLinks(this IMarkdownItem item, string namespaceValue, MarkdownBuilder mb) { var namespaceItems = namespaceValue.Split('.'); string globalNamespace = ""; mb.Append("Namespace: "); foreach (var namespaceItem in namespaceItems) { if (!String.IsNullOrEmpty(globalNamespace)) { globalNamespace += "."; mb.Append(" > "); } globalNamespace = globalNamespace + namespaceItem; if (item.Project.TryGetValue(new TypeWrapper(globalNamespace), out IMarkdownItem foundItem)) { mb.Link(namespaceItem, item.To(foundItem)); } else { mb.Link(namespaceItem, ""); } } mb.AppendLine().AppendLine(); }
/// <summary> /// Calculates the path for the markdown item /// </summary> /// <param name="item">The item to get a path for</param> /// <returns>String path to where to place the markdown item page</returns> public string GetPath(IMarkdownItem item) { switch (item) { case MarkdownProject proj: return(""); case MarkdownNamespace nameItem: return(nameItem.FullName.Replace('.', Path.DirectorySeparatorChar)); case MarkdownProperty prop: return(Path.Combine(GetPath(prop.ParentType), "Properties")); case MarkdownType type: return(GetPath(type.NamespaceItem)); case MarkdownEvent eventItem: return(Path.Combine(GetPath(eventItem.ParentType), "Events")); case MarkdownField field: return(Path.Combine(GetPath(field.ParentType), "Fields")); case MarkdownMethod method: return(Path.Combine(GetPath(method.ParentType), "Methods")); case MarkdownEnum enumItem: return(GetPath(enumItem.NamespaceItem)); default: return(item.FullName.Replace('.', Path.DirectorySeparatorChar)); } }
/// <summary> /// Will return a relative link from one markdown item to the other /// </summary> /// <param name="from">The starting place where you need to link from</param> /// <param name="dest">The place you need the link to take you</param> /// <returns>The file url or empty string if no file exists</returns> public static string To(this IMarkdownItem from, IMarkdownItem dest) { if (dest.FileName == null) { return(""); } return(from.Location.AddRoot().UpdatedRelativePath(dest.Location.AddRoot()).CombinePath(dest.FileName).AddRoot()); }
public static MarkdownNamespace TryGetOrAdd(string name, IMarkdownItem item) { if (!Items.ContainsKey(name)) { Constants.Logger?.LogTrace("Add Markdown Namespace {namespaceName}", name); TryAdd(name, item); } return(Items[name] as MarkdownNamespace); }
private static bool TryAdd(string name, IMarkdownItem item) { if (!Items.ContainsKey(name)) { Items.Add(name, item); return(true); } Console.WriteLine($"Item {name} already exists"); return(false); }
public static string GetLink(this IMarkdownItem currentItem, TypeWrapper info) { if (currentItem.Project.TryGetValue(info, out IMarkdownItem lookupItem)) { return(currentItem.To(lookupItem)); } else if (info.FullName.StartsWith("System") || info.FullName.StartsWith("Microsoft")) { return("https://docs.microsoft.com/en-us/dotnet/api/" + Cleaner.CleanName(info.FullName, true, false)); } return(null); }
/// <summary> /// Gets the filename for the markdown item /// </summary> /// <param name="item">The item to get a filename for</param> /// <returns>String name of the file that the item's content should be in</returns> public string GetFileName(IMarkdownItem item) { switch (item) { case MarkdownProject proj: return(_options.RootFileName); case MarkdownNamespace nameItem: if (_options.BuildNamespacePages) { return(_options.RootFileName); } return(null); case MarkdownType type: if (_options.BuildTypePages) { return($"{Cleaner.CleanName(type.Name, true, false)}.md"); } return(null); case MarkdownEnum enumItem: if (_options.BuildTypePages) { return($"{Cleaner.CleanName(enumItem.Name, true, false)}.md"); } return(null); case MarkdownConstructor constructor: if (_options.BuildConstructorPages) { return($"{constructor.ParentType.Name}--{constructor.InternalItem.MetadataToken}.md"); } return(null); case MarkdownMethod method: if (_options.BuildMethodPages) { return($"{method.ParentType.Name}--{method.InternalItem.Name}.md"); } return(null); } return(null); }
/// <summary> /// Create a constructor with links and parameters /// </summary> /// <param name="currentItem">The current location to create this Constructor name with links</param> /// <param name="constructor">The constructor to clean up</param> /// <param name="useFullName">Use full name of the constructor</param> /// <param name="useParameterNames">Use parameter names if set to false only type will be shown</param> /// <returns>The markdown string</returns> public static string CreateFullConstructorsWithLinks(IMarkdownItem currentItem, MarkdownConstructor constructor, bool useFullName, bool useParameterNames) { var parameters = constructor.InternalItem.GetParameters(); MarkdownBuilder mb = new MarkdownBuilder(); string name = useFullName ? CleanFullName(constructor.ParentType.InternalType, false, false) : CleanName(constructor.ParentType.Name, false, false); if (constructor.FileName != null) { mb.Link(name, currentItem.To(constructor)); } else { mb.Append(name); } mb.Append(" ("); if (parameters.Length > 0) { StringBuilder sb = new StringBuilder(); for (var i = 0; i < parameters.Length; i++) { var type = parameters[i].ParameterType; var link = CreateFullTypeWithLinks(currentItem, type, useFullName, true); sb.Append(link); if (useParameterNames) { sb.Append($" {parameters[i].Name}"); } if (i + 1 != parameters.Length) { sb.Append(", "); } } mb.Append(sb.ToString()); } mb.Append(")"); return(mb.ToString()); }
private void BuildTable(MarkdownBuilder mb, IMarkdownItem item, string[] headers, MarkdownMethod mdType) { mb.AppendLine(); List <string[]> data = new List <string[]>(); string[] dataValues = new string[headers.Length]; Type lookUpType = null; if (item.ItemType == MarkdownItemTypes.Method) { lookUpType = item.As <MarkdownMethod>().ReturnType; } dataValues[0] = Cleaner.CreateFullTypeWithLinks(mdType, lookUpType, false, false); string name = item.FullName; if (item.ItemType == MarkdownItemTypes.Method) { name = Cleaner.CreateFullMethodWithLinks(mdType, item.As <MarkdownMethod>(), false, true, false); } else if (item.ItemType == MarkdownItemTypes.Property) { name = Cleaner.CreateFullParameterWithLinks(mdType, item.As <MarkdownProperty>(), false, _options.ShowParameterNames); } else if (item.ItemType == MarkdownItemTypes.Constructor) { name = Cleaner.CreateFullConstructorsWithLinks(mdType, item.As <MarkdownConstructor>(), false, _options.BuildConstructorPages); } dataValues[1] = name; data.Add(dataValues); mb.Table(headers, data, true); mb.AppendLine(); }
/// <summary> /// Create a Link to the target from the MarkadownItem with full type information /// </summary> /// <param name="currentItem">The current location to create a link from</param> /// <param name="target">The target to link to </param> /// <param name="useFullName">Use the full name of the type with namespace</param> /// <param name="useSpecialText">Use special code quote on link title to make it look nicer</param> /// <returns>The markdown string</returns> public static string CreateFullTypeWithLinks(IMarkdownItem currentItem, Type target, bool useFullName, bool useSpecialText) { StringBuilder sb = new StringBuilder(); var genericArray = target.GetGenericArguments(); for (var i = 0; i < genericArray.Length; i++) { var link = CreateFullTypeWithLinks(currentItem, genericArray[i], useFullName, useSpecialText); sb.Append(link); if (i + 1 != genericArray.Length) { sb.Append(", "); } } var actualName = currentItem.GetNameOrNameLink(target, useFullName, useSpecialText); if (genericArray.Length > 0) { return($"{actualName}\\<{sb.ToString()}>"); } return(actualName); }
/// <summary> /// Create a full parameter name with links /// </summary> /// <param name="currentItem">The current markdown item with the property to be rendered</param> /// <param name="property">The markdown property</param> /// <param name="useFullName">Determines if the fullName of Markdown property should be used</param> /// <param name="useParameterNames">Determines if parameter names should be shown on property</param> /// <returns>Returns the full parameter name with links rendered in markdown</returns> public static string CreateFullParameterWithLinks(IMarkdownItem currentItem, MarkdownProperty property, bool useFullName, bool useParameterNames) { var fullParameterName = property.InternalItem.ToString(); MarkdownBuilder mb = new MarkdownBuilder(); if (property.FileName != null) { mb.Link(property.Name, currentItem.To(property)); } else { mb.Append(property.Name); } var parts = fullParameterName.Split('[', ']'); var index = property.InternalItem.GetPropertyKeyType(); if (index.Key != null) { mb.Append(" [ "); var link = CreateFullTypeWithLinks(currentItem, index.Key, useFullName, true); mb.Append(link); if (useParameterNames) { mb.Append($" {index.Name}"); } mb.Append(" ]"); } return(mb.ToString()); }
/// <summary> /// Try to get a markdownitem with the given TypeWrapper lookup key /// </summary> /// <param name="wrapper">The key to look up the markdownitem</param> /// <param name="value">If found the IMarkdownItem</param> /// <returns>True if found or false if not found</returns> public bool TryGetValue(TypeWrapper wrapper, out IMarkdownItem value) { return(AllItems.TryGetValue(wrapper.GetId(), out value)); }
/// <summary> /// Cleans a method and adds the appropiate links /// </summary> /// <param name="currentItem">The current markdown item containing the method to be cleaned</param> /// <param name="method">The method to be cleaned</param> /// <param name="useFullName">Determine if full name of method should be shown</param> /// <param name="useParameterNames">Determines if parameter names should be shown</param> /// <param name="parameterList">Determines if parameter names should be listed vertically</param> /// <returns>The cleaned string</returns> public static string CreateFullMethodWithLinks(IMarkdownItem currentItem, MarkdownMethod method, bool useFullName, bool useParameterNames, bool parameterList) { var parameters = method.InternalItem.GetParameters(); MarkdownBuilder mb = new MarkdownBuilder(); if (!parameterList) { if (method.FileName != null) { mb.Link(method.Name, currentItem.To(method)); } else { mb.Append(method.Name); } mb.Append(" ("); } if (parameters.Length > 0) { StringBuilder sb = new StringBuilder(); for (var i = 0; i < parameters.Length; i++) { var type = parameters[i].ParameterType; var link = CreateFullTypeWithLinks(currentItem, type, useFullName, true); if (link.IndexOf('&') > 0) { link = link.Replace("&", ""); sb.Append("out "); } if (!parameterList) { sb.Append(link); if (useParameterNames) { sb.Append($" {parameters[i].Name}"); } } else { if (useParameterNames) { sb.Append($" {parameters[i].Name}"); } sb.Append(link); } if (!parameterList) { if (i + 1 != parameters.Length) { sb.Append(", "); } } else { if (i + 1 != parameters.Length) { sb.Append("<br>"); } } } mb.Append(sb.ToString()); } if (!parameterList) { mb.Append(")"); } return(mb.ToString()); }
public static bool TryAdd(IMarkdownItem item) { return(TryAdd(item.TypeInfo.GetId(), item)); }
public static T As <T>(this IMarkdownItem item) where T : class { return(item as T); }
public static string GetNameOrNameLink(this IMarkdownItem currentType, Type targetType, bool useFullName, bool specialText) { MarkdownBuilder tempMB = new MarkdownBuilder(); if (targetType == null) { return(""); } if (targetType == typeof(void)) { return("[Void]" + "(https://docs.microsoft.com/en-us/dotnet/api/System.Void)"); } if (targetType.FullName == null && targetType.Name == null) { return(""); } // exceptions if (targetType.ToString().Equals("System.Collections.Generic.IEnumerable`1[T]") || targetType.ToString().Equals("System.Collections.Generic.IEnumerable`1[P]")) { return("[IEnumerable]" + "(https://docs.microsoft.com/en-us/dotnet/api/System.Collections.Ienumerable)"); } if (targetType.ToString().Contains("System.Func`3")) { return("[Func]" + "(https://docs.microsoft.com/en-us/dotnet/api/System.Func-3)"); } string name = targetType.Name; string fullName = targetType.ToString(); if (useFullName) { name = Cleaner.CleanName(fullName, false, specialText); } else { name = Cleaner.CleanName(targetType.Name.GetBaseName(), false, specialText); } var link = currentType.GetLink(new TypeWrapper(targetType)); if (link != null) { tempMB.Link(name, link); } else { tempMB.Link(name, currentType.To(currentType)); } if (targetType.IsArray) { tempMB.Append("[]"); } return(tempMB.ToString()); }