Class: Jekyll::OpenProjectHelpers::CombinedPostArrayGenerator

Inherits:
Generator
  • Object
show all
Defined in:
lib/jekyll-theme-open-project-helpers/blog_index.rb

Overview

Adds a variable holding the array of posts of open hub blog and from each individual project blog, combined and sorted by date.

It also does some processing on the posts as required by the Open Project theme.

Instance Method Summary collapse

Instance Method Details

#generate(site) ⇒ Object



23
24
25
26
27
28
29
30
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
68
69
70
71
72
73
74
75
# File 'lib/jekyll-theme-open-project-helpers/blog_index.rb', line 23

def generate(site)
  site_posts = site.posts.docs

  if site.config['is_hub']
    # Get documents representing projects
    projects = site.collections['projects'].docs.select do |item|
      pieces = item.url.split('/')
      pieces.length == 4 && pieces[-1] == 'index' && pieces[1] == 'projects'
    end
    # Add project name (matches directory name, may differ from title)
    projects = projects.map do |project|
      project.data['name'] = project.url.split('/')[2]
      project
    end

    # Get documents representnig posts from each project’s blog
    project_posts = site.collections['projects'].docs.select { |item| item.url.include? '_posts' }

    # Add parent project’s data hash onto each
    project_posts = project_posts.map do |post|
      project_name = post.url.split('/')[2]
      post.data['parent_project'] = projects.detect { |p| p.data['name'] == project_name }
      post.content = ""
      post
    end

    posts_combined = (project_posts + site_posts)

  else
    posts_combined = site_posts

  end

  # On each post, replace authors’ emails with corresponding md5 hashes
  # suitable for hotlinking authors’ Gravatar profile pictures.
  posts_combined = posts_combined.sort_by(&:date).reverse.map do |post|
    if post.data.key? 'author'
      process_author(post.data['author'])
    end

    if post.data.key? 'authors'
      post.data['authors'].map do |author|
        process_author(author)
      end
    end

    post
  end

  # Make combined blog post array available site-wide
  site.config['posts_combined'] = posts_combined
  site.config['num_posts_combined'] = posts_combined.size
end