Class: Zine::Site

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

Overview

the site

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSite

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 = {}
  init_options
  clean_option_paths
  init_templates
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



14
15
16
# File 'lib/zine.rb', line 14

def options
  @options
end

Instance Method Details

#build_siteObject



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_pathsObject



49
50
51
52
53
54
55
# File 'lib/zine.rb', line 49

def clean_option_paths
  directories = @options['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_pagesObject



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

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

#housekeeping_copyObject



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 = @options['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_optionsObject



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

def init_options
  @options ||= begin
    YAML.safe_load File.open('zine.yaml')
  rescue ArgumentError => err
    puts Rainbow("Could not parse YAML options: #{err.message}").red
  end
end

#init_templatesObject



32
33
34
35
36
37
38
# File 'lib/zine.rb', line 32

def init_templates
  tem_array = Dir[File.join(@options['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

#previewObject



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

def preview
  Server.new File.absolute_path(@options['directories']['build'])
end

#read_post_markdown_filesObject



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(@options['directories']['posts'], '*.md')]
  post_name = @options['templates']['post']
  file_name_array.each do |file|
    @post_array << Zine::Post.new(file,
                                  make_template_bundle(post_name),
                                  @options)
  end
end

#sort_posts_by_dateObject



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_headlinesObject



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]),
                           @options, data[:suffix])
  data_page.write
end

#write_other_markdown_pagesObject



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
  dir_options = @options['directories']
  src_dir = dir_options['source']
  search = File.join src_dir, '**', '*.md'
  default_name = @options['templates']['default']
  Dir[search].reject { |found| found[dir_options['posts']] }.each do |file|
    dir = Pathname(File.dirname(file)).relative_path_from(Pathname(src_dir))
    file_name = "#{File.basename(file, '.*')}.html"
    dest = File.join dir_options['build'], dir
    FileUtils.mkdir_p dest
    page = Zine::Page.new(file, File.join(dest, file_name),
                          make_template_bundle(default_name), @options)
    page.process
  end
end

#write_posts_and_headlinesObject



158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/zine.rb', line 158

def write_posts_and_headlines
  tags_by_post = []
  @post_array.each do |post|
    tags_by_post << post.process
  end
  tag_name = @options['templates']['tag']
  tag_index_name = @options['templates']['tag_index']
  tags = Zine::Tag.new tags_by_post, make_template_bundle(tag_name),
                       make_template_bundle(tag_index_name), @options
  tags.write_tags
  wrangle_headlines
end