Skip to content

Commit

Permalink
Added a GenerateRedirects.AlwaysCreateAdditionalOutput() method
Browse files Browse the repository at this point in the history
  • Loading branch information
daveaglick committed Dec 27, 2022
1 parent 57dd807 commit 91fce01
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 5 deletions.
4 changes: 4 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.0.0-beta.65

- Added a `GenerateRedirects.AlwaysCreateAdditionalOutput()` method to allow creating additional redirect files even if no redirects are specified (I.e. if redirects are also being generated from another source).

# 1.0.0-beta.64

- Fixed `ElseIf` when `Predicate.RequiresDocument` is false in `ExecuteIf` module (#255, #256, thanks @nils-a).
Expand Down
5 changes: 5 additions & 0 deletions src/core/Statiq.Common/IO/NormalizedPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,11 @@ private NormalizedPath(string fullPath, ReadOnlyMemory<char>[] segments, bool is
/// <inheritdoc />
public string ToDisplayString() => IsNull ? string.Empty : FullPath;

/// <summary>
/// A display string that can be used for tracing that displays a custom message if the path is null.
/// </summary>
public string ToDisplayString(string nullText) => IsNull ? nullText : FullPath;

/// <summary>
/// Returns a <see cref="string" /> that represents this path.
/// </summary>
Expand Down
14 changes: 13 additions & 1 deletion src/core/Statiq.Core/Modules/Content/GenerateRedirects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class GenerateRedirects : Module
private Config<IReadOnlyList<NormalizedPath>> _paths = Config.FromDocument<IReadOnlyList<NormalizedPath>>(Keys.RedirectFrom);
private bool _metaRefreshPages = true;
private bool _includeHost = false;
private bool _alwaysCreateAdditionalOutput = false;

/// <summary>
/// Controls where the redirected paths come from. By default, values from the metadata
Expand Down Expand Up @@ -92,6 +93,17 @@ public GenerateRedirects WithAdditionalOutput(in NormalizedPath path, Func<IDict
return this;
}

/// <summary>
/// Always creates any additional output documents even if there are no redirected paths.
/// </summary>
/// <param name="alwaysCreateAdditionalOutput"><c>true</c> to always create additional output documents.</param>
/// <returns>The current module instance.</returns>
public GenerateRedirects AlwaysCreateAdditionalOutput(bool alwaysCreateAdditionalOutput = true)
{
_alwaysCreateAdditionalOutput = alwaysCreateAdditionalOutput;
return this;
}

/// <inheritdoc />
protected override async Task<IEnumerable<IDocument>> ExecuteContextAsync(IExecutionContext context)
{
Expand All @@ -109,7 +121,7 @@ protected override async Task<IEnumerable<IDocument>> ExecuteContextAsync(IExecu
}

// Generate other output documents if requested
if (redirects.Count > 0)
if (redirects.Count > 0 || _alwaysCreateAdditionalOutput)
{
foreach (KeyValuePair<NormalizedPath, Func<IDictionary<NormalizedPath, string>, string>> additionalOutput in _additionalOutputs)
{
Expand Down
2 changes: 2 additions & 0 deletions src/core/Statiq.Core/Modules/Control/ProcessSidecarFile.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Statiq.Common;

namespace Statiq.Core
Expand Down Expand Up @@ -69,6 +70,7 @@ protected override async Task<IEnumerable<IDocument>> ExecuteInputAsync(IDocumen
IFile sidecarFile = context.FileSystem.GetInputFile(sidecarPath);
if (sidecarFile.Exists)
{
context.LogDebug($"Processing sidecar file {sidecarPath} for {input.Source.ToDisplayString("unknown")}");
IContentProvider sidecarContent = sidecarFile.GetContentProvider();
foreach (IDocument result in await context.ExecuteModulesAsync(Children, input.Clone(sidecarContent).Yield()))
{
Expand Down
17 changes: 15 additions & 2 deletions src/core/Statiq.Core/Modules/IO/SetDestination.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Statiq.Common;

namespace Statiq.Core
Expand Down Expand Up @@ -141,7 +142,19 @@ public static NormalizedPath GetCurrentDestinationFromMetadata(IDocument doc)
return null;
}

protected override Task<IEnumerable<IDocument>> ExecuteConfigAsync(IDocument input, IExecutionContext context, NormalizedPath value) =>
Task.FromResult(value.IsNull ? input.Yield() : input.Clone(value).Yield());
protected override Task<IEnumerable<IDocument>> ExecuteConfigAsync(
IDocument input,
IExecutionContext context,
NormalizedPath value)
{
if (value.IsNull)
{
return Task.FromResult(input.Yield());
}

context.LogDebug(
$"Setting destination of {input.Source.ToDisplayString("null")} to {value.ToDisplayString("null")}");
return Task.FromResult(input.Clone(value).Yield());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ public async Task WithAdditionalOutput()
{
{ Keys.RedirectFrom, new List<NormalizedPath> { new NormalizedPath("bar/baz.html") } }
});
GenerateRedirects redirect = new GenerateRedirects().WithAdditionalOutput(new NormalizedPath("a/b"), x => string.Join("|", x.Select(y => $"{y.Key} {y.Value}")));
GenerateRedirects redirect = new GenerateRedirects()
.WithAdditionalOutput(new NormalizedPath("a/b"), x => string.Join("|", x.Select(y => $"{y.Key} {y.Value}")));

// When
IReadOnlyList<TestDocument> results = await ExecuteAsync(new[] { redirected1, redirected2 }, redirect);
Expand Down Expand Up @@ -216,6 +217,40 @@ public async Task WithAdditionalOutputWithoutMetaRefresh()
results.Select(x => x.Destination.FullPath).ShouldBe(new[] { "a/b" });
}

[Test]
public async Task ShouldNotGenerateAdditionalOutputIfNoRedirects()
{
// Given
TestDocument redirected1 = new TestDocument(new NormalizedPath("foo2.html"));
TestDocument redirected2 = new TestDocument(new NormalizedPath("fizz.html"));
GenerateRedirects redirect = new GenerateRedirects()
.WithAdditionalOutput(new NormalizedPath("a/b"), x => "foobar");

// When
IReadOnlyList<TestDocument> results = await ExecuteAsync(new[] { redirected1, redirected2 }, redirect);

// Then
results.Select(x => x.Destination.FullPath).ShouldBeEmpty();
}

[Test]
public async Task ShouldGenerateAdditionalOutputIfNoRedirects()
{
// Given
TestDocument redirected1 = new TestDocument(new NormalizedPath("foo2.html"));
TestDocument redirected2 = new TestDocument(new NormalizedPath("fizz.html"));
GenerateRedirects redirect = new GenerateRedirects()
.WithAdditionalOutput(new NormalizedPath("a/b"), x => "foobar")
.AlwaysCreateAdditionalOutput();

// When
IReadOnlyList<TestDocument> results = await ExecuteAsync(new[] { redirected1, redirected2 }, redirect);

// Then
results.Select(x => x.Destination.FullPath).ShouldBe(new[] { "a/b" });
results.Single(x => x.Destination.FullPath == "a/b").Content.ShouldBe("foobar");
}

[Test]
public async Task DifferentBody()
{
Expand All @@ -236,4 +271,4 @@ public async Task DifferentBody()
results.Single().Content.ShouldContain("<p>FOOBAR</p>");
}
}
}
}

0 comments on commit 91fce01

Please sign in to comment.