require 'fileutils' module StaticMatic class Base # Directories generated for a new site setup @@base_dirs = %w{ site/ site/stylesheets site/images site/javascripts src/ src/pages/ src/layouts src/stylesheets } # Templates for setup and their location @@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__) + '/templates' @layout = "application" @scope = Object.new @scope.instance_variable_set("@staticmatic", self) end def base_dir @base_dir end def run(command) valid_commands = ['build', 'setup', 'preview'] if valid_commands.include?(command) send(command) else puts "#{command} is not a valid StaticMatic command" end 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| copy_file("#{@templates_dir}/#{template}", "#{@src_dir}/#{destination}") end puts "Done" end def preview puts "StaticMatic Preview Server Starting..." StaticMatic::Server.start(@base_dir) end def copy_file(from, to) FileUtils.cp(from, to) end def save_page(filename, content) generate_site_file(filename, 'html', content) end def save_stylesheet(filename, content) generate_site_file(File.join('stylesheets', filename), 'css', content) end def generate_site_file(filename, extension, content) path = File.join(@site_dir,"#{filename}.#{extension}") FileUtils.mkdir_p(File.dirname(path)) File.open(path, 'w+') do |f| f << content end puts "created #{path}" end def source_for_layout if layout_exists?(@layout) File.read(full_layout_path(@layout)) else raise StaticMatic::Error.new("", full_layout_path(@layout), "Layout not found") end end # Generate html from source file: # generate_html("index.haml") def generate_html(source_file, source_dir = '') full_file_path = File.join(@src_dir, 'pages', source_dir, "#{source_file}.haml") begin html = generate_html_from_template_source(File.read(full_file_path)) @layout = detirmine_layout(source_dir) rescue StaticMatic::Error => staticmatic_error # Catch any errors from the actual template - otherwise the error will be assumed to be from the # layout raise staticmatic_error rescue Haml::Error => haml_error raise StaticMatic::Error.new(haml_error.haml_line, "#{source_dir}/#{source_file}", haml_error.message) end # # # TODO: DRY this up # if @scope.instance_variable_get("@layout") # @layout = @scope.instance_variable_get("@layout") # end # html end def generate_html_with_layout(source, source_dir = '') template_content = generate_html(source, source_dir) @layout = detirmine_layout(source_dir) begin generate_html_from_template_source(source_for_layout) { template_content } rescue StaticMatic::Error => staticmatic_error # Catch any errors from the actual template - otherwise the error will be assumed to be from the # layout raise staticmatic_error rescue Haml::Error => haml_error raise StaticMatic::Error.new(haml_error.haml_line, "Layout: #{source_dir}/#{@layout}", haml_error.message) end end def generate_partial(name) partial_path = File.join(@src_dir, 'partials', "#{name}.haml") if File.exists?(partial_path) generate_html_from_template_source(File.read(partial_path)) end end def generate_css(source, source_dir = '') full_file_path = File.join(@src_dir, 'stylesheets', source_dir, "#{source}.sass") begin stylesheet = Sass::Engine.new(File.read(full_file_path)) stylesheet.to_css rescue Sass::SyntaxError => sass_error raise StaticMatic::Error.new(sass_error.sass_line, full_file_path, sass_error.message) end end # Generates html from the passed source string # # generate_html_from_template_source("%h1 Welcome to My Site") -> "