コード例 #1
0
        private static MethodResult ParseAndWriteMethods(HtmlDocument doc, string source)
        {
            var dependencies = new List <string>();
            var writer       = new StringWriter();
            var selector     = "Methods";

            writer.WriteLine($"\t//{selector}");
            var a = doc.DocumentNode.SelectSingleNode($@"//*[@id=""main""]/section/article/h3[text() = '{selector}']/following-sibling::dl");

            if (a == null)
            {
                return new MethodResult {
                           part = writer.ToString(), dependencies = new List <string>()
                }
            }
            ;
            foreach (var dt in a.SelectNodes(".//dt"))
            {
                var method = dt.SelectSingleNode(".//div/h4");

                var memberName = method.Id.Trim(' ', '.');

                var staticMember = method.SelectSingleNode(".//span[@class='type-signature attribute-static']");
                var signature    = method.SelectSingleNode(".//span[@class='signature']").InnerText;
                var type         = method.SelectSingleNode(".//span[@class='type-signature returnType']");
                var typeList     = TypeReader(type);
                typeList = extractDependencies(dependencies, typeList);

                var optionsParser   = new OptionsWriter(memberName, source);
                var signatureParams = optionsParser.GetSignatureTypes(dt.SelectSingleNode(".//following-sibling::dd"));

                var optionalFound = false;
                signature = $@"({string.Join(", ", signature.Split(',')
                    .Select(s => s.Trim(')', '(', ' '))
                    .Select(s =>
                    {


                        if (signatureParams.ContainsKey(s))
                        {

                            return $"{s + (optionalFound ? "?" : "")} : { extractDependencies(dependencies,signatureParams[s].Replace("Object", "any"))}";
                        }
                        if (signatureParams.ContainsKey(s + "?"))
                        {
                            optionalFound = true;
                            return $" { s }? : { extractDependencies(dependencies, signatureParams[s + "?"].Replace("Object", "any")) } ";
                        }
                        return s;

                    }))})";


                writer.WriteLine($"\t{(staticMember == null ? "" : "static ")}{memberName}{signature.Replace("arguments", "args")} : {typeList}");
            }

            return(new MethodResult {
                part = writer.ToString(), dependencies = dependencies.Distinct().ToList()
            });
        }
コード例 #2
0
        private static MethodResult ParseAndWriteMethods(HtmlDocument doc,string source)
        {
            var dependencies = new List<string>();
            var writer = new StringWriter();
            var selector = "Methods";
            writer.WriteLine($"\t//{selector}");
            var a = doc.DocumentNode.SelectSingleNode($@"//*[@id=""main""]/section/article/h3[text() = '{selector}']/following-sibling::dl");
            if (a == null)
                return new MethodResult { part = writer.ToString(), dependencies=new List<string>() };
            foreach (var dt in a.SelectNodes(".//dt"))
            {
                var method = dt.SelectSingleNode(".//div/h4");
                var memberName = method.Id.Trim(' ', '.');

                var staticMember = method.SelectSingleNode(".//span[@class='type-signature attribute-static']");
                var signature = method.SelectSingleNode(".//span[@class='signature']").InnerText;
                var type = method.SelectSingleNode(".//span[@class='type-signature returnType']");
                var typeList =TypeReader(type);
                typeList = extractDependencies(dependencies, typeList);

                var optionsParser = new OptionsWriter(memberName, source);
                var signatureParams = optionsParser.GetSignatureTypes(dt.SelectSingleNode(".//following-sibling::dd"));

                var optionalFound = false;
                signature = $@"({string.Join(", ", signature.Split(',')
                    .Select(s => s.Trim(')', '(', ' '))
                    .Select(s =>
                    {


                        if (signatureParams.ContainsKey(s))
                        {

                            return $"{s + (optionalFound ? "?" : "")} : { extractDependencies(dependencies,signatureParams[s].Replace("Object", "any"))}";
                        }
                        if (signatureParams.ContainsKey(s + "?"))
                        {
                            optionalFound = true;
                            return $"{s}? : {extractDependencies(dependencies, signatureParams[s + "?"].Replace("Object", "any"))}";
                        }
                        return s;

                    }))})";


                writer.WriteLine($"\t{(staticMember == null ? "" : "static ")}{memberName}{signature.Replace("arguments", "args")} : {typeList}");

            }

            return new MethodResult { part = writer.ToString(), dependencies = dependencies.Distinct().ToList() };
        }
