Beispiel #1
0
        static void Main(string[] args)
        {
            var assembly = Assembly.LoadFile(GetAssemblyPath());

            var apiClasses = ApiClass.GetApiClasses(assembly);
            var prototypes = ApiPrototype.GetApiPrototypes(assembly).Concat(BuiltInTypes.GetBuiltInPrototypes()).ToArray();
            var emitter    = new PageEmitter(apiClasses, prototypes);

            emitter.Emit();
        }
        public string EmitHtml(PageEmitter emitter)
        {
            var methods     = Methods.OrderBy(m => m.Name).ToArray();
            var methodsText = "";

            foreach (var method in methods)
            {
                methodsText += method.EmitHtml(Name) + Environment.NewLine;
            }
            var methodList = "<ul>" +
                             string.Join(Environment.NewLine, methods
                                         .Select(m => $"<li>{PageEmitter.GetImg("method") + PageEmitter.GetImg("static")} <a href=\"#method-{m.Name}\">{m.Name}</a></li>")) +
                             "</ul>";
            var content = emitter.FillTemplate("apiclass",
                                               ("NAME", Name),
                                               ("METHODLIST", methodList),
                                               ("METHODS", methodsText),
                                               ("SOURCELINK", GetSourceLink()));

            return(content);
        }
Beispiel #3
0
        public static string LinkType(string typeName)
        {
            var target = typeName;
            var link   = typeName;
            var isArr  = false;

            if (target.EndsWith("[]"))
            {
                target = target.Substring(0, target.Length - 2);
                isArr  = true;
            }
            if (target.EndsWith("Prototype"))
            {
                var href = "proto-" + PageEmitter.GetClassLink(target.Replace("Prototype", ""));
                link = $"<a href=\"{href}\">{target.Replace("Prototype", "")}</a>";
            }
            else if (new[] { "undefined", "void", "int", "any" }.Contains(target))
            {
                var href = $"doc-{target}.html";
                link = $"<a href=\"{href}\">{target}</a>";
            }
            else if (new[] { "bool", "string", "number", "object" }.Contains(target))
            {
                var href = $"proto-{target}.html";
                if (target == "bool")
                {
                    href = "proto-boolean.html";
                }
                link = $"<a href=\"{href}\">{target}</a>";
            }

            if (isArr)
            {
                link += "<a href=\"proto-array.html\">[]</a>";
            }

            return(link);
        }
        public string EmitHtml(string className)
        {
            var shortClassName = "";

            if (IsStatic)
            {
                shortClassName = className;
            }
            else if (!string.IsNullOrEmpty(className))
            {
                shortClassName = className[0].ToString().ToLower();
            }

            var id  = "method";
            var img = PageEmitter.GetImg("method");

            switch (FunctionType)
            {
            case ScriptFunctionType.Getter:
                id  = "get";
                img = PageEmitter.GetImg("getter", "Getter");
                break;

            case ScriptFunctionType.Setter:
                id  = "set";
                img = PageEmitter.GetImg("setter", "Setter");
                break;

            case ScriptFunctionType.IndexerGet:
                id  = "index-get";
                img = PageEmitter.GetImg("indexer-get", "Indexer");
                break;

            case ScriptFunctionType.IndexerSet:
                id  = "index-set";
                img = PageEmitter.GetImg("indexer-set", "Indexer");
                break;

            case ScriptFunctionType.Constructor:
                img = PageEmitter.GetImg("ctor", "Constructor");
                break;
            }

            if (IsStatic)
            {
                img += PageEmitter.GetImg("static", "Static Method");
            }

            var prefix = "";

            if (IsAccessor)
            {
                prefix = "(" + id + ") ";
            }

            var methodName = Name;

            if (FunctionType == ScriptFunctionType.IndexerGet)
            {
                methodName = "(get) indexer";
            }
            else if (FunctionType == ScriptFunctionType.IndexerSet)
            {
                methodName = "(set) indexer";
            }

            var html = $"<span class=\"method-title\">{img}</span> <b id=\"{id}-{Name}\">{prefix}{methodName}</b><br />";

            if (IsStatic)
            {
                html += "<i>Static</i><br />";
            }
            if (!string.IsNullOrEmpty(Description))
            {
                html += $"<i>{Description}</i><br />";
            }

            if (FunctionType == ScriptFunctionType.Getter)
            {
                if (Signatures.Length == 1)
                {
                    var usageText = $"<span class=\"arg-type\">var</span> {Name} = {shortClassName}.<b>{Name}</b>;";
                    html += $"<br /><div><code>Type: <span class=\"arg-type\">" +
                            string.Join("</span> or <span class=\"arg-type\">", Signatures[0].ReturnTypes
                                        .Select(t => TypeHelper.LinkType(t))) +
                            $"</span></code><code>Usage: {usageText}</code></div>";
                }
            }
            else if (FunctionType == ScriptFunctionType.Setter)
            {
                if (Signatures.Length == 1)
                {
                    var usageText = $"{shortClassName}.<b>{Name}</b> = {Name};";
                    html += $"<br /><div><code>Type: <span class=\"arg-type\">" +
                            string.Join("</span> or <span class=\"arg-type\">", Signatures[0].ReturnTypes
                                        .Select(t => TypeHelper.LinkType(t))) +
                            $"</span></code><code>Usage: {usageText}</code></div>";
                }
            }
            else
            {
                if (Signatures.Length > 1)
                {
                    html += "<br />Signatures:";
                }
                foreach (var signature in Signatures)
                {
                    html += $"<br /><div>";
                    if (FunctionType != ScriptFunctionType.Constructor && FunctionType != ScriptFunctionType.IndexerSet)
                    {
                        html += $"<code>Return: <span class=\"arg-type\">" +
                                string.Join("</span> or <span class=\"arg-type\">", signature.ReturnTypes
                                            .Select(t => TypeHelper.LinkType(t))) +
                                $"</span></code>";
                    }

                    var paramArgs = new List <string>();
                    if (signature.ParamNames.Length > 0)
                    {
                        html += "<code>Arguments: ";
                        if (signature.OptionalNum > 0)
                        {
                            var requiredNum        = signature.ParamNames.Length - signature.OptionalNum;
                            var requiredParamNames = signature.ParamNames.Take(requiredNum).ToArray();
                            var requiredParamTypes = signature.ParamTypes.Take(requiredNum).ToArray();
                            var optionalParamNames = signature.ParamNames.Skip(requiredNum).ToArray();
                            var optionalParamTypes = signature.ParamTypes.Skip(requiredNum).ToArray();
                            for (int i = 0; i < requiredNum; i++)
                            {
                                if (i > 0)
                                {
                                    html += ", ";
                                }
                                html += "<span class=\"arg-type\">" + TypeHelper.LinkType(requiredParamTypes[i]) + "</span> " + requiredParamNames[i];
                                paramArgs.Add(requiredParamNames[i]);
                            }
                            for (int i = 0; i < signature.OptionalNum; i++)
                            {
                                if (i > 0 || requiredNum > 0)
                                {
                                    html += ", ";
                                }
                                html += "[<span class=\"arg-type\">" + TypeHelper.LinkType(optionalParamTypes[i]) + "</span> " + optionalParamNames[i] + "]";
                                paramArgs.Add("[" + optionalParamNames[i] + "]");
                            }
                        }
                        else
                        {
                            for (int i = 0; i < signature.ParamNames.Length; i++)
                            {
                                if (i > 0)
                                {
                                    html += ", ";
                                }
                                html += "<span class=\"arg-type\">" + TypeHelper.LinkType(signature.ParamTypes[i]) + "</span> " + signature.ParamNames[i];
                                paramArgs.Add(signature.ParamNames[i]);
                            }
                        }
                        html += "</code>";
                    }

                    if (FunctionType == ScriptFunctionType.Constructor)
                    {
                        html += $"<code>Usage: <span class=\"arg-type\">var</span> {shortClassName} = <span class=\"arg-type\">new</span> <b>{className}</b>({string.Join(", ", paramArgs)});</code>";
                    }
                    else if (FunctionType == ScriptFunctionType.IndexerGet)
                    {
                        html += $"<code>Usage: <span class=\"arg-type\">var</span> result = {shortClassName}<b>[{string.Join(", ", paramArgs)}]</b>;</code>";
                    }
                    else if (FunctionType == ScriptFunctionType.IndexerSet)
                    {
                        html += $"<code>Usage: {shortClassName}<b>[{string.Join(", ", paramArgs)}]</b> = value;</code>";
                    }
                    else
                    {
                        var accessStr = $"{shortClassName}.<b>{Name}</b>";
                        if (string.IsNullOrEmpty(className))
                        {
                            accessStr = $"<b>{Name}</b>";
                        }
                        if (signature.ReturnTypes.Length == 1 && signature.ReturnTypes[0] == "void")
                        {
                            html += $"<code>Usage: {accessStr}({string.Join(", ", paramArgs)});</code>";
                        }
                        else
                        {
                            html += $"<code>Usage: <span class=\"arg-type\">var</span> result = {accessStr}({string.Join(", ", paramArgs)});</code>";
                        }
                    }

                    html += "</div>";
                }
            }

            html += "<hr />";
            return(html);
        }
