Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
statiq-tutorial/Pipelines/FeaturesListingRazorPipeline.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
84 lines (77 sloc)
4.33 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using Statiq.Common; | |
using Statiq.Core; | |
using Statiq.Razor; | |
using Statiq.Yaml; | |
namespace StatiqTutorial | |
{ | |
/// <summary> | |
/// Pipeline responsible for processing features markdown files into features listing HTML documents using Razor templates. | |
/// </summary> | |
public class FeaturesListingRazorPipeline : Pipeline | |
{ | |
public FeaturesListingRazorPipeline() | |
{ | |
InputModules = new ModuleList | |
{ | |
// Reads the content of files from the file system into the content of new documents. | |
// <see href="https://github.com/statiqdev/Statiq.Framework/blob/main/src/core/Statiq.Core/Modules/IO/ReadFiles.cs">ReadFiles</see> | |
new ReadFiles(pattern: "content/features/*.md"), | |
}; | |
ProcessModules = new ModuleList | |
{ | |
// Extracts the first part of the content for each document and sends it to a child module for processing. | |
// <see href="https://github.com/statiqdev/Statiq.Framework/blob/main/src/core/Statiq.Core/Modules/Control/ExtractFrontMatter.cs">ExtractFrontMatter</see> | |
new ExtractFrontMatter( | |
// Parses YAML content for each input document and stores the result in it's metadata. | |
// <see href="https://github.com/statiqdev/Statiq.Framework/blob/main/src/extensions/Statiq.Yaml/ParseYaml.cs">ParseYaml</see> | |
new ParseYaml() | |
), | |
// Replaces documents in the current pipeline. | |
// <see href="https://github.com/statiqdev/Statiq.Framework/blob/main/src/core/Statiq.Core/Modules/Control/ReplaceDocuments.cs">ReplaceDocuments</see> | |
new ReplaceDocuments( | |
Config.FromContext(context => | |
{ | |
return (IEnumerable<IDocument>) new [] | |
{ | |
context.CreateDocument( | |
// Provides properties and instance methods for working with paths. | |
// <see href="https://github.com/statiqdev/Statiq.Framework/blob/main/src/core/Statiq.Common/IO/NormalizedPath.css">NormalizedPath</see> | |
new NormalizedPath("features-razor.html"), | |
new[] | |
{ | |
new KeyValuePair<string, object>(Keys.Children, context.Inputs) | |
} | |
) | |
}; | |
}) | |
), | |
}; | |
OutputModules = new ModuleList | |
{ | |
// Load Razor template to IDocument content. | |
// <see href="https://github.com/statiqdev/Statiq.Framework/blob/main/src/core/Statiq.Core/Modules/Content/MergeContent.cs">MergeContent</see>. | |
// <see href="https://statiq.dev/web/content-and-data/content/">Content propery of IDocument</see> | |
new MergeContent(new ReadFiles("FeaturesListing.cshtml")), | |
// Render HTML file from Razor template and document typed as HomeViewModel model. | |
// <see href="https://github.com/statiqdev/Statiq.Framework/blob/main/src/extensions/Statiq.Razor/RenderRazor.cs">RenderRazor</see> | |
new RenderRazor().WithModel(Config.FromDocument((document, context) => | |
{ | |
var featureDocuments = document.GetChildren(); | |
List<Feature> features = new List<Feature>(); | |
foreach (var featureDocument in featureDocuments) | |
{ | |
var featureTitle = featureDocument.GetString("Title"); | |
var featureDescription = featureDocument.GetString("Description"); | |
var slug = featureDocument.Source.FileNameWithoutExtension.ToString(); | |
features.Add(new Feature(featureTitle, featureDescription, slug)); | |
} | |
return new FeaturesListingViewModel(features); | |
})), | |
// Flush to the output. | |
// <see href="https://github.com/statiqdev/Statiq.Framework/blob/main/src/core/Statiq.Core/Modules/IO/WriteFiles.cs">WriteFiles</see>. | |
new WriteFiles() | |
}; | |
} | |
} | |
} |