Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ordering pages and articles when iterating in templates. #836

Closed

Conversation

davidmarble
Copy link
Contributor

The order of pages and articles when iterated in templates can now be set to a metadata attribute or a sorting function. By default to articles are sorted by slug (same as before) and pages are sorted by filename (new).

This fixes #420.

You can tweak how pages and articles are sorted in your pelicanconf.py. For example:

PAGE_ORDER_BY = 'page-order'

In this case, just make sure that all pages have the metadata attribute 'page-order'.

Advanced: You could also use a sorting function. See the code for an example, as a sorting function is used to get filenames based on the source_path attribute of content.

Order can be set to a metadata attribute or a sorting function.
Default to order by slug for articles and order by filename for pages.

if order_by:
if hasattr(order_by, '__call__'):
index.sort(key=order_by)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a try/except with a more friendly logging message would be nicer, in case the user gives a bad function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. I'll do that.

@almet
Copy link
Member

almet commented Aug 16, 2013

Oh wow, sorry for not reacting earlier, this was lost in limbo.

+1 on this feature, however there are some merge problems with it right now. Could you try to rebase you work?
Also, that would really be appreciated if you can add tests for this, to be sure no-one will break this in the future.

Thanks,
Alexis

@justinmayer
Copy link
Member

Hey David. Any chance you could take another look at your pull request and make the changes Alexis suggested?

@justinmayer
Copy link
Member

Hi David. Would you be so kind as to rebase and squash your changes as @ametaireau suggested? Would be great to get this wrapped up. (^_^)

@davidmarble
Copy link
Contributor Author

Thanks for the nudge Justin. I've had replies to this starred in my inbox for the past few months. Just been too busy to come back to it. If anyone else is reading this and wants to jump in, that'd be great. I may get to it, but just can't promise anything on timing. Free time ebbs and flows!

@justinmayer
Copy link
Member

Hey David. Just thought I'd nudge you again in the off-chance that your free time might be ebbing instead of flowing. (^_^) What say you?

@malept
Copy link
Contributor

malept commented Nov 14, 2013

I squashed the commits (malept/pelican@0c69f4a) and added a test for ordered pages (malept/pelican@69ff7dd). I'm not quite sure how to test ordered articles, though. I can't seem to use the same pattern as the pages test because, I believe, of this code in pelican/generators.py, ArticlesGenerator.generate_context():

### line that got changed by this PR
        self.articles, self.translations = process_translations(all_articles,
                order_by=self.settings['ARTICLE_ORDER_BY'])

### [...block that I believe is not relevant to this discussion omitted for brevity...]

        # sort the articles by date
### here's the offending line
        self.articles.sort(key=attrgetter('date'), reverse=True)
        self.dates = list(self.articles)
        self.dates.sort(key=attrgetter('date'),
                reverse=self.context['NEWEST_FIRST_ARCHIVES'])

@peerst
Copy link

peerst commented Feb 7, 2014

+1 for getting this in ... just having built a site with very weird menu ordering I'd like to fix

@diegobz
Copy link

diegobz commented Feb 21, 2014

I'm Interested in it too.

@peerst
Copy link

peerst commented Sep 18, 2014

Is this scrapped because its no longer necessary due to other implementation of this feature, or does it meat Pelican won't get this feature ever?

@justinmayer
Copy link
Member

Because of the other existing pull request, and because the submitter of this pull request is no longer active.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add metadata for custom page ordering
7 participants