class FrenchPress::Blog

This represents a blog object in FrenchPress. Usually it is created from the FrenchPress object (through FrenchPress.open for convenience) and manipulated by .post(f) or .new_blog.

Attributes

dir[R]

Public Class Methods

new(dir = Dir.getwd) click to toggle source
# File lib/frenchpress/blog.rb, line 15
def initialize(dir = Dir.getwd)
  @dir = dir
end

Public Instance Methods

commit(post) click to toggle source
# File lib/frenchpress/blog.rb, line 61
def commit(post)
  puts "Committing #{post}"
  file_path = File.join(@dir, '_posts', "#{post.file_name}.html")
  raw_file_path = File.join(
    @dir, 'raw', "#{post.file_name.split('-').pop}.html")
  File.open(file_path, 'a+') { |f| f.puts post.render_with_tags }
  File.open(raw_file_path, 'a+') { |f| f.puts post.render }
  # TODO: Git commit
end
copy_install_files(dir, update = false) click to toggle source
# File lib/frenchpress/blog.rb, line 25
def copy_install_files(dir, update = false)
  dir = File.join dir, 'install'
  wd = File.expand_path Dir.getwd
  unless update
    Dir.mkdir '_posts' unless File.directory? '_posts'
    Dir.mkdir 'raw' unless File.directory? 'raw'
  end
  Dir.chdir(dir) do
    files = Dir.glob('**/*')
    pb = ProgressBar.create(total: files.length)
    Dir.glob('**/*/').each do |d|
      begin
        d = File.expand_path d
        Dir.mkdir(d.gsub(dir, wd))
      rescue
        puts "Error creating #{d.gsub(dir, wd)}"
      end
    end
    files.each do |f|
      next if f == 'frenchpress'
      next if File.directory? f
      f = File.expand_path f
      FileUtils.cp f, f.gsub(dir, wd)
      pb.increment
    end
  end
end
new_blog(args) click to toggle source
# File lib/frenchpress/blog.rb, line 19
def new_blog(args)
  dir = File.expand_path File.join(File.dirname(__FILE__), '..', '..')
  copy_install_files dir
  write_config(args) if args
end
post(*args) click to toggle source
# File lib/frenchpress/blog.rb, line 53
def post(*args)
  commit FrenchPress::Post.new_post_from_multiple(*args)
end
reply(url, *args) click to toggle source
# File lib/frenchpress/blog.rb, line 57
def reply(url, *args)
  commit FrenchPress::Post.generate_reply(url, *args)
end
update() click to toggle source
# File lib/frenchpress/blog.rb, line 71
def update
  Dir.chdir(@dir) { FileUtils.rm_r ['_layouts', 'css', 'index.html'] }
  dir = File.expand_path File.join(File.dirname(__FILE__), '..', '..')
  copy_install_files dir, true
end
write_config(args) click to toggle source
# File lib/frenchpress/blog.rb, line 77
def write_config(args)
  Dir.chdir(@dir) do
    File.open('_config.yml', 'a+') do |f|
      f.write args.to_yaml
    end
  end
end