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

Added the option to specify the paginated url format #342

Merged
merged 4 commits into from
Apr 24, 2012
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions bin/jekyll
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ opts = OptionParser.new do |opts|
opts.on("--permalink [TYPE]", "Use 'date' (default) for YYYY/MM/DD") do |style|
options['permalink'] = style unless style.nil?
end

opts.on("--paginate [POSTS_PER_PAGE]", "Paginate a blog's posts") do |per_page|
begin
options['paginate'] = per_page.to_i
Expand All @@ -113,6 +113,16 @@ opts = OptionParser.new do |opts|
end
end

opts.on("--paginate_path [PAGINATED_URL_FORMAT]", "Leave blank for /page<num>") do |paginate_path|
begin
options['paginate_path'] = paginate_path
raise ArgumentError if options['paginate_path'].nil?
rescue
puts 'You must specify a pagination url format'
exit 0
end
end

opts.on("--limit_posts [MAX_POSTS]", "Limit the number of posts to publish") do |limit_posts|
begin
options['limit_posts'] = limit_posts.to_i
Expand Down Expand Up @@ -148,12 +158,12 @@ if ARGV.size > 0
else
migrator = migrator.downcase
end

cmd_options = []
['file', 'dbname', 'user', 'pass', 'host', 'site'].each do |p|
cmd_options << "\"#{options[p]}\"" unless options[p].nil?
end

# It's import time
puts "Importing..."

Expand Down
29 changes: 28 additions & 1 deletion features/pagination.feature
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,36 @@ Feature: Site pagination
And the "_site/page<exist>/index.html" file should exist
And I should see "<posts>" in "_site/page<exist>/index.html"
And the "_site/page<not_exist>/index.html" file should not exist

Examples:
| num | exist | posts | not_exist |
| 1 | 4 | 1 | 5 |
| 2 | 2 | 2 | 3 |
| 3 | 2 | 1 | 3 |

Scenario Outline: Setting a custom pagination path
Given I have a configuration file with:
| key | value |
| paginate | 1 |
| paginate_path | /blog/page-:num |
| permalink | /blog/:year/:month/:day/:title |
And I have a _layouts directory
And I have an "index.html" page that contains "{{ paginator.posts.size }}"
And I have a _posts directory
And I have the following post:
| title | date | layout | content |
| Wargames | 3/27/2009 | default | The only winning move is not to play. |
| Wargames2 | 4/27/2009 | default | The only winning move is not to play2. |
| Wargames3 | 5/27/2009 | default | The only winning move is not to play3. |
| Wargames4 | 6/27/2009 | default | The only winning move is not to play4. |
When I run jekyll
Then the _site/blog/page-<exist> directory should exist
And the "_site/blog/page-<exist>/index.html" file should exist
And I should see "<posts>" in "_site/blog/page-<exist>/index.html"
And the "_site/blog/page-<not_exist>/index.html" file should not exist

Examples:
| exist | posts | not_exist |
| 2 | 1 | 5 |
| 3 | 1 | 6 |
| 4 | 1 | 7 |
2 changes: 1 addition & 1 deletion features/step_definitions/jekyll_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
end

Then /^the (.*) directory should exist$/ do |dir|
assert File.directory?(dir)
assert File.directory?(dir), "The directory \"#{dir}\" does not exist"
end

Then /^I should see "(.*)" in "(.*)"$/ do |text, file|
Expand Down
8 changes: 7 additions & 1 deletion lib/jekyll/generators/pagination.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,19 @@ def paginate(site, page)
if num_page > 1
newpage = Page.new(site, site.source, page.dir, page.name)
newpage.pager = pager
newpage.dir = File.join(page.dir, "page#{num_page}")
newpage.dir = File.join(page.dir, paginate_path(site, num_page))
site.pages << newpage
else
page.pager = pager
end
end
end

private
def paginate_path(site, num_page)
format = site.config['paginate_path'] || "page:num"
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would be better if the default page link format was specified in the default config and allowed to be updated via the _config.yml file. Otherwise this looks great.

format.sub(':num', num_page.to_s)
end
end

class Pager
Expand Down