Class: Troy::Site

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ Site

Returns a new instance of Site.



5
6
7
8
9
10
11
# File 'lib/troy/site.rb', line 5

def initialize(root)
  @root = Pathname.new(root).realpath

  load_configuration
  load_extensions
  set_locale
end

Instance Attribute Details

#rootObject

Returns the value of attribute root.



3
4
5
# File 'lib/troy/site.rb', line 3

def root
  @root
end

Instance Method Details

#configObject

A shortcut to the configuration.



116
117
118
# File 'lib/troy/site.rb', line 116

def config
  Troy.configuration
end

#exportObject



37
38
39
40
41
42
# File 'lib/troy/site.rb', line 37

def export
  remove_public_dir
  export_assets
  export_files
  export_pages
end

#export_assetsObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/troy/site.rb', line 75

def export_assets
  sprockets = Sprockets::Environment.new
  sprockets.append_path root.join("assets/javascripts")
  sprockets.append_path root.join("assets/stylesheets")
  sprockets.css_compressor = Sprockets::SassCompressor if config.assets.compress_css
  sprockets.js_compressor = Uglifier.new(uglifier_options) if config.assets.compress_js

  config.assets.precompile.each do |asset_name|
    asset = sprockets[asset_name]
    output_file = asset.pathname.to_s
      .gsub(root.join("assets").to_s, "")
      .gsub(%r[^/], "")
      .gsub(/\.scss$/, ".css")
      .gsub(/\.coffee$/, ".js")

    asset.write_to root.join("public/#{output_file}")
  end
end

#export_filesObject



46
47
48
49
50
51
52
53
54
55
# File 'lib/troy/site.rb', line 46

def export_files
  assets = root.join("assets")

  assets.entries.each do |entry|
    basename = entry.to_s
    next if [".", "..", "javascripts", "stylesheets"].include?(basename)
    FileUtils.rm_rf root.join("public/#{basename}")
    FileUtils.cp_r assets.join(entry), root.join("public/#{basename}")
  end
end

#export_pages(file = nil) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/troy/site.rb', line 65

def export_pages(file = nil)
  file = File.expand_path(file) if file

  pages
    .select {|page| file.nil? || page.path == file }
    .each(&:save)
end

#load_configurationObject



31
32
33
# File 'lib/troy/site.rb', line 31

def load_configuration
  load root.join("config/troy.rb")
end

#load_extensionsObject



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

def load_extensions
  Dir[root.join("config/**/*helpers.rb")].each do |file|
    require file
  end
end

#pagesObject

Return all pages wrapped in Troy::Page class.



111
112
113
# File 'lib/troy/site.rb', line 111

def pages
  @pages ||= source.map {|path| Page.new(self, path) }
end

#remove_public_dirObject



59
60
61
# File 'lib/troy/site.rb', line 59

def remove_public_dir
  FileUtils.rm_rf root.join("public")
end

#set_localeObject



23
24
25
26
27
# File 'lib/troy/site.rb', line 23

def set_locale
  I18n.load_path += config.i18n.load_path
  I18n.default_locale = config.i18n.locale
  I18n.locale = config.i18n.locale
end

#sourceObject



105
106
107
# File 'lib/troy/site.rb', line 105

def source
  Dir[root.join("source/**/*.{html,erb,md,builder,xml,txt}").to_s]
end

#uglifier_optionsObject



97
98
99
100
101
# File 'lib/troy/site.rb', line 97

def uglifier_options
  options = Uglifier::DEFAULTS.dup
  options[:output][:comments] = :none
  options
end