Class: Zine::PostsAndHeadlines

Inherits:
Object
  • Object
show all
Defined in:
lib/zine/posts_and_headlines.rb

Overview

The blog posts and their associate headline files (the home page, an articles index, and RSS feed)

Instance Method Summary collapse

Constructor Details

#initialize(site, options) ⇒ PostsAndHeadlines

Returns a new instance of PostsAndHeadlines.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/zine/posts_and_headlines.rb', line 12

def initialize(site, options)
  @options = options
  @post_array = []
  @site = site
  @tags_by_post = []
  dir = @options['directories']
  @guard = Zine::Watcher.new self, dir['build'], dir['source']
  @guard.start
  read_post_markdown_files
  sort_posts_by_date
end

Instance Method Details

#find_page_from_path(file_path) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/zine/posts_and_headlines.rb', line 24

def find_page_from_path(file_path)
  post_to_rebuild = @post_array.detect do |post|
    File.expand_path(post.source_file) == file_path
  end
  index = @post_array.find_index post_to_rebuild
  { post: post_to_rebuild, index: index }
end

#headline_pagesObject

The headlines pages - the home page, an articles index, and RSS feed



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
76
77
78
79
80
81
82
83
84
85
# File 'lib/zine/posts_and_headlines.rb', line 33

def headline_pages
  dir = @options['directories']['build']
  options = @options['options']
  templates = @options['templates']

  headlines = []
  if templates['home']
    headlines <<
      {
        build_dir: dir,
        name: 'index',
        number: options['num_items_on_home'],
        suffix: '.html',
        template_name: templates['home'],
        title: 'Home'
      }
  end
  if templates['articles']
    headlines <<
      {
        build_dir: dir,
        name: templates['articles'],
        number: @post_array.size,
        suffix: '.html',
        template_name: templates['articles'],
        title: 'Articles'
      }
  end
  if templates['rss']
    headlines <<
      {
        build_dir: dir,
        name: 'rss',
        number: options['number_items_in_RSS'],
        suffix: '.xml',
        template_name: templates['rss'],
        title: ''
      }
  end
  if templates['atom']
    headlines <<
      {
        build_dir: dir,
        name: 'atom',
        number: options['number_items_in_RSS'],
        suffix: '.xml',
        template_name: templates['atom'],
        title: ''
      }
  end

  headlines
end

#once_only(source_file) ⇒ Object



87
88
89
90
91
# File 'lib/zine/posts_and_headlines.rb', line 87

def once_only(source_file)
  @post_array = @post_array.delete_if do |post|
    post.source_file == source_file
  end
end

#one_new_post(source_file, source_full_path) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/zine/posts_and_headlines.rb', line 93

def one_new_post(source_file, source_full_path)
  once_only(source_file)
  post_name = @options['templates']['post']
  @post_array << Zine::Post.new(source_full_path,
                                @site.make_template_bundle(post_name),
                                @options)
  @post_array.last.source_file = source_file # TODO: path when this's frozen
  @tags_by_post << @post_array.last.process(File)
  dest_path = @post_array.last.dest_path
  sort_posts_by_date
  write_tags_and_headlines
  dest_path
end

#preview_delete(file_path) ⇒ Object

get build file from post or location, delete build, remove form post_array



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/zine/posts_and_headlines.rb', line 108

def preview_delete(file_path)
  page = find_page_from_path file_path
  if page[:index].nil?
    directories = @options['directories']
    full = Pathname(file_path)
    relative = full.relative_path_from(
      Pathname(File.absolute_path(directories['source']))
    )
    relative_path = File.dirname(relative)
    file = "#{File.basename(relative, '.md')}.html"
    File.delete(File.join(directories['build'], relative_path, file))
  else
    File.delete(page[:post].dest_path)
    @post_array.delete_at(page[:index])
  end
end

#preview_rebuild(file_path) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/zine/posts_and_headlines.rb', line 125

def preview_rebuild(file_path)
  page = find_page_from_path file_path
  if page[:index].nil?
    if File.dirname(file_path) ==
       File.absolute_path(@options['directories']['posts'])
      one_new_post file_path
    else
      rebuild_page file_path
    end
  else
    rebuild_post page[:post], page[:index]
  end
end

#preview_relative_equivalent(file) ⇒ Object

the build folder equivalent of a non Markdown file in the source tree TODO: move from posts & headlines



141
142
143
144
145
146
147
# File 'lib/zine/posts_and_headlines.rb', line 141

def preview_relative_equivalent(file)
  directories = @options['directories']
  source_dir = Pathname(File.absolute_path(directories['source']))
  build_dir = Pathname(File.absolute_path(directories['build']))
  file_dir = Pathname(File.dirname(file))
  File.join build_dir, file_dir.relative_path_from(source_dir)
