/// <summary>
        /// Builds the markdown method page content
        /// </summary>
        /// <param name="item">The markdown method item</param>
        /// <returns>The markdown content</returns>
        public string BuildPage(MarkdownMethod item)
        {
            Dictionary <string, string> parameterPairs = new Dictionary <string, string>();
            Dictionary <string, string> returnPairs    = new Dictionary <string, string>();

            XmlDocumentComment[] comments = new XmlDocumentComment[0];
            MarkdownBuilder      mb       = new MarkdownBuilder();


            var name = Cleaner.CreateFullMethodWithLinks(item, item.As <MarkdownMethod>(), false, true, true);
            // method name + params name with type
            var FullName = item.Name + name;

            var typeZeroHeaders = new[] { "Return", "Name" };


            mb.HeaderWithLink(1, item.FullName, item.To(item));
            mb.AppendLine();


            mb.AppendLine(item.Summary);

            BuildTable(mb, item, typeZeroHeaders, item);

            mb.Append("#### Parameters");
            mb.AppendLine();

            if (File.Exists(MarkdownItemBuilder.xmlPath))
            {
                comments = VSDocParser.ParseXmlParameterComment(XDocument.Parse(File.ReadAllText(MarkdownItemBuilder.xmlPath)), "");

                foreach (var comment in comments)
                {
                    foreach (var param in item.Parameters)
                    {
                        var foundParameterComment = comment.Parameters.FirstOrDefault(x => x.Key == param.Name).Value;
                        if (foundParameterComment != null)
                        {
                            foundParameterComment = foundParameterComment.Substring(0, foundParameterComment.LastIndexOf('<'));
                            foundParameterComment = foundParameterComment.Substring(foundParameterComment.IndexOf('>') + 1);

                            var MethodName = Cleaner.CleanName(comment.MemberName, false, false);

                            // method name + param name + parameter summary
                            if (!parameterPairs.ContainsKey(MethodName + " " + param.Name))
                            {
                                parameterPairs.Add(MethodName + " " + param.Name, foundParameterComment);
                            }
                        }
                    }
                }
            }


            var numberOfParameters = item.Parameters.Length;

            for (int i = 1; i <= numberOfParameters; i++)
            {
                if (i == numberOfParameters)
                {
                    ConstructParameter(mb, FullName, parameterPairs, false, i);
                }
                else
                {
                    ConstructParameter(mb, FullName, parameterPairs, true, i);
                }
            }

            mb.AppendLine();
            mb.Append("#### Returns");
            mb.AppendLine();

            Type lookUpType = null;

            if (item.ItemType == MarkdownItemTypes.Method)
            {
                lookUpType = item.As <MarkdownMethod>().ReturnType;
            }
            var    returned           = Cleaner.CreateFullTypeWithLinks(item, lookUpType, false, false);
            string foundReturnComment = string.Empty;

            if (File.Exists(MarkdownItemBuilder.xmlPath))
            {
                comments = VSDocParser.ParseXmlComment(XDocument.Parse(File.ReadAllText(MarkdownItemBuilder.xmlPath)), "");
                if (comments != null)
                {
                    foreach (var k in comments)
                    {
                        k.MemberName = Cleaner.CleanName(k.MemberName, false, false);
                        returnPairs[k.MemberName] = k.Returns;
                    }
                    foundReturnComment = returnPairs.FirstOrDefault(x => x.Key == item.Name).Value;
                }
            }
            foundReturnComment = Regex.Replace(foundReturnComment, @"<see cref=""\w:([^\""]*)""\s*\/>", m => VSDocParser.ResolveSeeElement(m, ""));
            Console.WriteLine(returned);
            Console.WriteLine(foundReturnComment);
            mb.Append(returned);
            mb.AppendLine("<br>");
            mb.Append(foundReturnComment);

            return(mb.ToString());
        }
        /// <summary>
        /// Builds the page of a MarkdownType and return the rendered markdown
        /// </summary>
        /// <param name="item">The markdown item to be rendered</param>
        /// <returns>The markdown text</returns>
        public string BuildPage(MarkdownType item)
        {
            DefaultTheme.ThemeLogger?.LogDebug("Building Type Page");
            var mb = new MarkdownBuilder();

            mb.HeaderWithCode(1, Cleaner.CreateFullTypeWithLinks(item, item.InternalType, false, false));

            mb.AppendLine();

            item.BuildNamespaceLinks(item.Namespace, mb);

            if (_options.ShowAssembly)
            {
                mb.Append("Assembly: ").AppendLine(item.InternalType.Module.Name).AppendLine();
            }


            bool firstInterface = true;

            foreach (var interfaceItem in item.InternalType.GetInterfaces())
            {
                if (firstInterface)
                {
                    firstInterface = false;
                    mb.Append("Implements ");
                }
                else
                {
                    mb.Append(", ");
                }

                var link = Cleaner.CreateFullTypeWithLinks(item, interfaceItem, false, false);
                if (string.IsNullOrEmpty(link))
                {
                    mb.Link(Cleaner.CleanName(interfaceItem.Name, false, false), "");
                }
                else
                {
                    mb.Append(link);
                }
            }

            if (!firstInterface)
            {
                mb.AppendLine().AppendLine();
            }

            if (!string.IsNullOrEmpty(item.Summary))
            {
                mb.Header(2, "Summary");
                mb.AppendLine(item.Summary);
            }


            mb.AppendLine();
            var typeZeroHeaders = new[] { "Name", "Summary" };
            var typeOneHeaders  = new[] { "Type", "Name", "Summary" };
            var typeTwoHeaders  = new[] { "Return", "Name", "Summary" };

            BuildTable(mb, "Static Constructors", item.GetConstructors(true), typeZeroHeaders, item);
            BuildTable(mb, "Constructors", item.GetConstructors(false), typeZeroHeaders, item);

            BuildTable(mb, "Fields", item.GetFields(false), typeZeroHeaders, item);
            BuildTable(mb, "Properties", item.GetProperties(false), typeZeroHeaders, item);
            BuildTable(mb, "Methods", item.GetMethods(false), typeZeroHeaders, item);
            BuildTable(mb, "Events", item.GetEvents(false), typeZeroHeaders, item);

            BuildTable(mb, "Static Fields", item.GetFields(true), typeZeroHeaders, item);
            BuildTable(mb, "Static Properties", item.GetProperties(true), typeZeroHeaders, item);
            BuildTable(mb, "Static Methods", item.GetMethods(true), typeZeroHeaders, item);
            BuildTable(mb, "Static Events", item.GetEvents(true), typeZeroHeaders, item);

            return(mb.ToString());
        }
