Skip to content
This repository has been archived by the owner on Jun 25, 2020. It is now read-only.

[RFC] new extension point IBeforeProcessingTransform #288

Merged
merged 4 commits into from
Jan 2, 2016

Conversation

thoemmi
Copy link
Contributor

@thoemmi thoemmi commented Dec 23, 2015

This PR adds a new extension point IBeforeProcessingTransform. Its method IBeforeProcessingTransform.Transform(SiteContext) is called after all pages and posts are loaded. This enables plugins to add additional information to a page's bag.

E.g. I've written this plugin to support series, which replaces the seriestag in the frontmatter with a list of all posts with the same series tag:

public class SeriesPageTransform : IBeforeProcessingTransform {
    private const string SeriesTag = "series";

    public void Transform(SiteContext context) {
        var allPages = context.Posts.Concat(context.Pages);

        var allSeries = from page in allPages
            let series = GetSeriesFromPost(page)
            where series != null
            group page by series;

        foreach (var series in allSeries) {
            var allPagesForSeries = series.OrderBy(p => p.Date).ToList();
            foreach (var page in series) {
                page.Bag[SeriesTag] = allPagesForSeries;
            }
        }
    }

    private static string GetSeriesFromPost(Page page) {
        object obj;
        return page.Bag.TryGetValue(SeriesTag, out obj) ? obj as string : null;
    }
}

I'm unsure about the name IBeforeProcessingTransform but I couldn't think of something better. (You know, the two hard things in CS 😉). If you know a better name I'd appreciate it.

BTW, I had to make Configuration public so I can use it in my unit tests.

@laedit
Copy link
Member

laedit commented Dec 28, 2015

Excellent, this could also fix #270. @k94ll13nn3, can you test it for your issue?

You are absolutely right about the name, I can't find a good one either 😄
Let's just roll with this one for now, a documentation on the wiki will do fine.

But I don't want Configuration to be public, only IConfiguration should be.
Could you reverse the change on Configuration and create a quick mock for your unit tests?
Or should we ship a Pretzel.Logic.TestingHelpers with an 'official' mock like System.IO.Abstractions is doing?

@thoemmi
Copy link
Contributor Author

thoemmi commented Dec 28, 2015

Yeah, naming is hard 😆

I reverted the changes to Configuration, it's internal again.

I don't think it's worth to introduce Pretzel.Logic.TestingHelpers.

@k94ll13nn3
Copy link

Yep, IBeforeProcessingTransform solved my problem 👍

laedit pushed a commit that referenced this pull request Jan 2, 2016
[RFC] new extension point `IBeforeProcessingTransform`
@laedit laedit merged commit 165f644 into Code52:master Jan 2, 2016
@thoemmi thoemmi deleted the IBeforeProcessingTransforms branch January 2, 2016 16:28
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants