require 'rubygems' require 'haml' require File.dirname(__FILE__) + '/staticmatic/helpers' class StaticMatic VERSION = '0.0.1' cattr_reader :base_dirs @@base_dirs = %w{ site/ site/stylesheets site/images site/javascripts src/ src/pages/ src/layouts src/stylesheets } @@templates = { 'application.haml' => 'layouts', 'application.sass' => 'stylesheets', 'index.haml' => 'pages' } def initialize(base_dir) @base_dir = base_dir @src_dir = "#{@base_dir}/src" @site_dir = "#{@base_dir}/site" @templates_dir = File.dirname(__FILE__) + '/staticmatic/templates' end def build build_css build_html end def setup if !File.exists?(@base_dir) Dir.mkdir(@base_dir) end @@base_dirs.each do |directory| directory = "#{@base_dir}/#{directory}" if !File.exists?(directory) Dir.mkdir(directory) puts "created #{directory}" end end @@templates.each do |template, destination| `cp #{@templates_dir}/#{template} #{@src_dir}/#{destination}` end puts "Done" end private def copy_file(from, to) end def save_page(filename, content) filename = "#{filename}" generate_site_file(filename, 'html', content) end def save_stylesheet(filename, content) filename = "stylesheets/#{filename}" generate_site_file(filename, 'css', content) end def generate_site_file(filename, extension, content) site_file = File.new("#{@site_dir}/#{filename}.#{extension}", 'w+') site_file << content site_file.close puts "created #{@site_dir}/#{filename}.#{extension}" end def build_html layout_content = File.read("#{@src_dir}/layouts/application.haml") Dir.entries("#{@src_dir}/pages").each do |file| if matches = file.match(/([a-z0-9_-]+)\.haml$/) filename_without_extension = matches[1] page = Haml::Engine.new(File.read("#{@src_dir}/pages/#{file}")) page_content = page.render content_with_layout = Haml::Engine.new(layout_content, :locals => {:base_dir => @base_dir}).render { page_content } save_page(filename_without_extension, content_with_layout) end end end def build_css Dir.entries("#{@src_dir}/stylesheets").each do |file| if matches = file.match(/([a-z0-9_-]+)\.sass$/) filename_without_extension = matches[1] stylesheet = Sass::Engine.new(File.read("#{@src_dir}/stylesheets/#{file}")) save_stylesheet(filename_without_extension, stylesheet.render) end end end end