コード例 #3
0
        static string ExtractCLass(string url)
        {
            if (urlsToClass.ContainsKey(url))
                return urlsToClass[url];

            urlsToClass.Add(url, Path.GetFileNameWithoutExtension(url));
            Console.WriteLine($"Dowloadinging {url}");
            HtmlDocument doc = GetDocument(url);

            var sourceLinkNode = doc.DocumentNode.SelectSingleNode(@"//*[@id=""main""]/section/article/div/dd/dl/div") ?? doc.DocumentNode.SelectSingleNode(@"//*[@id=""main""]/section/article/div/dl/div");
            var source = sourceLinkNode.SelectSingleNode(".//a").InnerText.Substring(0, sourceLinkNode.SelectSingleNode(".//a").InnerText.LastIndexOf(","));
            Console.WriteLine($"Source : {source}");

            var classdt = doc.DocumentNode.SelectSingleNode(@"//*[@id=""main""]/section/article/div/dt");
            var signatureName = Path.GetFileNameWithoutExtension(url);
            var signature = "()";
            var signatureReturnType = " : void";
            var dependencies = new List<string>();
            StreamWriter writer = null;
            if (classdt != null)
            {
                var ctor = classdt.SelectSingleNode(".//div/h4");
                signatureName = ctor.GetAttributeValue("id", null);

                if (nameMaps.ContainsKey(signatureName))
                    signatureName = nameMaps[signatureName];

                writer = GetWriter(signatureName, source);

                var optionsParser = new OptionsWriter(signatureName,source);

                var signatureParams = optionsParser.GetSignatureTypes(classdt.SelectSingleNode(".//following-sibling::dd"));
                var signatureNode = ctor.SelectSingleNode(@".//span[@class=""signature"" ]");
                signature = signatureOverrides.ContainsKey(signatureName) ? signatureOverrides[signatureName] : signatureNode.InnerText;
              

                var signatureReturnNode = ctor.SelectSingleNode(@".//span[@class=""type-signature returnType"" ]");
                if (signatureReturnNode != null)
                    signatureReturnType = " : " + ArrayTypeFixer(TypeNormalizer(signatureReturnNode.InnerText));
                if (signatureOverrides.ContainsKey(signatureName))
                    signatureReturnType = "";

                if (!signatureOverrides.ContainsKey(signatureName) && signatureParams.Any())
                {
                    var optionals = (signatureNode.SelectNodes(@".//span[@class=""optional"" ]") ?? new HtmlNodeCollection(signatureNode))
                        .Select(o => o.InnerText).ToArray();

                    foreach (var ctorParam in signatureParams)
                    {
                        var name = ctorParam.Key;
                        var types = ctorParam.Value.Replace("Object", "any");
                        
                        types = extractDependencies(dependencies, types);


                        signature = signature.Replace(name.TrimEnd('?'), $"{name} : {types}");

                    }
                }
            }

            if(writer == null)
                writer = GetWriter(signatureName, source); 


            var methods = ParseAndWriteMethods(doc, source);
            
           

            if (Char.IsLower(signatureName.First()))
            {
                var members = ParseAndWriteMembers(doc, true);
                WriteDependencies(signatureName, dependencies, writer, methods, members,source);

                //var interfaceName = $"{signatureName.Substring(0, 1).ToUpper()}{signatureName.Substring(1)}Static";

                //writer.WriteLine($"interface {interfaceName}");
                //writer.WriteLine("{");
                //writer.WriteLine($"\t{signature}{signatureReturnType};");
                //writer.WriteLine(members.part);
                //writer.WriteLine();
                //writer.WriteLine(methods.part);
                //writer.WriteLine("}");
                //  writer.WriteLine($"export var {signatureName} : {interfaceName}");
                

                writer.WriteLine($"function {signatureName}{signature}{signatureReturnType};");
                writer.WriteLine($"export = {signatureName}");
            }
            else
            {

                var members = ParseAndWriteMembers(doc, false);
                var extends = classExtents.ContainsKey(signatureName) ? classExtents[signatureName] : "";
                extends = extractDependencies(dependencies, extends);
                WriteDependencies(signatureName, dependencies, writer, methods, members,source);

              
                
                writer.WriteLine($"class {signatureName} {extends}");
                writer.WriteLine("{");
                writer.WriteLine($"\tconstructor{signature};");
                writer.WriteLine(members.part);
                writer.WriteLine();
                writer.WriteLine(methods.part);
                writer.WriteLine("}");
                writer.WriteLine($"export = {signatureName}");
            }


            return signatureName;
        }
