Class: Middleman::BlogExtension

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options_hash = {}, &block) ⇒ BlogExtension

Returns a new instance of BlogExtension.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/middleman-blog/extension_3_1.rb', line 31

def initialize(app, options_hash={}, &block)
  super

  @uid = options.name

  require 'middleman-blog/blog_data'
  require 'middleman-blog/blog_article'
  require 'active_support/core_ext/time/zones'

  # app.set :time_zone, 'UTC'

  # optional: :tag_template
  # optional: :year_template
  # optional: :month_template
  # optional: :day_template
  # Allow one setting to set all the calendar templates
  if options.calendar_template
    options.year_template  ||= options.calendar_template
    options.month_template ||= options.calendar_template
    options.day_template   ||= options.calendar_template
  end

  # If "prefix" option is specified, all other paths are relative to it.
  if options.prefix
    options.prefix = "/#{options.prefix}" unless options.prefix.start_with? '/'
    options.permalink = File.join(options.prefix, options.permalink)
    options.sources = File.join(options.prefix, options.sources)
    options.taglink = File.join(options.prefix, options.taglink)
    options.year_link = File.join(options.prefix, options.year_link)
    options.month_link = File.join(options.prefix, options.month_link)
    options.day_link = File.join(options.prefix, options.day_link)

    options.custom_collections.each do |key, opts|
      opts[:link] = File.join(options.prefix, opts[:link])
    end
  end
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



29
30
31
# File 'lib/middleman-blog/extension_3_1.rb', line 29

def data
  @data
end

#uidObject

Returns the value of attribute uid.



29
30
31
# File 'lib/middleman-blog/extension_3_1.rb', line 29

def uid
  @uid
end

Instance Method Details

#after_configurationObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/middleman-blog/extension_3_1.rb', line 69

def after_configuration
  @uid ||= "blog#{@app.blog_instances.keys.length}"

  @app.ignore(options.calendar_template) if options.calendar_template
  @app.ignore(options.year_template) if options.year_template
  @app.ignore(options.month_template) if options.month_template
  @app.ignore(options.day_template) if options.day_template

  @app.blog_instances[@uid.to_sym] = self

  # Make sure ActiveSupport's TimeZone stuff has something to work with,
  # allowing people to set their desired time zone via Time.zone or
  # set :time_zone
  Time.zone = app.config[:time_zone] if app.config[:time_zone]
  time_zone = Time.zone if Time.zone
  zone_default = Time.find_zone!(time_zone || 'UTC')
  unless zone_default
    raise 'Value assigned to time_zone not recognized.'
  end
  Time.zone_default = zone_default

  # Initialize blog with options

  @data = ::Middleman::Blog::BlogData.new(@app, options, self)

  @app.sitemap.register_resource_list_manipulator(
    :"blog_#{uid}_articles",
    @data,
    false
  )

  if options.tag_template
    @app.ignore options.tag_template

    require 'middleman-blog/tag_pages'
    @app.sitemap.register_resource_list_manipulator(
      :"blog_#{uid}_tags",
      ::Middleman::Blog::TagPages.new(@app, self),
      false
    )
  end

  if options.year_template || options.month_template || options.day_template
    require 'middleman-blog/calendar_pages'
    @app.sitemap.register_resource_list_manipulator(
      :"blog_#{uid}_calendar",
      ::Middleman::Blog::CalendarPages.new(@app, self),
      false
    )
  end

  if options.paginate
    require 'middleman-blog/paginator'
    @app.sitemap.register_resource_list_manipulator(
      :"blog_#{uid}_paginate",
      ::Middleman::Blog::Paginator.new(@app, self),
      false
    )
  end

  if options.custom_collections
    require 'middleman-blog/custom_pages'
    register_custom_pages
  end
end

#generate_custom_helper(custom_property) ⇒ Object

Generate helpers to access the path to a custom collection.

For example, when using a custom property called “category” to collect articles on the method category_path will be generated.

Parameters:

  • custom_property (Symbol)

    Custom property which is being used to collect articles on



171
172
173
174
175
176
177
178
179
180
# File 'lib/middleman-blog/extension_3_1.rb', line 171

def generate_custom_helper(custom_property)
  m = Module.new
  m.module_eval(%Q{
    def #{custom_property}_path(value, key = nil)
      sitemap.find_resource_by_path(::Middleman::Blog::CustomPages.link(blog_controller(key).options, :#{custom_property}, value)).try(:url)
    end
  })

  app.class.send(:include, m)
end

#register_custom_pagesObject

Register any custom page collections that may be set in the config

A custom resource list manipulator will be generated for each key in the custom collections hash.

The following will collect posts on the “category” frontmatter property:

```
activate :blog do |blog|
  blog.custom_collections = {
    link: "/categories/:category.html",
    template: "/category.html"
  }
end
```

Category pages in the example above will use the category.html as a template file and it will be ignored when building.



152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/middleman-blog/extension_3_1.rb', line 152

def register_custom_pages
  options.custom_collections.each do |property, options|
    @app.ignore options[:template]
    @app.sitemap.register_resource_list_manipulator(
      :"blog_#{uid}_#{property}",
      ::Middleman::Blog::CustomPages.new(property, @app, self),
      false
    )

    generate_custom_helper(property)
  end
end