end

#preview_straight_copy(file) ⇒ Object

copy a non Markdown file, TODO: move form posts & headlines



150
151
152
# File 'lib/zine/posts_and_headlines.rb', line 150

def preview_straight_copy(file)
  FileUtils.cp(file, preview_relative_equivalent(file))
end

#preview_straight_delete(file) ⇒ Object

delete a non Markdown file, TODO: move form posts & headlines



155
156
157
158
159
# File 'lib/zine/posts_and_headlines.rb', line 155

def preview_straight_delete(file)
  FileUtils.rm(File.join(
                 preview_relative_equivalent(file), File.basename(file)
               ))
end

#read_post_markdown_filesObject

Read markdown files in the posts folder into an array of Posts



181
182
183
184
185
186
187
188
189
# File 'lib/zine/posts_and_headlines.rb', line 181

def read_post_markdown_files
  file_name_array = Dir[File.join(@options['directories']['posts'], '*.md')]
  post_name = @options['templates']['post']
  file_name_array.each do |file|
    @post_array << Zine::Post.new(file,
                                  @site.make_template_bundle(post_name),
                                  @options)
  end
end

#rebuild_page(file) ⇒ Object

rebuild a page that’s not a post - doesn’t create the file structure for a new file with new parent folders



163
164
165
166
167
# File 'lib/zine/posts_and_headlines.rb', line 163

def rebuild_page(file)
  @site.write_markdown(@options['templates']['default'],
                       File.expand_path(@options['directories']['source']),
                       file)
end

#rebuild_post(post, index) ⇒ Object

inserts the new post into the @post_array, builds the file, calls write_tags_and_headlines to rewrites the headline pages & tags RSS & home page will be redundant re-builds if not a recent page



172
173
174
175
176
177
178
# File 'lib/zine/posts_and_headlines.rb', line 172

def rebuild_post(post, index)
  @post_array[index] = Zine::Post.new post.source_file,
                                      post.template_bundle, @options
  @tags_by_post[index] = @post_array[index].process File
  sort_posts_by_date
  write_tags_and_headlines
end

#sort_posts_by_dateObject

Sort the Posts array into date order, newest first



192
193
194
195
196
197
# File 'lib/zine/posts_and_headlines.rb', line 192

def sort_posts_by_date
  @post_array.sort_by! do |post|
    post.formatted_data.page[:date_rfc3339]
  end.reverse!
  # TODO: .freeze -- currently modified during preview
end

#wrangle_headlinesObject

Process each of the headlines pages



200
201
202
203
204
# File 'lib/zine/posts_and_headlines.rb', line 200

def wrangle_headlines
  headline_pages.each do |page|
    write_headline page
  end
end

#writeObject

Write out the Posts, calls write_tags_and_headlines



207
208
209
210
211
212
213
214
215
216
# File 'lib/zine/posts_and_headlines.rb', line 207

def write
  @tags_by_post = []
  @post_array.each do |post|
    @tags_by_post << post.process(File)
  end
  write_tags_and_headlines

  # end point
  { posts: @post_array, guard: @guard }
end

#write_headline(page) ⇒ Object

Pass headline data to the DataPage class to write the files



230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/zine/posts_and_headlines.rb', line 230

def write_headline(page)
  data = page
  data[:post_array] = []
  @post_array.first(page[:number]).each do |post|
    post_data = post.formatted_data
    data[:post_array] << { page: post_data.page, html: post_data.html,
                           uri: post_data.uri }
  end
  data_page = DataPage.new(data,
                           @site.make_template_bundle(data[:template_name]),
                           @options, data[:suffix])
  data_page.write
end

#write_tags_and_headlinesObject

Write out the tags and headline files



245
246
247
248
249
250
251
252
# File 'lib/zine/posts_and_headlines.rb', line 245

def write_tags_and_headlines
  tag_name = @options['templates']['tag']
  tag_index_name = @options['templates']['tag_index']
  tags = Zine::Tag.new @tags_by_post, @site.make_template_bundle(tag_name),
                       @site.make_template_bundle(tag_index_name), @options
  tags.write_tags
  wrangle_headlines
end

#writelessObject

Generate data without writing files (for incremnetal builds & uploads)



219
220
221
222
223
224
225
226
227
# File 'lib/zine/posts_and_headlines.rb', line 219

def writeless
  @tags_by_post = []
  @post_array.each do |post|
    @tags_by_post << post.process_without_writing
  end

  # end point
  { posts: @post_array, guard: @guard }
end