Module: StaticMatic::RenderMixin
- Included in:
- Base
- Defined in:
- lib/staticmatic/mixins/render.rb
Instance Method Summary collapse
- #determine_layout(dir = '') ⇒ Object
- #generate_css(source, source_dir = '') ⇒ Object
-
#generate_html(source_file, source_dir = '') ⇒ Object
Generate html from source file: generate_html(“index”).
-
#generate_html_from_template_source(source, options = {}) ⇒ Object
Generates html from the passed source string.
- #generate_html_with_layout(source, source_dir = '') ⇒ Object
- #generate_partial(name, options = {}) ⇒ Object
- #source_for_layout ⇒ Object
-
#source_template_from_path(path) ⇒ Object
Returns a raw template name from a source file path: source_template_from_path(“/path/to/site/src/stylesheets/application.sass”) -> “application”.
Instance Method Details
#determine_layout(dir = '') ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/staticmatic/mixins/render.rb', line 101 def determine_layout(dir = '') layout_name = "application" if @scope.instance_variable_get("@layout") layout_name = @scope.instance_variable_get("@layout") elsif dir dirs = dir.split("/") dir_layout_name = dirs[1] if layout_exists?(dir_layout_name) layout_name = dir_layout_name end end layout_name end |
#generate_css(source, source_dir = '') ⇒ Object
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/staticmatic/mixins/render.rb', line 76 def generate_css(source, source_dir = '') full_file_path = File.join(@src_dir, 'stylesheets', source_dir, "#{source}.sass") begin = { :load_paths => [ File.join(@src_dir, 'stylesheets') ] }.merge(self.configuration.) stylesheet = Sass::Engine.new(File.read(full_file_path), ) stylesheet.to_css rescue Exception => e render_rescue_from_error(StaticMatic::TemplateError.new(full_file_path, e)) end end |
#generate_html(source_file, source_dir = '') ⇒ Object
Generate html from source file: generate_html(“index”)
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/staticmatic/mixins/render.rb', line 13 def generate_html(source_file, source_dir = '') full_file_path = File.join(@src_dir, 'pages', source_dir, "#{source_file}.haml") begin # clear all scope variables except @staticmatic @scope.instance_variables.each do |var| @scope.instance_variable_set(var, nil) unless var == '@staticmatic' end html = generate_html_from_template_source(File.read(full_file_path)) @layout = determine_layout(source_dir) rescue StaticMatic::TemplateError => e raise e # re-raise inline errors rescue Exception => e raise StaticMatic::TemplateError.new(full_file_path, e) end html end |
#generate_html_from_template_source(source, options = {}) ⇒ Object
Generates html from the passed source string
generate_html_from_template_source(“%h1 Welcome to My Site”) -> “<h1>Welcome to My Site</h1>”
Pass a block containing a string to yield within in the passed source:
generate_html_from_template_source(“content:n= yield”) { “blah” } -> “content: blah”
95 96 97 98 99 |
# File 'lib/staticmatic/mixins/render.rb', line 95 def generate_html_from_template_source(source, = {}) html = Haml::Engine.new(source, self.configuration..merge()) locals = [:locals] || {} html.render(@scope, locals) { yield } end |
#generate_html_with_layout(source, source_dir = '') ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/staticmatic/mixins/render.rb', line 33 def generate_html_with_layout(source, source_dir = '') @current_page = File.join(source_dir, "#{source}.html") @current_file_stack.unshift(File.join(source_dir, "#{source}.haml")) begin template_content = generate_html(source, source_dir) @layout = determine_layout(source_dir) generate_html_from_template_source(source_for_layout) { template_content } rescue Exception => e render_rescue_from_error(e) ensure @current_page = nil @current_file_stack.shift end end |
#generate_partial(name, options = {}) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/staticmatic/mixins/render.rb', line 48 def generate_partial(name, = {}) partial_dir, partial_name = File.dirname(self.current_file), name # default relative to current file partial_dir, partial_name = File.split(name) if name.index('/') # contains a path so it's absolute from src/pages dir partial_name = "_#{partial_name}.haml" partial_path = File.join(@src_dir, 'pages', partial_dir, partial_name) unless File.exists?(partial_path) # couldn't find it in the pages subdirectory tree so try old way (ignoring the path) partial_dir = 'partials' partial_name = "#{File.basename(name)}.haml" partial_path = File.join(@src_dir, partial_dir, partial_name) end if File.exists?(partial_path) partial_rel_path = "/#{partial_dir}/#{partial_name}".gsub(/\/+/, '/') @current_file_stack.unshift(partial_rel_path) begin generate_html_from_template_source(File.read(partial_path), ) rescue Exception => e raise StaticMatic::TemplateError.new(partial_path, e) ensure @current_file_stack.shift end else raise StaticMatic::Error.new("", name, "Partial not found") end end |
#source_for_layout ⇒ Object
3 4 5 6 7 8 9 |
# File 'lib/staticmatic/mixins/render.rb', line 3 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 |
#source_template_from_path(path) ⇒ Object
Returns a raw template name from a source file path: source_template_from_path(“/path/to/site/src/stylesheets/application.sass”) -> “application”
120 121 122 123 124 |
# File 'lib/staticmatic/mixins/render.rb', line 120 def source_template_from_path(path) file_dir, file_name = File.split(path) file_name.chomp!(File.extname(file_name)) [ file_dir, file_name ] end |