Class: FrenchPress::Blog

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

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir = Dir.getwd) ⇒ Blog

Returns a new instance of Blog.



15
16
17
# File 'lib/frenchpress/blog.rb', line 15

def initialize(dir = Dir.getwd)
  @dir = dir
end

Instance Attribute Details

#dirObject (readonly)

Returns the value of attribute dir.



13
14
15
# File 'lib/frenchpress/blog.rb', line 13

def dir
  @dir
end

Instance Method Details

#commit(post) ⇒ Object



61
62
63
64
65
66
67
68
69
# 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) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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) ⇒ Object



19
20
21
22
23
# 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) ⇒ Object



53
54
55
# File 'lib/frenchpress/blog.rb', line 53

def post(*args)
  commit FrenchPress::Post.new_post_from_multiple(*args)
end

#reply(url, *args) ⇒ Object



57
58
59
# File 'lib/frenchpress/blog.rb', line 57

def reply(url, *args)
  commit FrenchPress::Post.generate_reply(url, *args)
end

#updateObject



71
72
73
74
75
# 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) ⇒ Object



77
78
79
80
81
82
83
# 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