Class: Malt::Formats::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.

Direct Known Subclasses

CSS, HTML, Haml, LESS, Latex, Markdown, PDF, RDoc, Radius, Rtals, Ruby, Sass, Text, Textile, YAML

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.



54
55
56
# File 'lib/malt/formats/abstract.rb', line 54

def options
  @options
end

Class Method Details

.engine(set = nil) ⇒ Object



32
33
34
35
# File 'lib/malt/formats/abstract.rb', line 32

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

.extensionsObject



27
28
29
# File 'lib/malt/formats/abstract.rb', line 27

def self.extensions
  @extensions
end

.register(*exts) ⇒ Object

Register the class to an extension type.



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/malt/formats/abstract.rb', line 13

def self.register(*exts)
  @extensions = exts
  Malt.register(self, *exts)

  #exts.each do |ext|
  #  Abstract.module_eval %{
  #    def to_#{ext}(*db,&yld)
  #      convert(:#{ext},*db,&yld)
  #    end
  #  }
  #end
end

Instance Method Details

#defaultObject

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



163
164
165
# File 'lib/malt/formats/abstract.rb', line 163

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.



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

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

#extensionsObject



147
148
149
# File 'lib/malt/formats/abstract.rb', line 147

def extensions
  self.class.extensions
end

#fileObject

File name of document.



62
63
64
# File 'lib/malt/formats/abstract.rb', line 62

def file
  @file ||= options[:file].to_s
end

#parse_type_and_data(type_and_data) ⇒ Object



168
169
170
171
172
173
174
175
176
177
# File 'lib/malt/formats/abstract.rb', line 168

def parse_type_and_data(type_and_data)
  if Symbol === type_and_data.first
    type = type_and_data.first
    data = type_and_data.last
  else
    type = nil
    data = type_and_data.first
  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.



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/malt/formats/abstract.rb', line 130

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 database for rendering template variables.



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

def render(*type_and_data, &yld)
  type, data = parse_type_and_data(type_and_data)
  __send__(type || default, data, &yld)
end

#subtypeObject



152
153
154
# File 'lib/malt/formats/abstract.rb', line 152

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

#textObject

Document source text.



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

def text
  @text ||= options[:text] || File.read(file)
end

#to(type, data = nil, &yld) ⇒ Object



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

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

#to_sObject



157
158
159
# File 'lib/malt/formats/abstract.rb', line 157

def to_s
  text
end

#typeObject

File extension (with prefixed dot).



67
68
69
# File 'lib/malt/formats/abstract.rb', line 67

def type
  @type ||= File.extname(file)
end