-
Notifications
You must be signed in to change notification settings - Fork 226
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Encoding Support #107
Encoding Support #107
Changes from all commits
d602069
e18b99c
ee65ca9
35e5a48
7ef2de5
9c0ecf8
b73c7da
fcb9a1e
f36cc18
ae8900b
9a474c5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,29 @@ | ||
require 'tilt/template' | ||
|
||
module Tilt | ||
# Builder template implementation. See: | ||
# http://builder.rubyforge.org/ | ||
# XML Builder Template implementation | ||
# | ||
# - http://builder.rubyforge.org/ | ||
# | ||
# Builder templates support three types of template input: string, file, | ||
# and block. When the initialize block returns a non-string object that | ||
# responds to call (Proc), template execution consists of calling the block | ||
# with a Builder::XmlMarkup instance: | ||
# | ||
# BuilderTemplate.new do | ||
# lambda do |xml| | ||
# xml.h1 'howdy dudy' | ||
# xml.p 'blaahhh' | ||
# end | ||
# end | ||
# | ||
# Builder templates can also be instantiated from a string or file. In that | ||
# case, the source encoding is determined according to the rules documented | ||
# in the Tilt README under Encodings. The ruby magic comment line is supported | ||
# for specifying an alternative encoding. | ||
# | ||
# Builder templates always produce utf-8 encoded result strings regardless of | ||
# the source string / file encoding. | ||
class BuilderTemplate < Template | ||
self.default_mime_type = 'text/xml' | ||
|
||
|
@@ -14,7 +35,10 @@ def initialize_engine | |
require_template_library 'builder' | ||
end | ||
|
||
def prepare; end | ||
def prepare | ||
return if !data.respond_to?(:to_str) | ||
@source = assign_source_encoding(data.to_str) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like the opt-in for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah. I'm going with an approach where the base class provides some convenience APIs but the behavior is more or less in the hands of the template subclass. I don't think there's any other way since template's vary so much. |
||
end | ||
|
||
def evaluate(scope, locals, &block) | ||
return super(scope, locals, &block) if data.respond_to?(:to_str) | ||
|
@@ -23,6 +47,10 @@ def evaluate(scope, locals, &block) | |
xml.target! | ||
end | ||
|
||
def precompiled_template(locals) | ||
@source | ||
end | ||
|
||
def precompiled_preamble(locals) | ||
return super if locals.include? :xml | ||
"xml = ::Builder::XmlMarkup.new(:indent => 2)\n#{super}" | ||
|
@@ -31,10 +59,6 @@ def precompiled_preamble(locals) | |
def precompiled_postamble(locals) | ||
"xml.target!" | ||
end | ||
|
||
def precompiled_template(locals) | ||
data.to_str | ||
end | ||
end | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this
:transcode
option is implemented yet. Is this going to be set as an option oninitialize
or supposed to be passed each time to therender
method?We might be able to provide some of these things for the handler by adding them to the default
render
implementation. The before evaluating step doesn't always exist for every handler. For an example, coffee, sass and markdown are contextless and ignore thelocals
option. So these handlers only need to callencode
on the final string they produce. So we might be able to move that final encode to ourrender
. But I'm not sure if thats entirely a good idea.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope I haven't started in on any of the transcoding yet.
I hadn't even considered making this an option to
render
. I figured we'd want it oninitialize
and also maybe as a Template class attribute that can be overridden by subclasses. Having it onrender
could be interesting. I'm not sure how much it'd be used though. I think the 99% case for transcoding is going to be people settingEncoding.default_internal = 'utf-8'
or wanting the same effect when running templates.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't really want to put it on
render
cause we have nooptions
arg. Passing toinitialize
makes the most sense.We could provide some sort of
Template#encode()
helper that encodes the string tooptions[:transcode] || default_internal
. So handlers can call that before they return the final result.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exactly what I'm thinking. It needs to happen before evaluation for context / interpolating templates though.