class Malt::Format::Abstract

Abstract format class serves as the base class for all other format classes.

Attributes

options[R]

Access to the options given to the initializer.

Public Class Methods

engine(set=nil) click to toggle source
# File lib/malt/formats/abstract.rb, line 56
def self.engine(set=nil)
  @engine = set if set
  @engine
end
extensions() click to toggle source
# File lib/malt/formats/abstract.rb, line 49
def self.extensions
  @file_extensions
end
file_extension(*exts) click to toggle source

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
new(options={}) click to toggle source
# 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
register(*exts) click to toggle source

@deprecated

# File lib/malt/formats/abstract.rb, line 44
def self.register(*exts)
  self.file_extension(*exts)
end

Public Instance Methods

default() click to toggle source

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
engine() click to toggle source

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
extensions() click to toggle source
# File lib/malt/formats/abstract.rb, line 194
def extensions
  self.class.extensions
end
file() click to toggle source

File name of document.

# File lib/malt/formats/abstract.rb, line 95
def file
  @file
end
file_read(file) click to toggle source
# File lib/malt/formats/abstract.rb, line 252
def file_read(file)
  File.read(file)
end
file_type(file) click to toggle source
# File lib/malt/formats/abstract.rb, line 257
def file_type(file)
  if file
    File.extname(file)
  else
    nil
  end
end
parse_type_from_data(*data) click to toggle source
# 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
refile(type=nil) click to toggle source

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(*type_and_data, &yld) click to toggle source

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
render_into(into, *data, &content) click to toggle source
# File lib/malt/formats/abstract.rb, line 129
def render_into(into, *data, &content)
  parameters = rendering_parameters(into, data)
  Malt.render(parameters, &content)
end
rendering_parameters(into, data) click to toggle source
# 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
scope_vs_data(scope, data=nil) click to toggle source

@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
subtype() click to toggle source
# File lib/malt/formats/abstract.rb, line 199
def subtype
  File.extname(file.chomp(type))
end
text() click to toggle source

Document source text.

# File lib/malt/formats/abstract.rb, line 90
def text
  @text
end
to(type, *data, &yld) click to toggle source
# File lib/malt/formats/abstract.rb, line 113
def to(type, *data, &yld)
  __send__("to_#{type}", *data, &yld)
end
to_default(*data, &yld) click to toggle source
# File lib/malt/formats/abstract.rb, line 209
def to_default(*data, &yld)
  to(default, *data, &yld)
end
to_s() click to toggle source
# File lib/malt/formats/abstract.rb, line 204
def to_s
  text
end
type() click to toggle source

File extension (with prefixed dot).

# File lib/malt/formats/abstract.rb, line 100
def type
  @type
end
with(options={}) click to toggle source

Change options on the fly.

# File lib/malt/formats/abstract.rb, line 78
def with(options={})
  alt = dup
  alt.with!(options)
end
with!(options) click to toggle source

Change options in-place.

# File lib/malt/formats/abstract.rb, line 84
def with!(options)
  @options.update(options.rekey)
  self
end