Class: Malt::Machine
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
- #engine?(ext) ⇒ Boolean
- #engines ⇒ Object
- #file(file, options = {}) ⇒ Object
- #format?(ext) ⇒ Boolean
- #formats ⇒ Object
-
#initialize(config = {}) ⇒ Machine
constructor
New Malt Machine.
-
#open(url, options = {}) ⇒ Malt::Format
Open a URL as a Malt Format object.
-
#priority(type = nil) ⇒ Object
(also: #prefer)
Engine priorities.
-
#render(parameters = {}, &content) ⇒ Object
Render template directly.
- #text(text, options = {}) ⇒ Object
Constructor Details
#initialize(config = {}) ⇒ Machine
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
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 |
#engines ⇒ Object
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, ={}) type = [:type] || [:format] || File.extname(file) type = ext_to_type(type) malt_class = formats[type] raise "unknown type -- #{type}" unless malt_class malt_class.new(.merge(:file=>file,:type=>type)) end |
#format?(ext) ⇒ Boolean
86 87 88 89 |
# File 'lib/malt/machine.rb', line 86 def format?(ext) type = ext_to_type(ext) formats.key?(type) end |
#formats ⇒ Object
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.
116 117 118 119 120 121 122 |
# File 'lib/malt/machine.rb', line 116 def open(url, ={}) require 'open-uri' text = open(url).read file = File.basename(url) # parse better with URI.parse [:file] = file text(text, ) 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, ={}) if file = [:file] ext = File.extname(file) ext = nil if ext.empty? end type = [:type] || [:format] || ext type = ext_to_type(type) malt_class = formats[type] || Format::Text # :pass ? #raise "unkown type -- #{type}" unless malt_class malt_class.new(.merge(:text=>text,:file=>file,:type=>type)) end |