Class: Webby::Builder
- Inherits:
-
Object
- Object
- Webby::Builder
- Defined in:
- lib/webby/builder.rb
Overview
The Builder class performs the work of scanning the content folder, creating Resource objects, and converting / copying the contents to the output folder as needed.
Class Method Summary collapse
-
.create(page, opts = {}) ⇒ Object
call-seq: Builder.create( page, :from => template, :locals => {} ).
-
.new_page_info ⇒ Object
call-seq: Builder.new_page_info => [page, title, directory].
-
.run(opts = {}) ⇒ Object
call-seq: Builder.run( :rebuild => false ).
Instance Method Summary collapse
-
#initialize ⇒ Builder
constructor
call-seq: Builder.new.
-
#load_files ⇒ Object
Scan the
layouts/
folder and thecontent/
folder and create a new Resource object for each file found there. -
#run(opts = {}) ⇒ Object
call-seq: run( :rebuild => false, :load_files => true ).
Constructor Details
Class Method Details
.create(page, opts = {}) ⇒ Object
call-seq:
Builder.create( page, :from => template, :locals => {} )
This mehod is used to create a new page in the content folder based on the specified template. page is the relative path to the new page from the content/
folder. The template is the name of the template to use from the templates/
folder.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/webby/builder.rb', line 34 def create( page, opts = {} ) tmpl = opts[:from] raise Error, "template not given" unless tmpl name = ::Webby::Resources.basename(page) ext = ::Webby::Resources.extname(page) dir = ::File.dirname(page) dir = '' if dir == '.' if tmpl.pathmap('%n') =~ %r/^_/ page = ::File.join(::Webby.site.content_dir, dir, '_'+name) page << '.' << (ext.empty? ? 'txt' : ext) elsif ::Webby.site.create_mode == 'directory' and name != 'index' page = ::File.join(::Webby.site.content_dir, dir, name, 'index') page << '.' << (ext.empty? ? 'txt' : ext) else page = ::File.join(::Webby.site.content_dir, page) page << '.txt' if ext.empty? end raise Error, "#{page} already exists" if test ?e, page Logging::Logger[self].info "creating #{page}" FileUtils.mkdir_p ::File.dirname(page) context = scope opts[:locals].each do |k,v| Thread.current[:value] = v definition = "#{k} = Thread.current[:value]" eval(definition, context) end if opts.has_key?(:locals) str = ERB.new(::File.read(tmpl), nil, '-').result(context) ::File.open(page, 'w') {|fd| fd.write str} page end |
.new_page_info ⇒ Object
call-seq:
Builder.new_page_info => [page, title, directory]
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/webby/builder.rb', line 74 def new_page_info args = Webby.site.args if args.raw.empty? task_name = Rake.application.top_level_tasks.first raise "Usage: webby #{task_name} path" end [args.page, args.title, args.dir] end |
.run(opts = {}) ⇒ Object
call-seq:
Builder.run( :rebuild => false )
Create a new instance of the Builder class and invoke the run method. If the :rebuild
option is given as true
, then all pages will be recreated / copied.
22 23 24 |
# File 'lib/webby/builder.rb', line 22 def run( opts = {} ) self.new.run opts end |
Instance Method Details
#load_files ⇒ Object
Scan the layouts/
folder and the content/
folder and create a new Resource object for each file found there.
163 164 165 166 167 168 169 |
# File 'lib/webby/builder.rb', line 163 def load_files ::Find.find(layout_dir, content_dir) do |path| next unless test ?f, path next if path =~ ::Webby.exclude Resources.new path end end |
#run(opts = {}) ⇒ Object
call-seq:
run( :rebuild => false, :load_files => true )
Runs the Webby builder by loading in the layout files from the layouts/
folder and the content from the contents/
folder. Content is analyzed, and those that need to be copied or compiled (filtered using ERB, Texttile, Markdown, etc.) are handled. The results are placed in the output/
folder.
If the :rebuild
flag is set to true
, then all content is copied and/or compiled to the output folder.
A content file can mark itself as dirty by setting the dirty
flag to true
in the meta-data of the file. This will cause the contenet to always be compiled when the builder is run. Conversely, setting the dirty flag to false
will cause the content to never be compiled or copied to the output folder.
A content file needs to be built if the age of the file is less then the age of the output product – i.e. the content file has been modified more recently than the output file.
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/webby/builder.rb', line 126 def run( opts = {} ) opts[:load_files] = true unless opts.has_key?(:load_files) unless test(?d, output_dir) @log.info "creating #{output_dir}" FileUtils.mkdir output_dir end load_files if opts[:load_files] Resources.pages.each do |page| next unless page.dirty? or opts[:rebuild] @log.info "creating #{page.destination}" # make sure the directory exists FileUtils.mkdir_p ::File.dirname(page.destination) # copy the resource to the output directory if it is static if page.instance_of? Resources::Static FileUtils.cp page.path, page.destination FileUtils.chmod 0644, page.destination # otherwise, layout the resource and write the results to # the output directory else Renderer.write(page) end end # touch the cairn so we know when the website was last generated FileUtils.touch ::Webby.cairn nil end |