Class: Vanilla::Renderers::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/vanilla/renderers/base.rb

Direct Known Subclasses

Dynasnip, Bold, Erb, Markdown, Raw, Ruby, Textile

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Base

Returns a new instance of Base.



18
19
20
# File 'lib/vanilla/renderers/base.rb', line 18

def initialize(app)
  @app = app
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



16
17
18
# File 'lib/vanilla/renderers/base.rb', line 16

def app
  @app
end

Class Method Details

.escape_curly_braces(str) ⇒ Object



12
13
14
# File 'lib/vanilla/renderers/base.rb', line 12

def self.escape_curly_braces(str)
  str.gsub("{", "{").gsub("}", "}")
end

.render(snip, part = :content) ⇒ Object

Render a snip.



8
9
10
# File 'lib/vanilla/renderers/base.rb', line 8

def self.render(snip, part=:content)
  new(app).render(snip, part)
end

.snip_regexpObject



22
23
24
25
26
27
# File 'lib/vanilla/renderers/base.rb', line 22

def self.snip_regexp
  %r{ \{
    ([\w\-]+) (?: \.([\w\-]+) )?
    (?: \s+ ([\w\-,]+) )?
  \} }x
end

Instance Method Details

#include_snips(content) ⇒ Object

Default behaviour to include a snip’s content



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/vanilla/renderers/base.rb', line 30

def include_snips(content)
  content.gsub(Vanilla::Renderers::Base.snip_regexp) do
    snip_name = $1
    snip_attribute = $2
    snip_args = $3 ? $3.split(',') : []
    
    # Render the snip or snip part with the given args, and the current
    # context, but with the default renderer for that snip. We dispatch
    # *back* out to the root Vanilla.render method to do this.
    snip = Vanilla.snip(snip_name)
    if snip
      app.render(snip, snip_attribute, snip_args)
    else
      app.render_missing_snip(snip_name)
    end
  end
end

#prepare(snip, part, args) ⇒ Object

Subclasses should override this to perform any actions required before rendering



57
58
59
# File 'lib/vanilla/renderers/base.rb', line 57

def prepare(snip, part, args)
  # do nothing, by default
end

#process_text(content) ⇒ Object

Handles processing the text of the content. Subclasses should override this method to do fancy text processing like markdown, or loading the content as Ruby code.



68
69
70
# File 'lib/vanilla/renderers/base.rb', line 68

def process_text(content)
  content
end

#raw_content(snip, part) ⇒ Object

Returns the raw content for the selected part of the selected snip



73
74
75
# File 'lib/vanilla/renderers/base.rb', line 73

def raw_content(snip, part)
  snip.__send__((part || :content).to_sym)
end

#render(snip, part = :content, args = []) ⇒ Object

Default rendering behaviour. Subclasses shouldn’t really need to touch this.



49
50
51
52
53
# File 'lib/vanilla/renderers/base.rb', line 49

def render(snip, part=:content, args=[])
  prepare(snip, part, args)
  processed_text = render_without_including_snips(snip, part)
  include_snips(processed_text)
end

#render_without_including_snips(snip, part = :content) ⇒ Object



61
62
63
# File 'lib/vanilla/renderers/base.rb', line 61

def render_without_including_snips(snip, part=:content)
  process_text(raw_content(snip, part))
end