lib/cutaneous/loader.rb in cutaneous-0.1.7 vs lib/cutaneous/loader.rb in cutaneous-0.2.0

- old
+ new

@@ -17,32 +17,28 @@ def convert(template, to_syntax) template(template).convert(to_syntax) end def template(template) - return proc_template(template) if template.is_a?(Proc) + template = open_template(template) if String === template + instance = @template_class.new(lexer(template)) + instance.path = template.path if template.respond_to?(:path) + instance.loader = self + instance + end + + def open_template(template) template_path = path(template) raise UnknownTemplateError.new(@roots, filename(template)) if template_path.nil? - - @template_class.new(file_lexer(template_path)).tap do |template| - template.path = template_path - template.loader = self - end + # TODO: Make the encoding configurable? + TemplateReader.new(template_path, Encoding::UTF_8) end - def proc_template(lmda) - StringLoader.new(self).template(lmda.call) + def lexer(template) + Lexer.new(template, syntax) end - def file_lexer(template_path) - lexer(SourceFile.new(template_path)) - end - - def lexer(template_string) - Lexer.new(template_string, syntax) - end - def path(template_name) filename = filename(template_name) return filename if ::File.exists?(filename) # Test for an absolute path @roots.map { |root| ::File.join(root, filename)}.detect { |path| ::File.exists?(path) } end @@ -50,45 +46,34 @@ def filename(template_name) [template_name, @format, @extension].join(".") end def exists?(template_root, template_name) - File.exists?(File.join(template_root, filename(template_name))) + path = ::File.join(template_root, filename(template_name)) + ::File.exists?(path) end - end - # Converts a template string into a Template instance. - # - # Because a string template can only come from the engine instance - # we need a FileLoader to delegate all future template loading to. - class StringLoader < FileLoader - def initialize(file_loader) - @file_loader = file_loader + def location(template_root, template_name) + return ::File.join(template_root, template_name) if exists?(template_root, template_name) + nil end - def syntax - @file_loader.syntax - end + # An IO-like interface that provides #read and #path methods + class TemplateReader + def initialize(path, encoding) + @path = path + @encoding = encoding + end - def template(template_string) - Template.new(lexer(template_string)).tap do |template| - template.loader = @file_loader + def read(*args) + ::File.open(@path, 'r', external_encoding: @encoding) { |f| f.read } end - end - end - # Converts a filepath to a template string as and when necessary - class SourceFile - attr_reader :path - - def initialize(filepath) - @path = filepath + def path + @path + end end - - def to_s - File.read(@path) - end end # Caches Template instances class CachedFileLoader < FileLoader def initialize(template_roots, format, extension = Cutaneous.extension) @@ -117,35 +102,42 @@ # Provides an additional caching mechanism by writing generated template # scripts to a .rb file. class CachedTemplate < Template def script - if cached? - script = File.read(script_path) + script = nil + path = script_path + if path && cached? + script = File.read(path) else script = super - File.open(script_path, "w") do |f| - f.write(script) - end + write_cached_script(script, path) unless path.nil? end script end def cached? File.exist?(script_path) && (File.mtime(script_path) >= File.mtime(template_path)) end def template_path - lexer.template.path + path end def script_path @source_path ||= generate_script_path end def generate_script_path path = template_path + return nil if path.nil? ext = File.extname path path.gsub(/#{ext}$/, ".rb") + end + + def write_cached_script(script, path) + File.open(script_path, "w") do |f| + f.write(script) + end end end end