Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: mustache/mustache
base: master
...
head fork: corpgovrisk/mustache
compare: master
Checking mergeability… Don't worry, you can still create the pull request.
  • 3 commits
  • 6 files changed
  • 0 commit comments
  • 1 contributor
View
6 lib/mustache.rb
@@ -106,6 +106,12 @@ class << self
#
# Returns a rendered String version of a template
def render(data = template, ctx = {})
+ puts "Context is #{ctx.class.name}"
+ if ctx.is_a?(Hash) && ctx[:helper_context].present?
+ context.helper = ctx.delete :helper_context
+ ctx = ctx.values.first if ctx.length.eql?(1)
+ end
+
if data.is_a? Hash
ctx = data
tpl = templateify(template)
View
9 lib/mustache/context.rb
@@ -15,6 +15,7 @@ class Context
# Expect to be passed an instance of `Mustache`.
def initialize(mustache)
@stack = [mustache]
+ @helper_context = nil
end
# A {{>partial}} tag translates into a call to the context's
@@ -65,6 +66,10 @@ def pop
self
end
+ def helper=(helper_context)
+ @helper_context = helper_context
+ end
+
# Can be used to add a value to the context in a hash-like way.
#
# context[:name] = "Chris"
@@ -138,5 +143,9 @@ def find(obj, key, default = nil)
default
end
end
+
+ def helper(symbol, args = [])
+ @helper_context.send(symbol, args)
+ end
end
end
View
31 lib/mustache/generator.rb
@@ -101,6 +101,8 @@ def on_section(name, content, raw, delims)
if v = #{compile!(name)}
if v == true
#{code}
+ elsif v.is_a?(String)
+ #{code} if v.length > 0
elsif v.is_a?(Proc)
t = Mustache::Template.new(v.call(#{raw.inspect}).to_s)
def t.tokens(src=@source)
@@ -109,6 +111,8 @@ def t.tokens(src=@source)
p.compile(src)
end
t.render(ctx.dup)
+ elsif !(v.is_a?(Array) || defined?(Enumerator) && v.is_a?(Enumerator))
+ #{code} if v != nil
else
# Shortcut when passed non-array
v = [v] unless v.is_a?(Array) || defined?(Enumerator) && v.is_a?(Enumerator)
@@ -182,6 +186,33 @@ def on_fetch(names)
end
end
+ def on_helper(names)
+ method_name = names[0]
+ call_string = ""
+ names[1..names.length].each do |name|
+ unless (name[0].eql?("'") && name[name.length - 1].eql?("'")) || (name[0].eql?("\"") && name[name.length - 1].eql?("\""))
+ # resolve the method
+ index = names.index(name)
+ initial, *rest = name.split(".")
+ if rest.length == 0
+ code = "ctx[#{initial.to_sym.inspect}]"
+ else
+ code = <<-compiled
+ #{rest.inspect}.inject(ctx[#{initial.inspect}]) { |value, key|
+ value && ctx.find(value, key)
+ }
+ compiled
+ end
+ names[index] = code
+ name = code
+ end
+ call_string << name
+ call_string << ", " unless names.last.eql?(name)
+ end
+
+ "ctx.helper :#{method_name}, [#{call_string}]"
+ end
+
# An interpolation-friendly version of a string, for use within a
# Ruby string.
def ev(s)
View
16 lib/mustache/parser.rb
@@ -50,11 +50,11 @@ def to_s
SKIP_WHITESPACE = [ '#', '^', '/', '<', '>', '=', '!' ]
# The content allowed in a tag name.
- ALLOWED_CONTENT = /(\w|[?!\/.-])*/
+ ALLOWED_CONTENT = /(\s|\w|[?!\/.-])*/
# These types of tags allow any content,
# the rest only allow ALLOWED_CONTENT.
- ANY_CONTENT = [ '!', '=' ]
+ ANY_CONTENT = [ '!', '=', '{']
attr_reader :scanner, :result
attr_writer :otag, :ctag
@@ -139,7 +139,17 @@ def scan_tags
# We found {{ but we can't figure out what's going on inside.
error "Illegal content in tag" if content.empty?
- fetch = [:mustache, :fetch, content.split('.')]
+ splits = []
+ mode = :fetch
+
+ if content.match(/\w+\s(\w|['"])+/).nil?
+ splits = content.split(".")
+ else # detected a handlebars style helper call
+ splits = content.scan(/"[^"]*"|'[^']*'|[^"'\s]+/)
+ mode = :helper
+ end
+
+ fetch = [:mustache, mode, splits]
prev = @result
# Based on the sigil, do what needs to be done.
View
2  lib/mustache/template.rb
@@ -31,6 +31,7 @@ def initialize(source)
# the compilation step and run the Ruby version of the template
# directly.
def render(context)
+ puts context.class.name
# Compile our Mustache template into a Ruby string
compiled = "def render(ctx) #{compile} end"
@@ -46,6 +47,7 @@ def render(context)
# Does the dirty work of transforming a Mustache template into an
# interpolation-friendly Ruby string.
def compile(src = @source)
+ puts "Running compile"
Generator.new.compile(tokens(src))
end
alias_method :to_s, :compile
View
2  lib/mustache/version.rb
@@ -1,3 +1,3 @@
class Mustache
- Version = VERSION = '0.99.4'
+ Version = VERSION = '0.99.4.1'
end

No commit comments for this range

Something went wrong with that request. Please try again.