| 1 |
require 'cgi' |
| 2 |
|
| 3 |
require 'mustache/parser' |
| 4 |
require 'mustache/generator' |
| 5 |
|
| 6 |
class Mustache |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
class Template |
| 18 |
attr_reader :source |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
def initialize(source, options = {}) |
| 23 |
@source = source |
| 24 |
@options = options |
| 25 |
end |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
def render(context) |
| 35 |
|
| 36 |
compiled = "def render(ctx) #{compile} end" |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
instance_eval(compiled, __FILE__, __LINE__ - 1) |
| 42 |
|
| 43 |
|
| 44 |
render(context) |
| 45 |
end |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
def compile(src = @source) |
| 50 |
Generator.new(@options).compile(tokens(src)) |
| 51 |
end |
| 52 |
alias_method :to_s, :compile |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
def tokens(src = @source) |
| 59 |
Parser.new(@options).compile(src) |
| 60 |
end |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 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 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 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 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 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 |
|
| 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 |