def HTree.expand_template(*args, &block)
if block
template = block.call
binding = block.binding
else
pathname = args.fetch(0) { raise ArgumentError, "pathname not given" }
args.shift
obj = args.fetch(0) { Object.new }
args.shift
if pathname.respond_to? :read
template = pathname.read.untaint
if template.respond_to? :charset
if template.respond_to? :encode
template = template.encode(HTree::Encoder.internal_charset, template.charset)
else
template = Iconv.conv(HTree::Encoder.internal_charset, template.charset, template)
end
end
else
template = File.read(pathname).untaint
end
Thread.current[:htree_expand_template_obj] = obj
binding = eval("Thread.current[:htree_expand_template_obj].class.class_eval <<-'EE'\nThread.current[:htree_expand_template_obj].instance_eval { binding }\nEE\n",
HTree::EmptyBindingObject.empty_binding, "(eval:#{__FILE__}:#{__LINE__})")
Thread.current[:htree_expand_template_obj] = nil
end
out = args.shift || $stdout
encoding = args.shift || HTree::Encoder.internal_charset
if !args.empty?
raise ArgumentError, "wrong number of arguments"
end
HTree::TemplateCompiler.new.expand_template(template, out, encoding, binding)
end