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: Handling of data (scope, local) seems a bit wonky. Can it be

improved and simplified?

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


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

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)

77
78
79
80
81
82
83
84
85
86
# File 'lib/malt/machine.rb', line 77

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


59
60
61
# File 'lib/malt/machine.rb', line 59

def engines
  @engines
end

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


95
96
97
98
99
100
101
# File 'lib/malt/machine.rb', line 95

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)

89
90
91
92
# File 'lib/malt/machine.rb', line 89

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

#formatsObject


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

def formats
  @formats
end

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

Open a URL as a Malt Format object.

Returns:


119
120
121
122
123
124
125
# File 'lib/malt/machine.rb', line 119

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.


66
67
68
69
70
71
72
73
# File 'lib/malt/machine.rb', line 66

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`).


136
137
138
139
140
141
142
143
144
# File 'lib/malt/machine.rb', line 136

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


104
105
106
107
108
109
110
111
112
113
114
# File 'lib/malt/machine.rb', line 104

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