Malt  Malt::Machine

[Validate]
Generated with RDazzle Newfish 1.4.0

Machine

The Machine class encapsulates Malt’s main methods along with configuratable settings to control which engines and formats are used for rendering.

Constants

MARKUP

List of markup types. These are formats that just allow markup transformations and do not provide for data injection.

TEMPLATES

List of template types. These are template formats that provide data injection.

TEMPLATES_SAFE

Template types that prevent arbitrary Ruby code execution.

Public Class Methods

new(config={}) click to toggle source

New Malt Machine.

  • config[:types] - list of formats to handle

  • config[:priority] - list of prioritized engines

    # File lib/malt/machine.rb, line 27
27:     def initialize(config={})
28:       if priority = config[:priority]
29:         priority.map!{ |e| e.to_sym }
30:       else
31:         priority = []
32:       end
33:       if types = config[:types] || config[:type]
34:         formats = {}
35:         engines = {}
36:         types.each do |type|
37:           k = ext_to_type(type)
38:           formats[k] = Malt::Format.registry[k]
39:           engines[k] = Malt::Engine.registry[k]
40:         end
41:       else
42:         formats = Malt::Format.registry #.dup
43:         engines = Malt::Engine.registry #.dup
44:       end
45:       @formats  = formats
46:       @engines  = engines
47:       @priority = priority
48:     end

Public Instance Methods

engine?(ext) click to toggle source
    # File lib/malt/machine.rb, line 73
73:     def engine?(ext)
74:       type = ext_to_type(ext)
75:       engines.key?(type)
76:       ##ext  = ext.to_s
77:       ##type = ext.sub(/^\./, '').strip
78:       ##return false if type.empty?
79:       ###@registry.key?(ext.to_sym)
80:       ###Engine.registry[type.to_sym]
81:       ##Engine.registry.key?(type.to_sym)
82:     end
engines() click to toggle source
    # File lib/malt/machine.rb, line 56
56:     def engines
57:       @engines
58:     end
file(file, options={}) click to toggle source
    # File lib/malt/machine.rb, line 91
91:     def file(file, options={})
92:       type = options[:type] || options[:format] || File.extname(file)
93:       type = ext_to_type(type)
94:       malt_class = formats[type]
95:       raise "unkown type -- #{type}" unless malt_class
96:       malt_class.new(options.merge(:file=>file,:type=>type))
97:     end
format?(ext) click to toggle source
    # File lib/malt/machine.rb, line 85
85:     def format?(ext)
86:       type = ext_to_type(ext)
87:       formats.key?(type)
88:     end
formats() click to toggle source
    # File lib/malt/machine.rb, line 51
51:     def formats
52:       @formats
53:     end
open(url, options={}) click to toggle source

Open a URL as a Malt Format object.

     # File lib/malt/machine.rb, line 113
113:     def open(url, options={})
114:       require 'open-uri'
115:       text = open(url).read
116:       file = File.basename(url) # parse better with URI.parse
117:       text(text, :file=>file)
118:     end
priority(type=nil) click to toggle source

Engine priorities.

Returns an Array of symbolic engine names.

    # File lib/malt/machine.rb, line 63
63:     def priority(type=nil)
64:       if type
65:         type = type.to_s.downcase.to_sym
66:         @priority.unshift(type)
67:         @priority.uniq!  # assuming first are kept
68:       end
69:       @priority
70:     end
render(parameters={}, &body) click to toggle source

Render template directly.

parameters[:file] - File name of template. Used to read text. parameters[:text] - Text of template document. parameters[:type] - File type/extension used to look up engine. parameters[:pass] - If not a supported type return text rather than raise an error. parameters[:engine] - Force the use of a this specific engine. parameters[:to] - Format to convert to (usual default is `html`).

     # File lib/malt/machine.rb, line 129
129:     def render(parameters={}, &body)
130:       type   = parameters[:type]
131:       file   = parameters[:file]
132:       text   = parameters[:text]
133:       engine = parameters[:engine]
134: 
135:       type   = file_type(file, type)
136:       text   = file_read(file) unless text
137: 
138:       engine_class = engine(type, engine)
139: 
140:       if engine_class
141:         parameters[:type] = type
142:         parameters[:text] = text
143: 
144:         engine = engine_class.new
145:         engine.render(parameters, &body)
146:       else
147:         if parameters[:pass]
148:           text
149:         else
150:           raise NoEngineError, "no engine to handle `#{type}' format"      
151:         end
152:       end
153:     end
text(text, options={}) click to toggle source
     # File lib/malt/machine.rb, line 100
100:     def text(text, options={})
101:       if file = options[:file]
102:         ext = File.extname(file)
103:         ext = nil if ext.empty?
104:       end
105:       type = options[:type] || options[:format] || ext
106:       type = ext_to_type(type)
107:       malt_class = formats[type] || Format::Text  # :pass ?
108:       #raise "unkown type -- #{type}" unless malt_class
109:       malt_class.new(options.merge(:text=>text,:file=>file,:type=>type))
110:     end

Private Instance Methods

engine(type, engine=nil) click to toggle source
     # File lib/malt/machine.rb, line 158
158:     def engine(type, engine=nil)
159:       type = ext_to_type(type)
160:       #engine = engine || Malt.config.engine[type]  # FIXME
161:       case engine
162:       when Class
163:         #raise unless Engine.registry[type].include?(engine)
164:         engine
165:       when String, Symbol
166:         match = engine.to_s.downcase.to_sym
167:         #Engine.registry[type].find{ |e| e.basename.downcase == match }
168:         #engines[type].find{ |e| e.basename.downcase == match }
169:         engines[type].find{ |e| match == e.type }
170:       else
171:         if engines[type]
172:           #Engine.registry[type].first
173:           types = engines[type]
174:           if prior = types.find{ |e| priority.include?(e.type) }
175:             return prior
176:           end
177:           if default = Engine.defaults[type]           
178:             return default #if engine?(default.type)
179:           end
180:           types.first
181:         else
182:           nil
183:         end
184:       end
185:     end
ext_to_type(ext) click to toggle source

Normal file extension to a symbol type reference.

     # File lib/malt/machine.rb, line 211
211:     def ext_to_type(ext)
212:       ext = ext.to_s.downcase
213:       return nil if ext.empty?
214:       if ext[0,1] == '.'
215:         ext[1..1].to_sym
216:       else
217:         ext.to_sym
218:       end
219:     end
file_read(file) click to toggle source

TODO: Handle File objects and URLs.

     # File lib/malt/machine.rb, line 206
206:     def file_read(file)
207:       File.read(file)
208:     end
file_type(file, type=nil) click to toggle source
     # File lib/malt/machine.rb, line 188
188:     def file_type(file, type=nil)
189:       ext = type || File.extname(file)
190:       ext = ext.to_s.downcase
191:       if ext.empty?
192:         nil
193:       elsif ext[0,1] == '.'
194:         ext[1..1].to_sym
195:       else
196:         ext.to_sym
197:       end
198:     end

Disabled; run with --debug to generate this.