lib/commands/build_command.rb in ignoramos-1.0.1 vs lib/commands/build_command.rb in ignoramos-1.1.0
- old
+ new
@@ -1,20 +1,21 @@
require 'liquid'
-require 'fileutils'
+require 'file_helper'
require 'models/post'
require 'models/page'
require 'models/app_config'
class BuildCommand
def initialize(dir = Dir.pwd)
@dir = dir
+ @file_helper = FileHelper.new(dir)
end
def execute
Liquid::Template.file_system = Liquid::LocalFileSystem.new("#{ @dir }/_includes")
FileUtils.rm_rf("#{ @dir }/_site")
- mkdir_p("_site")
+ @file_helper.mkdir_p("_site")
generate_pages
generate_posts
generate_tags_index
generate_homepage
@@ -22,11 +23,11 @@
copy_custom_files
end
private
def config
- @config ||= AppConfig.new(read_file("_config.yml"))
+ @config ||= AppConfig.new(@file_helper.read_file("_config.yml"))
end
def copy_custom_files
custom_files.each { |fn| copy_into_site(fn) }
end
@@ -61,11 +62,11 @@
posts = []
Dir.foreach("#{ @dir }/#{ dir }") do |item|
next if item == '.' || item == '..'
- contents = read_file("#{ dir }/#{ item }")
+ contents = @file_helper.read_file("#{ dir }/#{ item }")
posts << if block_given?
yield contents
else
Post.new(contents)
end
@@ -83,15 +84,15 @@
end
def generate_dir(posts, template, &block)
posts.each do |post|
params = yield post
- layout = read_file("_layouts/#{ post.vars['layout'] }/#{ template }.liquid")
+ layout = @file_helper.read_file("_layouts/#{ post.vars['layout'] }/#{ template }.liquid")
post_params = { 'site' => site_config }.merge(params.merge(post.vars))
post_params['title'] = "#{ post_params['title'] } - #{ config.vars['site']['name'] }"
- mkdir_p("_site#{ post.path }")
- new_file("_site#{ post.permalink }",
+ @file_helper.mkdir_p("_site#{ post.path }")
+ @file_helper.new_file("_site#{ post.permalink }",
Liquid::Template.parse(layout).render(post_params))
end
end
def tags
@@ -115,24 +116,24 @@
generate_index_per_tag
end
def tags_layout
- @layout ||= read_file("_layouts/default/tags.liquid")
+ @layout ||= @file_helper.read_file("_layouts/default/tags.liquid")
end
def new_tags_index_file(filename, title, tags)
- new_file(filename,
+ @file_helper.new_file(filename,
Liquid::Template.parse(tags_layout).render({
'title' => title,
'tags' => tags,
'site' => site_config
}))
end
def generate_index_per_tag
- mkdir_p("_site/tags")
+ @file_helper.mkdir_p("_site/tags")
tags.each do |tag|
new_tags_index_file("_site/tags/#{ tag['name'] }.html",
"##{ tag['name'] } - #{ config.vars['site']['name'] }",
[tag])
@@ -142,47 +143,31 @@
def generate_homepage
homepage_posts = posts.sort_by { |p| [p.timestamp, p.title] }.
reverse.
first(5)
- layout = read_file("_layouts/default/posts.liquid")
+ layout = @file_helper.read_file("_layouts/default/posts.liquid")
- new_file("_site/index.html",
- Liquid::Template.parse(layout).render({
- 'posts' => homepage_posts,
- 'title' => config.vars['site']['name'],
- 'site' => site_config
- }))
+ @file_helper.new_file("_site/index.html",
+ Liquid::Template.parse(layout).render({
+ 'posts' => homepage_posts,
+ 'title' => config.vars['site']['name'],
+ 'site' => site_config
+ }))
end
def site_config
{
"description" => config.site_description,
"site_map" => config.site_map,
"user" => config.user
}
end
- def mkdir_p(dir)
- FileUtils.mkdir_p("#{ @dir }/#{ dir }")
- end
-
- def new_file(filename, contents)
- new_post_file = File.new("#{ @dir }/#{ filename }", 'w')
- new_post_file.write(contents)
- new_post_file.close
- end
-
- def read_file(filename)
- File.open("#{ @dir }/#{ filename }", 'r') do |file|
- file.read()
- end
- end
-
def cache
@cache ||= {}
end
def template(layout)
- cache[layout.to_sym] ||= read_file("_layouts/#{ layout }/post.liquid")
+ cache[layout.to_sym] ||= @file_helper.read_file("_layouts/#{ layout }/post.liquid")
end
end