Ejemplo n.º 3
0
        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());
        }
        private void BuildTable(MarkdownBuilder mb, string label, IMarkdownItem[] items, string[] headers, MarkdownType mdType)
        {
            if (items.Any())
            {
                mb.Header(2, label);
                mb.AppendLine();

                var seq = items.OrderBy(x => x.Name);

                List <string[]> data = new List <string[]>();

                foreach (var item in seq)
                {
                    string[] dataValues = new string[headers.Length];


                    Type lookUpType = null;

                    if (item.ItemType == MarkdownItemTypes.Method)
                    {
                        lookUpType = item.As <MarkdownMethod>().ReturnType;
                    }
                    else if (item.ItemType == MarkdownItemTypes.Constructor)
                    {
                        lookUpType = null;
                    }
                    else
                    {
                        lookUpType = item.As <IMarkdownTypePartValue>().Type;
                    }



                    if (item.ItemType == MarkdownItemTypes.Constructor)
                    {
                        dataValues[0] = Cleaner.CreateFullConstructorsWithLinks(mdType, item.As <MarkdownConstructor>(), false, _options.ShowParameterNames);
                    }
                    else
                    {
                        dataValues[0] = Cleaner.CreateFullTypeWithLinks(mdType, lookUpType, false, false);
                    }

                    string name    = item.FullName;
                    string summary = item.Summary;



                    if (item.ItemType == MarkdownItemTypes.Method)
                    {
                        name = Cleaner.CreateFullMethodWithLinks(mdType, item.As <MarkdownMethod>(), false, _options.ShowParameterNames, 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[0] = name;

                    if (headers.Length > 1)
                    {
                        dataValues[1] = item.Summary;
                    }

                    data.Add(dataValues);
                }

                mb.Table(headers, data, true);
                mb.AppendLine();
            }
        }