Example #1
0
        public async Task PrepareAssets(DocsSiteRouter router)
        {
            foreach (var(path, contentItem) in _uiBundle.GetContentItems(new Path[]
            {
                "**/*.js",
                "**/*.css",
                "**/*.png",
                "**/*.jpg",
                "**/*.gif"
            }))
            {
                var  xref  = new Xref(_uiBundle.Version, _uiBundle.Id, path);
                Path route = router.GenerateRoute(xref)
                             ?? throw new InvalidOperationException($"Could not generate route for '{xref}'.");

                await using var inputStream = await contentItem.File.OpenRead();

                await _output.GetOrCreateDirectory(route.GetDirectoryPath());

                var outputFile = await _output.GetOrCreateFile(route);

                var outputStream = await outputFile.OpenWrite();

                await inputStream.CopyToAsync(outputStream);
            }
        }
Example #2
0
        private async Task ComposeIndexPage(Site site)
        {
            var redirectoToPage = site.Definition.IndexPage;

            string target = string.Empty;

            if (redirectoToPage.IsXref)
            {
                var xref          = redirectoToPage.Xref.Value;
                var targetSection = site.GetSectionByXref(xref);

                if (targetSection == null)
                {
                    throw new InvalidOperationException(
                              $"Cannot generate site index page. " +
                              $"Target section of '{redirectoToPage}' not found.");
                }

                var router = new DocsSiteRouter(site, targetSection);
                target = router.GenerateRoute(xref) ?? string.Empty;
            }
            else
            {
                target = redirectoToPage.Uri ?? string.Empty;
            }


            var generatedHtml = string.Format(
                PageComposer.RedirectPageHtml,
                site.BasePath,
                target);

            // create output dir for page
            Path targetFilePath = "index.html";

            if (targetFilePath == new Path(target))
            {
                throw new InvalidOperationException(
                          $"Cannot generate a index.html redirect page '{targetFilePath}'. " +
                          $"Redirect would point to same file as the generated file and would" +
                          "end in a endless loop");
            }

            await _output.GetOrCreateDirectory(targetFilePath.GetDirectoryPath());

            // create output file
            var outputFile = await _output.GetOrCreateFile(targetFilePath);

            await using var outputStream = await outputFile.OpenWrite();

            await using var writer = new StreamWriter(outputStream);
            await writer.WriteAsync(generatedHtml);
        }
Example #3
0
        private async Task <(ContentItem Page, PageFrontmatter?Frontmatter)> ComposePartialHtmlPage(
            Path relativePath,
            ContentItem page,
            DocsSiteRouter router,
            Func <Path, PipeReader, Task <PipeReader> > preprocessorPipe)
        {
            Path outputPath = router.GenerateRoute(new Xref(page.Version, _section.Id, relativePath))
                              ?? throw new InvalidOperationException($"Could not generate output path for '{page}'");

            // create output dir for page
            await _cache.GetOrCreateDirectory(outputPath.GetDirectoryPath());

            // create output file
            var outputFile = await _cache.GetOrCreateFile(outputPath);

            // open file streams
            await using var inputStream = await page.File.OpenRead();

            // stream for preprocessed content
            await using var processedStream = new MemoryStream();

            // process preprocessor directives
            var reader = await preprocessorPipe(relativePath, PipeReader.Create(inputStream));

            var writer = PipeWriter.Create(processedStream, new StreamPipeWriterOptions(leaveOpen: true));
            await reader.CopyToAsync(writer);

            await reader.CompleteAsync();

            await writer.CompleteAsync();

            processedStream.Position = 0;

            // render markdown
            await using var outputStream = await outputFile.OpenWrite();

            var frontmatter = await _docsMarkdownService.RenderPage(processedStream, outputStream);

            return(page.WithFile(outputFile, "text/html"), frontmatter);
        }
Example #4
0
        private async Task ComposeAssets(Section section, DocsSiteRouter router)
        {
            // compose assets from sections
            foreach (var(relativePath, assetItem) in section.ContentItems.Where(ci => IsAsset(ci.Key, ci.Value)))
            {
                // open file streams
                await using var inputStream = await assetItem.File.OpenRead();

                // create output dir for page
                Path outputPath = router.GenerateRoute(new Xref(assetItem.Version, section.Id, relativePath))
                                  ?? throw new InvalidOperationException($"Could not generate output path for '{outputPath}'.");

                await _output.GetOrCreateDirectory(outputPath.GetDirectoryPath());

                // create output file
                var outputFile = await _output.GetOrCreateFile(outputPath);

                await using var outputStream = await outputFile.OpenWrite();

                await inputStream.CopyToAsync(outputStream);
            }
        }
Example #5
0
        public async Task ComposeRedirectPage(Path relativePath, Link redirectToPage)
        {
            string?target = redirectToPage.IsXref
                ? _router.GenerateRoute(redirectToPage.Xref.Value)
                : redirectToPage.Uri;

            if (target == null)
            {
                throw new InvalidOperationException(
                          $"Cannot generate redirect target from '{redirectToPage}'.");
            }

            var generatedHtml = string.Format(RedirectPageHtml, _site.BasePath, target);

            // create output dir for page
            Path targetFilePath = _router.GenerateRoute(
                new Xref(_section.Version, _section.Id, relativePath));

            if (targetFilePath == new Path(target))
            {
                throw new InvalidOperationException(
                          $"Cannot generate a index.html redirect page '{targetFilePath}'. " +
                          $"Redirect would point to same file as the generated file and would" +
                          "end in a endless loop");
            }

            await _output.GetOrCreateDirectory(targetFilePath.GetDirectoryPath());

            // create output file
            var outputFile = await _output.GetOrCreateFile(targetFilePath);

            await using var outputStream = await outputFile.OpenWrite();

            await using var writer = new StreamWriter(outputStream);
            await writer.WriteAsync(generatedHtml);
        }