Class: Zine::PostsAndHeadlines
- Inherits:
-
Object
- Object
- Zine::PostsAndHeadlines
- 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
- #find_page_from_path(file_path) ⇒ Object
-
#headline_pages ⇒ Object
The headlines pages - the home page, an articles index, and RSS feed.
-
#initialize(site, options) ⇒ PostsAndHeadlines
constructor
A new instance of PostsAndHeadlines.
- #once_only(source_file) ⇒ Object
- #one_new_post(source_file, source_full_path) ⇒ Object
-
#preview_delete(file_path) ⇒ Object
get build file from post or location, delete build, remove form post_array.
- #preview_rebuild(file_path) ⇒ Object
-
#preview_relative_equivalent(file) ⇒ Object
the build folder equivalent of a non Markdown file in the source tree TODO: move from posts & headlines.
-
#preview_straight_copy(file) ⇒ Object
copy a non Markdown file, TODO: move form posts & headlines.
-
#preview_straight_delete(file) ⇒ Object
delete a non Markdown file, TODO: move form posts & headlines.
-
#read_post_markdown_files ⇒ Object
Read markdown files in the posts folder into an array of Posts.
-
#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.
-
#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.
-
#sort_posts_by_date ⇒ Object
Sort the Posts array into date order, newest first.
-
#wrangle_headlines ⇒ Object
Process each of the headlines pages.
-
#write ⇒ Object
Write out the Posts, calls write_tags_and_headlines.
-
#write_headline(page) ⇒ Object
Pass headline data to the DataPage class to write the files.
-
#write_tags_and_headlines ⇒ Object
Write out the tags and headline files.
-
#writeless ⇒ Object
Generate data without writing files (for incremnetal builds & uploads).
Constructor Details
#initialize(site, options) ⇒ PostsAndHeadlines
Returns a new instance of PostsAndHeadlines.
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/zine/posts_and_headlines.rb', line 10 def initialize(site, ) @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
22 23 24 25 26 27 28 |
# File 'lib/zine/posts_and_headlines.rb', line 22 def find_page_from_path(file_path) post_to_rebuild = @post_array.detect do |post| File.(post.source_file) == file_path end index = @post_array.find_index post_to_rebuild { post: post_to_rebuild, index: index } end |
#headline_pages ⇒ Object
The headlines pages - the home page, an articles index, and RSS feed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/zine/posts_and_headlines.rb', line 31 def headline_pages dir = @options['directories']['build'] = @options['options'] templates = @options['templates'] [{ build_dir: dir, name: templates['articles'], number: @post_array.size, suffix: '.html', template_name: templates['articles'], title: 'Articles' }, { build_dir: dir, name: 'index', number: ['num_items_on_home'], suffix: '.html', template_name: templates['home'], title: 'Home' }, { build_dir: dir, name: 'rss', number: ['number_items_in_RSS'], suffix: '.xml', template_name: templates['rss'], title: '' }] end |
#once_only(source_file) ⇒ Object
46 47 48 49 50 |
# File 'lib/zine/posts_and_headlines.rb', line 46 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
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/zine/posts_and_headlines.rb', line 52 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 dest_path end |
#preview_delete(file_path) ⇒ Object
get build file from post or location, delete build, remove form post_array
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/zine/posts_and_headlines.rb', line 67 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
84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/zine/posts_and_headlines.rb', line 84 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
100 101 102 103 104 105 106 |
# File 'lib/zine/posts_and_headlines.rb', line 100 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
109 110 111 |
# File 'lib/zine/posts_and_headlines.rb', line 109 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
114 115 116 117 118 |
# File 'lib/zine/posts_and_headlines.rb', line 114 def preview_straight_delete(file) FileUtils.rm(File.join( preview_relative_equivalent(file), File.basename(file) )) end |
#read_post_markdown_files ⇒ Object
Read markdown files in the posts folder into an array of Posts
140 141 142 143 144 145 146 147 148 |
# File 'lib/zine/posts_and_headlines.rb', line 140 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
122 123 124 125 126 |
# File 'lib/zine/posts_and_headlines.rb', line 122 def rebuild_page(file) @site.write_markdown(@options['templates']['default'], File.(@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
131 132 133 134 135 136 137 |
# File 'lib/zine/posts_and_headlines.rb', line 131 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 end |
#sort_posts_by_date ⇒ Object
Sort the Posts array into date order, newest first
151 152 153 154 155 156 |
# File 'lib/zine/posts_and_headlines.rb', line 151 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_headlines ⇒ Object
Process each of the headlines pages
159 160 161 162 163 |
# File 'lib/zine/posts_and_headlines.rb', line 159 def wrangle_headlines headline_pages.each do |page| write_headline page end end |
#write ⇒ Object
Write out the Posts, calls write_tags_and_headlines
166 167 168 169 170 171 172 173 174 175 |
# File 'lib/zine/posts_and_headlines.rb', line 166 def write @tags_by_post = [] @post_array.each do |post| @tags_by_post << post.process(File) end # end point { posts: @post_array, guard: @guard } end |
#write_headline(page) ⇒ Object
Pass headline data to the DataPage class to write the files
189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/zine/posts_and_headlines.rb', line 189 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_headlines ⇒ Object
Write out the tags and headline files
204 205 206 207 208 209 210 211 |
# File 'lib/zine/posts_and_headlines.rb', line 204 def tag_name = @options['templates']['tag'] tag_index_name = @options['templates']['tag_index'] = Zine::Tag.new @tags_by_post, @site.make_template_bundle(tag_name), @site.make_template_bundle(tag_index_name), @options . wrangle_headlines end |
#writeless ⇒ Object
Generate data without writing files (for incremnetal builds & uploads)
178 179 180 181 182 183 184 185 186 |
# File 'lib/zine/posts_and_headlines.rb', line 178 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 |