getpelican/pelican — pull request #465 by nother (2012-08-19 19:27:32+00:00)
Title: related_posts plugin rewrite + unit tests

I think the related_posts functionality broke when it was split out into a plugin [here](https://github.com/getpelican/pelican/pull/415).

I rewrote the plugin to fix the biggest problem: at the point the related_posts plugin gets run for an article, the generator only has tag information for the articles it has processed up to that point. So, other than for the very last article, the related posts are generated from incomplete tag information.

That seems to be the nature of how the context signal works, so rather than change that or add a new signal, I changed related_posts into an object that does not do its calculations until requested. In normal usage that's in the template and at that point all the articles have been processed so it's ok.

I also fixed two smaller bugs, both of which were also apparently not present in the non-plugin version (I'm not 100% sure since I only looked at the diff but didn't run the old version):

- Reverse the order of related posts so the most closely related ones are returned first.
- Add a check so that an article is not considered related to itself. This is done by comparing metadata since the plugin doesn't have access to the article itself unlike the original implementation.

I also added an optional setting, `MAX_RELATED_POSTS`, that controls the maximum number of related posts returned.

I added some basic unit tests that show these problems. I didn't see any unit tests for plugins so I created a `tests/test_plugins` directory.

Apologies if I misunderstood anything or made any style faux pas, I'm new to Pelican. :)