Beispiel #1
0
        private static async Task <string> GetTocMdAsync(string Branch, string Endpoint = "toc.md", int Level = 0)
        {
            var toc   = (await GetRawFileAsync(Branch, Endpoint)).Split('\n').Select(x => x.TrimEnd('\r')).ToList();
            var tasks = new List <Task>();

            Parallel.For(0, toc.Count, i => {
                tasks.Add(Task.Run(async() =>
                {
                    var href  = AHrefRegex.Match(toc[i]).Value;
                    var title = AInnerTextRegex.Match(toc[i]).Value;

                    if (toc[i].StartsWith("#"))
                    {
                        toc[i] = BuildSharps(Level) + toc[i];
                        if (!string.IsNullOrWhiteSpace(href))
                        {
                            toc[i] = toc[i].Replace(href, Endpoint.Substring(0, Endpoint.Length - "toc.md".Length) + href);
                        }
                    }

                    if (toc[i].EndsWith("toc.md)"))
                    {
                        var cnt = CountLeft(toc[i], '#');
                        if (!string.IsNullOrWhiteSpace(href))
                        {
                            toc[i] = BuildSharps(cnt) + " " + title + "\n" + await GetTocMdAsync(Branch, Endpoint.Substring(0, Endpoint.Length - "toc.md".Length) + href, cnt);
                        }
                    }
                }));
            });
            Task.WaitAll(tasks.ToArray());
            return(string.Join("\n", toc));
        }
        private static string GetTocMd(string Endpoint = "toc.md", int Level = 0)
        {
            var toc   = GetRawFile(Endpoint).Split('\n').Select(x => x.TrimEnd('\r')).ToList();
            var tasks = new List <Task>();

            for (var i = 0; i < toc.Count; i++)
            {
                var href  = AHrefRegex.Match(toc[i]).Value;
                var title = AInnerTextRegex.Match(toc[i]).Value;

                if (toc[i].StartsWith("#"))
                {
                    toc[i] = BuildSharps(Level) + toc[i];
                    if (!string.IsNullOrWhiteSpace(href))
                    {
                        toc[i] = toc[i].Replace(href, Endpoint.Substring(0, Endpoint.Length - "toc.md".Length) + href);
                    }
                }

                if (toc[i].EndsWith("toc.md)"))
                {
                    var cnt = CountLeft(toc[i], '#');
                    if (!string.IsNullOrWhiteSpace(href))
                    {
                        toc[i] = BuildSharps(cnt) + " " + title + "\n" + GetTocMd(Endpoint.Substring(0, Endpoint.Length - "toc.md".Length) + href, cnt);
                    }
                }
            }
            return(string.Join("\n", toc));
        }