Class: Middleman::Blog::CustomPages

Inherits:
Object
  • Object
show all
Defined in:
lib/middleman-blog/custom_pages.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(property, app, controller = nil) ⇒ CustomPages

Returns a new instance of CustomPages.



22
23
24
25
26
# File 'lib/middleman-blog/custom_pages.rb', line 22

def initialize(property, app, controller = nil)
  @property        = property
  @app             = app
  @blog_controller = controller
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



20
21
22
# File 'lib/middleman-blog/custom_pages.rb', line 20

def app
  @app
end

#blog_controllerObject (readonly)

Returns the value of attribute blog_controller.



20
21
22
# File 'lib/middleman-blog/custom_pages.rb', line 20

def blog_controller
  @blog_controller
end

#propertyObject (readonly)

Returns the value of attribute property.



20
21
22
# File 'lib/middleman-blog/custom_pages.rb', line 20

def property
  @property
end

Class Method Details

Return a path to the given property / value pair

Parameters:

  • blog_options (Hash)
  • property (String|Symbol)

    Frontmatter property used to collect on

  • String| (String| value Frontmatter value for the given article for the given property)

    value Frontmatter value for the given article for the given property



13
14
15
16
# File 'lib/middleman-blog/custom_pages.rb', line 13

def link(blog_options, property, value)
  link_template = blog_options.custom_collections[property][:link]
  ::Middleman::Util.normalize_path(link_template.sub(":#{property}", value.parameterize))
end

Instance Method Details

#articlesObject



44
45
46
# File 'lib/middleman-blog/custom_pages.rb', line 44

def articles
  blog_data.articles
end

#blog_dataObject



28
29
30
31
32
33
34
# File 'lib/middleman-blog/custom_pages.rb', line 28

def blog_data
  if blog_controller
    blog_controller.data
  else
    app.blog
  end
end

#blog_optionsObject



36
37
38
39
40
41
42
# File 'lib/middleman-blog/custom_pages.rb', line 36

def blog_options
  if blog_controller
    blog_controller.options
  else
    app.blog.options
  end
end

#build_resource(path, value, property_articles) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/middleman-blog/custom_pages.rb', line 59

def build_resource(path, value, property_articles)
  p = ::Middleman::Sitemap::Resource.new(app.sitemap, path)
  p.proxy_to(template_for_page)
  p. :locals => {
    "page_type"       => property,
    "#{property}"     => value,
    "articles"        => property_articles,
    "blog_controller" => blog_controller
  }

  prop_name = property
  p. do
    instance_variable_set("@#{prop_name}", value)
    @articles = property_articles
  end

  p
end

#manipulate_resource_list(resources) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/middleman-blog/custom_pages.rb', line 48

def manipulate_resource_list(resources)
  new_resources = []

  articles.group_by { |a| a.data[property] }.each do |property_value, property_articles|
    path = CustomPages.link(blog_options, property, property_value)
    new_resources << build_resource(path, property_value, property_articles)
  end

  resources + new_resources
end

#template_for_pageObject



78
79
80
# File 'lib/middleman-blog/custom_pages.rb', line 78

def template_for_page
  blog_options.custom_collections[property][:template]
end