class Jekyll::PaginateV2::Generator::Utils

Static utility functions that are used in the code and don't belong in once place in particular

Public Class Methods

calculate_number_of_pages(all_posts, per_page) click to toggle source

Static: Calculate the number of pages.

all_posts - The Array of all Posts. per_page - The Integer of entries per page.

Returns the Integer number of pages.

# File lib/jekyll-paginate-v2-redux/generator/utils.rb, line 16
def self.calculate_number_of_pages(all_posts, per_page)
  (all_posts.size.to_f / per_page.to_i).ceil
end
ensure_leading_slash(path) click to toggle source

Static: Return a String version of the input which has a leading slash.

If the input already has a forward slash in position zero, it will be
returned unchanged.

path - a String path

Returns the path with a leading slash

# File lib/jekyll-paginate-v2-redux/generator/utils.rb, line 43
def self.ensure_leading_slash(path)
  path[0..0] == "/" ? path : "/#{path}"
end
ensure_trailing_slash(path) click to toggle source

Static: Return a String version of the input which has a trailing slash.

If the input already has a forward slash at the end, it will be
returned unchanged.

path - a String path

Returns the path with a trailing slash

# File lib/jekyll-paginate-v2-redux/generator/utils.rb, line 63
def self.ensure_trailing_slash(path)
  path[-1] == "/" ? path : "#{path}/"
end
format_page_number(toFormat, cur_page_nr, total_page_count=nil) click to toggle source

Static: returns a fully formatted string with the current (:num) page number and maximum (:max) page count replaced if configured

# File lib/jekyll-paginate-v2-redux/generator/utils.rb, line 22
def self.format_page_number(toFormat, cur_page_nr, total_page_count=nil)
  s = toFormat.sub(':num', cur_page_nr.to_s)
  if !total_page_count.nil?
    s = s.sub(':max', total_page_count.to_s)
  end
  return s
end
format_page_title(toFormat, title, cur_page_nr=nil, total_page_count=nil) click to toggle source

Static: returns a fully formatted string with the :title variable and the current (:num) page number and maximum (:max) page count replaced

# File lib/jekyll-paginate-v2-redux/generator/utils.rb, line 32
def self.format_page_title(toFormat, title, cur_page_nr=nil, total_page_count=nil)
  return format_page_number(toFormat.sub(':title', title.to_s), cur_page_nr, total_page_count)
end
remove_leading_slash(path) click to toggle source

Static: Return a String version of the input without a leading slash.

path - a String path

Returns the input without the leading slash

# File lib/jekyll-paginate-v2-redux/generator/utils.rb, line 52
def self.remove_leading_slash(path)
  path[0..0] == "/" ? path[1..-1] : path
end
sort_get_post_data(post_data, sort_field) click to toggle source

Retrieves the given sort field from the given post the sort_field variable can be a hierarchical value on the form “parent_field:child_field” repeated as many times as needed only the leaf child_field will be retrieved

# File lib/jekyll-paginate-v2-redux/generator/utils.rb, line 83
def self.sort_get_post_data(post_data, sort_field)
  
  # Begin by splitting up the sort_field by (;,:.)
  sort_split = sort_field.split(":")
  sort_value = post_data

  for r_key in sort_split
    key = r_key.downcase.strip # Remove any erronious whitespace and convert to lower case
    if !sort_value.has_key?(key)
      return nil
    end
    # Work my way through the hash
    sort_value = sort_value[key]
  end

  # If the sort value is a hash then return nil else return the value
  if( sort_value.is_a?(Hash) )
    return nil
  else
    return sort_value
  end
end
sort_values(a, b) click to toggle source

Sorting routine used for ordering posts by custom fields. Handles Strings separately as we want a case-insenstive sorting

# File lib/jekyll-paginate-v2-redux/generator/utils.rb, line 71
def self.sort_values(a, b)
  if a.is_a?(String)
    return a.downcase <=> b.downcase
  end
  
  # By default use the built in sorting for the data type
  return a <=> b
end