Module: Middleman::Blog::Helpers

Defined in:
lib/middleman-blog/helpers.rb

Overview

Blog-related helpers that are available to the Middleman application in config.rb and in templates.

Instance Method Summary collapse

Instance Method Details

#blog(blog_name = nil) ⇒ BlogData

Get a BlogData instance for the given blog. Follows the same rules as #blog_controller.

Parameters:

  • blog_name (Symbol, String) (defaults to: nil)

    Optional name of the blog to get data for. Blogs can be named as an option or will default to ‘blog0’, ‘blog1’, etc..

Returns:



57
58
59
# File 'lib/middleman-blog/helpers.rb', line 57

def blog(blog_name = nil)
  blog_controller(blog_name).data
end

#blog_controller(blog_name = nil) ⇒ BlogExtension

Retrieve a Middleman::BlogExtension instance. If blog_name is provided, the instance with that name will be returned. Otherwise, an attempt is made to find the appropriate blog controller for the current resource. For articles this is always available, but for other pages it may be necessary to name the blog in frontmatter using the “blog” blog_name. If there is only one blog, this method will always return that blog.

Parameters:

  • blog_name (Symbol, String) (defaults to: nil)

    Optional name of the blog to get a controller for.

Returns:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/middleman-blog/helpers.rb', line 31

def blog_controller(blog_name = nil)
  if !blog_name && current_resource
    blog_name = current_resource.[:page][:blog]

    unless blog_name
      blog_controller = current_resource.blog_controller if current_resource.respond_to?(:blog_controller)
      return blog_controller if blog_controller
    end
  end

  # In multiblog situations, force people to specify the blog
  raise "You have more than one blog so you must either use the flag --blog (ex. --blog 'myBlog') when calling this method, or add blog: [blog_name] to your page's frontmatter" if !blog_name && blog_instances.size > 1

  # Warn if a non-existent blog name provided
  raise "Non-existent blog name provided: #{blog_name}." if blog_name && !blog_instances.key?(blog_name.to_sym)

  blog_name ||= blog_instances.keys.first
  blog_instances[blog_name.to_sym]
end

#blog_day_path(year, month, day, blog_name = nil) ⇒ String

Get a path to the given day-based calendar page, based on the day_link blog setting.

Parameters:

  • year (Number)
  • month (Number)
  • day (Number)
  • blog_name (Symbol, String) (defaults to: nil)

    Optional name of the blog to use.

Returns:

  • (String)


106
107
108
# File 'lib/middleman-blog/helpers.rb', line 106

def blog_day_path(year, month, day, blog_name = nil)
  build_url blog_controller(blog_name).calendar_pages.link(year, month, day)
end

#blog_instancesHash<Symbol,BlogExtension>

All the blog instances known to this Middleman app, keyed by name. A new blog is added every time the blog extension is activated. Name them by setting the :name option when activating - otherwise they get an automatic name like ‘blog0’, ‘blog1’, etc.

Returns:

  • (Hash<Symbol,BlogExtension>)

    a hash of all blog instances by name



12
13
14
15
16
17
18
19
# File 'lib/middleman-blog/helpers.rb', line 12

def blog_instances
  return nil unless app.extensions[:blog]

  app.extensions[:blog].keys.each_with_object({}) do |k, sum|
    ext = app.extensions[:blog][k]
    sum[ext.name.to_sym] = ext
  end
end

#blog_month_path(year, month, blog_name = nil) ⇒ String

Get a path to the given month-based calendar page, based on the month_link blog setting.

Parameters:

  • year (Number)
  • month (Number)
  • blog_name (Symbol, String) (defaults to: nil)

    Optional name of the blog to use.

Returns:

  • (String)


96
97
98
# File 'lib/middleman-blog/helpers.rb', line 96

def blog_month_path(year, month, blog_name = nil)
  build_url blog_controller(blog_name).calendar_pages.link(year, month)
end

#blog_year_path(year, blog_name = nil) ⇒ String

Get a path to the given year-based calendar page, based on the year_link blog setting.

Parameters:

  • year (Number)
  • blog_name (Symbol, String) (defaults to: nil)

    Optional name of the blog to use.

Returns:

  • (String)


87
88
89
# File 'lib/middleman-blog/helpers.rb', line 87

def blog_year_path(year, blog_name = nil)
  build_url blog_controller(blog_name).calendar_pages.link(year)
end

#current_articleBlogArticle

Get a BlogArticle representing the current article.

Returns:



70
71
72
73
# File 'lib/middleman-blog/helpers.rb', line 70

def current_article
  article = current_resource
  article if article&.is_a?(BlogArticle)
end

#is_blog_article?Boolean

Determine whether the currently rendering template is a BlogArticle. This can be useful in layouts and helpers.

Returns:

  • (Boolean)


64
65
66
# File 'lib/middleman-blog/helpers.rb', line 64

def is_blog_article? # rubocop:disable Naming/PredicateName
  !current_article.nil?
end

#page_articles(blog_name = nil) ⇒ Array<Middleman::Sitemap::Resource>

Returns the list of articles to display on this particular page (when using pagination).

Parameters:

  • blog_name (Symbol, String) (defaults to: nil)

    Optional name of the blog to use.

Returns:

  • (Array<Middleman::Sitemap::Resource>)


120
121
122
123
124
125
126
127
128
129
# File 'lib/middleman-blog/helpers.rb', line 120

def page_articles(blog_name = nil)
  meta = current_resource.
  limit = current_resource.data[:per_page]

  # "articles" local variable is populated by Calendar and Tag page generators
  # If it's not set then use the complete list of articles
  articles = meta[:locals]['articles'] || blog(blog_name).articles

  limit ? articles.first(limit) : articles
end

#paginateBoolean

Whether or not pagination is enabled for this template. This can be used to allow a single template to work in both paginating and non-paginating modes.

Returns:

  • (Boolean)


113
114
115
# File 'lib/middleman-blog/helpers.rb', line 113

def paginate
  false
end

#tag_path(tag, blog_name = nil) ⇒ String

Get a path to the given tag page, based on the taglink blog setting.

Parameters:

  • tag (String)
  • blog_name (Symbol, String) (defaults to: nil)

    Optional name of the blog to use.

Returns:

  • (String)


79
80
81
# File 'lib/middleman-blog/helpers.rb', line 79

def tag_path(tag, blog_name = nil)
  build_url blog_controller(blog_name).tag_pages.link(tag)
end