malt 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.ruby +156 -0
- data/Assembly +28 -0
- data/COPYING.rdoc +33 -0
- data/Gemfile +10 -0
- data/HISTORY.rdoc +102 -0
- data/README.rdoc +16 -10
- data/Reapfile +4 -0
- data/lib/malt.rb +36 -8
- data/lib/malt.yml +156 -0
- data/lib/malt/conversions.rb +42 -0
- data/lib/malt/core_ext.rb +81 -3
- data/lib/malt/engines/abstract.rb +259 -50
- data/lib/malt/engines/bluecloth.rb +19 -9
- data/lib/malt/engines/builder.rb +93 -32
- data/lib/malt/engines/coffee.rb +46 -0
- data/lib/malt/engines/creole.rb +60 -0
- data/lib/malt/engines/erb.rb +69 -44
- data/lib/malt/engines/erector.rb +61 -30
- data/lib/malt/engines/erubis.rb +41 -31
- data/lib/malt/engines/haml.rb +13 -37
- data/lib/malt/engines/kramdown.rb +40 -15
- data/lib/malt/engines/less.rb +15 -14
- data/lib/malt/engines/liquid.rb +24 -14
- data/lib/malt/engines/markaby.rb +44 -22
- data/lib/malt/engines/maruku.rb +89 -0
- data/lib/malt/engines/mustache.rb +20 -14
- data/lib/malt/engines/nokogiri.rb +89 -0
- data/lib/malt/engines/radius.rb +72 -34
- data/lib/malt/engines/ragtag.rb +26 -18
- data/lib/malt/engines/rdiscount.rb +18 -11
- data/lib/malt/engines/rdoc.rb +21 -15
- data/lib/malt/engines/redcarpet.rb +137 -0
- data/lib/malt/engines/redcloth.rb +15 -7
- data/lib/malt/engines/ruby.rb +13 -12
- data/lib/malt/engines/sass.rb +30 -17
- data/lib/malt/engines/string.rb +36 -0
- data/lib/malt/engines/tenjin.rb +70 -27
- data/lib/malt/engines/wikicloth.rb +48 -0
- data/lib/malt/formats/abstract.rb +90 -29
- data/lib/malt/formats/abstract_template.rb +10 -8
- data/lib/malt/formats/builder.rb +39 -13
- data/lib/malt/formats/coffee.rb +54 -0
- data/lib/malt/formats/css.rb +3 -3
- data/lib/malt/formats/erb.rb +31 -66
- data/lib/malt/formats/haml.rb +8 -8
- data/lib/malt/formats/html.rb +1 -7
- data/lib/malt/formats/javascript.rb +27 -0
- data/lib/malt/formats/latex.rb +1 -1
- data/lib/malt/formats/less.rb +13 -11
- data/lib/malt/formats/liquid.rb +7 -7
- data/lib/malt/formats/markdown.rb +43 -44
- data/lib/malt/formats/mediawiki.rb +70 -0
- data/lib/malt/formats/mustache.rb +5 -5
- data/lib/malt/formats/pdf.rb +1 -7
- data/lib/malt/formats/radius.rb +5 -4
- data/lib/malt/formats/ragtag.rb +14 -13
- data/lib/malt/formats/rbhtml.rb +28 -20
- data/lib/malt/formats/rdoc.rb +5 -4
- data/lib/malt/formats/rhtml.rb +19 -43
- data/lib/malt/formats/ruby.rb +11 -45
- data/lib/malt/formats/sass.rb +16 -24
- data/lib/malt/formats/scss.rb +13 -30
- data/lib/malt/formats/string.rb +61 -0
- data/lib/malt/formats/tenjin.rb +4 -4
- data/lib/malt/formats/text.rb +1 -1
- data/lib/malt/formats/textile.rb +7 -19
- data/lib/malt/kernel.rb +1 -2
- data/lib/malt/machine.rb +83 -13
- data/lib/malt/tilted.rb +216 -0
- data/lib/malt/version.rb +21 -0
- data/test/helper.rb +15 -0
- data/test/unit/engines/case_engine_bluecloth.rb +40 -0
- data/test/unit/engines/case_engine_builder.rb +30 -0
- data/test/unit/engines/case_engine_coffee.rb +30 -0
- data/test/unit/engines/case_engine_creole.rb +35 -0
- data/test/unit/engines/case_engine_erb.rb +28 -0
- data/test/unit/engines/case_engine_erector.rb +36 -0
- data/test/unit/engines/case_engine_erubis.rb +28 -0
- data/test/unit/engines/case_engine_haml.rb +30 -0
- data/test/unit/engines/case_engine_kramdown.rb +30 -0
- data/test/unit/engines/case_engine_less.rb +40 -0
- data/test/unit/engines/case_engine_liquid.rb +28 -0
- data/test/unit/engines/case_engine_markaby.rb +20 -0
- data/test/unit/engines/case_engine_maruku.rb +30 -0
- data/test/unit/engines/case_engine_mustache.rb +28 -0
- data/test/unit/engines/case_engine_nokogiri.rb +30 -0
- data/test/unit/engines/case_engine_radius.rb +30 -0
- data/test/unit/engines/case_engine_ragtag.rb +40 -0
- data/test/unit/engines/case_engine_rdiscount.rb +30 -0
- data/test/unit/engines/case_engine_rdoc.rb +31 -0
- data/test/unit/engines/case_engine_redcarpet.rb +30 -0
- data/test/unit/engines/case_engine_redcloth.rb +31 -0
- data/test/unit/engines/case_engine_ruby.rb +28 -0
- data/test/unit/engines/case_engine_sass.rb +36 -0
- data/test/unit/engines/case_engine_string.rb +28 -0
- data/test/unit/engines/case_engine_tenjin.rb +28 -0
- data/test/unit/engines/case_engine_wikicloth.rb +25 -0
- data/test/unit/machine.rb +27 -0
- data/test/unit/malt.rb +12 -0
- metadata +364 -266
- data/History.rdoc +0 -64
- data/License.txt +0 -205
- data/Syckfile +0 -80
- data/features/consistent_rendering.feature +0 -36
- data/features/samples/sample.erb +0 -1
- data/features/samples/sample.erubis +0 -1
- data/features/samples/sample.liquid +0 -1
- data/features/samples/sample.mustache +0 -1
- data/features/samples/sample.radius +0 -1
- data/features/step_definitions/engine_steps.rb +0 -49
- data/features/support/loadpath.rb +0 -1
- data/features/support/sample_class.rb +0 -8
- data/lib/malt/formats/erector.rb +0 -53
- data/lib/malt/formats/markaby.rb +0 -53
- data/lib/malt/meta/data.rb +0 -26
- data/lib/malt/meta/package +0 -21
- data/lib/malt/meta/profile +0 -21
- data/meta/data.rb +0 -26
- data/meta/package +0 -21
- data/meta/profile +0 -21
- data/qed/01_overview.rdoc +0 -8
- data/qed/02_formats.rdoc +0 -39
- data/qed/03_formats/01_overview.rdoc +0 -7
- data/qed/03_formats/02_rdoc.rdoc +0 -83
- data/qed/03_formats/03_textile.rdoc +0 -48
- data/qed/03_formats/04_markdown.rdoc +0 -66
- data/qed/03_formats/05_erb.rdoc +0 -65
- data/qed/03_formats/06_liquid.rdoc +0 -54
- data/qed/03_formats/07_haml.rdoc +0 -44
- data/qed/03_formats/08_ragtag.rdoc +0 -19
- data/qed/03_formats/09_radius.rdoc +0 -43
- data/qed/03_formats/11_tenjin.rdoc +0 -47
- data/qed/03_formats/12_rbhtml.rdoc +0 -55
- data/qed/03_formats/13_sass.rdoc +0 -55
- data/qed/03_formats/14_scss.rdoc +0 -58
- data/qed/03_formats/15_less.rdoc +0 -46
- data/qed/03_formats/16_ruby.rdoc +0 -48
- data/qed/03_formats/17_markaby.rdoc +0 -50
- data/qed/03_formats/18_builder.rb +0 -50
- data/qed/03_formats/19_erector.rb +0 -50
- data/qed/03_formats/20_mustache.rdoc +0 -54
- data/qed/05_machine/01_limited_formats.rdoc +0 -29
- data/qed/05_machine/02_prioritized_engines.rdoc +0 -34
- data/qed/applique/malt.rb +0 -13
- data/qed/samples/data.yml +0 -4
- data/qed/samples/output-erb.txt +0 -1
- data/qed/samples/output-liquid.txt +0 -1
- data/qed/samples/output-mustache.txt +0 -1
- data/qed/samples/output-radius.txt +0 -1
- data/qed/samples/sample.erb +0 -1
- data/qed/samples/sample.liquid +0 -1
- data/qed/samples/sample.markdown +0 -308
- data/qed/samples/sample.mustache +0 -1
- data/qed/samples/sample.radius +0 -1
- data/qed/samples/sample.rdoc +0 -8
data/lib/malt/formats/ruby.rb
CHANGED
@@ -1,70 +1,36 @@
|
|
1
1
|
require 'malt/formats/abstract'
|
2
|
-
require 'malt/formats/html'
|
3
2
|
require 'malt/engines/erb'
|
4
3
|
|
5
4
|
module Malt::Format
|
6
5
|
|
7
6
|
# Yes, pure Ruby as a template format.
|
8
7
|
#
|
9
|
-
# The ruby code is run through eval and whatever
|
8
|
+
# The ruby code is run through eval and whatever it returns is given
|
10
9
|
# as the rendering.
|
11
10
|
#
|
12
11
|
# The Ruby format is a *polyglot* format --it accepts all conversion
|
13
12
|
# types and assumes the end-user knows it will be the result.
|
14
13
|
#
|
15
|
-
#
|
16
|
-
# as ERB.
|
14
|
+
# In the future, the Ruby type might also used for "precompiling" other
|
15
|
+
# formats such as ERB.
|
17
16
|
#
|
18
|
-
class Ruby <
|
17
|
+
class Ruby < AbstractTemplate
|
19
18
|
|
20
|
-
|
19
|
+
file_extension 'rb'
|
21
20
|
|
22
21
|
#
|
23
|
-
def rb(*)
|
24
|
-
|
25
|
-
|
26
|
-
#
|
27
|
-
def to_rb(*) ; self ; end
|
28
|
-
alias_method :to_ruby, :to_rb
|
29
|
-
|
30
|
-
#
|
31
|
-
def to(type, data=nil, &yld)
|
32
|
-
new_class = Malt::Format.registry[type.to_sym] # TODO: Malt.machine.format?
|
33
|
-
new_text = render(data, &yld)
|
34
|
-
new_file = refile(type)
|
35
|
-
new_options = options.merge(:text=>new_text, :file=>new_file)
|
36
|
-
new_class.new(new_options)
|
22
|
+
def rb(*)
|
23
|
+
text
|
37
24
|
end
|
38
25
|
|
39
|
-
|
40
|
-
def method_missing(sym, *args, &yld)
|
41
|
-
if Malt::Format.registry.key?(sym)
|
42
|
-
return to(sym, *args, &yld).to_s
|
43
|
-
elsif md = /^to_/.match(sym.to_s)
|
44
|
-
type = md.post_match.to_sym
|
45
|
-
if Malt::Format.registry.key?(type) # TODO: Malt.machine.format?
|
46
|
-
return to(type, *args, &yld)
|
47
|
-
end
|
48
|
-
end
|
49
|
-
super(sym, *args, &yld)
|
50
|
-
end
|
26
|
+
alias_method :ruby, :rb
|
51
27
|
|
52
28
|
#
|
53
|
-
|
54
|
-
|
55
|
-
#end
|
56
|
-
|
57
|
-
def render(*type_and_data, &yld)
|
58
|
-
type, data = parse_type_and_data(type_and_data)
|
59
|
-
render_engine.render(:text=>text, :file=>file, :data=>data, &yld)
|
29
|
+
def to_rb(*)
|
30
|
+
self
|
60
31
|
end
|
61
32
|
|
62
|
-
|
63
|
-
|
64
|
-
#
|
65
|
-
def render_engine
|
66
|
-
@render_engine ||= Malt::Engine::Ruby.new(options)
|
67
|
-
end
|
33
|
+
alias_method :to_ruby, :to_rb
|
68
34
|
|
69
35
|
end
|
70
36
|
|
data/lib/malt/formats/sass.rb
CHANGED
@@ -6,9 +6,10 @@ module Malt::Format
|
|
6
6
|
|
7
7
|
# Sass Format
|
8
8
|
#
|
9
|
+
#
|
9
10
|
class Sass < Abstract
|
10
11
|
|
11
|
-
|
12
|
+
file_extension 'sass'
|
12
13
|
|
13
14
|
#
|
14
15
|
def sass(*)
|
@@ -21,13 +22,14 @@ module Malt::Format
|
|
21
22
|
end
|
22
23
|
|
23
24
|
#
|
24
|
-
def css(data
|
25
|
-
|
25
|
+
def css(*data, &content)
|
26
|
+
render_into(:css, *data, &content)
|
27
|
+
#render_engine.render(:format=>:css, :text=>text, :file=>file, :data=>data, :type=>type, &yld)
|
26
28
|
end
|
27
29
|
|
28
30
|
#
|
29
|
-
def to_css(data
|
30
|
-
result = css(data, &
|
31
|
+
def to_css(*data, &content)
|
32
|
+
result = css(*data, &content)
|
31
33
|
CSS.new(:text=>result, :file=>refile(:css), :type=>:css)
|
32
34
|
end
|
33
35
|
|
@@ -38,27 +40,17 @@ module Malt::Format
|
|
38
40
|
# CSS.new(opts)
|
39
41
|
#end
|
40
42
|
|
41
|
-
|
42
|
-
#def render_to(to, db, &yld)
|
43
|
-
# case to
|
44
|
-
# when :css
|
45
|
-
# malt_engine.render_css(text, file, db, &yld)
|
46
|
-
# else
|
47
|
-
# raise UnspportedConversion.new(type, to)
|
48
|
-
# end
|
49
|
-
#end
|
50
|
-
|
51
|
-
private
|
43
|
+
private
|
52
44
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
45
|
+
##
|
46
|
+
#def render_engine
|
47
|
+
# @render_engine ||= Malt::Engine::Sass.new(options)
|
48
|
+
#end
|
57
49
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
50
|
+
# Sass default output type is CSS.
|
51
|
+
def default
|
52
|
+
:css
|
53
|
+
end
|
62
54
|
|
63
55
|
end
|
64
56
|
|
data/lib/malt/formats/scss.rb
CHANGED
@@ -9,7 +9,7 @@ module Malt::Format
|
|
9
9
|
# This uses the same engine as Sass.
|
10
10
|
class SCSS < Abstract
|
11
11
|
|
12
|
-
|
12
|
+
file_extension 'scss'
|
13
13
|
|
14
14
|
#
|
15
15
|
def scss(*)
|
@@ -22,44 +22,27 @@ module Malt::Format
|
|
22
22
|
end
|
23
23
|
|
24
24
|
#
|
25
|
-
def css(data
|
26
|
-
|
25
|
+
def css(*data, &content)
|
26
|
+
render_into(:css, *data, &content)
|
27
27
|
end
|
28
28
|
|
29
29
|
#
|
30
|
-
def to_css(data
|
31
|
-
result = css(data, &
|
30
|
+
def to_css(*data, &content)
|
31
|
+
result = css(*data, &content)
|
32
32
|
CSS.new(:text=>result, :file=>refile(:css), :type=>:css)
|
33
33
|
end
|
34
34
|
|
35
|
-
|
36
|
-
#def compile(db, &yld)
|
37
|
-
# result = render_engine.render(text, db, &yld)
|
38
|
-
# opts = options.merge(:text=>result, file=>refile(:css))
|
39
|
-
# CSS.new(opts)
|
40
|
-
#end
|
35
|
+
private
|
41
36
|
|
42
|
-
|
43
|
-
#def
|
44
|
-
#
|
45
|
-
# when :css
|
46
|
-
# malt_engine.render_css(text, file, db, &yld)
|
47
|
-
# else
|
48
|
-
# raise UnspportedConversion.new(type, to)
|
49
|
-
# end
|
37
|
+
##
|
38
|
+
#def render_engine
|
39
|
+
# @render_engine ||= Malt::Engine::Sass.new(options)
|
50
40
|
#end
|
51
41
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
@render_engine ||= Malt::Engine::Sass.new(options)
|
57
|
-
end
|
58
|
-
|
59
|
-
# Sass default output type is CSS.
|
60
|
-
def default
|
61
|
-
:css
|
62
|
-
end
|
42
|
+
# Sass default output type is CSS.
|
43
|
+
def default
|
44
|
+
:css
|
45
|
+
end
|
63
46
|
|
64
47
|
end
|
65
48
|
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'malt/formats/abstract'
|
2
|
+
require 'malt/formats/html'
|
3
|
+
require 'malt/engines/erb'
|
4
|
+
|
5
|
+
module Malt::Format
|
6
|
+
|
7
|
+
# Yes, pure Ruby as a template format.
|
8
|
+
#
|
9
|
+
# The ruby code is run through eval and whatever is returned is given
|
10
|
+
# as the rendering.
|
11
|
+
#
|
12
|
+
# The Ruby format is a *polyglot* format --it accepts all conversion
|
13
|
+
# types and assumes the end-user knows it will be the result.
|
14
|
+
#
|
15
|
+
# The Ruby type is also used for "precompiling" other formats such
|
16
|
+
# as ERB.
|
17
|
+
#
|
18
|
+
class String < AbstractTemplate
|
19
|
+
|
20
|
+
file_extension 'str'
|
21
|
+
|
22
|
+
#
|
23
|
+
def string(*) ; text ; end
|
24
|
+
|
25
|
+
#
|
26
|
+
def to_string(*) ; self ; end
|
27
|
+
|
28
|
+
=begin
|
29
|
+
#
|
30
|
+
def to(type, *data, &yld)
|
31
|
+
new_class = Malt::Format.registry[type.to_sym] # TODO: Malt.machine.format?
|
32
|
+
new_text = render(*data, &yld)
|
33
|
+
new_file = refile(type)
|
34
|
+
new_options = options.merge(:text=>new_text, :file=>new_file)
|
35
|
+
new_class.new(new_options)
|
36
|
+
end
|
37
|
+
=end
|
38
|
+
|
39
|
+
# # Ruby templates can be any type.
|
40
|
+
# def method_missing(sym, *args, &yld)
|
41
|
+
# if Malt::Format.registry.key?(sym)
|
42
|
+
# return to(sym, *args, &yld).to_s
|
43
|
+
# elsif md = /^to_/.match(sym.to_s)
|
44
|
+
# type = md.post_match.to_sym
|
45
|
+
# if Malt::Format.registry.key?(type) # TODO: Malt.machine.format?
|
46
|
+
# return to(type, *args, &yld)
|
47
|
+
# end
|
48
|
+
# end
|
49
|
+
# super(sym, *args, &yld)
|
50
|
+
# end
|
51
|
+
|
52
|
+
#private
|
53
|
+
|
54
|
+
##
|
55
|
+
#def render_engine
|
56
|
+
# @render_engine ||= Malt::Engine::String.new(options)
|
57
|
+
#end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
data/lib/malt/formats/tenjin.rb
CHANGED
@@ -25,13 +25,13 @@ module Malt::Format
|
|
25
25
|
alias_method(:to_ruby, :to_rb)
|
26
26
|
|
27
27
|
#
|
28
|
-
def html(data
|
29
|
-
|
28
|
+
def html(*data, &content)
|
29
|
+
render_into(:html, *data, &content)
|
30
30
|
end
|
31
31
|
|
32
32
|
#
|
33
|
-
def to_html(data
|
34
|
-
new_text = render(:html, data, &yld)
|
33
|
+
def to_html(*data, &yld)
|
34
|
+
new_text = render(:html, *data, &yld)
|
35
35
|
new_file = refile(:html)
|
36
36
|
new_options = options.merge(:text=>new_text, :file=>new_file, :type=>:html)
|
37
37
|
HTML.new(new_options)
|
data/lib/malt/formats/text.rb
CHANGED
data/lib/malt/formats/textile.rb
CHANGED
@@ -6,7 +6,7 @@ module Malt::Format
|
|
6
6
|
#
|
7
7
|
class Textile < Abstract
|
8
8
|
|
9
|
-
|
9
|
+
file_extension 'textile', 'tt'
|
10
10
|
|
11
11
|
#
|
12
12
|
def textile(*)
|
@@ -24,8 +24,8 @@ module Malt::Format
|
|
24
24
|
alias_method :to_tt, :to_textile
|
25
25
|
|
26
26
|
#
|
27
|
-
def html(*)
|
28
|
-
|
27
|
+
def html(*data, &content)
|
28
|
+
render_into(:html, *data, &content)
|
29
29
|
end
|
30
30
|
|
31
31
|
#
|
@@ -34,25 +34,13 @@ module Malt::Format
|
|
34
34
|
HTML.new(opts)
|
35
35
|
end
|
36
36
|
|
37
|
+
#private
|
37
38
|
#
|
38
|
-
|
39
|
-
#
|
40
|
-
#
|
41
|
-
# self
|
42
|
-
# when :html
|
43
|
-
# malt_engine.render_html(text, file)
|
44
|
-
# else
|
45
|
-
# raise "can't render textile to #{to} type" #?
|
46
|
-
# end
|
39
|
+
##
|
40
|
+
#def render_engine
|
41
|
+
# @render_engine ||= Malt::Engine::RedCloth.new(options)
|
47
42
|
#end
|
48
43
|
|
49
|
-
private
|
50
|
-
|
51
|
-
#
|
52
|
-
def render_engine
|
53
|
-
@render_engine ||= Malt::Engine::RedCloth.new(options)
|
54
|
-
end
|
55
|
-
|
56
44
|
end
|
57
45
|
|
58
46
|
end
|
data/lib/malt/kernel.rb
CHANGED
data/lib/malt/machine.rb
CHANGED
@@ -5,19 +5,19 @@ module Malt
|
|
5
5
|
# which engines and formats are used for rendering.
|
6
6
|
#--
|
7
7
|
# TODO: Can we dynamically generate the MARKUP and TEMPLATE constants
|
8
|
-
# from the format classes? In anycase, the still
|
8
|
+
# from the format classes? In anycase, the still needs tweaking.
|
9
9
|
#++
|
10
10
|
class Machine
|
11
11
|
|
12
12
|
# List of markup types. These are formats that just allow markup transformations
|
13
13
|
# and do not provide for data injection.
|
14
|
-
MARKUP = [:rdoc, :markdown, :textile, :scss, :sass, :less, :css, :html, :xml]
|
14
|
+
MARKUP = [:rdoc, :markdown, :textile, :scss, :sass, :less, :css, :html, :xml, :wikicloth]
|
15
15
|
|
16
16
|
# List of template types. These are template formats that provide data injection.
|
17
|
-
|
17
|
+
TEMPLATE = [:erb, :liquid, :mustache, :tenjin, :ragtag, :radius, :erector, :builder, :ruby, :string]
|
18
18
|
|
19
19
|
# Template types that prevent arbitrary Ruby code execution.
|
20
|
-
|
20
|
+
TEMPLATE_SAFE = [:liquid, :mustache]
|
21
21
|
|
22
22
|
# New Malt Machine.
|
23
23
|
#
|
@@ -26,7 +26,7 @@ module Malt
|
|
26
26
|
#
|
27
27
|
def initialize(config={})
|
28
28
|
if priority = config[:priority]
|
29
|
-
priority.map
|
29
|
+
priority = priority.map{ |e| e.to_sym }
|
30
30
|
else
|
31
31
|
priority = []
|
32
32
|
end
|
@@ -68,6 +68,7 @@ module Malt
|
|
68
68
|
end
|
69
69
|
@priority
|
70
70
|
end
|
71
|
+
alias_method :prefer, :priority
|
71
72
|
|
72
73
|
#
|
73
74
|
def engine?(ext)
|
@@ -92,7 +93,7 @@ module Malt
|
|
92
93
|
type = options[:type] || options[:format] || File.extname(file)
|
93
94
|
type = ext_to_type(type)
|
94
95
|
malt_class = formats[type]
|
95
|
-
raise "
|
96
|
+
raise "unknown type -- #{type}" unless malt_class
|
96
97
|
malt_class.new(options.merge(:file=>file,:type=>type))
|
97
98
|
end
|
98
99
|
|
@@ -110,11 +111,14 @@ module Malt
|
|
110
111
|
end
|
111
112
|
|
112
113
|
# Open a URL as a Malt Format object.
|
114
|
+
#
|
115
|
+
# @return [Malt::Format]
|
113
116
|
def open(url, options={})
|
114
117
|
require 'open-uri'
|
115
118
|
text = open(url).read
|
116
119
|
file = File.basename(url) # parse better with URI.parse
|
117
|
-
|
120
|
+
options[:file] = file
|
121
|
+
text(text, options)
|
118
122
|
end
|
119
123
|
|
120
124
|
# Render template directly.
|
@@ -126,14 +130,67 @@ module Malt
|
|
126
130
|
# parameters[:engine] - Force the use of a this specific engine.
|
127
131
|
# parameters[:to] - Format to convert to (usual default is `html`).
|
128
132
|
#
|
129
|
-
def render(parameters={}, &
|
133
|
+
def render(parameters={}, &content)
|
134
|
+
parameters = normalize_parameters(parameters)
|
135
|
+
|
136
|
+
if parameters[:multi]
|
137
|
+
multi_render(parameters, &content)
|
138
|
+
else
|
139
|
+
single_render(parameters, &content)
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
private
|
144
|
+
|
145
|
+
# Normalize parameters.
|
146
|
+
def normalize_parameters(parameters)
|
147
|
+
params = parameters.rekey
|
148
|
+
|
149
|
+
if data = params.delete(:data)
|
150
|
+
scope, locals = split_data(data)
|
151
|
+
params[:scope] ||= scope
|
152
|
+
params[:locals] ||= locals
|
153
|
+
end
|
154
|
+
|
155
|
+
file = params[:file]
|
156
|
+
text = params[:text]
|
157
|
+
|
158
|
+
unless text
|
159
|
+
params[:text] = file_read(file)
|
160
|
+
end
|
161
|
+
|
162
|
+
params
|
163
|
+
end
|
164
|
+
|
165
|
+
#
|
166
|
+
def multi_render(parameters={}, &content)
|
167
|
+
type = parameters[:type]
|
168
|
+
#file = parameters[:file]
|
169
|
+
text = parameters[:text]
|
170
|
+
|
171
|
+
if type = parameters[:type]
|
172
|
+
types = [type].flatten
|
173
|
+
else
|
174
|
+
types = file.split('.')[1..-1]
|
175
|
+
end
|
176
|
+
|
177
|
+
types.reverse_each do |t|
|
178
|
+
parameters[:type] = t
|
179
|
+
parameters[:text] = single_render(parameters, &content)
|
180
|
+
end
|
181
|
+
|
182
|
+
parameters[:text]
|
183
|
+
end
|
184
|
+
|
185
|
+
#
|
186
|
+
def single_render(parameters={}, &content)
|
130
187
|
type = parameters[:type]
|
131
188
|
file = parameters[:file]
|
132
189
|
text = parameters[:text]
|
133
190
|
engine = parameters[:engine]
|
134
191
|
|
135
192
|
type = file_type(file, type)
|
136
|
-
text = file_read(file) unless text
|
193
|
+
#text = file_read(file) unless text
|
137
194
|
|
138
195
|
engine_class = engine(type, engine)
|
139
196
|
|
@@ -142,22 +199,22 @@ module Malt
|
|
142
199
|
parameters[:text] = text
|
143
200
|
|
144
201
|
engine = engine_class.new
|
145
|
-
|
202
|
+
|
203
|
+
engine.render(parameters, &content)
|
146
204
|
else
|
147
205
|
if parameters[:pass]
|
148
206
|
text
|
149
207
|
else
|
150
208
|
raise NoEngineError, "no engine to handle `#{type}' format"
|
151
209
|
end
|
152
|
-
end
|
210
|
+
end
|
153
211
|
end
|
154
212
|
|
155
|
-
private
|
156
|
-
|
157
213
|
#
|
158
214
|
def engine(type, engine=nil)
|
159
215
|
type = ext_to_type(type)
|
160
216
|
#engine = engine || Malt.config.engine[type] # FIXME
|
217
|
+
|
161
218
|
case engine
|
162
219
|
when Class
|
163
220
|
#raise unless Engine.registry[type].include?(engine)
|
@@ -218,6 +275,19 @@ module Malt
|
|
218
275
|
end
|
219
276
|
end
|
220
277
|
|
278
|
+
# Unlike +#scope_and_locals+, this method returns +nil+ for missing
|
279
|
+
# scope or locals.
|
280
|
+
#
|
281
|
+
def split_data(data)
|
282
|
+
scope, locals = *[data].flatten
|
283
|
+
if scope.respond_to?(:to_hash)
|
284
|
+
locals ||= {}
|
285
|
+
locals = locals.merge(scope.to_hash)
|
286
|
+
scope = nil
|
287
|
+
end
|
288
|
+
return scope, locals
|
289
|
+
end
|
290
|
+
|
221
291
|
end
|
222
292
|
|
223
293
|
#
|