lib/brite/controller.rb in brite-0.6.0 vs lib/brite/controller.rb in brite-0.7.0
- old
+ new
@@ -1,160 +1,102 @@
require 'neapolitan'
require 'brite/config'
require 'brite/layout'
-require 'brite/models/site'
-require 'brite/models/page'
-require 'brite/models/post'
+require 'brite/site'
+require 'brite/page'
+require 'brite/post'
+require 'brite/part'
module Brite
# The Controller class is the primary Brite class, handling
# the generation of site files.
+ #
class Controller
# New Controller.
+ #
+ # @param [Hash] options
+ # Controller options.
+ #
+ # @option options [String] :location
+ # Location of brite project.
+ #
+ # @option options [String] :output
+ # Redirect all output to this directory.
+ #
def initialize(options={})
@location = options[:location] || Dir.pwd
@output = options[:output]
- @url = options[:url]
- @dryrun = options[:dryrun]
- @trace = options[:trace]
+ #@dryrun = options[:dryrun]
+ #@trace = options[:trace]
- @layouts = []
-
- initialize_site
+ @site = Site.new(location, :url=>options[:url])
end
# Returns an instance of Site.
- def initialize_site
- @site = Site.new(:url=>url)
-
- Dir.chdir(location) do
- files = Dir['**/*']
- files.each do |file|
- name = File.basename(file)
- ext = File.extname(file)
- case ext
- when '.layout'
- layouts << Layout.new(self, file)
- when '.page' #*%w{.markdown .rdoc .textile .whtml}
- @site.pages << initialize_page(file)
- when '.post'
- @site.posts << initialize_post(file)
- end
- end
- end
- @site.posts.sort!{ |a,b| b.date <=> a.date }
- @site
- end
-
- # Returns an instance of Page.
- #++
- # TODO: Limit Neapolitan to Markup formats only.
- #--
- def initialize_page(file)
- template = Neapolitan.file(file, :stencil=>config.stencil)
- settings = template.header
-
- settings[:site] = site
- settings[:file] = file
-
- Page.new(settings){ |page| render(template, page) }
- end
-
- # Returns an instance of Post.
- def initialize_post(file)
- template = Neapolitan.file(file, :stencil=>config.stencil)
- settings = template.header
-
- settings[:site] = site
- settings[:file] = file
-
- Post.new(settings){ |post| render(template, post) }
- end
-
- # Returns an instance of Site.
attr :site
- #
+ # Directory of site files on disk.
attr :location
- #
+ # Where to save results. Usually the templates are shadowed, which means
+ # the ouput is the same a location.
attr :output
- # Is `dryrun` mode on?
- def dryrun?
- @dryrun
- end
+ ## Returns an Array of Layouts.
+ #def layouts
+ # @site.layouts
+ #end
- # Is `trace` mode on?
- def trace?
- @trace
- end
-
- # Returns an Array of Layouts.
- def layouts
- @layouts
- end
-
# Access to configuration file data.
def config
- @config ||= Config.new(location)
+ site.config
end
# URL of site as set in initializer or configuration file.
def url
- @url ||= config.url
+ site.url
end
- #
- def render(template, model) #scope=nil, &body)
- #if scope
- # scope.merge!(attributes)
- #else
- # scope = to_scope
- #end
-
- render = template.render(model) #, &body)
-
- model.summary = render.summary # TODO: make part of neapolitan?
-
- result = render.to_s
-
- if model.layout
- layout = lookup_layout(model.layout)
- raise "No such layout -- #{layout}" unless layout
- result = layout.render(model){ result }
- end
-
- result.to_s.strip
+ # Is `dryrun` mode on? Checks the global variable `$DRYRUN`.
+ def dryrun?
+ $DRYRUN #@dryrun
end
- # Lookup layout by name.
- def lookup_layout(name)
- layouts.find{ |layout| name == layout.name }
+ # Is `trace` mode on? Checks global variable `$TRACE`.
+ def trace?
+ $TRACE #@trace
end
- # Build site.
+ # Build the site.
def build
if trace?
- puts "Layouts: " + layouts.map{ |layout| layout.name }.join(", ")
- puts "Pages: " + pages.map{ |page| page.file }.join(", ")
- puts "Posts: " + posts.map{ |post| post.file }.join(", ")
+ puts "Layouts: " + site.layouts.map{ |layout| layout.name }.join(", ")
+ puts "Parts: " + site.parts.map{ |part| part.name }.join(", ")
+ puts "Pages: " + site.pages.map{ |page| page.file }.join(", ")
+ puts "Posts: " + site.posts.map{ |post| post.file }.join(", ")
puts
end
+
Dir.chdir(location) do
site.posts.each do |post|
+ puts "Rendering #{post.file}" if $DEBUG
save(post)
end
site.pages.each do |page|
+ puts "Rendering #{page.file}" if $DEBUG
save(page)
end
end
- puts "#{site.pages.size + site.posts.size} Files: #{site.pages.size} Pages, #{site.posts.size} Posts"
+ puts "\n#{site.pages.size + site.posts.size} Files: #{site.pages.size} Pages, #{site.posts.size} Posts"
end
# Save page/post redering to disk.
+ #
+ # @param [Model] model
+ # The {Page} or {Post} to save to disk.
+ #
def save(model)
file = output ? File.join(output, model.output) : model.output
text = model.to_s
if File.exist?(file)