Class: Usmu::SiteGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/usmu/site_generator.rb

Overview

This is the class that brings everything together to generate a new website.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ SiteGenerator

Returns a new instance of SiteGenerator.

Parameters:



10
11
12
13
# File 'lib/usmu/site_generator.rb', line 10

def initialize(configuration)
  @log = Logging.logger[self]
  @configuration = configuration
end

Instance Attribute Details

#filesArray<Usmu::StaticFile> (readonly)

Returns a list of static files from the source folder.

Returns:



49
50
51
# File 'lib/usmu/site_generator.rb', line 49

def files
  renderables.select {|r| r.class.name == 'Usmu::StaticFile'}
end

#layoutsArray<Usmu::Layout> (readonly)

Returns a list of layouts available in this website.

Returns:

  • (Array<Usmu::Layout>)

    a list of layouts available in this website.



17
18
19
# File 'lib/usmu/site_generator.rb', line 17

def layouts
  @configuration.layouts_files.map {|l| Usmu::Layout.new(@configuration, l) }
end

#pagesArray<Usmu::Page> (readonly)

Returns a list of pages from the source folder. This is any file in the source folder which is not static.

Returns:

  • (Array<Usmu::Page>)

    a list of pages from the source folder. This is any file in the source folder which is not static.



43
44
45
# File 'lib/usmu/site_generator.rb', line 43

def pages
  renderables.select {|r| r.class.name != 'Usmu::StaticFile'}
end

#renderablesArray<Usmu::StaticFile> (readonly)

Returns a list of renderable files from the source folder.

The only guarantee made for individual files is that they will conform to the interface defined by Usmu::StaticFile and thus be renderable, however most files will be one of the subclasses of that class.

Returns:

  • (Array<Usmu::StaticFile>)

    a list of renderable files from the source folder. will be a subclass of this class.

See Also:



30
31
32
33
34
35
36
37
38
# File 'lib/usmu/site_generator.rb', line 30

def renderables
  @configuration.source_files.map do |filename|
    if Usmu::Layout.is_valid_file? 'source', filename
      Usmu::Page.new(@configuration, filename)
    else
      Usmu::StaticFile.new(@configuration, filename)
    end
  end
end

Instance Method Details

#generate

This method returns an undefined value.

Generate the website according to the configuration given.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/usmu/site_generator.rb', line 56

def generate
  @log.info("Source: #{@configuration.source_path}")
  @log.info("Destination: #{@configuration.destination_path}")

  renderables.each do |page|
    @log.success("creating #{page.output_filename}...")
    file = File.join(@configuration.destination_path, page.output_filename)
    directory = File.dirname(file)

    unless File.directory?(directory)
      FileUtils.mkdir_p(directory)
    end

    File.write file, page.render
    FileUtils.touch file, :mtime => File.stat(page.input_path).mtime
  end
  nil
end