Class: Zine::Site
- Inherits:
-
Object
- Object
- Zine::Site
- Defined in:
- lib/zine.rb
Overview
the site
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #build_site ⇒ Object
- #clean_option_paths ⇒ Object
- #headline_pages ⇒ Object
- #housekeeping_copy ⇒ Object
- #init_options ⇒ Object
- #init_templates ⇒ Object
-
#initialize ⇒ Site
constructor
A new instance of Site.
- #make_template_bundle(type) ⇒ Object
- #preview ⇒ Object
- #read_post_markdown_files ⇒ Object
- #sort_posts_by_date ⇒ Object
- #wrangle_headlines ⇒ Object
- #write_headline(page) ⇒ Object
- #write_other_markdown_pages ⇒ Object
- #write_posts_and_headlines ⇒ Object
Constructor Details
#initialize ⇒ Site
Returns a new instance of Site.
16 17 18 19 20 21 22 |
# File 'lib/zine.rb', line 16 def initialize @post_array = [] @templates_by_name = {} clean_option_paths init_templates end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
14 15 16 |
# File 'lib/zine.rb', line 14 def end |
Instance Method Details
#build_site ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/zine.rb', line 40 def build_site read_post_markdown_files sort_posts_by_date write_posts_and_headlines housekeeping_copy write_other_markdown_pages preview end |
#clean_option_paths ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/zine.rb', line 49 def clean_option_paths directories = ['directories'] %w(assets posts styles templates).each do |dir| directories[dir] = File.join directories['source'], directories[dir] end directories['blog'] = File.join directories['build'], directories['blog'] end |
#headline_pages ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/zine.rb', line 108 def headline_pages dir = ['directories']['build'] = ['options'] templates = ['templates'] [{ build_dir: dir, name: '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 |
#housekeeping_copy ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/zine.rb', line 57 def housekeeping_copy directories = ['directories'] src_dir = directories['source'] search = File.join src_dir, '**', '*.*' possible = Dir.glob(search, File::FNM_DOTMATCH).reject do |found| found =~ /^.+\.md$|^.+\.erb$|^\.DS_Store$|^\.$|^\.\.$'/ || File.directory?(found) || found[directories['posts']] || found[directories['templates']] end possible.each do |file| dir = Pathname(File.dirname(file)).relative_path_from(Pathname(src_dir)) filename = File.basename file dest = File.join directories['build'], dir FileUtils.mkdir_p dest FileUtils.cp file, File.join(dest, filename) end end |
#init_options ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/zine.rb', line 24 def ||= begin YAML.safe_load File.open('zine.yaml') rescue ArgumentError => err puts Rainbow("Could not parse YAML options: #{err.message}").red end end |
#init_templates ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/zine.rb', line 32 def init_templates tem_array = Dir[File.join(['directories']['templates'], '*.erb')] tem_array.each do |tem| @templates_by_name.merge!(File.basename(tem, '.*') => ERB.new(File.read(tem), 0, '-')) end end |
#make_template_bundle(type) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/zine.rb', line 75 def make_template_bundle(type) TemplateFiles.new( if @templates_by_name.key?(type) @templates_by_name[type] else @templates_by_name['default'] end, @templates_by_name['header_partial'], @templates_by_name['footer_partial'] ) end |
#preview ⇒ Object
87 88 89 |
# File 'lib/zine.rb', line 87 def preview Server.new File.absolute_path(['directories']['build']) end |
#read_post_markdown_files ⇒ Object
91 92 93 94 95 96 97 98 99 |
# File 'lib/zine.rb', line 91 def read_post_markdown_files file_name_array = Dir[File.join(['directories']['posts'], '*.md')] post_name = ['templates']['post'] file_name_array.each do |file| @post_array << Zine::Post.new(file, make_template_bundle(post_name), ) end end |
#sort_posts_by_date ⇒ Object
101 102 103 104 105 106 |
# File 'lib/zine.rb', line 101 def sort_posts_by_date @post_array.sort_by! do |post| post.formatted_data.page[:date_rfc3339] end.reverse! @post_array.freeze end |
#wrangle_headlines ⇒ Object
123 124 125 126 127 |
# File 'lib/zine.rb', line 123 def wrangle_headlines headline_pages.each do |page| write_headline page end end |
#write_headline(page) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/zine.rb', line 129 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, make_template_bundle(data[:template_name]), , data[:suffix]) data_page.write end |
#write_other_markdown_pages ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/zine.rb', line 142 def write_other_markdown_pages = ['directories'] src_dir = ['source'] search = File.join src_dir, '**', '*.md' default_name = ['templates']['default'] Dir[search].reject { |found| found[['posts']] }.each do |file| dir = Pathname(File.dirname(file)).relative_path_from(Pathname(src_dir)) file_name = "#{File.basename(file, '.*')}.html" dest = File.join ['build'], dir FileUtils.mkdir_p dest page = Zine::Page.new(file, File.join(dest, file_name), make_template_bundle(default_name), ) page.process end end |
#write_posts_and_headlines ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/zine.rb', line 158 def write_posts_and_headlines = [] @post_array.each do |post| << post.process end tag_name = ['templates']['tag'] tag_index_name = ['templates']['tag_index'] = Zine::Tag.new , make_template_bundle(tag_name), make_template_bundle(tag_index_name), . wrangle_headlines end |