Class: Postwave::BlogBuilder

Inherits:
Object
  • Object
show all
Includes:
BlogUtilities, DisplayHelper, RssHelper, 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::RSS_FILE_NAME, Postwave::BlogUtilities::SUMMARY_FILE_NAME, Postwave::BlogUtilities::TAGS_DIR

Instance Method Summary collapse

Methods included from RssHelper

#build_rss, #rss_content

Methods included from BlogUtilities

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

Methods included from DisplayHelper

#output_blog_created, #output_blog_description_prompt, #output_blog_name_prompt, #output_blog_posts_path_prompt, #output_blog_url_prompt, #output_build_completed, #output_building, #output_config_message, #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

Instance Method Details

#buildObject



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
59
60
61
62
# File 'lib/postwave/blog_builder.rb', line 20

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_rss(published_posts)

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

#build_summary(posts, tags) ⇒ Object



99
100
101
102
103
104
105
106
107
# File 'lib/postwave/blog_builder.rb', line 99

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



88
89
90
91
92
93
94
95
96
97
# File 'lib/postwave/blog_builder.rb', line 88

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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/postwave/blog_builder.rb', line 72

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



64
65
66
67
68
69
70
# File 'lib/postwave/blog_builder.rb', line 64

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