Class: Postwave::BlogBuilder

Inherits:
Object
  • Object
show all
Includes:
BlogUtilities, DisplayHelper, Singleton
Defined in:
lib/postwave/blog_builder.rb

Constant Summary collapse

INDEX_HEADERS =
["slug", "date", "title"]

Constants included from BlogUtilities

Postwave::BlogUtilities::CONFIG_FILE_NAME, Postwave::BlogUtilities::INDEX_FILE_NAME, Postwave::BlogUtilities::META_DIR, Postwave::BlogUtilities::POSTS_DIR, Postwave::BlogUtilities::SUMMARY_FILE_NAME, Postwave::BlogUtilities::TAGS_DIR

Instance Method Summary collapse

Methods included from DisplayHelper

#output_blog_created, #output_build_completed, #output_building, #output_creating_blog, #output_creating_post, #output_drafts_skipped, #output_exising_setup, #output_general_error, #output_missing_setup, #output_post_created, #output_post_processed, #output_tags_created

Methods included from BlogUtilities

#directory_paths, #file_paths, #find_missing_paths, #is_set_up?

Instance Method Details

#buildObject



18
19
20
21
22
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
# File 'lib/postwave/blog_builder.rb', line 18

def build
  start = Time.now
  
  output_building

  if !is_set_up?
    output_missing_setup
    return
  end

  # load, rename, and sort post file names
  posts = load_posts
  posts = ensure_unique_slugs(posts).sort_by { |p| p.date }.reverse
  draft_posts, published_posts = posts.partition { |p| p.respond_to?(:draft) ? p.draft : false }
  tags = {}

  CSV.open(File.join(Dir.pwd, POSTS_DIR, META_DIR, INDEX_FILE_NAME), "w") do |csv|
    csv << INDEX_HEADERS
    published_posts.each do |post|
      post.update_file_name!

      csv << [post.slug, post.date, post.title]

      post.tags.each do |tag|
        if tags.has_key? tag
          tags[tag] << post.slug
        else
          tags[tag] = [post.slug]
        end
      end
    end
  end
  output_post_processed(published_posts)
  output_drafts_skipped(draft_posts)

  build_tags_files(tags)
  build_summary(published_posts, tags)

  build_time = Time.now - start
  output_build_completed(build_time)
end

#build_summary(posts, tags) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/postwave/blog_builder.rb', line 95

def build_summary(posts, tags)
  summary = {
    post_count: posts.count,
    most_recent_file_name: posts.first.file_name,
    most_recent_date: posts.first.date,
    tags: tags.keys
  }
  File.write(File.join(Dir.pwd, POSTS_DIR, META_DIR, SUMMARY_FILE_NAME), summary.to_yaml)
end

#build_tags_files(tags) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/postwave/blog_builder.rb', line 84

def build_tags_files(tags)
  tags.each do |tag, post_slugs|
    tag_info = {
      count: post_slugs.count,
      post_slugs: post_slugs
    }
    File.write(File.join(Dir.pwd, POSTS_DIR, META_DIR, TAGS_DIR, "#{tag}.yaml"), tag_info.to_yaml)
  end
  output_tags_created(tags)
end

#ensure_unique_slugs(posts) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/postwave/blog_builder.rb', line 68

def ensure_unique_slugs(posts)
  slug_count = {}

  posts.sort_by { |p| p.date }.each do |post|
    title_slug = post.title_slug
    if slug_count.key?(title_slug)
      slug_count[title_slug] += 1
      post.slug = "#{title_slug}-#{slug_count[title_slug]}"
    else
      slug_count[title_slug] = 0
    end
  end

  posts
end

#load_postsObject



60
61
62
63
64
65
66
# File 'lib/postwave/blog_builder.rb', line 60

def load_posts
  posts = []
  Dir.glob(File.join(Dir.pwd, POSTS_DIR, "*.md")) do |post_file_path|
    posts << Postwave::Post.new_from_file_path(post_file_path)
  end
  posts
end