class Amber::Render::Template
Constants
- ERB_PLACEHOLDER_RE
- ERB_TAG_RE
- PROPERTY_HEADER
- RENDER_MAP
- TEXTILE_TOC_RE
Attributes
content[R]
file[R]
partial[R]
type[R]
Public Class Methods
new(options={})
click to toggle source
# File lib/amber/render/template.rb, line 33 def initialize(options={}) if options[:file] @file = options[:file] @type = options[:type] || type_from_file(@file) elsif options[:content] @content = options[:content] @type = options[:type] # e.g. :haml. required if @content end @partial = options[:partial] end
Public Instance Methods
render(view, options={})
click to toggle source
returns rendered content or title, depending on render_mode. anchors are always automatically added to content headings.
# File lib/amber/render/template.rb, line 48 def render(view, options={}) view.locals[:_type] = @type render_mode = options.delete(:mode) || :content toc_option = options.delete(:toc) if render_mode == :title render_title(view) else html = render_html(view) toc_renderer = RegexTableOfContents.new(html, options) if render_mode == :toc toc_renderer.to_toc elsif toc_option === false toc_renderer.to_html elsif toc_option || render_mode == :toc_and_content %(<div id="TOC">%s</div>\n\n%s) % [toc_renderer.to_toc, toc_renderer.to_html] else toc_renderer.to_html end end end
Private Instance Methods
add_bracket_links(view, content)
click to toggle source
# File lib/amber/render/template.rb, line 160 def add_bracket_links(view, content) content = Filter::Bracketlink.run(content) do |from, to| view.link({from => to}) end return content end
add_variables(view, content)
click to toggle source
# File lib/amber/render/template.rb, line 167 def add_variables(view, content) locale = view.locals[:locale] content = Filter::Variables.run(content) do |var_name| view.page.var(var_name, locale) || var_name end return content end
render_erb(string, view)
click to toggle source
# File lib/amber/render/template.rb, line 86 def render_erb(string, view) template = Tilt::ERBTemplate.new {string} template.render(view) end
render_haml(file_path, view)
click to toggle source
# File lib/amber/render/template.rb, line 126 def render_haml(file_path, view) template = Tilt::HamlTemplate.new(file_path, {:format => :html5, :default_encoding => 'UTF-8'}) html = template.render(view, view.locals) html = add_variables(view, html) html = add_bracket_links(view, html) return html rescue Haml::Error => ex msg = "Line #{ex.line + 1} of file `#{file_path}`: #{ex.to_s}" Amber::logger.error(msg) return msg end
render_html(view)
click to toggle source
# File lib/amber/render/template.rb, line 71 def render_html(view) if @type == :haml return render_haml(@file, view) else @content ||= File.read(@file, :encoding => 'UTF-8').sub(PROPERTY_HEADER, '') # remove property header if method = RENDER_MAP[@type] content, erb_tags = replace_erb_tags(@content) html = self.send(method, view, content) return render_erb(restore_erb_tags(html, erb_tags), view) else return "sorry, i don't understand how to render `#{@type}`" end end end
render_markdown(view, content)
click to toggle source
# File lib/amber/render/template.rb, line 144 def render_markdown(view, content) content = add_variables(view, content) content = add_bracket_links(view, content) return RDiscount.new(content, :smart, :autolink).to_html end
render_none(view, content)
click to toggle source
# File lib/amber/render/template.rb, line 156 def render_none(view, content) return content end
render_raw(view, content)
click to toggle source
# File lib/amber/render/template.rb, line 150 def render_raw(view, content) content = add_variables(view, content) content = add_bracket_links(view, content) return content end
render_textile(view, content)
click to toggle source
# File lib/amber/render/template.rb, line 138 def render_textile(view, content) content = add_variables(view, content) content = add_bracket_links(view, content) return Filter::Autolink.run(RedCloth.new(content).to_html) end
render_title(view)
click to toggle source
# File lib/amber/render/template.rb, line 117 def render_title(view) locale = view.locals[:locale] if title = view.page.explicit_title(locale) "<h1>#{title}</h1>\n" else "" end end
type_from_file(file_path)
click to toggle source
# File lib/amber/render/template.rb, line 175 def type_from_file(file_path) suffix = File.extname(file_path) if suffix suffix.sub!(/^\./, '') suffix = suffix.to_sym end suffix end