Module: Octopress::Ink::Bootstrap

Included in:
Plugin
Defined in:
lib/octopress-ink/plugin/bootstrap.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.categoriesObject (readonly)

Returns the value of attribute categories.



12
13
14
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 12

def categories
  @categories
end

Returns the value of attribute category_links.



13
14
15
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 13

def category_links
  @category_links
end

.category_listObject

Returns the value of attribute category_list.



13
14
15
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 13

def category_list
  @category_list
end

.feedsObject (readonly)

Returns the value of attribute feeds.



12
13
14
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 12

def feeds
  @feeds
end

.pagesObject (readonly)

Returns the value of attribute pages.



12
13
14
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 12

def pages
  @pages
end

Returns the value of attribute tag_links.



13
14
15
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 13

def tag_links
  @tag_links
end

.tag_listObject

Returns the value of attribute tag_list.



13
14
15
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 13

def tag_list
  @tag_list
end

.tagsObject (readonly)

Returns the value of attribute tags.



12
13
14
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 12

def tags
  @tags
end

Instance Attribute Details

#articles_feedObject (readonly)

Returns the value of attribute articles_feed.



4
5
6
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 4

def articles_feed
  @articles_feed
end

#category_feedObject (readonly)

Returns the value of attribute category_feed.



4
5
6
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 4

def category_feed
  @category_feed
end

#category_indexObject (readonly)

Returns the value of attribute category_index.



4
5
6
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 4

def category_index
  @category_index
end

Returns the value of attribute links_feed.



4
5
6
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 4

def links_feed
  @links_feed
end

#main_feedObject (readonly)

Returns the value of attribute main_feed.



4
5
6
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 4

def main_feed
  @main_feed
end

#post_archiveObject (readonly)

Returns the value of attribute post_archive.



4
5
6
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 4

def post_archive
  @post_archive
end

#post_indexObject (readonly)

Returns the value of attribute post_index.



4
5
6
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 4

def post_index
  @post_index
end

#tag_feedObject (readonly)

Returns the value of attribute tag_feed.



4
5
6
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 4

def tag_feed
  @tag_feed
end

#tag_indexObject (readonly)

Returns the value of attribute tag_index.



4
5
6
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 4

def tag_index
  @tag_index
end

Class Method Details

