Пример #1
0
    internal static RentedCustomHtmlRenderer RentCustomHtmlRenderer(this MarkdownPipeline pipeline)
    {
        HtmlRendererCache  cache    = _rendererCache ??= new(pipeline);
        CustomHtmlRenderer renderer = cache.Get();

        return(new(cache, renderer));
    }
Пример #2
0
    private static string ToHtml(string markdown, MarkdownPipeline pipeline)
    {
        string markdownPreProcessed = MarkdownPreProcessors.Process(markdown);

        MarkdownPipeline GetPipeline()
        {
            var selfPipeline = pipeline.Extensions.Find <SelfPipelineExtension>();

            return(selfPipeline is not null?selfPipeline.CreatePipelineFromInput(markdownPreProcessed) : pipeline);
        }

        pipeline = GetPipeline();

        var document = MarkdownParser.Parse(markdownPreProcessed, pipeline);

        using var rentedRenderer = pipeline.RentCustomHtmlRenderer();
        CustomHtmlRenderer renderer = rentedRenderer.Instance;

        renderer.Render(document);
        renderer.Writer.Flush();
        return(HtmlPostProcessors.Process(renderer.Writer.ToString() ?? string.Empty));
    }
Пример #3
0
 internal RentedCustomHtmlRenderer(HtmlRendererCache cache, CustomHtmlRenderer renderer)
 {
     _cache   = cache;
     Instance = renderer;
 }
Пример #4
0
        public async Task ImportAllBranches()
        {
            var branches = await _githubDataRepository.GetBranches();

            var        versions       = new List <DocVersion>();
            DocVersion defaultVersion = null;

            foreach (var branch in branches)
            {
                _dataRepository.DeleteBranch(branch.Name);

                var version = new DocVersion(BranchNameToPathString(branch.Name), branch.Name);

                versions.Add(version);
                if (branch.IsDefault)
                {
                    defaultVersion = version;
                }

                var archive = await _githubDataRepository.GetArchive(branch);

                using (var memoryStream = new MemoryStream(archive))
                    using (var gzipStream = new GZipStream(memoryStream, CompressionMode.Decompress))
                        using (var tarReader = new TarReader(gzipStream))
                        {
                            string rootDirectory = null;
                            while (tarReader.Next() is var entry && entry != null)
                            {
                                if (entry.Header.Flag == TarHeaderFlag.Directory && rootDirectory == null)
                                {
                                    rootDirectory = entry.Header.Name;
                                }
                                if (entry.Header.Flag == TarHeaderFlag.NormalFile)
                                {
                                    if (Path.GetExtension(entry.Header.Name) == ".md")
                                    {
                                        using (var streamReader = new StreamReader(entry))
                                        {
                                            var markdown = streamReader.ReadToEnd();

                                            var meta = ParseMeta(ref markdown);

                                            // Parse markdown
                                            var document = Markdig.Markdown.Parse(markdown,
                                                                                  new MarkdownPipelineBuilder()
                                                                                  .UseAdvancedExtensions()
                                                                                  .UsePipeTables()
                                                                                  .Build());

                                            var sw       = new StringWriter();
                                            var renderer = new CustomHtmlRenderer(sw);
                                            renderer.Render(document);
                                            sw.Flush();
                                            var html = sw.ToString();

                                            var file = new DocFile
                                            {
                                                Content = html,
                                                Meta    = meta
                                            };

                                            var docPath = entry.Header.Name;

                                            if (rootDirectory != null && docPath.StartsWith(rootDirectory))
                                            {
                                                docPath = docPath.Substring(rootDirectory.Length).TrimStart('/', '\\');
                                            }

                                            var fileName = Path.GetFileNameWithoutExtension(docPath);
                                            docPath = Path.Combine(Path.GetDirectoryName(docPath), fileName);

                                            if (fileName == "index")
                                            {
                                                version.DefaultPage = meta.RedirectPage;
                                            }

                                            meta.LastModification = entry.Header.LastModification ?? DateTime.UtcNow;
                                            meta.EditUrl          = $"{_githubDataRepository.PublicUrl}/blob/{branch.Name}/{docPath}.md";
                                            meta.Title            = meta.Title ?? fileName.Replace('-', ' ');

                                            _dataRepository.StoreDocFile(branch.Name, docPath, file);
                                        }
                                    }
                                    else
                                    {
                                        var assetPath = entry.Header.Name;

                                        if (rootDirectory != null && assetPath.StartsWith(rootDirectory))
                                        {
                                            assetPath = assetPath.Substring(rootDirectory.Length).TrimStart('/', '\\');
                                        }

                                        assetPath = assetPath.ToLower();

                                        // Skip unaccepted asset types
                                        if (_options.Value.AcceptedAssets.Contains(Path.GetExtension(assetPath)))
                                        {
                                            _dataRepository.StoreAsset(branch.Name, assetPath, entry);
                                        }
                                    }
                                }
                            }
                        }
            }

            var docConfig = new DocConfiguration
            {
                Versions       = versions.ToArray(),
                DefaultVersion = defaultVersion
            };

            _dataRepository.StoreDocConfiguration(docConfig);
        }