Class: Troy::Site
- Inherits:
-
Object
- Object
- Troy::Site
- Defined in:
- lib/troy/site.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
-
#root ⇒ Object
Returns the value of attribute root.
Instance Method Summary collapse
-
#config ⇒ Object
A shortcut to the configuration.
- #export ⇒ Object
- #export_asset(sprockets, asset_name) ⇒ Object
- #export_assets ⇒ Object
- #export_file(assets, entry) ⇒ Object
- #export_files ⇒ Object
- #export_pages(file = nil) ⇒ Object
-
#initialize(root, options) ⇒ Site
constructor
A new instance of Site.
- #load_configuration ⇒ Object
- #load_extensions ⇒ Object
-
#pages ⇒ Object
Return all pages wrapped in Troy::Page class.
- #remove_public_dir ⇒ Object
- #set_locale ⇒ Object
- #source ⇒ Object
- #uglifier_options ⇒ Object
Constructor Details
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
5 6 7 |
# File 'lib/troy/site.rb', line 5 def @options end |
#root ⇒ Object
Returns the value of attribute root.
5 6 7 |
# File 'lib/troy/site.rb', line 5 def root @root end |
Instance Method Details
#config ⇒ Object
A shortcut to the configuration.
130 131 132 |
# File 'lib/troy/site.rb', line 130 def config Troy.configuration end |
#export ⇒ Object
32 33 34 35 36 37 |
# File 'lib/troy/site.rb', line 32 def export remove_public_dir export_assets export_files export_pages end |
#export_asset(sprockets, asset_name) ⇒ Object
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/troy/site.rb', line 101 def export_asset(sprockets, asset_name) asset = sprockets[asset_name] output_file = asset.filename.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 |
#export_assets ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/troy/site.rb', line 79 def export_assets sprockets = Sprockets::Environment.new sprockets.append_path root.join("assets/javascripts") sprockets.append_path root.join("assets/stylesheets") sprockets.append_path root.join("assets/scripts") sprockets.append_path root.join("assets/styles") if config.assets.compress_css sprockets.css_compressor = Sprockets::SassCompressor end if config.assets.compress_js sprockets.js_compressor = Uglifier.new() end config.assets.precompile.each_slice([:concurrency]) do |slice| slice.map do |asset_name| Thread.new { export_asset(sprockets, asset_name) } end.each(&:join) end end |
#export_file(assets, entry) ⇒ Object
49 50 51 52 53 54 55 56 57 |
# File 'lib/troy/site.rb', line 49 def export_file(assets, entry) basename = entry.to_s ignore = [".", "..", "javascripts", "stylesheets", "scripts", "styles"] return if ignore.include?(basename) FileUtils.rm_rf root.join("public/#{basename}") FileUtils.cp_r assets.join(entry), root.join("public/#{basename}") end |
#export_files ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/troy/site.rb', line 39 def export_files assets = root.join("assets") assets.entries.each_slice([:concurrency]) do |slice| slice.map do |entry| Thread.new { export_file(assets, entry) } end.each(&:join) end end |
#export_pages(file = nil) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/troy/site.rb', line 63 def export_pages(file = nil) file = File.(file) if file pages .select {|page| file.nil? || page.path == file } .each_slice([:concurrency]) do |slice| threads = slice.map do |page| Thread.new do page.save end end threads.each(&:join) end end |
#load_configuration ⇒ Object
28 29 30 |
# File 'lib/troy/site.rb', line 28 def load_configuration load root.join("config/troy.rb") end |
#load_extensions ⇒ Object
16 17 18 19 20 |
# File 'lib/troy/site.rb', line 16 def load_extensions Dir[root.join("config/**/*helpers.rb")].each do |file| require file end end |
#pages ⇒ Object
Return all pages wrapped in Troy::Page class.
125 126 127 |
# File 'lib/troy/site.rb', line 125 def pages @pages ||= source.map {|path| Page.new(self, path) } end |
#remove_public_dir ⇒ Object
59 60 61 |
# File 'lib/troy/site.rb', line 59 def remove_public_dir FileUtils.rm_rf root.join("public") end |
#set_locale ⇒ Object
22 23 24 25 26 |
# File 'lib/troy/site.rb', line 22 def set_locale I18n.load_path += config.i18n.load_path I18n.default_locale = config.i18n.locale I18n.locale = config.i18n.locale end |
#source ⇒ Object
119 120 121 |
# File 'lib/troy/site.rb', line 119 def source Dir[root.join("source/**/*.{html,erb,md,builder,xml,txt}").to_s] end |
#uglifier_options ⇒ Object
112 113 114 115 116 117 |
# File 'lib/troy/site.rb', line 112 def = Uglifier::DEFAULTS.dup [:output][:comments] = :none [:harmony] = true end |