Class: Malt::Format::Abstract

Inherits:
Object
  • Object
show all
Includes:
Kernel
Defined in:
lib/malt/formats/abstract.rb

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optionsObject (readonly)

Access to the options given to the initializer.



75
76
77
# File 'lib/malt/formats/abstract.rb', line 75

def options
  @options
end

Class Method Details

.engine(set = nil) ⇒ Object

– TODO: warning: instance variable @engine not initialized ++



56
57
58
59
# File 'lib/malt/formats/abstract.rb', line 56

def self.engine(set=nil)
  @engine = set if set
  @engine
end

.extensionsObject



49
50
51
# File 'lib/malt/formats/abstract.rb', line 49

def self.extensions
  @file_extensions
end

.file_extension(*exts) ⇒ Object

Register the class to an extension type.



30
31
32
33
34
35
36
37
38
39
40
41
# 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

.register(*exts) ⇒ Object

Deprecated.


44
45
46
# File 'lib/malt/formats/abstract.rb', line 44

def self.register(*exts)
  self.file_extension(*exts)
end

Instance Method Details

#defaultObject

Default rendering type is :html. Override if it differs for the subclassing format.



215
216
217
# File 'lib/malt/formats/abstract.rb', line 215

def default
  :html
end

#engineObject

Specified engine to use for rendering.

Keep in mind that the ability to specify the engine varies based on engine, format and output format.



108
109
110
# File 'lib/malt/formats/abstract.rb', line 108

def engine
  options[:engine] || self.class.engine
end

#extensionsObject



194
195
196
# File 'lib/malt/formats/abstract.rb', line 194

def extensions
  self.class.extensions
end

#fileObject

File name of document.



95
96
97
# File 'lib/malt/formats/abstract.rb', line 95

def file
  @file
end

#file_read(file) ⇒ Object



252
253
254
# File 'lib/malt/formats/abstract.rb', line 252

def file_read(file)
  File.read(file)
end

#file_type(file) ⇒ Object



257
258
259
260
261
262
263
# 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) ⇒ Object



220
221
222
223
224
225
226
227
# 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) ⇒ Object

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.



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# 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) ⇒ Object

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.



121
122
123
124
125
126
# 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) ⇒ Object



129
130
131
132
# 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) ⇒ Object



135
136
137
138
139
140
141
142
143
# 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) ⇒ Object



241
242
243
244
245
246
247
248
249
# 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

#subtypeObject



199
200
201
# File 'lib/malt/formats/abstract.rb', line 199

def subtype
  File.extname(file.chomp(type))
end

#textObject

Document source text.



90
91
92
# File 'lib/malt/formats/abstract.rb', line 90

def text
  @text
end

#to(type, *data, &yld) ⇒ Object



113
114
115
# File 'lib/malt/formats/abstract.rb', line 113

def to(type, *data, &yld)
  __send__("to_#{type}", *data, &yld)
end

#to_default(*data, &yld) ⇒ Object



209
210
211
# File 'lib/malt/formats/abstract.rb', line 209

def to_default(*data, &yld)
  to(default, *data, &yld)
end

#to_sObject



204
205
206
# File 'lib/malt/formats/abstract.rb', line 204

def to_s
  text
end

#typeObject

File extension (with prefixed dot).



100
101
102
# File 'lib/malt/formats/abstract.rb', line 100

def type
  @type
end

#with(options = {}) ⇒ Object

Change options on the fly.



78
79
80
81
# File 'lib/malt/formats/abstract.rb', line 78

def with(options={})
  alt = dup
  alt.with!(options)
end

#with!(options) ⇒ Object

Change options in-place.



84
85
86
87
# File 'lib/malt/formats/abstract.rb', line 84

def with!(options)
  @options.update(options.rekey)
  self
end