Skip to content

Instantly share code, notes, and snippets.

@namelessjon
Created June 8, 2010 14:27
Show Gist options
  • Save namelessjon/430099 to your computer and use it in GitHub Desktop.
Save namelessjon/430099 to your computer and use it in GitHub Desktop.
Simple implentation for embedding mustache template in a script
#!/usr/bin/ruby
# Copyright (c) 2010 Jonathan Stott, unless otherwise noted
#
# Permission is hereby granted, free of charge, to any person ob-
# taining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without restric-
# tion, including without limitation the rights to use, copy, modi-
# fy, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is fur-
# nished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONIN-
# FRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
require 'mustache'
class Mustache
class OwnMustacheNotFound < StandardError
def initialize(message="Have you shaved today?")
super(message)
end
end
def self.own(context={})
own_template = find_own_template
raise OwnMustacheNotFound if own_template.nil?
own_template.strip! # oh my!
render(own_template, context)
end
# The following methods are flat out lifted, or heavily based on methods
# taken from sinatra/base.rb
#
# Copyright (c) 2007, 2008, 2009 Blake Mizerany
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
def self.find_own_template
file = caller_files.first
begin
app, data =
::IO.read(file).gsub("\r\n", "\n").split(/^__END__$/, 2)
rescue Errno::ENOENT
app, data = nil
end
data
end
CALLERS_TO_IGNORE = [
/own-mustache\.rb$/, # ourself.
/\(.*\)/, # generated code
/custom_require\.rb$/, # rubygems require hacks
/active_support/, # active_support require hacks
]
# add rubinius (and hopefully other VM impls) ignore patterns ...
CALLERS_TO_IGNORE.concat(RUBY_IGNORE_CALLERS) if defined?(RUBY_IGNORE_CALLERS)
# Like Kernel#caller but excluding certain magic entries and without
# line / method information; the resulting array contains filenames only.
def self.caller_files
caller_locations.
map { |file,line| file }
end
def self.caller_locations
caller(1).
map { |line| line.split(/:(?=\d|in )/)[0,2] }.
reject { |file,line| CALLERS_TO_IGNORE.any? { |pattern| file =~ pattern } }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment