Class: JekyllDefaultLayout::Generator

Inherits:
Jekyll::Generator
  • Object
show all
Defined in:
lib/jekyll-default-layout/generator.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site) ⇒ Generator

Returns a new instance of Generator.



11
12
13
# File 'lib/jekyll-default-layout/generator.rb', line 11

def initialize(site)
  @site = site
end

Instance Attribute Details

#siteObject

Returns the value of attribute site.



6
7
8
# File 'lib/jekyll-default-layout/generator.rb', line 6

def site
  @site
end

Instance Method Details

#documentsObject

rubocop:enable Metrics/PerceivedComplexity



58
59
60
# File 'lib/jekyll-default-layout/generator.rb', line 58

def documents
  [site.pages, site.posts.docs].flatten
end

#generate(site) ⇒ Object



15
16
17
18
19
20
21
22
# 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) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/jekyll-default-layout/generator.rb', line 74

def index?(document)
  document.url == "/"
end

#layout_exists?(layout) ⇒ Boolean

Does the given layout exist for the site?

Returns:

  • (Boolean)


29
30
31
# File 'lib/jekyll-default-layout/generator.rb', line 29

def layout_exists?(layout)
  !site.layouts[layout].nil?
end

#layout_for(document) ⇒ Object

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



45
46
47
48
49
50
51
52
53
54
55
# 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) ⇒ Boolean

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

Returns:

  • (Boolean)


35
36
37
# File 'lib/jekyll-default-layout/generator.rb', line 35

def layout_specified?(document)
  document.to_liquid.key? "layout"
end

#markdown?(document) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/jekyll-default-layout/generator.rb', line 39

def markdown?(document)
  markdown_converter.matches(document.extname)
end

#markdown_converterObject



62
63
64
# File 'lib/jekyll-default-layout/generator.rb', line 62

def markdown_converter
  @markdown_converter ||= site.find_converter_instance(Jekyll::Converters::Markdown)
end

#page?(document) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/jekyll-default-layout/generator.rb', line 70

def page?(document)
  document.is_a?(Jekyll::Page)
end

#post?(document) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# 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) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/jekyll-default-layout/generator.rb', line 24

def should_set_layout?(document)
  markdown?(document) && !layout_specified?(document)
end