Class: Vanilla::Renderers::Base

Inherits:
Object
  • Object
show all
Includes:
Vanilla::Routes
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

Methods included from Vanilla::Routes

#edit_link, #existing_link, #link_to, #new_link, #url_to, #url_to_raw

Constructor Details

#initialize(app) ⇒ Base

Returns a new instance of Base.



20
21
22
# File 'lib/vanilla/renderers/base.rb', line 20

def initialize(app)
  @app = app
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



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

def app
  @app
end

Class Method Details

.escape_curly_braces(str) ⇒ Object



14
15
16
# File 'lib/vanilla/renderers/base.rb', line 14

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

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

Render a snip.



10
11
12
# File 'lib/vanilla/renderers/base.rb', line 10

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

.snip_regexpObject



29
30
31
# File 'lib/vanilla/renderers/base.rb', line 29

def self.snip_regexp
  %r{(\{[^\}.]+\})}
end

Instance Method Details

#include_snips(content, enclosing_snip) ⇒ Object

Default behaviour to include a snip’s content



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/vanilla/renderers/base.rb', line 34

def include_snips(content, enclosing_snip)
  content.gsub(Vanilla::Renderers::Base.snip_regexp) do
    snip_tree = parse_snip_reference($1)
    if snip_tree
      snip_name = snip_tree.snip
      snip_attribute = snip_tree.attribute
      snip_args = snip_tree.arguments
    
      # 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 = soup[snip_name]
      if snip
        app.render(snip, snip_attribute, snip_args, enclosing_snip)
      else
        app.render_missing_snip(snip_name)
      end
    else
      "malformed snip reference: #{$1.inspect}"
    end
  end
end

#parse_snip_reference(string) ⇒ Object



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

def parse_snip_reference(string)
  @parser ||= SnipReferenceParser.new
  @parser.parse(string)
end

#prepare(snip, part, args, enclosing_snip) ⇒ Object

Subclasses should override this to perform any actions required before rendering



71
72
73
# File 'lib/vanilla/renderers/base.rb', line 71

def prepare(snip, part, args, enclosing_snip)
  # 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.



82
83
84
# File 'lib/vanilla/renderers/base.rb', line 82

def process_text(content)
  content
end

#raw_content(snip, part) ⇒ Object

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



87
88
89
# File 'lib/vanilla/renderers/base.rb', line 87

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

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

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



63
64
65
66
67
# File 'lib/vanilla/renderers/base.rb', line 63

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

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



75
76
77
# File 'lib/vanilla/renderers/base.rb', line 75

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

#soupObject

defined for the routes



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

def soup
  @app.soup
end