class ::StaticCms::Page require 'fileutils' require 'haml' require 'ruby_patch' extend ::RubyPatch::AutoLoad PARAMS_DEFAULT ={ 'visible' => true, 'title' => '', 'template' => 'default', 'articles' => [], 'statics' => [], 'sources' => [], 'base' => nil, } attr_accessor :site, :name, :dir, :template_file attr_accessor *(PARAMS_DEFAULT.keys) def initialize(site, name, params = {}) @site = site @name = name @dir = File.join("root", @name) params = PARAMS_DEFAULT.merge(params) @template = params.delete('template') @template_file = File.join('templates', @template) + '.html.haml' @articles = params.delete('articles').map{|name| ::StaticCms::Article.use(self, name)} @statics = params.delete('statics').map{|file| File.join('statics', file)} @sources = params.delete('sources').map{|file| File.join('sources', file)} @base = params.delete('base') || @site.www params.each{|k, v| instance_variable_set("@#{k}", v) } end def generate FileUtils.mkdir_p(@dir) if @visible @statics.each{|file| cp_if_new(file)} @sources.each{|file| compile(file)} template_file = @template_file else @statics.each{|file| FileUtils.rm_rf(static_target(file))} @sources.each{|file| FileUtils.rm_rf(compile_target(file))} template_file = 'templates/moved.html.haml' end html = ::Haml::Engine.new(File.read(template_file)).render(self) target = File.join(@dir, 'index.html') open(target, 'w'){|io| io.write(html) io.flush io.fsync } end private def haml(text) ::Haml::Engine.new(text).render(self) end def nth_page_from_self(n) index = (@site.page_alist.to_a.index{|name, page| name == @name} + n)%@site.page_alist.size @site.page_alist.to_a[index][1] end def link_to_article(name) article = ::StaticCms::Article.alist[name] page = article.pages.first url = File.join(@site.www, page.name, "index.html\##{name}") "#{article.title}" end def cp_if_new(source) target = static_target(source) if !File.file?(target) || File.mtime(source) > File.mtime(target) FileUtils.cp(source, target) FileUtils.chmod(0644, target) end end def compile(source) ext = File.extname(source) target = compile_target(source) case ext when /\A\.sass\z/ system "sass #{source} > #{target}" else raise NotImplementedError, file end end def static_target(source) File.join(@dir, File.basename(source)) end def compile_target(source) ext = File.extname(source) File.join(@dir, File.basename(source, ext)) end end