Class: Henshin::Site
- Inherits:
-
Object
- Object
- Henshin::Site
- Defined in:
- lib/henshin/site.rb
Instance Attribute Summary collapse
- #archive ⇒ Archive
-
#base ⇒ String
A path which should be prepended to all urls.
- #categories ⇒ Categories
-
#config ⇒ Hash
The configuration made up of the override, options.yaml and Henshin::Defaults.
- #gens ⇒ Array
- #layouts ⇒ Hash{String => String}
- #plugins ⇒ Hash{String => Plugin}
- #posts ⇒ Array
-
#root ⇒ Pathname
The path to the site to generate from where the command is executed.
- #statics ⇒ Array
- #tags ⇒ Tags
-
#target ⇒ Pathname
The path to where the finished site should be written.
Instance Method Summary collapse
-
#build ⇒ Object
Read, process, render and write.
-
#configure(override) ⇒ Object
Creates the configuration hash by merging defaults, supplied options and options read from the ‘options.yaml’ file.
-
#gen?(path) ⇒ Bool
Whether the path points to a gen.
-
#ignored?(path) ⇒ Bool
Whether the path points to a file which should be ignored.
-
#initialize(override = {}) ⇒ self
constructor
A new instance of site.
-
#layout?(path) ⇒ Bool
Whether the path points to a layout.
-
#load_plugins ⇒ Object
Requires each plugin in @config, then loads and sorts them into
plugins
by type. -
#payload ⇒ Hash
The payload for the layout engine.
-
#post?(path) ⇒ Bool
Whether the path points to a post.
-
#process ⇒ Object
Processes all of the necessary files.
-
#read ⇒ self
Reads all necessary files and puts them into the necessary arrays.
-
#read_gens ⇒ Object
Adds all files that need to be run through a plugin in an array.
-
#read_layouts ⇒ Object
Adds all items in ‘/layouts’ to the layouts array.
-
#read_posts ⇒ Object
Adds all items in ‘/posts’ to the posts array.
-
#read_statics ⇒ Object
Adds all static files to an array.
-
#render ⇒ Object
Renders the files.
-
#reset ⇒ self
Resets all instance variables, except
config
andplugins
as these shouldn’t need to be reloaded each time. -
#static?(path) ⇒ Bool
Whether the path points to a static.
-
#write ⇒ Object
Writes the files.
Constructor Details
#initialize(override = {}) ⇒ self
A new instance of site
39 40 41 42 43 44 |
# File 'lib/henshin/site.rb', line 39 def initialize(override={}) self.reset self.configure(override) self.load_plugins self end |
Instance Attribute Details
#base ⇒ String
Returns a path which should be prepended to all urls.
15 16 17 |
# File 'lib/henshin/site.rb', line 15 def base @base end |
#categories ⇒ Categories
27 28 29 |
# File 'lib/henshin/site.rb', line 27 def categories @categories end |
#config ⇒ Hash
Returns the configuration made up of the override, options.yaml and Henshin::Defaults.
6 7 8 |
# File 'lib/henshin/site.rb', line 6 def config @config end |
#gens ⇒ Array
18 19 20 |
# File 'lib/henshin/site.rb', line 18 def gens @gens end |
#layouts ⇒ Hash{String => String}
21 22 23 |
# File 'lib/henshin/site.rb', line 21 def layouts @layouts end |
#plugins ⇒ Hash{String => Plugin}
33 34 35 |
# File 'lib/henshin/site.rb', line 33 def plugins @plugins end |
#posts ⇒ Array
18 19 20 |
# File 'lib/henshin/site.rb', line 18 def posts @posts end |
#root ⇒ Pathname
Returns the path to the site to generate from where the command is executed.
9 10 11 |
# File 'lib/henshin/site.rb', line 9 def root @root end |
#statics ⇒ Array
18 19 20 |
# File 'lib/henshin/site.rb', line 18 def statics @statics end |
#tags ⇒ Tags
24 25 26 |
# File 'lib/henshin/site.rb', line 24 def @tags end |
#target ⇒ Pathname
Returns the path to where the finished site should be written.
12 13 14 |
# File 'lib/henshin/site.rb', line 12 def target @target end |
Instance Method Details
#build ⇒ Object
Read, process, render and write
118 119 120 121 122 123 124 |
# File 'lib/henshin/site.rb', line 118 def build self.reset self.read self.process self.render self.write end |
#configure(override) ⇒ Object
Creates the configuration hash by merging defaults, supplied options and options read from the ‘options.yaml’ file. Then sets root, target, base and appends special directories to @config
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/henshin/site.rb', line 67 def configure(override) @config = {} config_file = File.join((override['root'] || Defaults['root']), '/options.yaml') # change target to represent given root, only if root given if override['root'] && !override['target'] override['target'] = File.join(override['root'], Defaults['target']) end begin config = YAML.load_file(config_file) @config = Defaults.merge(config).merge(override) rescue => e $stderr.puts "\nCould not read configuration, falling back to defaults..." $stderr.puts "-> #{e.to_s}" @config = Defaults.merge(override) end @root = @config['root'].to_p @target = @config['target'].to_p @base = @config['base'] || "/" @base = '/' + @base unless @base[0] == '/' # need to make sure it starts with slash @config['exclude'] << '/_site' << '/plugins' end |
#gen?(path) ⇒ Bool
Returns whether the path points to a gen.
244 245 246 247 248 249 |
# File 'lib/henshin/site.rb', line 244 def gen?( path ) return false if post?(path) || layout?(path) || ignored?(path) return true if @plugins[:generators].has_key? path.to_p.extname[1..-1] return true if File.open(path, "r").read(3) == "---" false end |
#ignored?(path) ⇒ Bool
Returns whether the path points to a file which should be ignored.
253 254 255 256 257 258 259 260 |
# File 'lib/henshin/site.rb', line 253 def ignored?( path ) ignored = ['/options.yaml'] + @config['exclude'] ignored.collect! {|i| File.join(@root, i)} ignored.each do |i| return true if path.include? i end false end |
#layout?(path) ⇒ Bool
Returns whether the path points to a layout.
232 233 234 |
# File 'lib/henshin/site.rb', line 232 def layout?( path ) path.include?('layouts/') && !ignored?(path) end |
#load_plugins ⇒ Object
Requires each plugin in @config, then loads and sorts them into plugins
by type
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/henshin/site.rb', line 95 def load_plugins @plugins = {:generators => {}, :layoutors => []} @config['plugins'].each do |plugin| begin require File.join('henshin', 'plugins', plugin) rescue LoadError require File.join(@root, 'plugins', plugin) end end Henshin::Generator.subclasses.each do |plugin| plugin = plugin.new(self) plugin.extensions[:input].each do |ext| @plugins[:generators][ext] = plugin end end @plugins[:layoutors] = Henshin::Layoutor.subclasses.map {|l| l.new(self)}.sort end |
#payload ⇒ Hash
Returns the payload for the layout engine.
190 191 192 193 194 195 196 197 198 |
# File 'lib/henshin/site.rb', line 190 def payload r = {'site' => @config} r['site']['created_at'] = Time.now r['site']['posts'] = @posts.collect{|i| i.to_hash} r['site']['tags'] = @tags.to_hash r['site']['categories'] = @categories.to_hash r['site']['archive'] = @archive.to_hash r end |
#post?(path) ⇒ Bool
Returns whether the path points to a post.
238 239 240 |
# File 'lib/henshin/site.rb', line 238 def post?( path ) path.include?('posts/') && !ignored?(path) end |
#process ⇒ Object
Processes all of the necessary files
177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/henshin/site.rb', line 177 def process @posts.sort! @gens.sort! @posts.each do |post| @tags << post if post.data['tags'] @categories << post if post.data['category'] @archive << post end self end |
#read ⇒ self
Reads all necessary files and puts them into the necessary arrays
131 132 133 134 135 136 137 |
# File 'lib/henshin/site.rb', line 131 def read self.read_layouts self.read_posts self.read_gens self.read_statics self end |
#read_gens ⇒ Object
Adds all files that need to be run through a plugin in an array
157 158 159 160 161 162 163 |
# File 'lib/henshin/site.rb', line 157 def read_gens files = Dir.glob( File.join(@root, '**', '*.*') ) gens = files.select {|i| gen?(i) } gens.each do |gen| @gens << Gen.new(gen.to_p, self).read end end |
#read_layouts ⇒ Object
Adds all items in ‘/layouts’ to the layouts array
140 141 142 143 144 145 146 |
# File 'lib/henshin/site.rb', line 140 def read_layouts path = File.join(@root, 'layouts') Dir.glob(path + '/*.*').each do |layout| layout =~ /([a-zA-Z0-9 _-]+)\.([a-zA-Z0-9-]+)/ @layouts[$1] = File.open(layout, 'r') {|f| f.read} end end |
#read_posts ⇒ Object
Adds all items in ‘/posts’ to the posts array
149 150 151 152 153 154 |
# File 'lib/henshin/site.rb', line 149 def read_posts path = File.join(@root, 'posts') Dir.glob(path + '/**/*.*').each do |post| @posts << Post.new(post.to_p, self).read end end |
#read_statics ⇒ Object
Adds all static files to an array
166 167 168 169 170 171 172 |
# File 'lib/henshin/site.rb', line 166 def read_statics files = Dir.glob( File.join(@root, '**', '*.*') ) static = files.select {|i| static?(i) } static.each do |static| @statics << Static.new(static.to_p, self) end end |
#render ⇒ Object
Renders the files
202 203 204 205 206 207 |
# File 'lib/henshin/site.rb', line 202 def render @posts.each {|post| post.render} @gens.each {|gen| gen.render} self end |
#reset ⇒ self
Resets all instance variables, except config
and plugins
as these shouldn’t need to be reloaded each time.
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/henshin/site.rb', line 50 def reset @gens = [] @posts = [] @statics = [] @layouts = {} @archive = Archive.new(self) @tags = Labels.new('tag', self) @categories = Labels.new('category', self) self end |
#static?(path) ⇒ Bool
Returns whether the path points to a static.
226 227 228 |
# File 'lib/henshin/site.rb', line 226 def static?( path ) !( layout?(path) || post?(path) || gen?(path) || ignored?(path) ) end |
#write ⇒ Object
Writes the files
212 213 214 215 216 217 218 219 220 221 |
# File 'lib/henshin/site.rb', line 212 def write @posts.each {|post| post.write} @gens.each {|gen| gen.write} @statics.each {|static| static.write} @archive.write @tags.write @categories.write self end |