Class: Vanilla::Renderers::Base

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

Direct Known Subclasses

Dynasnip, Bold, Erb, Haml, 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



38
39
40
# File 'lib/vanilla/renderers/base.rb', line 38

def self.snip_regexp
  %r|(\{[\w\-_\d\.\"\' ]+( +[^\}]+)?\})|
end

Instance Method Details

#default_layout_snipObject



42
43
44
# File 'lib/vanilla/renderers/base.rb', line 42

def default_layout_snip
  app.default_layout_snip
end

#include_snips(content, enclosing_snip) ⇒ Object

Default behaviour to include a snip’s content



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/vanilla/renderers/base.rb', line 51

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.
      if snip = soup[snip_name]
        app.render(snip, snip_attribute, snip_args, enclosing_snip)
      else
        render_missing_snip(snip_name)
      end
    else
      %|<span class="malformed_snip_inclusion">(malformed snip inclusion: #{self.class.escape_curly_braces($1)})</span>|
    end
  end
end

#layout_for(snip) ⇒ Object



46
47
48
# File 'lib/vanilla/renderers/base.rb', line 46

def layout_for(snip)
  layout_snip = (snip && snip.layout) ? soup[snip.layout] : default_layout_snip
end


30
31
32
33
34
35
36
# File 'lib/vanilla/renderers/base.rb', line 30

def link_to(link_text, snip_name=link_text, part=nil)
  if soup[snip_name]
    %{<a href="#{url_to(snip_name, part)}">#{link_text}</a>}
  else
    %{<a class="missing" href="#{url_to(snip_name, part)}">#{link_text}</a>}
  end
end

#parse_snip_reference(string) ⇒ Object



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

def parse_snip_reference(string)
  @parser ||= Vanilla::SnipReferenceParser.new
  @parser.parse(string)
rescue Vanilla::SnipReferenceParser::ParseError
  nil
end

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

Subclasses should override this to perform any actions required before rendering



93
94
95
# File 'lib/vanilla/renderers/base.rb', line 93

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.



109
110
111
# File 'lib/vanilla/renderers/base.rb', line 109

def process_text(content)
  content
end

#raw_content(snip, part) ⇒ Object

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



114
115
116
# File 'lib/vanilla/renderers/base.rb', line 114

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.



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

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_missing_snip(snip_name) ⇒ Object



80
81
82
# File 'lib/vanilla/renderers/base.rb', line 80

def render_missing_snip(snip_name)
  "[snip '#{snip_name}' cannot be found]"
end

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



97
98
99
100
101
102
103
104
# File 'lib/vanilla/renderers/base.rb', line 97

def render_without_including_snips(snip, part=:content)
  if content = raw_content(snip, part)
    process_text(content)
  else
    app.response.status = 404
    %{Couldn't find part "#{part}" for snip "#{snip.name}"}
  end
end

#soupObject



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

def soup
  @app.soup
end

#url_to(*args) ⇒ Object



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

def url_to(*args)
  @app.url_to(*args)
end