Beispiel #5
0
        public string EmitHtml(PageEmitter emitter)
        {
            var ctor           = Methods.FirstOrDefault(m => m.FunctionType == ScriptFunctionType.Constructor);
            var shortClassName = Name[0].ToString().ToLower();
            var ctorText       = $"<code><span class=\"arg-type\">var</span> {shortClassName} = <span class=\"arg-type\">new</span> <b>{Name}</b>();</code>";

            if (ctor.FunctionType == ScriptFunctionType.Constructor)
            {
                ctorText = ctor.EmitHtml(Name);
            }

            var varsIndex    = new List <string>();
            var methodsIndex = new List <string>();
            var getsetIndex  = new List <string>();

            var varsText = "-- No Variables --";

            if (Variables.Length > 0)
            {
                varsText = "";
                foreach (var variable in Variables)
                {
                    varsText += variable.EmitHtml(Name) + Environment.NewLine;
                    varsIndex.Add($"<li>{PageEmitter.GetImg("variable")} <a href=\"#var-{variable.Name}\">{variable.Name}</a></li>");
                }
            }

            var getsetText = "-- No Getters &amp; Setters --";
            var getsets    = Methods
                             .Where(m => m.IsAccessor)
                             .OrderBy(m => m.Name)
                             .GroupBy(m => m.FunctionType);

            if (getsets.Count() > 0)
            {
                getsetText = "";
                foreach (var methodSet in getsets)
                {
                    foreach (var getset in methodSet)
                    {
                        getsetText += getset.EmitHtml(Name) + Environment.NewLine;
                        var getsetid = "get";
                        var img      = PageEmitter.GetImg("getter", "Getter");
                        if (getset.IsStatic)
                        {
                            img += PageEmitter.GetImg("static", "Static Getter");
                        }
                        if (getset.FunctionType == ScriptFunctionType.Setter)
                        {
                            getsetid = "set";
                            img      = PageEmitter.GetImg("setter", "Setter");
                            if (getset.IsStatic)
                            {
                                img += PageEmitter.GetImg("static", "Static Setter");
                            }
                        }
                        getsetIndex.Add($"<li>{img} <a href=\"#{getsetid}-{getset.Name}\">({getsetid}) {getset.Name}</a></li>");
                    }
                }
            }

            var methodsText = "-- No Methods --";
            var methods     = Methods
                              .Where(m => m.FunctionType == ScriptFunctionType.Standard)
                              .OrderBy(m => m.Name).ToArray();

            if (methods.Length > 0)
            {
                methodsText = "";
                foreach (var method in methods)
                {
                    methodsText += method.EmitHtml(Name) + Environment.NewLine;

                    var img = PageEmitter.GetImg("method");
                    if (method.IsStatic)
                    {
                        img += PageEmitter.GetImg("static");
                    }
                    methodsIndex.Add($"<li>{img} <a href=\"#method-{method.Name}\">{method.Name}</a></li>");
                }
            }

            var indexersText    = "-- No Indexers --";
            var hasIndexer      = false;
            var indexerGetIndex = "";
            var indexerSetIndex = "";
            var indexers        = Methods
                                  .Where(m => m.IsIndexer)
                                  .GroupBy(m => m.FunctionType);

            if (indexers.Count() > 0)
            {
                indexersText = "";
                hasIndexer   = true;
                foreach (var group in indexers)
                {
                    foreach (var indexer in group)
                    {
                        if (indexer.FunctionType == ScriptFunctionType.IndexerGet)
                        {
                            indexerGetIndex = $"<li>{PageEmitter.GetImg("indexer-get")} <a href=\"#index-get-{indexer.Name}\">(get) indexer</a></li>";
                        }
                        if (indexer.FunctionType == ScriptFunctionType.IndexerSet)
                        {
                            indexerSetIndex = $"<li>{PageEmitter.GetImg("indexer-set")} <a href=\"#index-set-{indexer.Name}\">(set) indexer</a></li>";
                        }
                        indexersText += indexer.EmitHtml(Name) + Environment.NewLine;
                    }
                }
            }

            var index = $"<ul><li>{PageEmitter.GetImg("ctor")} <a href=\"#ctor\">Constructor</a></li>";

            if (varsIndex.Count > 0)
            {
                index += $"<li>{PageEmitter.GetImg("variable")} <a href=\"#vars\">Variables</a>";
                if (varsIndex.Count > 0)
                {
                    index += "<ul>";
                    index += string.Join(Environment.NewLine, varsIndex);
                    index += "</ul>";
                }
                index += "</li>";
            }
            if (getsetIndex.Count > 0)
            {
                index += $"<li>{PageEmitter.GetImg("accessor", "Getters and Setters")} <a href=\"#get-set\">Getters &amp; Setters</a>";
                if (getsetIndex.Count > 0)
                {
                    index += "<ul>";
                    index += string.Join(Environment.NewLine, getsetIndex);
                    index += "</ul>";
                }
                index += "</li>";
            }
            if (methodsIndex.Count > 0)
            {
                index += $"<li>{PageEmitter.GetImg("method")} <a href=\"#methods\">Methods</a>";
                if (methodsIndex.Count > 0)
                {
                    index += "<ul>";
                    index += string.Join(Environment.NewLine, methodsIndex);
                    index += "</ul>";
                }
                index += "</li>";
            }
            if (hasIndexer)
            {
                index += $"<li>{PageEmitter.GetImg("indexer")} <a href=\"#indexers\">Indexers</a></li><ul>";
                index += indexerGetIndex;
                index += indexerSetIndex;
                index += "</ul>";
            }
            index += "</ul>";

            var description = "";

            if (!string.IsNullOrEmpty(Description))
            {
                description = $"<i>{Description}</i><br />";
            }

            var codeIcon = IsBuiltIn ? "cs" : "vb";

            var content = emitter.FillTemplate("prototype",
                                               ("NAME", Name),
                                               ("INDEX", index),
                                               ("CONSTRUCTOR", ctorText),
                                               ("VARS", varsText),
                                               ("GETTERSSETTERS", getsetText),
                                               ("METHODS", methodsText),
                                               ("SOURCELINK", GetSourceLink()),
                                               ("INDEXERS", indexersText),
                                               ("DESCRIPTION", description),
                                               ("CODEICON", codeIcon));

            return(content);
        }