Class: Dropsite::Site

Inherits:
Object
  • Object
show all
Defined in:
lib/dropsite/site.rb

Overview

Main site builder class. Some inspiration here from Jekyll (github.com/mojombo/jekyll).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Site

Returns a new instance of Site.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/dropsite/site.rb', line 8

def initialize(config)
  if config.is_a? String
    @public_dir = config
    @exclude, @disabled_plugins = [], []
    @quiet = false
  elsif config.is_a? OpenStruct
    @config = config.clone
    @public_dir = config.public_dir
    @exclude = config.exclude || []
    @disabled_plugins = config.disabled_plugins || []
    @quiet = config.quiet || false
  elsif config.is_a? Hash
    @config = config.clone
    @public_dir = config[:public_dir]
    @exclude = config[:exclude] || []
    @disabled_plugins = config[:disabled_plugins] || []
    @quiet = config[:quiet] || false
  end

  @site_tree = nil
  @site_files_dir = File.join(@public_dir, 'dropsite')
end

Instance Attribute Details

#disabled_pluginsObject (readonly)

Returns the value of attribute disabled_plugins.



6
7
8
# File 'lib/dropsite/site.rb', line 6

def disabled_plugins
  @disabled_plugins
end

#excludeObject (readonly)

Returns the value of attribute exclude.



6
7
8
# File 'lib/dropsite/site.rb', line 6

def exclude
  @exclude
end

#public_dirObject (readonly)

Returns the value of attribute public_dir.



6
7
8
# File 'lib/dropsite/site.rb', line 6

def public_dir
  @public_dir
end

#site_files_dirObject (readonly)

Returns the value of attribute site_files_dir.



6
7
8
# File 'lib/dropsite/site.rb', line 6

def site_files_dir
  @site_files_dir
end

#site_treeObject (readonly)

Returns the value of attribute site_tree.



6
7
8
# File 'lib/dropsite/site.rb', line 6

def site_tree
  @site_tree
end

Instance Method Details

#assets_dirObject



75
76
77
# File 'lib/dropsite/site.rb', line 75

def assets_dir
  File.join(@site_files_dir, 'dropsite-assets')
end

#cleanObject

Cleans up files previously generated by Dropsite (or at least ones) that look like they are. By default this will be an index.html file and dropsite directory in the Public directory root.



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/dropsite/site.rb', line 41

def clean
  if File.exist?(@site_files_dir)
    notice "Deleting existing site files dir at #{@site_files_dir}"
    FileUtils.rm_rf(@site_files_dir)
  end

  index_file = File.join(@public_dir, 'index.html')
  if File.exist?(index_file)
    notice "Deleting existing top-level index at #{index_file}"
    File.delete(index_file)
  end
end

#error(msg, err = nil) ⇒ Object

Output an error message



95
96
97
98
99
# File 'lib/dropsite/site.rb', line 95

def error(msg, err=nil)
  return if @quiet
  puts "#{Tty.red}Error#{Tty.reset}: #{msg}#{', details follow...' if err}"
  puts err.to_s if err
end

#notice(msg, strong = false) ⇒ Object

Output a normal user message



82
83
84
85
86
# File 'lib/dropsite/site.rb', line 82

def notice(msg, strong=false)
  # TODO: add a verbosity setting and use it here
  return if @quiet
  puts "#{Tty.blue}==>#{strong ? Tty.bold_white : Tty.reset + Tty.white} #{msg}#{Tty.reset}"
end

#processObject



31
32
33
34
35
36
# File 'lib/dropsite/site.rb', line 31

def process
  clean
  read
  render
  write
end

#readObject



54
55
56
# File 'lib/dropsite/site.rb', line 54

def read
  @site_tree = read_directory
end

#renderObject



58
59
60
# File 'lib/dropsite/site.rb', line 58

def render
  site_tree.render
end

#warning(msg) ⇒ Object

Output a warning message



89
90
91
92
# File 'lib/dropsite/site.rb', line 89

def warning(msg)
  return if @quiet
  puts "#{Tty.yellow}Warning#{Tty.reset}: #{msg}"
end

#writeObject



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/dropsite/site.rb', line 62

def write
  notice "Creating site files dir at #{site_files_dir}"
  Dir.mkdir(site_files_dir)
  site_tree.write

  Dir.mkdir(assets_dir)
  DirRenderer.renderers.each do |r|
    if File.exist? r.assets_dir
      FileUtils.cp_r(r.assets_dir, File.join(assets_dir, underscorize(r.class.to_s)))
    end
  end
end