private static ModuleCollection GetIndexPageModules(Documents documents, ArchiveSettings settings) { IModule[] paginateModules = settings.Sort == null ? new IModule[] { documents } : new IModule[] { documents, new Sort(settings.Sort) }; return(new ModuleCollection { new Execute(c => { Paginate paginate = new Paginate(settings.PageSize?.Invoke <int>(c) ?? int.MaxValue, paginateModules); paginate = paginate.WithPageMetadata(Keys.Title, (doc, ctx) => { string indexTitle = settings.Title?.Invoke <string>(doc, ctx) ?? string.Empty; return doc.Get <int>(Keys.CurrentPage) <= 1 ? indexTitle : (string.IsNullOrEmpty(indexTitle) ? $"Page {doc[Keys.CurrentPage]}" : $"{indexTitle} (Page {doc[Keys.CurrentPage]})"); }); if (settings.TakePages != null) { paginate = paginate.TakePages(settings.TakePages.Invoke <int>(c)); } if (settings.SkipPages != null) { paginate = paginate.SkipPages(settings.SkipPages.Invoke <int>(c)); } if (!string.IsNullOrEmpty(settings.GroupDocumentsMetadataKey)) { paginate = paginate.WithPageMetadata(settings.GroupDocumentsMetadataKey, (doc, ctx) => doc[Keys.GroupDocuments]); } if (!string.IsNullOrEmpty(settings.GroupKeyMetadataKey)) { paginate = paginate.WithPageMetadata(settings.GroupKeyMetadataKey, (doc, ctx) => doc.String(Keys.GroupKey)); } paginate = paginate.WithPageMetadata(Keys.RelativeFilePath, (doc, ctx) => { FilePath filePath = settings.RelativePath?.Invoke <FilePath>(doc, ctx) ?? settings.TemplateFile.Invoke <FilePath>(ctx); bool hasExtension = filePath.HasExtension; if (hasExtension) { filePath = filePath.Directory.CombineFile(filePath.FileNameWithoutExtension); } string path = filePath .FullPath .Replace(' ', '-') .Replace("'", string.Empty) .Replace(".", string.Empty); return doc.Get <int>(Keys.CurrentPage) <= 1 ? (hasExtension ? $"{path}.html" : $"{path}/index.html") : (hasExtension ? $"{path}{doc.String(Keys.CurrentPage)}.html" : $"{path}/page{doc.String(Keys.CurrentPage)}.html"); }); return paginate; }) }); }
private static IModuleList GetModules(ArchiveSettings settings) => new ModuleList { { ReadFile, new ModuleCollection { new ReadFiles(settings.TemplateFile), new FrontMatter(new Yaml.Yaml()) } }, { Populate, settings.Group != null ? new ModuleCollection { new Execute(ctx => new GroupByMany(settings.Group, new Documents().FromPipelines(settings.Pipelines)) .WithComparer(settings.CaseInsensitiveGroupComparer?.Invoke <bool>(ctx) == true ? StringComparer.OrdinalIgnoreCase : null) .WithEmptyOutputIfNoGroups()), new Where((doc, ctx) => !string.IsNullOrEmpty(doc.String(Keys.GroupKey))), new ForEach((IModule)GetIndexPageModules( new Documents((doc, _) => doc[Keys.GroupDocuments]), settings)) } : GetIndexPageModules( new Documents().FromPipelines(settings.Pipelines), settings) }, { Render, new If( ctx => settings.WriteIfEmpty || settings.Pipelines.Any(x => ctx.Documents[x].Any()), new Shortcodes(true), new Razor.Razor() .IgnorePrefix(null) .WithLayout(settings.Layout), new Shortcodes(false)) .WithoutUnmatchedDocuments() }, { WriteFiles, new WriteFiles() } };
/// <summary> /// Creates the pipeline. /// </summary> /// <param name="name">The name of this pipeline.</param> /// <param name="settings">The settings for the pipeline.</param> public Archive(string name, ArchiveSettings settings) : base(name, GetModules(settings)) { }