Class: Webby::Builder

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeBuilder

call-seq:

Builder.new

Creates a new Builder object for creating pages from the content and layout directories.



56
57
58
# File 'lib/webby/builder.rb', line 56

def initialize
  @log = Logging::Logger[self]
end

Class Method Details

.create(page, opts = {}) ⇒ Object

call-seq:

Builder.create( page, :from => template )

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.

Raises:



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/webby/builder.rb', line 36

def create( page, opts = {} )
  tmpl = opts[:from]
  raise Error, "template not given" unless tmpl
  raise Error, "#{page} already exists" if test ?e, page

  Logging::Logger[self].info "creating #{page}"
  FileUtils.mkdir_p ::File.dirname(page)
  str = ERB.new(::File.read(tmpl), nil, '-').result
  ::File.open(page, 'w') {|fd| fd.write str}

  return nil
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.



24
25
26
# File 'lib/webby/builder.rb', line 24

def run( opts = {} )
  self.new.run opts
end

Instance Method Details

#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.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/webby/builder.rb', line 82

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]
  loop_check

  Resource.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.is_static?
      FileUtils.cp page.path, 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