Class: Jekyll::J1Paginator::AutoPages::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/j1-paginator/autopages/utils.rb

Class Method Summary collapse

Class Method Details

.ap_index_posts_by(all_posts, index_key) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/j1-paginator/autopages/utils.rb', line 42

def self.ap_index_posts_by(all_posts, index_key)
  return nil if all_posts.nil?
  return all_posts if index_key.nil?
  index = {}
  all_posts.each do |post|
    next if post.data.nil?
    next if !post.data.has_key?(index_key)
    next if post.data[index_key].nil?
    next if post.data[index_key].size <= 0
    next if post.data[index_key].to_s.strip.length == 0

    # Only tags and categories come as premade arrays, locale does not, so convert any data
    # elements that are strings into arrays
    post_data = post.data[index_key]
    if post_data.is_a?(String)
      post_data = post_data.split(/;|,|\s/)
    end

    post_data.each do |key|
      key = key.to_s.strip
      processed_key = key.downcase #Clean whitespace and junk
      if !index.has_key?(processed_key)
        # Need to store the original key value here so that I can present it to the users as a page variable they can use (unmodified, e.g. tags not being 'sci-fi' but "Sci-Fi")
        # Also, only interested in storing all the keys not the pages in this case
        index[processed_key] = [processed_key, key]
      end
    end
  end
  return index
end

.collect_all_docs(site_collections) ⇒ Object

Static: returns all documents from all collections defined in the hash of collections passed in excludes all pagination pages though



32
33
34
35
36
37
38
39
40
# File 'lib/j1-paginator/autopages/utils.rb', line 32

def self.collect_all_docs(site_collections)
  coll = []
  site_collections.each do |coll_name, coll_data|
    if !coll_data.nil?
      coll += coll_data.docs.select { |doc| !doc.data.has_key?('pagination') }.each{ |doc| doc.data['__coll'] = coll_name } # Exclude all pagination pages and then for every page store it's collection name
    end
  end
  return coll
end

.format_cat_macro(toFormat, category, slugify_config = nil) ⇒ Object

Static: returns a fully formatted string with the category macro (:cat) replaced



16
17
18
19
20
# File 'lib/j1-paginator/autopages/utils.rb', line 16

def self.format_cat_macro(toFormat, category, slugify_config=nil)
  slugify_mode = slugify_config.has_key?('mode') ? slugify_config['mode'] : nil
  slugify_cased = slugify_config.has_key?('cased') ? slugify_config['cased'] : false
  return toFormat.sub(':cat', Jekyll::Utils.slugify(category.to_s, mode:slugify_mode, cased:slugify_cased))
end

.format_coll_macro(toFormat, collection, slugify_config = nil) ⇒ Object

Static: returns a fully formatted string with the collection macro (:coll) replaced



24
25
26
27
28
# File 'lib/j1-paginator/autopages/utils.rb', line 24

def self.format_coll_macro(toFormat, collection, slugify_config=nil)
  slugify_mode = slugify_config.has_key?('mode') ? slugify_config['mode'] : nil
  slugify_cased = slugify_config.has_key?('cased') ? slugify_config['cased'] : false
  return toFormat.sub(':coll', Jekyll::Utils.slugify(collection.to_s, mode:slugify_mode, cased:slugify_cased))
end

.format_tag_macro(toFormat, tag, slugify_config = nil) ⇒ Object

Static: returns a fully formatted string with the tag macro (:tag) replaced



8
9
10
11
12
# File 'lib/j1-paginator/autopages/utils.rb', line 8

def self.format_tag_macro(toFormat, tag, slugify_config=nil)
  slugify_mode = slugify_config.has_key?('mode') ? slugify_config['mode'] : nil
  slugify_cased = slugify_config.has_key?('cased') ? slugify_config['cased'] : false
  return toFormat.sub(':tag', Jekyll::Utils.slugify(tag.to_s, mode:slugify_mode, cased:slugify_cased))
end