Class: Malt::Machine

Inherits:
Object show all
Defined in:
lib/malt/machine.rb

Overview

The Machine class encapsulates Malt’s main methods along with configuratable settings to control which engines and formats are used for rendering. – TODO: Can we dynamically generate the MARKUP and TEMPLATE constants from the format classes? In anycase, the still needs tweaking. ++

Constant Summary collapse

MARKUP =

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

[:rdoc, :markdown, :textile, :scss, :sass, :less, :css, :html, :xml, :wikicloth]
TEMPLATE =

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

[:erb, :liquid, :mustache, :tenjin, :ragtag, :radius, :erector, :builder, :ruby, :string]
TEMPLATE_SAFE =

Template types that prevent arbitrary Ruby code execution.

[:liquid, :mustache]

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Machine

New Malt Machine.

  • config - list of formats to handle

  • config - list of prioritized engines



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/malt/machine.rb', line 27

def initialize(config={})
  if priority = config[:priority]
    priority = priority.map{ |e| e.to_sym }
  else
    priority = []
  end
  if types = config[:types] || config[:type]
    formats = {}
    engines = {}
    types.each do |type|
      k = ext_to_type(type)
      formats[k] = Malt::Format.registry[k]
      engines[k] = Malt::Engine.registry[k]
    end
  else
    formats = Malt::Format.registry #.dup
    engines = Malt::Engine.registry #.dup
  end
  @formats  = formats
  @engines  = engines
  @priority = priority
end

Instance Method Details

#engine?(ext) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
82
83
# File 'lib/malt/machine.rb', line 74

def engine?(ext)
  type = ext_to_type(ext)
  engines.key?(type)
  ##ext  = ext.to_s
  ##type = ext.sub(/^\./, '').strip
  ##return false if type.empty?
  ###@registry.key?(ext.to_sym)
  ###Engine.registry[type.to_sym]
  ##Engine.registry.key?(type.to_sym)
end

#enginesObject



56
57
58
# File 'lib/malt/machine.rb', line 56

def engines
  @engines
end

#file(file, options = {}) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/malt/machine.rb', line 92

def file(file, options={})
  type = options[:type] || options[:format] || File.extname(file)
  type = ext_to_type(type)
  malt_class = formats[type]
  raise "unknown type -- #{type}" unless malt_class
  malt_class.new(options.merge(:file=>file,:type=>type))
end

#format?(ext) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
89
# File 'lib/malt/machine.rb', line 86

def format?(ext)
  type = ext_to_type(ext)
  formats.key?(type)
end

#formatsObject



51
52
53
# File 'lib/malt/machine.rb', line 51

def formats
  @formats
end

#open(url, options = {}) ⇒ Malt::Format

Open a URL as a Malt Format object.

Returns:



116
117
118
119
120
121
122
# File 'lib/malt/machine.rb', line 116

def open(url, options={})
  require 'open-uri'
  text = open(url).read
  file = File.basename(url) # parse better with URI.parse
  options[:file] = file
  text(text, options)
end

#priority(type = nil) ⇒ Object Also known as: prefer

Engine priorities.

Returns an Array of symbolic engine names.



63
64
65
66
67
68
69
70
# File 'lib/malt/machine.rb', line 63

def priority(type=nil)
  if type
    type = type.to_s.downcase.to_sym
    @priority.unshift(type)
    @priority.uniq!  # assuming first are kept
  end
  @priority
end

#render(parameters = {}, &content) ⇒ Object

Render template directly.

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



133
134
135
136
137
138
139
140
141
# File 'lib/malt/machine.rb', line 133

def render(parameters={}, &content)
  parameters = normalize_parameters(parameters)

  if parameters[:multi]
    multi_render(parameters, &content)
  else
    single_render(parameters, &content)
  end
end

#text(text, options = {}) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/malt/machine.rb', line 101

def text(text, options={})
  if file = options[:file]
    ext = File.extname(file)
    ext = nil if ext.empty?
  end
  type = options[:type] || options[:format] || ext
  type = ext_to_type(type)
  malt_class = formats[type] || Format::Text  # :pass ?
  #raise "unkown type -- #{type}" unless malt_class
  malt_class.new(options.merge(:text=>text,:file=>file,:type=>type))
end