コード例 #4
0
        static string ExtractCLass(string url)
        {
            if (urlsToClass.ContainsKey(url))
            {
                return(urlsToClass[url]);
            }

            urlsToClass.Add(url, Path.GetFileNameWithoutExtension(url));
            Console.WriteLine($"Dowloadinging {url}");
            HtmlDocument doc = GetDocument(url);

            var sourceLinkNode = doc.DocumentNode.SelectSingleNode(@"//*[@id=""main""]/section/article/div/dd/dl/div") ?? doc.DocumentNode.SelectSingleNode(@"//*[@id=""main""]/section/article/div/dl/div");
            var source         = sourceLinkNode.SelectSingleNode(".//a").InnerText.Substring(0, sourceLinkNode.SelectSingleNode(".//a").InnerText.LastIndexOf(","));

            Console.WriteLine($"Source : {source}");

            var          classdt             = doc.DocumentNode.SelectSingleNode(@"//*[@id=""main""]/section/article/div/dt");
            var          signatureName       = Path.GetFileNameWithoutExtension(url);
            var          signature           = "()";
            var          signatureReturnType = " : void";
            var          dependencies        = new List <string>();
            StreamWriter writer = null;

            if (classdt != null)
            {
                var ctor = classdt.SelectSingleNode(".//div/h4");
                signatureName = ctor.GetAttributeValue("id", null);

                if (nameMaps.ContainsKey(signatureName))
                {
                    signatureName = nameMaps[signatureName];
                }

                writer = GetWriter(signatureName, source);

                var optionsParser = new OptionsWriter(signatureName, source);

                var signatureParams = optionsParser.GetSignatureTypes(classdt.SelectSingleNode(".//following-sibling::dd"));
                var signatureNode   = ctor.SelectSingleNode(@".//span[@class=""signature"" ]");
                signature = signatureOverrides.ContainsKey(signatureName) ? signatureOverrides[signatureName] : signatureNode.InnerText;


                var signatureReturnNode = ctor.SelectSingleNode(@".//span[@class=""type-signature returnType"" ]");
                if (signatureReturnNode != null)
                {
                    signatureReturnType = " : " + ArrayTypeFixer(TypeNormalizer(signatureReturnNode.InnerText));
                }
                if (signatureOverrides.ContainsKey(signatureName))
                {
                    signatureReturnType = "";
                }

                if (!signatureOverrides.ContainsKey(signatureName) && signatureParams.Any())
                {
                    var optionals = (signatureNode.SelectNodes(@".//span[@class=""optional"" ]") ?? new HtmlNodeCollection(signatureNode))
                                    .Select(o => o.InnerText).ToArray();

                    foreach (var ctorParam in signatureParams)
                    {
                        var name  = ctorParam.Key;
                        var types = ctorParam.Value.Replace("Object", "any");

                        types = extractDependencies(dependencies, types);


                        signature = signature.Replace(name.TrimEnd('?'), $"{name} : {types}");
                    }
                }
            }

            if (writer == null)
            {
                writer = GetWriter(signatureName, source);
            }


            var methods = ParseAndWriteMethods(doc, source);



            if (Char.IsLower(signatureName.First()))
            {
                var members = ParseAndWriteMembers(doc, true);
                WriteDependencies(signatureName, dependencies, writer, methods, members, source);

                //var interfaceName = $"{signatureName.Substring(0, 1).ToUpper()}{signatureName.Substring(1)}Static";

                //writer.WriteLine($"interface {interfaceName}");
                //writer.WriteLine("{");
                //writer.WriteLine($"\t{signature}{signatureReturnType};");
                //writer.WriteLine(members.part);
                //writer.WriteLine();
                //writer.WriteLine(methods.part);
                //writer.WriteLine("}");
                //  writer.WriteLine($"export var {signatureName} : {interfaceName}");


                writer.WriteLine($"function {signatureName}{signature}{signatureReturnType};");
                writer.WriteLine($"export = {signatureName}");
            }
            else
            {
                var members = ParseAndWriteMembers(doc, false);
                var extends = classExtents.ContainsKey(signatureName) ? classExtents[signatureName] : "";
                extends = extractDependencies(dependencies, extends);
                WriteDependencies(signatureName, dependencies, writer, methods, members, source);



                writer.WriteLine($"class {signatureName} {extends}");
                writer.WriteLine("{");
                writer.WriteLine($"\tconstructor{signature};");
                writer.WriteLine(members.part);
                writer.WriteLine();
                writer.WriteLine(methods.part);
                writer.WriteLine("}");
                writer.WriteLine($"export = {signatureName}");
            }


            return(signatureName);
        }