class Zine::Site
the site
Attributes
Site
options read from YAML during the initialisation of the class
Public Class Methods
Create a new instance of Site
, called by CLI#init_site
# File lib/zine.rb, line 17 def initialize @templates_by_name = {} init_options clean_option_paths end
Public Instance Methods
A dry run build that generates baseline data to compare changes to during editting/preview, then generate files where the source files change. Called by CLI#build
# File lib/zine.rb, line 26 def build_site init_templates FileUtils.mkdir_p @options['directories']['build'] # posts_and_guard is { posts: @post_array, guard: @guard } posts_and_guard = posts_and_headlines_without_writing preview posts_and_guard end
Build the files of the site, generating files immeadiately, suitable for the initial build or complete rebuild. Called by CLI#force
# File lib/zine.rb, line 37 def build_site_forcing_writes init_templates FileUtils.mkdir_p @options['directories']['build'] # posts_and_guard is { posts: @post_array, guard: @guard } posts_and_guard = write_posts_and_headlines housekeeping write_other_markdown_pages preview posts_and_guard end
Create a new instance of TemplateFiles
, looking up the body template in @templates_by_name, defaults to the template named 'default'
Called in PostsAndHeadlines.read_post_markdown_files
, PostsAndHeadlines.one_new_post
, PostsAndHeadlines.write_headline
, PostsAndHeadlines.write_tags_and_headlines
Attributes¶ ↑
-
type
- The name of the template used for the body eg post
Eg¶ ↑
make_template_bundle 'post'
# File lib/zine.rb, line 62 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
Build the site, noticing this file as having changed Called by CLI#notice
TODO: common function call across other methods
# File lib/zine.rb, line 84 def notice(file) # build init_templates FileUtils.mkdir_p @options['directories']['build'] # posts_and_headlines_without_writing posts = Zine::PostsAndHeadlines.new self, @options # starts watcher posts_and_guard = posts.writeless guard = posts_and_guard[:guard] # build the file & add it to the upload array posts_dir = @options['directories']['posts'] file_to_notice = File.join posts_dir, file path_to_notice = File.join Dir.getwd, file_to_notice dest_path = posts.one_new_post file_to_notice, path_to_notice guard.notice(dest_path) # TODO: everywhere guard.listener_array.each(&:stop) # preview posts_and_guard -- no preview needed... return if @options['upload']['method'] == 'none' || (guard.delete_array.empty? && guard.upload_array.empty?) uploader = Zine::Upload.new(@options['directories']['build'], @options['upload'], guard.delete_array, guard.upload_array) uploader.upload_decision MockYes end
# File lib/zine.rb, line 115 def write_markdown(default_name, src_dir, file) dir = Pathname(File.dirname(file)).relative_path_from(Pathname(src_dir)) file_name = "#{File.basename(file, '.*')}.html" dest = File.join @options['directories']['build'], dir FileUtils.mkdir_p dest page = Zine::Page.new(file, File.join(dest, file_name), make_template_bundle(default_name), @options) page.process File end
Private Instance Methods
# File lib/zine.rb, line 127 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
# File lib/zine.rb, line 135 def housekeeping directories = @options['directories'] source_directory = directories['source'] search = File.join source_directory, '**', '*.*' possible = Dir.glob(search, File::FNM_DOTMATCH).reject do |found| housekeeping_files_not_to_copy found, directories end possible.each do |file| housekeeping_copy(file, source_directory, directories['build']) end end
# File lib/zine.rb, line 155 def housekeeping_copy(file, src_dir, build_dir) dir = Pathname(File.dirname(file)).relative_path_from(Pathname(src_dir)) filename = File.basename file dest = File.join build_dir, dir FileUtils.mkdir_p dest FileUtils.cp file, File.join(dest, filename) end
# File lib/zine.rb, line 147 def housekeeping_files_not_to_copy(found, directories) found =~ /^.+\.md$|^.+\.erb$|^\.DS_Store$|^\.$|^\.\.$'/ || File.directory?(found) || found[directories['posts']] || found[directories['templates']] || found[directories['styles']] end
# File lib/zine.rb, line 163 def init_options @options ||= YAML.safe_load File.open('zine.yaml') rescue ArgumentError => e puts Rainbow("Could not parse YAML options: #{e.message}").red end
# File lib/zine.rb, line 169 def init_templates Encoding.default_external = 'UTF-8' 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), trim_mode: '-')) end end
Generate data without writing files (for incremental builds & uploads) returns posts & guard to use during edits under preview
# File lib/zine.rb, line 181 def posts_and_headlines_without_writing posts = Zine::PostsAndHeadlines.new self, @options posts.writeless end
# File lib/zine.rb, line 186 def preview(posts_and_guard) guard = posts_and_guard[:guard] Server.new @options['directories']['build'], @options['upload'], guard.delete_array, guard.upload_array end
# File lib/zine.rb, line 192 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| write_markdown(default_name, src_dir, file) end end
returns posts & guard to use during edits under preview
# File lib/zine.rb, line 203 def write_posts_and_headlines posts = Zine::PostsAndHeadlines.new self, @options posts.write end