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



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



60
61
62
63
64
65
66
67
68
# File 'lib/frenchpress/blog.rb', line 60

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



24
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
# File 'lib/frenchpress/blog.rb', line 24

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_blogObject



19
20
21
22
# File 'lib/frenchpress/blog.rb', line 19

def new_blog
  dir = File.expand_path File.join(File.dirname(__FILE__), '..', '..')
  copy_install_files dir
end

#post(*args) ⇒ Object



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

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

#reply(url, *args) ⇒ Object



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

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

#updateObject



70
71
72
73
74
75
76
77
78
# File 'lib/frenchpress/blog.rb', line 70

def update
  Dir.chdir(@dir) do
    FileUtils.rm_rf '_layouts'
    FileUtils.rm_rf 'css'
    FileUtils.rm_rf 'index.html'
  end
  dir = File.expand_path File.join(File.dirname(__FILE__), '..', '..')
  copy_install_files dir, true
end