Skip to content

Commit

Permalink
Fixing forced page extensions, fixes #41, #65 and others
Browse files Browse the repository at this point in the history
  • Loading branch information
sverrirs committed Jan 11, 2018
1 parent 76ec2da commit 7fec984
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 35 deletions.
7 changes: 4 additions & 3 deletions examples/01-typicalblog/_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ paginate: nil
paginate_path: nil

# Pagination Settings
offpagination:
pagination:
enabled: true
per_page: 3
permalink: '/page/:num/'
Expand All @@ -51,5 +51,6 @@ offpagination:
# Old jekyll-paginate pagination logic
# Uncomment thew two entries below to demonstrate how this new gem
# retains backwards compatibility with the old pagination logic
paginate: 3
paginate_path: "/legacy/page:num/"
# AS OF JANUARY 2018 this code does not work anymore!
#paginate: 3
#paginate_path: "/legacy/page:num/"
8 changes: 4 additions & 4 deletions examples/04-jsonapi/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
layout: null
permalink: /api
pagination:
permalink: 'feed-:num'
permalink: ''
enabled: true
extension: json
indexpage: 'feed-1'
extension: .json
indexpage: 'feed-:num'
---

{
Expand All @@ -15,7 +15,7 @@
"title": "{{ post.title }}",
"link": "{{ post.url }}"
}{% endfor %}
],
]
{% if paginator.next_page %}
,"next": "{{ paginator.next_page_path }}"
{% endif %}
Expand Down
4 changes: 2 additions & 2 deletions lib/jekyll-paginate-v2/generator/defaults.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ module PaginateV2::Generator
'before' => 0, # Limits how many links to show before the current page in the pagination trail (0, means off, default: 0)
'after' => 0, # Limits how many links to show after the current page in the pagination trail (0 means off, default: 0)
},
'indexpage' => 'index', # The default name of the index pages
'extension' => 'html', # The default extension for the output pages
'indexpage' => nil, # The default name of the index pages
'extension' => 'html', # The default extension for the output pages (ignored if indexpage is nil)
'debug' => false, # Turns on debug output for the gem
'legacy' => false # Internal value, do not use (will be removed after 2018-01-01)
}
Expand Down
4 changes: 2 additions & 2 deletions lib/jekyll-paginate-v2/generator/paginationModel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,8 @@ def paginate(template, config, site_title, all_posts, all_tags, all_categories,
newpages = []

# Consider the default index page name and extension
indexPageName = config['indexpage'].split('.')[0]
indexPageExt = Utils.ensure_leading_dot(config['extension'])
indexPageName = config['indexpage'].nil? ? '' : config['indexpage'].split('.')[0]
indexPageExt = config['extension'].nil? ? '' : Utils.ensure_leading_dot(config['extension'])
indexPageWithExt = indexPageName + indexPageExt

# In case there are no (visible) posts, generate the index file anyway
Expand Down
39 changes: 19 additions & 20 deletions lib/jekyll-paginate-v2/generator/paginator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,33 @@ def initialize(config_per_page, first_index_page_url, paginated_page_url, posts,
init = (@page - 1) * @per_page
offset = (init + @per_page - 1) >= posts.size ? posts.size : (init + @per_page - 1)

# Adjust the first index page url
if( first_index_page_url.end_with?('/'))
first_index_page_url = first_index_page_url + default_indexpage + default_ext
puts "Appending default index+ext: #{first_index_page_url}"
elsif !first_index_page_url.include?('.')
first_index_page_url = first_index_page_url + default_indexpage
puts "Appending default index only: #{first_index_page_url}"
end

# Adjust the paginated pages as well
if( paginated_page_url.end_with?('/'))
paginated_page_url = paginated_page_url + default_indexpage + default_ext
puts "Appending default paginated index+ext: #{paginated_page_url}"
elsif !paginated_page_url.include?('.')
paginated_page_url = paginated_page_url + default_ext
puts "Appending default paginated ext only: #{paginated_page_url}"
end
# Ensure that the current page has correct extensions if needed
this_page_url = Utils.ensure_full_path(@page == 1 ? first_index_page_url : paginated_page_url,
!default_indexpage || default_indexpage.length == 0 ? 'index' : default_indexpage,
!default_ext || default_ext.length == 0 ? '.html' : default_ext)

# To support customizable pagination pages we attempt to explicitly append the page name to
# the url incase the user is using extensionless permalinks.
if default_indexpage && default_indexpage.length > 0
# Adjust first page url
first_index_page_url = Utils.ensure_full_path(first_index_page_url, default_indexpage, default_ext)
# Adjust the paginated pages as well
paginated_page_url = Utils.ensure_full_path(paginated_page_url, default_indexpage, default_ext)
end

@total_posts = posts.size
@posts = posts[init..offset]
@page_path = @page == 1 ? first_index_page_url : Utils.format_page_number(paginated_page_url, cur_page_nr, @total_pages)
@page_path = Utils.format_page_number(this_page_url, cur_page_nr, @total_pages)

@previous_page = @page != 1 ? @page - 1 : nil
@previous_page_path = @page != 1 ? @page == 2 ? first_index_page_url : Utils.format_page_number(paginated_page_url, @previous_page, @total_pages) : nil
@previous_page_path = @page == 1 ? nil :
@page == 2 ? Utils.format_page_number(first_index_page_url, 1, @total_pages) :
Utils.format_page_number(paginated_page_url, @previous_page, @total_pages)
@next_page = @page != @total_pages ? @page + 1 : nil
@next_page_path = @page != @total_pages ? Utils.format_page_number(paginated_page_url, @next_page, @total_pages) : nil

@first_page = 1
@first_page_path = first_index_page_url
@first_page_path = Utils.format_page_number(first_index_page_url, 1, @total_pages)
@last_page = @total_pages
@last_page_path = Utils.format_page_number(paginated_page_url, @total_pages, @total_pages)
end
Expand Down
11 changes: 11 additions & 0 deletions lib/jekyll-paginate-v2/generator/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,17 @@ def self.sort_get_post_data(post_data, sort_field)
end
end

# Ensures that the passed in url has a index and extension applied
def self.ensure_full_path(url, default_index, default_ext)
if( url.end_with?('/'))
return url + default_index + default_ext
elsif !url.include?('.')
return url + default_index
end
# Default
return url
end

end

end # module PaginateV2
Expand Down
4 changes: 2 additions & 2 deletions lib/jekyll-paginate-v2/version.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module Jekyll
module PaginateV2
VERSION = "1.9.1"
VERSION = "1.9.2"
# When modifying remember to issue a new tag command in git before committing, then push the new tag
# git tag -a v1.9.1 -m "Gem v1.9.1"
# git tag -a v1.9.2 -m "Gem v1.9.2"
# git push origin --tags
# Yanking a published Gem
# gem yank jekyll-paginate-v2 -v VERSION
Expand Down
4 changes: 2 additions & 2 deletions spec/generator/paginator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ module Jekyll::PaginateV2::Generator
# Initialize a pager with
# 5 posts per page
# at page 2 out of 5 pages
pager = Paginator.new(5, "/", "/feed:num", posts, 2, 5, 'feed', '.json')
pager = Paginator.new(5, "/", "/", posts, 2, 5, 'feed:num', '.json')

pager.page.must_equal 2
pager.per_page.must_equal 5
Expand All @@ -148,7 +148,7 @@ module Jekyll::PaginateV2::Generator
pager.posts[4].must_equal '10'

pager.previous_page.must_equal 1
pager.previous_page_path.must_equal '/feed.json'
pager.previous_page_path.must_equal '/feed1.json'
pager.next_page.must_equal 3
pager.next_page_path.must_equal '/feed3.json'
end
Expand Down

0 comments on commit 7fec984

Please sign in to comment.