Class: Amber::Site

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/amber/site.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_dir) ⇒ Site

Returns a new instance of Site.



15
16
17
18
# File 'lib/amber/site.rb', line 15

def initialize(root_dir)
  @continue_on_error = true
  @config = SiteConfiguration.load(self, root_dir)
end

Instance Attribute Details

#continue_on_errorObject

Returns the value of attribute continue_on_error.



10
11
12
# File 'lib/amber/site.rb', line 10

def continue_on_error
  @continue_on_error
end

#page_listObject

Returns the value of attribute page_list.



8
9
10
# File 'lib/amber/site.rb', line 8

def page_list
  @page_list
end

#rootObject

Returns the value of attribute root.



9
10
11
# File 'lib/amber/site.rb', line 9

def root
  @root
end

Instance Method Details

#add_config(config) ⇒ Object



20
21
22
# File 'lib/amber/site.rb', line 20

def add_config(config)
  @config.children << config
end

#all_pagesObject



106
107
108
# File 'lib/amber/site.rb', line 106

def all_pages
  @page_list
end

#clearObject



63
64
65
66
67
# File 'lib/amber/site.rb', line 63

def clear
  Dir.glob("#{@config.dest_dir}/*").each do |file|
    FileUtils.rm_r(file)
  end
end

#find_page(filter) ⇒ Object



102
103
104
# File 'lib/amber/site.rb', line 102

def find_page(filter)
  find_pages(filter)
end

#find_page_by_name(name) ⇒ Object



122
123
124
# File 'lib/amber/site.rb', line 122

def find_page_by_name(name)
  @pages_by_name[name]
end

#find_page_by_path(path, locale = I18n.default_locale) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/amber/site.rb', line 110

def find_page_by_path(path, locale=I18n.default_locale)
  if locale.is_a? String
    if I18n.locale_available?(locale)
      locale = locale.to_sym
    end
  end
  if path.is_a? Array
    path = path.join('/')
  end
  @pages_by_locale_path[locale][path] || @pages_by_path[path]
end

#find_pages(filter) ⇒ Object

find pages by a filter. filter is a string composing a path segment. For example:

"chat/security"

Which would match “/services/chat/security” but not “/services/security”



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/amber/site.rb', line 83

def find_pages(filter)
 filter = filter.downcase
 if filter =~ /\//
    path = filter.split('/').map{|segment| segment.gsub(/[^0-9a-z_-]/, '')}
    path_str = path.join('/')
    if (page = @pages_by_path[path_str])
      page
    elsif matched_path = @page_paths.grep(/#{Regexp.escape(path_str)}/).first
      @pages_by_path[matched_path]
    elsif page = @pages_by_name[path.last]
      page
    else
      nil
    end
  else
    @pages_by_name[filter]
  end
end

#load_pagesObject



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/amber/site.rb', line 24

def load_pages
  @root          = nil
  @pages_by_path = {}  # hash of pages keyed by page path
  @pages_by_name = {}  # hash of pages keyed by page name
  @page_list     = PageArray.new
  @dir_list      = []  # an array of non-page directories
  @page_paths    = []  # an array of page paths, used for greping through paths.

  # some paths are specific to only one locale (e.g aliases)
  @pages_by_locale_path = POSSIBLE_LANGUAGES.keys.inject(Hash.new({})) {|h,locale| h[locale] = {}; h}

  add_configuration(@config)
end

#renderObject

def reload_pages_if_needed

if @pages_by_path.nil? || @config.pages_changed?
  puts "Reloading pages ................."
  load_pages
end

end



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/amber/site.rb', line 45

def render
  @page_list.each do |page|
    page.render_to_file(@config.dest_dir)
    putc '.'; $stdout.flush
  end
  @dir_list.each do |directory|
    src = File.join(@config.pages_dir, directory)
    dst = File.join(@config.dest_dir, directory)
    Render::Asset.render_dir(src, dst)
    putc '.'; $stdout.flush
  end
  if @config.short_paths
    render_short_path_symlinks
  end
  Render::Apache.write_htaccess(@config, @config.pages_dir, @config.dest_dir)
  puts
end

#shortest_path(page) ⇒ Object

returns the shortest possible path for a page, such that the path doesn’t collide with another page or another page’s aliases.



130
131
132
133
134
135
136
137
138
139
140
# File 'lib/amber/site.rb', line 130

def shortest_path(page)
  path_so_far = []
  page.path.reverse.each do |path_segment|
    path_so_far.push(path_segment)
    path_str = path_so_far.join('/')
    if @pages_by_path[path_str].nil? && short_paths[path_str] == page
      return path_so_far
    end
  end
  return []
end

#with_destination(new_dest) ⇒ Object



69
70
71
72
73
74
# File 'lib/amber/site.rb', line 69

def with_destination(new_dest)
  dest_dir = @config.dest_dir
  @config.dest_dir = new_dest
  yield
  @config.dest_dir = dest_dir
end