class JekyllDefaultLayout::Generator

Injects front matter defaults to set default layouts, if they exist

Attributes

site[RW]

Public Class Methods

new(site) click to toggle source
# File lib/jekyll-default-layout/generator.rb, line 11
def initialize(site)
  @site = site
end

Public Instance Methods

documents() click to toggle source

rubocop:enable Metrics/PerceivedComplexity

# File lib/jekyll-default-layout/generator.rb, line 58
def documents
  [site.pages, site.posts.docs].flatten
end
generate(site) click to toggle source
# File lib/jekyll-default-layout/generator.rb, line 15
def generate(site)
  @site = site
  documents.each do |document|
    next unless should_set_layout?(document)

    document.data["layout"] = layout_for(document)
  end
end
index?(document) click to toggle source
# File lib/jekyll-default-layout/generator.rb, line 74
def index?(document)
  document.url == "/"
end
layout_exists?(layout) click to toggle source

Does the given layout exist for the site?

# File lib/jekyll-default-layout/generator.rb, line 29
def layout_exists?(layout)
  !site.layouts[layout].nil?
end
layout_for(document) click to toggle source

What layout is appropriate for this document, if any rubocop:disable Metrics/PerceivedComplexity

# File lib/jekyll-default-layout/generator.rb, line 45
def layout_for(document)
  if index?(document) && layout_exists?("home")
    "home"
  elsif page?(document) && layout_exists?("page")
    "page"
  elsif post?(document) && layout_exists?("post")
    "post"
  elsif layout_exists?("default")
    "default"
  end
end
layout_specified?(document) click to toggle source

Has the user already specified a default for this layout? Note: We must use `to_liquid`, and not data, to ensure front matter defaults

# File lib/jekyll-default-layout/generator.rb, line 35
def layout_specified?(document)
  document.to_liquid.key? "layout"
end
markdown?(document) click to toggle source
# File lib/jekyll-default-layout/generator.rb, line 39
def markdown?(document)
  markdown_converter.matches(document.extname)
end
markdown_converter() click to toggle source
# File lib/jekyll-default-layout/generator.rb, line 62
def markdown_converter
  @markdown_converter ||= site.find_converter_instance(Jekyll::Converters::Markdown)
end
page?(document) click to toggle source
# File lib/jekyll-default-layout/generator.rb, line 70
def page?(document)
  document.is_a?(Jekyll::Page)
end
post?(document) click to toggle source
# File lib/jekyll-default-layout/generator.rb, line 66
def post?(document)
  document.is_a?(Jekyll::Document) && document.collection.label == "posts"
end
should_set_layout?(document) click to toggle source
# File lib/jekyll-default-layout/generator.rb, line 24
def should_set_layout?(document)
  markdown?(document) && !layout_specified?(document)
end