.add_page(page, key = nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 40

def add_page(page, key=nil)
  if @pages[page.url].nil?
    @pages[page.url] = page

    url = page.url.sub(/index.(xml|html)/, '')

    if key == 'feeds'
      @feeds[url] = page.data['title']
    elsif key == 'tag'
      tag = page.data[key]
      tag = "#{tag}_#{page.lang}" if Octopress.multilingual? && page.lang 
      @tags[tag] = url
    elsif key == 'category'
      category = page.data[key]
      category = "#{category}_#{page.lang}" if Octopress.multilingual? && page.lang 
      @categories[category] = url
    end
    page
  end
end

.category(category, lang) ⇒ Object



30
31
32
33
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 30

def category(category, lang)
  category = "#{category}_#{page.lang}" if Octopress.multilingual? && page.lang 
  @categories[category]
end

.page(lang, type, key) ⇒ Object



26
27
28
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 26

def page(lang, type, key)
  @pages[type][key]
end

.resetObject



15
16
17
18
19
20
21
22
23
24
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 15

def reset
  @pages           = {}
  @categories      = {}
  @tags            = {}
  @feeds           = {}
  @tag_links       = {}
  @tag_list        = {}
  @category_links  = {}
  @category_list   = {}
end

.tag(tag, lang) ⇒ Object



35
36
37
38
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 35

def tag(tag, lang)
  tag = "#{tag}_#{page.lang}" if Octopress.multilingual? && page.lang 
  @tags[tag]
end

Instance Method Details

#add_feeds(config, lang, template) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 192

def add_feeds(config, lang, template)
  return if template.nil?

  permalink = page_permalink(template, lang)

  if page = template.new_page({
      'lang'      => lang,
      'permalink' => permalink,
      'title'     => page_title(template, config, lang),
      'feed_type' => feed_type(template),
      'plugin'    => self
    })


    Bootstrap.add_page(page, "feeds")
    Octopress.site.pages << page
  end

  page
end

#add_indexes(config, lang, template) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 175

def add_indexes(config, lang, template)
  return if template.nil?

  permalink = page_permalink(template, lang)
  title     = page_title(template, config, lang)

  if page = template.new_page({
      'lang'      => lang,
      'title'     => title,
      'permalink' => permalink
    })

    Bootstrap.add_page(page)
    Octopress.site.pages << page
  end
end

#add_meta_indexes(config, lang, type, types) ⇒ Object

Generates tag or category index or feed pages for each category and language Unless configuration lists specific categories



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 216

def add_meta_indexes(config, lang, type, types)

  # Get page/feed template for category or tag
  page_template = self.send("#{type}_index")
  feed_template = self.send("#{type}_feed")

  # Don't continue if this plugin doesn't have templates for this
  return unless page_template || feed_template

  collection = if lang
    Octopress.site.send("#{types}_by_language", lang).keys
  else
    Octopress.site.send("#{types}").keys
  end

  # User configured categories or tags
  configured = Array(config[types]).map(&:downcase)

  # If configuration specifices tags or categories, only generate indexes for those
  if !configured.empty?
    collection.delete_if { |i| !configured.include?(i) }
  end

  collection.each do |item|
    item = item.downcase
    item_label = tag_or_category_display_label(item, config)

    # Only add pages if plugin has a feed template for this item
    # and it hasn't been disabled in the configuration
    #
    if page_template
      if config["#{type}_indexes"] == false
        page_template.disable
      else
        permalink = page_permalink(page_template, lang).sub(":#{type}", item)

        if page = page_template.new_page({
            'lang'      => lang,
            "#{type}"   => item,
            'title'     => page_title(page_template, config, lang).sub(":#{type}", item_label),
            'permalink' => permalink,
            'plugin'    => self
          })

          Bootstrap.add_page(page, type)
          Octopress.site.pages << page
        end
      end
    end

    # Only add feeds if plugin has a feed template for this item
    # and it hasn't been disabled in the configuration
    #
    if feed_template

      if config["#{type}_feeds"] == false
        feed_template.disable
      else
        permalink = page_permalink(feed_template, lang).sub(":#{type}", item)

        if page = feed_template.new_page({
            'lang'      => lang,
            "#{type}"   => item,
            'title'     => page_title(feed_template, config, lang).sub(":#{type}", item_label),
            'permalink' => permalink,
            'feed_type' => type,
            'plugin'    => self
          })

          Bootstrap.add_page(page, 'feeds')
          Octopress.site.pages << page
        end
      end
    end
  end
end

#articles_feed_configObject



433
434
435
436
437
438
439
440
441
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 433

def articles_feed_config
<<-CONFIG
titles:
  articles_feed: Articles #{"(:lang_name)" if Octopress.multilingual?}

permalinks:
  articles_feed: #{"/:lang" if Octopress.multilingual?}/feed/articles/
CONFIG
end

#bootstrap_pluginObject

Generate site pages from bootstrappable pages and templates



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 64

def bootstrap_plugin
  register_templates
  inject_configs

  # Add pages for other languages
  if Octopress.multilingual?
    Octopress::Multilingual.languages.each { |lang| inject_pages(lang) }
  else
    inject_pages
  end
end

#category_config_defaultsObject



463
464
465
466
467
468
469
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 463

def category_config_defaults
<<-CONFIG
#{"category_indexes: false" if category_index }
#{"category_feeds: false" if category_feed }
categories: []
CONFIG
end

#category_feed_configObject



443
444
445
446
447
448
449
450
451
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 443

def category_feed_config
<<-CONFIG
titles:
  category_feed: Posts in :category #{"(:lang_name)" if Octopress.multilingual?}

permalinks:
  category_feed: #{"/:lang" if Octopress.multilingual?}/feed/categories/:category/
CONFIG
end

#category_index_configObject



393
394
395
396
397
398
399
400
401
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 393

def category_index_config
<<-CONFIG
titles:
  category_index: Posts in :category

permalinks:
  category_index: #{"/:lang" if Octopress.multilingual?}/categories/:category/
CONFIG
end

#feed_config_defaultsObject



479
480
481
482
483
484
485
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 479

def feed_config_defaults
<<-CONFIG
feed_count: 20         # Number of items in feeds
feed_excerpts: false   # Use post excerpts in feeds
#{"posts_link_out: true" if links_feed }
CONFIG
end

#feed_type(page) ⇒ Object

Discern feed type based on filename



323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 323

def feed_type(page)
  if page.path.include? 'articles'
    'articles'
  elsif page.path.include? 'links'
    'links'
  elsif page.path.include? 'category'
    'category'
  elsif page.path.include? 'tag'
    'tag'
  else
    'main'
  end
end

#inject_configsObject

Merge optional configurations with plugin configuration Plugin configs overrides optional configs



92
93
94
95
96
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 92

def inject_configs
  optional_configs.each do |opt_config|
    @config = Jekyll::Utils.deep_merge_hashes(YAML.load(opt_config), @config)
  end
end

#inject_pages(lang = nil) ⇒ Object

Automatically clone pages or generate templates

This will only occur if:

- Site configuration warrants it
- Plugin assets are present

For example:

- Index pages are cloned only for additonal languages on multilingual sites
- Link-blogging feeds are only generated if the octopress-linkblog plugin is present
- Category and tag indexes and feeds depend on post metadata and configuration


137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 137

def inject_pages(lang=nil)

  # Save some time if there are no posts
  if posts?(lang)

    config = self.config(lang)

    add_indexes(config, lang, post_index)
    add_indexes(config, lang, post_archive)

    add_feeds(config, lang, main_feed)

    if linkposts?(lang)
      add_feeds(config, lang, links_feed)
      add_feeds(config, lang, articles_feed)
    end

    add_meta_indexes(config, lang, 'category', 'categories')
    add_meta_indexes(config, lang, 'tag', 'tags')
  end
end

#linkposts?(lang) ⇒ Boolean

Returns:

  • (Boolean)


167
168
169
170
171
172
173
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 167

def linkposts?(lang)
  if defined?(Octopress::Linkblog) && lang && Octopress.multilingual?
    !Octopress.site.linkposts_by_language(lang).empty?
  else
    !Octopress.site.posts.select {|p| p.data['linkpost']}.empty?
  end
end


423
424
425
426
427
428
429
430
431
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 423

def links_feed_config
<<-CONFIG
titles:
  links_feed: Links #{"(:lang_name)" if Octopress.multilingual?}

permalinks:
  links_feed: #{"/:lang" if Octopress.multilingual?}/feed/links/
CONFIG
end

#main_feed_configObject



413
414
415
416
417
418
419
420
421
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 413

def main_feed_config
<<-CONFIG
titles:
  main_feed: Posts #{"(:lang_name)" if Octopress.multilingual?}

permalinks:
  main_feed: #{"/:lang" if Octopress.multilingual?}/feed/
CONFIG
end

#optional_configsObject

Add default configurations based on matching pages and templates



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
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 100

def optional_configs
  opt_config = []
  opt_config << post_index_config      if post_index
  opt_config << post_archive_config    if post_archive
  opt_config << main_feed_config       if main_feed
  opt_config << links_feed_config      if links_feed
  opt_config << articles_feed_config   if articles_feed
  opt_config << category_index_config  if category_index
  opt_config << tag_index_config       if tag_index
  opt_config << category_feed_config   if category_feed
  opt_config << tag_feed_config        if tag_feed

  # Add shared configurations for tags and categories
  #
  opt_config << category_config_defaults if category_index || category_feed
  opt_config << tag_config_defaults      if tag_index || tag_feed

  # Add feed defaults if plugin has any feed pages
  #
  if main_feed || links_feed || articles_feed || category_feed || tag_feed
    opt_config << feed_config_defaults
  end

  opt_config
end

Ensure pages have language in their permalinks except the primary language pages Unless the user has specified /:lang/ in their permalink config



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 296

def page_permalink(page, lang)
  permalink = config(lang)['permalinks'][page_type(page)]

  # Obey the permalink configuration
  permalink = if lang && permalink.include?(":lang")
    permalink.sub(":lang", lang)

  # Otherwise only add lang for secondary languages
  elsif lang && lang != Octopress.site.config['lang']
    File.join("/#{lang}", permalink)

  # Finally strip language from url if primary language or no language defined
  else
    permalink.sub("/:lang/", '/')
  end

  if permalink.end_with?('/')
    ext = File.extname(page.path).match('xml') ? 'xml' : 'html'
    permalink += "index.#{ext}"
  end

  permalink
end

#page_title(page, config, lang) ⇒ Object



351
352
353
354
355
356
357
358
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 351

def page_title(page, config, lang)
  type = page_type(page)
  title = config['titles'][type]
  if lang && Octopress.multilingual?
    title = title.sub(':lang_name', Octopress::Multilingual.language_name(lang))
  end
  title
end

#page_type(page) ⇒ Object



337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 337

def page_type(page)
  if page.path.include? 'feed'
    "#{feed_type(page)}_feed"
  elsif page.path.include? 'post_index'
    "post_index"
  elsif page.path.include? 'post_archive'
    "post_archive"
  elsif page.path.include? 'category_index'
    "category_index"
  elsif page.path.include? 'tag_index'
    "tag_index"
  end
end

#post_archive_configObject



383
384
385
386
387
388
389
390
391
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 383

def post_archive_config
<<-CONFIG
titles:
  post_archive: Archive

permalinks:
  post_archive: /archive/
CONFIG
end

#post_index_configObject

Default configuration settings Plugin authors can use or override these settings



373
374
375
376
377
378
379
380
381
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 373

def post_index_config
<<-CONFIG
titles:
  post_index: Posts

permalinks:
  post_index: /
CONFIG
end

#posts?(lang) ⇒ Boolean

Returns:

  • (Boolean)


159
160
161
162
163
164
165
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 159

def posts?(lang)
  if lang && Octopress.multilingual?
    !Octopress.site.posts_by_language(lang).empty?
  else
    !Octopress.site.posts.empty?
  end
end

#register_templatesObject

Find templates



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 77

def register_templates
  @post_index       = find_template('post_index.html')
  @post_archive     = find_template('post_archive.html')
  @main_feed        = find_template('main_feed.xml')
  @articles_feed    = find_template('articles_feed.xml')
  @links_feed       = find_template('links_feed.xml')
  @category_index   = find_template('category_index.html')
  @tag_index        = find_template('tag_index.html')
  @category_feed    = find_template('category_feed.xml')
  @tag_feed         = find_template('tag_feed.xml')
end

#tag_config_defaultsObject



471
472
473
474
475
476
477
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 471

def tag_config_defaults
<<-CONFIG
#{"tag_indexes: false" if tag_index }
#{"tag_feeds: false" if tag_feed }
tags: []
CONFIG
end

#tag_feed_configObject



453
454
455
456
457
458
459
460
461
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 453

def tag_feed_config
<<-CONFIG
titles:
  tag_feed: Posts tagged with :tag #{"(:lang_name)" if Octopress.multilingual?}

permalinks:
  tag_feed: #{"/:lang" if Octopress.multilingual?}/feed/tags/:tag/
CONFIG
end

#tag_index_configObject



403
404
405
406
407
408
409
410
411
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 403

def tag_index_config
<<-CONFIG
titles:
  tag_index: Posts tagged with :tag

permalinks:
  tag_index: #{"/:lang" if Octopress.multilingual?}/tags/:tag/
CONFIG
end

#tag_or_category_display_label(label, config) ⇒ Object



360
361
362
363
364
365
366
367
368
# File 'lib/octopress-ink/plugin/bootstrap.rb', line 360

def tag_or_category_display_label(label, config)
  label = label.capitalize

  if labels = config["#{type}_labels"]
    label = labels[type] || label
  end

  label
end