public static List <ReferencePage> ParsePages(string path)
        {
            var pages = new Dictionary <string, ReferencePage>();

            using (var stream = GetStreamFromPath(path))
                using (var document = JsonDocument.Parse(stream))
                {
                    var i = 0;
                    foreach (var element in document.RootElement.EnumerateArray())
                    {
                        var lang = element.GetProperty("lang").GetString();

                        // skip anything other than console examples for now, such
                        // as console-result.
                        if (lang != "console")
                        {
                            continue;
                        }

                        var sourceLocation = element.GetProperty("source_location");
                        var file           = sourceLocation.GetProperty("file").GetString();
                        var match          = NameRegex.Match(file);
                        if (!match.Success)
                        {
                            Console.WriteLine($"Could not find title match, index {i}");
                            continue;
                        }

                        var name       = match.Groups["name"].Value;
                        var lineNumber = sourceLocation.GetProperty("line").GetInt32();
                        var hash       = element.GetProperty("digest").GetString();

                        var languages = element.GetProperty("found")
                                        .EnumerateArray()
                                        .Select(s => new Language {
                            Name = s.GetString(), Implemented = true
                        })
                                        .ToList();

                        var content = element.GetProperty("source").GetString();

                        if (!pages.TryGetValue(name, out var page))
                        {
                            page = new ReferencePage(name);
                            pages.Add(name, page);
                        }

                        var example = new ReferenceExample(hash, lineNumber, content);
                        example.Languages.AddRange(languages);

                        if (!page.Examples.Contains(example))
                        {
                            page.Examples.Add(example);
                        }

                        i++;
                    }
                }

            return(pages.Values.ToList());
        }
Esempio n. 2
0
        public static List <ReferencePage> ParsePages(string path)
        {
            var pages = new Dictionary <string, ReferencePage>();

            using (var stream = GetStreamFromPath(path))
                using (var document = JsonDocument.Parse(stream))
                {
                    var i = 0;
                    foreach (var element in document.RootElement.EnumerateArray())
                    {
                        var lang = element.GetProperty("lang").GetString();

                        // skip anything other than console examples for now, such
                        // as console-result.
                        if (lang != "console")
                        {
                            continue;
                        }

                        var sourceLocation = element.GetProperty("source_location");
                        var file           = sourceLocation.GetProperty("file").GetString();
                        var match          = NameRegex.Match(file);
                        if (!match.Success)
                        {
                            Console.WriteLine($"Could not find title match, index {i}");
                            continue;
                        }

                        var name       = match.Groups["name"].Value;
                        var lineNumber = sourceLocation.GetProperty("line").GetInt32();
                        var hash       = element.GetProperty("digest").GetString();

                        var languages = element.GetProperty("found")
                                        .EnumerateArray()
                                        .Select(s => new Language {
                            Name = s.GetString(), Implemented = true
                        })
                                        .ToList();

                        var content = element.GetProperty("source").GetString();

                        if (!pages.TryGetValue(name, out var page))
                        {
                            page = new ReferencePage(name);
                            pages.Add(name, page);
                        }

                        var methodName     = $"Line{lineNumber}";
                        var foundDuplicate = false;

                        // do we have duplicate line numbers with different examples?
                        while (page.Examples.Any(e => e.Name == methodName && e.Hash != hash))
                        {
                            if (!foundDuplicate)
                            {
                                foundDuplicate = true;
                                Console.WriteLine($"Found duplicate line {lineNumber} in {name}");
                            }

                            match = ReferencePageNameRegex.Match(methodName);
                            if (match.Success)
                            {
                                var increment = int.Parse(match.Groups["increment"].Value);
                                methodName = match.Groups["prefix"].Value + (increment + 1);
                            }
                            else
                            {
                                methodName += "_2";
                            }
                        }

                        var example = new ReferenceExample(file, hash, lineNumber, methodName, content);
                        example.Languages.AddRange(languages);

                        if (!page.Examples.Contains(example))
                        {
                            page.Examples.Add(example);
                        }

                        i++;
                    }
                }

            return(pages.Values.ToList());
        }
        public static List <ReferencePage> ParsePages(string path)
        {
            var file  = GetLinesFromPath(path);
            var pages = new Dictionary <string, ReferencePage>();

            for (var index = 0; index < file.Length; index++)
            {
                var line = file[index];
                if (line.StartsWith("=== "))
                {
                    var match = TitleRegex.Match(line);
                    if (!match.Success)
                    {
                        Console.WriteLine($"Could not find title match, line: {index}, input: {line}");
                        continue;
                    }

                    var name       = match.Groups["name"].Value;
                    var lineNumber = int.Parse(match.Groups["lineNumber"].Value);
                    var hash       = match.Groups["hash"].Value;

                    // skip to start of body
                    index += 3;
                    line   = file[index];
                    var builder = new StringBuilder();

                    while (line != "----")
                    {
                        builder.AppendLine(line);
                        index++;
                        line = file[index];
                    }

                    var content = builder.ToString();

                    index += 2;
                    line   = file[index];

                    var languages = line.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                    index += 2;
                    line   = file[index];
                    var implemented = line.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);

                    var exampleLanguages = languages
                                           .Zip(implemented, (l, i) => new Language {
                        Name = l.Trim(), Implemented = i.Trim() == "&check;"
                    })
                                           .ToList();

                    index++;

                    if (!pages.TryGetValue(name, out var page))
                    {
                        page = new ReferencePage(name);
                        pages.Add(name, page);
                    }

                    var example = new ReferenceExample(hash, lineNumber, content);
                    example.Languages.AddRange(exampleLanguages);

                    if (!page.Examples.Contains(example))
                    {
                        page.Examples.Add(example);
                    }
                }
            }

            return(pages.Values.ToList());
        }