Debian Codegraph @

lib/mustache/template.rb

raw symbols SCIP
1 require 'cgi'
2
3 require 'mustache/parser'
4 require 'mustache/generator'
5
6 class Mustache
7 # A Template represents a Mustache template. It compiles and caches
8 # a raw string template into something usable.
9 #
10 # The idea is this: when handed a Mustache template, convert it into
11 # a Ruby string by transforming Mustache tags into interpolated
12 # Ruby.
13 #
14 # You shouldn't use this class directly, instead:
15 #
16 # >> Mustache.render(template, hash)
17 class Template
18 attr_reader :source
19
20 # Expects a Mustache template as a string along with a template
21 # path, which it uses to find partials. Options may be passed.
22 def initialize(source, options = {})
23 @source = source
24 @options = options
25 end
26
27 # Renders the `@source` Mustache template using the given
28 # `context`, which should be a simple hash keyed with symbols.
29 #
30 # The first time a template is rendered, this method is overriden
31 # and from then on it is "compiled". Subsequent calls will skip
32 # the compilation step and run the Ruby version of the template
33 # directly.
34 def render(context)
35 # Compile our Mustache template into a Ruby string
36 compiled = "def render(ctx) #{compile} end"
37
38 # Here we rewrite ourself with the interpolated Ruby version of
39 # our Mustache template so subsequent calls are very fast and
40 # can skip the compilation stage.
41 instance_eval(compiled, __FILE__, __LINE__ - 1)
42
43 # Call the newly rewritten version of #render
44 render(context)
45 end
46
47 # Does the dirty work of transforming a Mustache template into an
48 # interpolation-friendly Ruby string.
49 def compile(src = @source)
50 Generator.new(@options).compile(tokens(src))
51 end
52 alias_method :to_s, :compile
53
54 # Returns an array of tokens for a given template.
55 #
56 # @return [Array] Array of tokens.
57 #
58 def tokens(src = @source)
59 Parser.new(@options).compile(src)
60 end
61
62 # Returns an array of tags.
63 #
64 # Tags that belong to sections will be of the form `section1.tag`.
65 #
66 # @return [Array] Returns an array of tags.
67 #
68 def tags
69 Template.recursor(tokens, []) do |token, section|
70 if [:etag, :utag].include?(token[1])
71 [ new_token=nil, new_section=nil, result=((section + [token[2][2][0]]).join('.')), stop=true ]
72 elsif [:section, :inverted_section].include?(token[1])
73 [ new_token=token[4], new_section=(section + [token[2][2][0]]), result=nil, stop=false ]
74 else
75 [ new_token=token, new_section=section, result=nil, stop=false ]
76 end
77 end.flatten.reject(&:nil?).uniq
78 end
79
80 # Returns an array of sections.
81 #
82 # Sections that belong to other sections will be of the form `section1.childsection`
83 #
84 # @return [Array] Returns an array of section.
85 #
86 def sections
87 Template.recursor(tokens, []) do |token, section|
88 if [:section, :inverted_section].include?(token[1])
89 new_section=(section + [token[2][2][0]])
90 [ new_token=token[4], new_section, result=new_section.join('.'), stop=false ]
91 else
92 [ new_token=token, new_section=section, result=nil, stop=false ]
93 end
94 end.flatten.reject(&:nil?).uniq
95 end
96
97 # Returns an array of partials.
98 #
99 # Partials that belong to sections are included, but the section name is not preserved
100 #
101 # @return [Array] Returns an array of partials.
102 #
103 def partials
104 Template.recursor(tokens, []) do |token, section|
105 if token[1] == :partial
106 [ new_token=token, new_section=section, result=token[2], stop=true ]
107 else
108 [ new_token=token, new_section=section, result=nil, stop=false ]
109 end
110 end.flatten.reject(&:nil?).uniq
111 end
112
113
114 # Simple recursive iterator for tokens
115 def self.recursor(toks, section, &block)
116 toks.map do |token|
117 next unless token.is_a? Array
118
119 if token.first == :mustache
120 new_token, new_section, result, stop = yield(token, section)
121 [ result ] + ( stop ? [] : recursor(new_token, new_section, &block))
122 else
123 recursor(token, section, &block)
124 end
125 end
126 end
127 end
128 end