Ejemplo n.º 1
0
        static string WriteTypeAsObject(ParsedType type, Dictionary <string, ParsedType> allPublicTypesByShortName, bool asJavascript)
        {
            if (!type.IsPublic || (type.DataType != ParsedDataType.Namespace && string.IsNullOrWhiteSpace(type.Namespace)))
            {
                return(null);
            }
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("  {");
            if (type.DataType == ParsedDataType.Namespace)
            {
                sb.AppendLine(KeyValString(4, "name", type.FullName, asJavascript) + ",");
            }
            else
            {
                sb.AppendLine(KeyValString(4, "namespace", type.Namespace, asJavascript) + ",");
                sb.AppendLine(KeyValString(4, "name", type.Name, asJavascript) + ",");
            }
            sb.Append(KeyValString(4, "dataType", type.DataType.ToString().ToLower(), asJavascript));
            string summary = type.Summary();

            if (!string.IsNullOrWhiteSpace(summary))
            {
                sb.AppendLine(",");
                sb.Append(KeyValString(4, "summary", summary, asJavascript));
            }
            string remarks = type.Remarks();

            if (!string.IsNullOrWhiteSpace(remarks))
            {
                sb.AppendLine(",");
                sb.Append(KeyValString(4, "remarks", remarks, asJavascript));
            }

            if (type.DataType == ParsedDataType.Namespace)
            {
                sb.AppendLine();
            }
            else
            {
                string[] baseList = type.IsClass ? type.GetBaseList(allPublicTypesByShortName) : null;
                if (baseList != null && baseList.Length > 0)
                {
                    sb.AppendLine(",");
                    int firstInterfaceIndex = -1;
                    for (int i = 0; i < baseList.Length; i++)
                    {
                        // TODO: guessing based on .Net naming conventions. I'm sure
                        // this can be improved
                        if (baseList[i].StartsWith("I") && char.IsUpper(baseList[i][1]))
                        {
                            firstInterfaceIndex = i;
                            break;
                        }
                    }
                    if (firstInterfaceIndex != 0)
                    {
                        sb.Append(KeyValString(4, "baseclass", baseList[0], asJavascript));
                    }
                    if (firstInterfaceIndex > -1)
                    {
                        if (firstInterfaceIndex > 0)
                        {
                            sb.AppendLine(",");
                        }
                        if (asJavascript)
                        {
                            sb.Append("    interfaces: [");
                        }
                        else
                        {
                            sb.Append("    \"interfaces\": [");
                        }
                        for (int i = firstInterfaceIndex; i < baseList.Length; i++)
                        {
                            if (i > firstInterfaceIndex)
                            {
                                sb.Append(", ");
                            }
                            sb.Append(JsonQuote(baseList[i], asJavascript, null));
                        }
                        sb.Append("]");
                    }
                }

                if (type.HasSinceTag())
                {
                    sb.AppendLine(",");
                    sb.Append(KeyValString(4, "since", type.Since, asJavascript));
                }
                if (type.HasDeprecatedTag())
                {
                    sb.AppendLine(",");
                    sb.Append(KeyValString(4, "deprecated", type.Deprecated, asJavascript));
                }

                string values       = MembersAsJsonArray(type, ParsedMemberType.EnumValue, asJavascript);
                string constructors = MembersAsJsonArray(type, ParsedMemberType.Constructor, asJavascript);
                string properties   = MembersAsJsonArray(type, ParsedMemberType.Property, asJavascript);
                string methods      = MembersAsJsonArray(type, ParsedMemberType.Method, asJavascript);
                string events       = MembersAsJsonArray(type, ParsedMemberType.Event, asJavascript);
                if (values != null || constructors != null || properties != null || methods != null || events != null)
                {
                    sb.AppendLine(",");
                }
                else
                {
                    sb.AppendLine();
                }

                if (!string.IsNullOrWhiteSpace(values))
                {
                    if (asJavascript)
                    {
                        sb.AppendLine($"    values: {values}");
                    }
                    else
                    {
                        sb.AppendLine($"    \"values\": {values}");
                    }
                }
                if (!string.IsNullOrWhiteSpace(constructors))
                {
                    if (asJavascript)
                    {
                        sb.Append($"    constructors: {constructors}");
                    }
                    else
                    {
                        sb.Append($"    \"constructors\": {constructors}");
                    }
                    if (properties != null || methods != null || events != null)
                    {
                        sb.AppendLine(",");
                    }
                    else
                    {
                        sb.AppendLine();
                    }
                }
                if (!string.IsNullOrWhiteSpace(properties))
                {
                    if (asJavascript)
                    {
                        sb.Append($"    properties: {properties}");
                    }
                    else
                    {
                        sb.Append($"    \"properties\": {properties}");
                    }

                    if (methods != null || events != null)
                    {
                        sb.AppendLine(",");
                    }
                    else
                    {
                        sb.AppendLine();
                    }
                }
                if (!string.IsNullOrWhiteSpace(methods))
                {
                    if (asJavascript)
                    {
                        sb.Append($"    methods: {methods}");
                    }
                    else
                    {
                        sb.Append($"    \"methods\": {methods}");
                    }

                    if (events != null)
                    {
                        sb.AppendLine(",");
                    }
                    else
                    {
                        sb.AppendLine();
                    }
                }
                if (!string.IsNullOrWhiteSpace(events))
                {
                    if (asJavascript)
                    {
                        sb.AppendLine($"    events: {events}");
                    }
                    else
                    {
                        sb.AppendLine($"    \"events\": {events}");
                    }
                }
            }
            sb.Append("  }");
            return(sb.ToString());
        }
