Class: Henshin::Site
- Inherits:
-
Object
- Object
- Henshin::Site
- Defined in:
- lib/henshin/site.rb
Instance Attribute Summary collapse
-
#archive ⇒ Object
Returns the value of attribute archive.
-
#base ⇒ String
A path which should be prepended to all urls.
-
#categories ⇒ Object
Returns the value of attribute categories.
-
#config ⇒ Hash
The configuration made up of the override, options.yaml and Henshin::Defaults.
-
#gens ⇒ Object
Returns the value of attribute gens.
-
#layouts ⇒ Object
Returns the value of attribute layouts.
-
#plugins ⇒ Object
Returns the value of attribute plugins.
-
#posts ⇒ Object
Returns the value of attribute posts.
-
#root ⇒ Pathname
The path to the site to generate from where the command is executed.
-
#statics ⇒ Object
Returns the value of attribute statics.
-
#tags ⇒ Object
Returns the value of attribute 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
25 26 27 28 29 30 |
# File 'lib/henshin/site.rb', line 25 def initialize(override={}) self.reset self.configure(override) self.load_plugins self end |
Instance Attribute Details
#archive ⇒ Object
Returns the value of attribute archive.
18 19 20 |
# File 'lib/henshin/site.rb', line 18 def archive @archive end |
#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 ⇒ Object
Returns the value of attribute categories.
18 19 20 |
# File 'lib/henshin/site.rb', line 18 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 ⇒ Object
Returns the value of attribute gens.
17 18 19 |
# File 'lib/henshin/site.rb', line 17 def gens @gens end |
#layouts ⇒ Object
Returns the value of attribute layouts.
17 18 19 |
# File 'lib/henshin/site.rb', line 17 def layouts @layouts end |
#plugins ⇒ Object
Returns the value of attribute plugins.
19 20 21 |
# File 'lib/henshin/site.rb', line 19 def plugins @plugins end |
#posts ⇒ Object
Returns the value of attribute posts.
17 18 19 |
# File 'lib/henshin/site.rb', line 17 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 ⇒ Object
Returns the value of attribute statics.
17 18 19 |
# File 'lib/henshin/site.rb', line 17 def statics @statics end |
#tags ⇒ Object
Returns the value of attribute tags.
18 19 20 |
# File 'lib/henshin/site.rb', line 18 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
104 105 106 107 108 109 110 |
# File 'lib/henshin/site.rb', line 104 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
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/henshin/site.rb', line 53 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.
230 231 232 233 234 235 |
# File 'lib/henshin/site.rb', line 230 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.
239 240 241 242 243 244 245 246 |
# File 'lib/henshin/site.rb', line 239 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.
218 219 220 |
# File 'lib/henshin/site.rb', line 218 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
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/henshin/site.rb', line 81 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.
176 177 178 179 180 181 182 183 184 |
# File 'lib/henshin/site.rb', line 176 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.
224 225 226 |
# File 'lib/henshin/site.rb', line 224 def post?( path ) path.include?('posts/') && !ignored?(path) end |
#process ⇒ Object
Processes all of the necessary files
163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/henshin/site.rb', line 163 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
117 118 119 120 121 122 123 |
# File 'lib/henshin/site.rb', line 117 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
143 144 145 146 147 148 149 |
# File 'lib/henshin/site.rb', line 143 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
126 127 128 129 130 131 132 |
# File 'lib/henshin/site.rb', line 126 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
135 136 137 138 139 140 |
# File 'lib/henshin/site.rb', line 135 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
152 153 154 155 156 157 158 |
# File 'lib/henshin/site.rb', line 152 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
188 189 190 191 192 193 |
# File 'lib/henshin/site.rb', line 188 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.
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/henshin/site.rb', line 36 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.
212 213 214 |
# File 'lib/henshin/site.rb', line 212 def static?( path ) !( layout?(path) || post?(path) || gen?(path) || ignored?(path) ) end |
#write ⇒ Object
Writes the files
198 199 200 201 202 203 204 205 206 207 |
# File 'lib/henshin/site.rb', line 198 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 |