/// <summary>
        /// Adds Statiq Web functionality to an existing bootstrapper.
        /// This method does not need to be called if using <see cref="BootstrapperFactoryExtensions.CreateWeb(BootstrapperFactory, string[])"/>.
        /// </summary>
        /// <remarks>
        /// This method is useful when you want to add Statiq Web support to an existing bootstrapper,
        /// for example because you created the bootstrapper without certain default functionality
        /// by calling <see cref="Statiq.App.BootstrapperFactoryExtensions.CreateDefaultWithout(BootstrapperFactory, string[], DefaultFeatures)"/>.
        /// </remarks>
        /// <param name="boostrapper">The bootstrapper to add Statiq Web functionality to.</param>
        /// <returns>The bootstrapper.</returns>
        public static Bootstrapper AddWeb(this Bootstrapper boostrapper) =>
        boostrapper
        .AddPipelines(typeof(BootstrapperFactoryExtensions).Assembly)
        .AddHostingCommands()
        .ConfigureServices(services => services
                           .AddSingleton(new Templates())
                           .AddSingleton(new ThemeManager()))
        .ConfigureEngine(engine =>
        {
            ThemeManager themeManager = engine.Services.GetRequiredService <ThemeManager>();

            // Add theme paths from settings
            IReadOnlyList <NormalizedPath> settingsThemePaths = engine.Settings.GetList <NormalizedPath>(WebKeys.ThemePaths);
            if (settingsThemePaths?.Count > 0)
            {
                themeManager.ThemePaths.Clear();
                foreach (NormalizedPath settingsThemePath in settingsThemePaths)
                {
                    themeManager.ThemePaths.Add(settingsThemePath);
                }
            }

            // Iterate in reverse order so we start with the highest priority
            foreach (NormalizedPath themePath in themeManager.ThemePaths.Reverse())
            {
                // Inserting at 0 preserves the original order since we're iterating in reverse
                engine.FileSystem.InputPaths.Insert(0, themePath.Combine("input"));

                // Build a configuration for each of the theme paths
                IDirectory themeDirectory = engine.FileSystem.GetRootDirectory(themePath);
                if (themeDirectory.Exists)
                {
                    IConfigurationRoot configuration = new ConfigurationBuilder()
                                                       .SetBasePath(themeDirectory.Path.FullPath)
                                                       .AddJsonFile("themesettings.json", true)
                                                       .AddJsonFile("statiq.json", true)
                                                       .Build();
                    foreach (KeyValuePair <string, string> config in configuration.AsEnumerable())
                    {
                        // Since we're iterating highest priority first, the first key will set and lower priority will be ignored
                        if (!engine.Settings.ContainsKey(config.Key))
                        {
                            engine.Settings[config.Key] = config.Value;
                        }
                    }
                }
            }
        })
        .AddSettingsIfNonExisting(new Dictionary <string, object>
        {
            { WebKeys.ContentFiles, "**/{!_,}*.{html,cshtml,md}" },
            { WebKeys.DataFiles, $"**/{{!_,}}*.{{{string.Join(",", ParseDataContent.SupportedExtensions)}}}" },
            { WebKeys.DirectoryMetadataFiles, "**/_{d,D}irectory.{json,yaml,yml}" },
            { WebKeys.Xref, Config.FromDocument(doc => doc.GetTitle().Replace(' ', '-')) },
            { WebKeys.Excluded, Config.FromDocument(doc => doc.GetPublishedDate(false) > DateTime.Today.AddDays(1)) }         // Add +1 days so the threshold is midnight on the current day
        });
Beispiel #2
0
 /// <summary>
 /// Adds Statiq Web functionality to an existing bootstrapper.
 /// This method does not need to be called if using <see cref="CreateWeb(BootstrapperFactory, string[])"/>.
 /// </summary>
 /// <remarks>
 /// This method is useful when you want to add Statiq Web support to an existing bootstrapper,
 /// for example because you created the bootstrapper without certain default functionality
 /// by calling <see cref="Statiq.App.BootstrapperFactoryExtensions.CreateDefaultWithout(BootstrapperFactory, string[], DefaultFeatures)"/>.
 /// </remarks>
 /// <param name="boostrapper">The bootstrapper to add Statiq Web functionality to.</param>
 /// <returns>The bootstrapper.</returns>
 public static Bootstrapper AddWeb(this Bootstrapper boostrapper) =>
 boostrapper
 .AddPipelines(typeof(BootstrapperFactoryExtensions).Assembly)
 .AddHostingCommands()
 .ConfigureEngine(engine => engine.FileSystem.InputPaths.Add("theme/input"))
 .ConfigureServices(services => services.AddSingleton(new Templates()))
 .AddSettingsIfNonExisting(new Dictionary <string, object>
 {
     { WebKeys.ContentFiles, "**/{!_,}*.{html,cshtml,md}" },
     { WebKeys.DataFiles, "**/{!_,}*.{json,yaml,yml}" },
     { WebKeys.DirectoryMetadataFiles, "**/_{d,D}irectory.{json,yaml,yml}" },
     { WebKeys.ValidateRelativeLinks, true },
     { WebKeys.GenerateSitemap, true },
     { WebKeys.Xref, Config.FromDocument(doc => doc.GetTitle().Replace(' ', '-')) },
     { WebKeys.Excluded, Config.FromDocument(doc => doc.GetPublishedDate(false) > DateTime.Today.AddDays(1)) }         // Add +1 days so the threshold is midnight on the current day
 });