Ejemplo n.º 2
0
        public static void WriteTypes(Dictionary <string, ParsedType> types, string outputDirectory)
        {
            var    di      = new System.IO.DirectoryInfo(outputDirectory);
            string apibase = di.Name;

            var parallelOptions = new ParallelOptions {
                MaxDegreeOfParallelism = 100
            };

            Parallel.ForEach(types, parallelOptions, (keyValue) =>
            {
                ParsedType basetype = keyValue.Value;
                if (!basetype.IsPublic)
                {
                    return;
                }

                var content = new StringBuilder();
                content.AppendLine("---");
                content.AppendLine($"title: \"{basetype.Name}\"");
                content.AppendLine($"date: {DateTime.Now.ToString("u")}");
                content.AppendLine("draft: false");
                content.AppendLine("---");
                content.AppendLine();

                content.AppendLine($"*Namespace: [{basetype.Namespace}](../)*");
                content.AppendLine();
                string baseTypeSummary = basetype.Summary();
                if (!string.IsNullOrEmpty(baseTypeSummary))
                {
                    content.AppendLine(baseTypeSummary);
                }

                if (basetype.IsClass)
                {
                    content.AppendLine("```cs");
                    string[] attributes = basetype.GetAttributes();
                    for (int i = 0; i < attributes.Length; i++)
                    {
                        content.AppendLine(attributes[i]);
                    }

                    content.Append($"public class {basetype.Name}");
                    string[] baseList = basetype.GetBaseList(null);
                    if (baseList != null)
                    {
                        for (int i = 0; i < baseList.Length; i++)
                        {
                            if (i == 0)
                            {
                                content.Append($": {baseList[i]}");
                            }
                            else
                            {
                                content.Append($", {baseList[i]}");
                            }
                        }
                    }
                    content.AppendLine();
                    content.AppendLine("```");
                }

                if (basetype.Members == null)
                {
                    return; // TODO: Figure out this case
                }
                ParsedMemberType state = ParsedMemberType.None;
                foreach (var item in basetype.Members)
                {
                    if (item.IsEvent && state != ParsedMemberType.Event)
                    {
                        content.AppendLine("## Events");
                        state = ParsedMemberType.Event;
                    }
                    if (item.IsProperty && state != ParsedMemberType.Property)
                    {
                        content.AppendLine("## Properties");
                        state = ParsedMemberType.Property;
                    }
                    if (item.IsMethod && state != ParsedMemberType.Method)
                    {
                        content.AppendLine("## Methods");
                        state = ParsedMemberType.Method;
                    }
                    if (item.IsConstructor && state != ParsedMemberType.Constructor)
                    {
                        content.AppendLine("## Constructors");
                        state = ParsedMemberType.Constructor;
                    }
                    content.AppendLine();
                    string signature = item.Signature(false);
                    var returntype   = item.ReturnType(types.Values);
                    if (returntype != null && returntype != item.ParentType)
                    {
                        string rn   = returntype.Name;
                        int index   = signature.IndexOf(rn);
                        string link = ParsedTypePath(apibase, returntype);
                        string s    = "";
                        if (index > 0)
                        {
                            s = signature.Substring(0, index);
                        }
                        s        += $"[{signature.Substring(index, rn.Length)}]({link}){signature.Substring(index + rn.Length)}";
                        signature = s;
                    }
                    content.AppendLine(signature);
                    content.AppendLine($": {item.Summary()}");
                    string returnString = item.ReturnDocString();
                    if (!string.IsNullOrWhiteSpace(returnString))
                    {
                        content.AppendLine($": Returns - {returnString}");
                    }
                    if (!item.Since.Equals("5.0")) // no need to add since tags initial items
                    {
                        content.AppendLine($": since {item.Since}");
                    }
                }

                string name      = basetype.Name;
                string directory = OutputDirectoryFromNamespace(outputDirectory, basetype.Namespace);
                string path      = System.IO.Path.Combine(directory, name.ToLower() + ".md");
                if (WriteContent(content, path, true))
                {
                    Console.WriteLine($"(write) {name}");
                }
                else
                {
                    Console.WriteLine($"(no change) {name}");
                }
            });
        }