Class: Vanilla::Renderers::Base
- Inherits:
-
Object
- Object
- Vanilla::Renderers::Base
show all
- Defined in:
- lib/vanilla/renderers/base.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#default_layout_snip ⇒ Object
-
#include_snips(content, enclosing_snip) ⇒ Object
Default behaviour to include a snip’s content.
-
#initialize(app) ⇒ Base
constructor
-
#layout_for(snip) ⇒ Object
-
#link_to(link_text, snip_name = link_text, part = nil) ⇒ Object
-
#parse_snip_reference(string) ⇒ Object
-
#prepare(snip, part, args, enclosing_snip) ⇒ Object
Subclasses should override this to perform any actions required before rendering.
-
#process_text(content) ⇒ Object
Handles processing the text of the content.
-
#raw_content(snip, part) ⇒ Object
Returns the raw content for the selected part of the selected snip.
-
#render(snip, part = :content, args = [], enclosing_snip = snip) ⇒ Object
Default rendering behaviour.
-
#render_missing_snip(snip_name) ⇒ Object
-
#render_without_including_snips(snip, part = :content) ⇒ Object
-
#soup ⇒ Object
-
#url_to(*args) ⇒ Object
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
#app ⇒ Object
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
8
9
10
|
# File 'lib/vanilla/renderers/base.rb', line 8
def self.render(snip, part=:content)
new(app).render(snip, part)
end
|
.snip_regexp ⇒ Object
38
39
40
|
# File 'lib/vanilla/renderers/base.rb', line 38
def self.snip_regexp
%r|(\{[\w\-_\d\.\"\' ]+( +[^\}]+)?\})|
end
|
Instance Method Details
#default_layout_snip ⇒ Object
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
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
|
#link_to(link_text, snip_name = link_text, part = nil) ⇒ Object
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
#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)
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
|
#soup ⇒ Object
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
|