class Malt::Format::Abstract
Abstract
format class serves as the base class for all other format classes.
Attributes
Access to the options given to the initializer.
Public Class Methods
# File lib/malt/formats/abstract.rb, line 56 def self.engine(set=nil) @engine = set if set @engine end
# File lib/malt/formats/abstract.rb, line 49 def self.extensions @file_extensions end
Register the class to an extension type.
# File lib/malt/formats/abstract.rb, line 30 def self.file_extension(*exts) @file_extensions = exts Malt::Format.register(self, *exts) #exts.each do |ext| # Abstract.module_eval %{ # def to_#{ext}(*db,&yld) # convert(:#{ext},*db,&yld) # end # } #end end
# File lib/malt/formats/abstract.rb, line 64 def initialize(options={}) @options = options.rekey @file = @options[:file] @text = @options[:text] || file_read(@file) @type = @options[:type] || file_type(@file) end
@deprecated
# File lib/malt/formats/abstract.rb, line 44 def self.register(*exts) self.file_extension(*exts) end
Public Instance Methods
Default rendering type is :html
. Override if it differs for the subclassing format.
# File lib/malt/formats/abstract.rb, line 215 def default :html end
Specified engine to use for rendering.
Keep in mind that the ability to specify the engine varies based on engine, format and output format.
# File lib/malt/formats/abstract.rb, line 108 def engine options[:engine] || self.class.engine end
# File lib/malt/formats/abstract.rb, line 194 def extensions self.class.extensions end
File name of document.
# File lib/malt/formats/abstract.rb, line 95 def file @file end
# File lib/malt/formats/abstract.rb, line 252 def file_read(file) File.read(file) end
# File lib/malt/formats/abstract.rb, line 257 def file_type(file) if file File.extname(file) else nil end end
# File lib/malt/formats/abstract.rb, line 220 def parse_type_from_data(*data) if Symbol === data.first type = data.shift else type = nil end return type, data end
Produce a new filename replacing old extension with new extension.
type - Symbol representation of extension (e.g. :html).
Returns a String
of the new file name.
# File lib/malt/formats/abstract.rb, line 177 def refile(type=nil) if file if type type = type.to_s.sub(/^\./,'') fext = self.class.extensions.find{|e| file.end_with?(e)} new_file = file.chomp(fext) + '.' + type else fext = self.class.extensions.find{|e| file.end_with?(e)} new_file = file.chomp('.'+fext) end else new_file = nil end new_file end
Render to default or given format.
If the first argument is a Symbol it is considered the format, otherwise it is taken to be the data for rendering template variables.
# File lib/malt/formats/abstract.rb, line 121 def render(*type_and_data, &yld) type, data = parse_type_from_data(*type_and_data) meth = method(type || default) #__send__(type || default, data, &yld) meth.arity == 0 ? meth.call(&yld) : meth.call(*data, &yld) end
# File lib/malt/formats/abstract.rb, line 129 def render_into(into, *data, &content) parameters = rendering_parameters(into, data) Malt.render(parameters, &content) end
# File lib/malt/formats/abstract.rb, line 135 def rendering_parameters(into, data) opts = options.merge( :to => into, :data => data, :text => text, :file => file, :engine => engine ) end
@deprecate
# File lib/malt/formats/abstract.rb, line 241 def scope_vs_data(scope, data=nil) if scope && !data if scope.respond_to?(:to_hash) data = scope scope = nil end end return scope, data end
# File lib/malt/formats/abstract.rb, line 199 def subtype File.extname(file.chomp(type)) end
Document source text.
# File lib/malt/formats/abstract.rb, line 90 def text @text end
# File lib/malt/formats/abstract.rb, line 113 def to(type, *data, &yld) __send__("to_#{type}", *data, &yld) end
# File lib/malt/formats/abstract.rb, line 209 def to_default(*data, &yld) to(default, *data, &yld) end
# File lib/malt/formats/abstract.rb, line 204 def to_s text end
File extension (with prefixed dot).
# File lib/malt/formats/abstract.rb, line 100 def type @type end
Change options on the fly.
# File lib/malt/formats/abstract.rb, line 78 def with(options={}) alt = dup alt.with!(options) end
Change options in-place.
# File lib/malt/formats/abstract.rb, line 84 def with!(options) @options.update(options.rekey) self end