class Jekyll::CategorizePageGenerator

Injects front matter defaults to set default category/sub-category values

Public Instance Methods

add_to_frontmatter(page, values) click to toggle source
# File lib/jekyll-categorize-pages.rb, line 15
def add_to_frontmatter(page, values)
  values.each do |key, value|
    # add to the page front matter header
    page.data[key] = value
  end
end
generate(site) click to toggle source

main generator init function

# File lib/jekyll-categorize-pages.rb, line 61
def generate(site)
  @site = site
  # The collections to add category/sub category front matter values
  collections = @site.data["categorize_collections"] || ["pages"]

  for c in collections
    for item in get_collection(c)
      valid_category = isValid?(item)
      if valid_category
        header_values = get_header_values_to_add(item)
        add_to_frontmatter(item, header_values)
      end
    end
  end
end
get_collection(name) click to toggle source
# File lib/jekyll-categorize-pages.rb, line 11
def get_collection(name)
  @site.collections[name] ? @site.collections[name].docs : []
end
get_header_values_to_add(item) click to toggle source
# File lib/jekyll-categorize-pages.rb, line 44
def get_header_values_to_add(item)
  path = item.relative_path.split('/')

  if path[1] and path[1] != "index.html" and path[1] != "index.md"
    category = path[1]
  end
  if path[2] and path[2] != "index.html" and path[2] != "index.md"
    sub_category = path[2]
  end
  
  return {
    "category" => category,
    "sub_category" => sub_category
  }
end
isValid?(item) click to toggle source
# File lib/jekyll-categorize-pages.rb, line 22
def isValid?(item)
  path = item.relative_path.split('/')
  # puts "item.relative_path: #{item.relative_path}"

  if path[1] and path[1] != "index.html" and path[1] != "index.md"
    category = path[1]
  end
  if path[2] and path[2] != "index.html" and path[2] != "index.md"
    sub_category = path[2]
  end

  # if category folder has no sub categories
  if path.length == 3
    header_values = { "category" => path[1] }
    add_to_frontmatter(item, header_values)
    return false
  end

  return true
end