Class: Amber::Render::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/amber/render/template.rb

Constant Summary collapse

PROPERTY_HEADER =
/^\s*(^(|- )@\w[^\n]*?\n)*/m
RENDER_MAP =
{
  :text => 'render_textile',
  :textile => 'render_textile',
  :md => 'render_markdown',
  :markdown => 'render_markdown',
  :html => 'render_raw',
  :raw  => 'render_raw',
  :none => 'render_none',
  :erb => 'render_none'
}
TEXTILE_TOC_RE =
/^\s*h([1-6])\.\s+(.*)/
ERB_TAG_RE =
/<%=.*?%>/
ERB_PLACEHOLDER_RE =
/xx erb tag\d+ xx/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Template

Returns a new instance of Template.



33
34
35
36
37
38
39
40
41
42
# 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

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



30
31
32
# File 'lib/amber/render/template.rb', line 30

def content
  @content
end

#fileObject (readonly)

Returns the value of attribute file.



28
29
30
# File 'lib/amber/render/template.rb', line 28

def file
  @file
end

#partialObject (readonly)

Returns the value of attribute partial.



31
32
33
# File 'lib/amber/render/template.rb', line 31

def partial
  @partial
end

#typeObject (readonly)

Returns the value of attribute type.



29
30
31
# File 'lib/amber/render/template.rb', line 29

def type
  @type
end

Instance Method Details

#render(view, options = {}) ⇒ Object

returns rendered content or title, depending on render_mode. anchors are always automatically added to content headings.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 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