malt 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (155) hide show
  1. data/.ruby +156 -0
  2. data/Assembly +28 -0
  3. data/COPYING.rdoc +33 -0
  4. data/Gemfile +10 -0
  5. data/HISTORY.rdoc +102 -0
  6. data/README.rdoc +16 -10
  7. data/Reapfile +4 -0
  8. data/lib/malt.rb +36 -8
  9. data/lib/malt.yml +156 -0
  10. data/lib/malt/conversions.rb +42 -0
  11. data/lib/malt/core_ext.rb +81 -3
  12. data/lib/malt/engines/abstract.rb +259 -50
  13. data/lib/malt/engines/bluecloth.rb +19 -9
  14. data/lib/malt/engines/builder.rb +93 -32
  15. data/lib/malt/engines/coffee.rb +46 -0
  16. data/lib/malt/engines/creole.rb +60 -0
  17. data/lib/malt/engines/erb.rb +69 -44
  18. data/lib/malt/engines/erector.rb +61 -30
  19. data/lib/malt/engines/erubis.rb +41 -31
  20. data/lib/malt/engines/haml.rb +13 -37
  21. data/lib/malt/engines/kramdown.rb +40 -15
  22. data/lib/malt/engines/less.rb +15 -14
  23. data/lib/malt/engines/liquid.rb +24 -14
  24. data/lib/malt/engines/markaby.rb +44 -22
  25. data/lib/malt/engines/maruku.rb +89 -0
  26. data/lib/malt/engines/mustache.rb +20 -14
  27. data/lib/malt/engines/nokogiri.rb +89 -0
  28. data/lib/malt/engines/radius.rb +72 -34
  29. data/lib/malt/engines/ragtag.rb +26 -18
  30. data/lib/malt/engines/rdiscount.rb +18 -11
  31. data/lib/malt/engines/rdoc.rb +21 -15
  32. data/lib/malt/engines/redcarpet.rb +137 -0
  33. data/lib/malt/engines/redcloth.rb +15 -7
  34. data/lib/malt/engines/ruby.rb +13 -12
  35. data/lib/malt/engines/sass.rb +30 -17
  36. data/lib/malt/engines/string.rb +36 -0
  37. data/lib/malt/engines/tenjin.rb +70 -27
  38. data/lib/malt/engines/wikicloth.rb +48 -0
  39. data/lib/malt/formats/abstract.rb +90 -29
  40. data/lib/malt/formats/abstract_template.rb +10 -8
  41. data/lib/malt/formats/builder.rb +39 -13
  42. data/lib/malt/formats/coffee.rb +54 -0
  43. data/lib/malt/formats/css.rb +3 -3
  44. data/lib/malt/formats/erb.rb +31 -66
  45. data/lib/malt/formats/haml.rb +8 -8
  46. data/lib/malt/formats/html.rb +1 -7
  47. data/lib/malt/formats/javascript.rb +27 -0
  48. data/lib/malt/formats/latex.rb +1 -1
  49. data/lib/malt/formats/less.rb +13 -11
  50. data/lib/malt/formats/liquid.rb +7 -7
  51. data/lib/malt/formats/markdown.rb +43 -44
  52. data/lib/malt/formats/mediawiki.rb +70 -0
  53. data/lib/malt/formats/mustache.rb +5 -5
  54. data/lib/malt/formats/pdf.rb +1 -7
  55. data/lib/malt/formats/radius.rb +5 -4
  56. data/lib/malt/formats/ragtag.rb +14 -13
  57. data/lib/malt/formats/rbhtml.rb +28 -20
  58. data/lib/malt/formats/rdoc.rb +5 -4
  59. data/lib/malt/formats/rhtml.rb +19 -43
  60. data/lib/malt/formats/ruby.rb +11 -45
  61. data/lib/malt/formats/sass.rb +16 -24
  62. data/lib/malt/formats/scss.rb +13 -30
  63. data/lib/malt/formats/string.rb +61 -0
  64. data/lib/malt/formats/tenjin.rb +4 -4
  65. data/lib/malt/formats/text.rb +1 -1
  66. data/lib/malt/formats/textile.rb +7 -19
  67. data/lib/malt/kernel.rb +1 -2
  68. data/lib/malt/machine.rb +83 -13
  69. data/lib/malt/tilted.rb +216 -0
  70. data/lib/malt/version.rb +21 -0
  71. data/test/helper.rb +15 -0
  72. data/test/unit/engines/case_engine_bluecloth.rb +40 -0
  73. data/test/unit/engines/case_engine_builder.rb +30 -0
  74. data/test/unit/engines/case_engine_coffee.rb +30 -0
  75. data/test/unit/engines/case_engine_creole.rb +35 -0
  76. data/test/unit/engines/case_engine_erb.rb +28 -0
  77. data/test/unit/engines/case_engine_erector.rb +36 -0
  78. data/test/unit/engines/case_engine_erubis.rb +28 -0
  79. data/test/unit/engines/case_engine_haml.rb +30 -0
  80. data/test/unit/engines/case_engine_kramdown.rb +30 -0
  81. data/test/unit/engines/case_engine_less.rb +40 -0
  82. data/test/unit/engines/case_engine_liquid.rb +28 -0
  83. data/test/unit/engines/case_engine_markaby.rb +20 -0
  84. data/test/unit/engines/case_engine_maruku.rb +30 -0
  85. data/test/unit/engines/case_engine_mustache.rb +28 -0
  86. data/test/unit/engines/case_engine_nokogiri.rb +30 -0
  87. data/test/unit/engines/case_engine_radius.rb +30 -0
  88. data/test/unit/engines/case_engine_ragtag.rb +40 -0
  89. data/test/unit/engines/case_engine_rdiscount.rb +30 -0
  90. data/test/unit/engines/case_engine_rdoc.rb +31 -0
  91. data/test/unit/engines/case_engine_redcarpet.rb +30 -0
  92. data/test/unit/engines/case_engine_redcloth.rb +31 -0
  93. data/test/unit/engines/case_engine_ruby.rb +28 -0
  94. data/test/unit/engines/case_engine_sass.rb +36 -0
  95. data/test/unit/engines/case_engine_string.rb +28 -0
  96. data/test/unit/engines/case_engine_tenjin.rb +28 -0
  97. data/test/unit/engines/case_engine_wikicloth.rb +25 -0
  98. data/test/unit/machine.rb +27 -0
  99. data/test/unit/malt.rb +12 -0
  100. metadata +364 -266
  101. data/History.rdoc +0 -64
  102. data/License.txt +0 -205
  103. data/Syckfile +0 -80
  104. data/features/consistent_rendering.feature +0 -36
  105. data/features/samples/sample.erb +0 -1
  106. data/features/samples/sample.erubis +0 -1
  107. data/features/samples/sample.liquid +0 -1
  108. data/features/samples/sample.mustache +0 -1
  109. data/features/samples/sample.radius +0 -1
  110. data/features/step_definitions/engine_steps.rb +0 -49
  111. data/features/support/loadpath.rb +0 -1
  112. data/features/support/sample_class.rb +0 -8
  113. data/lib/malt/formats/erector.rb +0 -53
  114. data/lib/malt/formats/markaby.rb +0 -53
  115. data/lib/malt/meta/data.rb +0 -26
  116. data/lib/malt/meta/package +0 -21
  117. data/lib/malt/meta/profile +0 -21
  118. data/meta/data.rb +0 -26
  119. data/meta/package +0 -21
  120. data/meta/profile +0 -21
  121. data/qed/01_overview.rdoc +0 -8
  122. data/qed/02_formats.rdoc +0 -39
  123. data/qed/03_formats/01_overview.rdoc +0 -7
  124. data/qed/03_formats/02_rdoc.rdoc +0 -83
  125. data/qed/03_formats/03_textile.rdoc +0 -48
  126. data/qed/03_formats/04_markdown.rdoc +0 -66
  127. data/qed/03_formats/05_erb.rdoc +0 -65
  128. data/qed/03_formats/06_liquid.rdoc +0 -54
  129. data/qed/03_formats/07_haml.rdoc +0 -44
  130. data/qed/03_formats/08_ragtag.rdoc +0 -19
  131. data/qed/03_formats/09_radius.rdoc +0 -43
  132. data/qed/03_formats/11_tenjin.rdoc +0 -47
  133. data/qed/03_formats/12_rbhtml.rdoc +0 -55
  134. data/qed/03_formats/13_sass.rdoc +0 -55
  135. data/qed/03_formats/14_scss.rdoc +0 -58
  136. data/qed/03_formats/15_less.rdoc +0 -46
  137. data/qed/03_formats/16_ruby.rdoc +0 -48
  138. data/qed/03_formats/17_markaby.rdoc +0 -50
  139. data/qed/03_formats/18_builder.rb +0 -50
  140. data/qed/03_formats/19_erector.rb +0 -50
  141. data/qed/03_formats/20_mustache.rdoc +0 -54
  142. data/qed/05_machine/01_limited_formats.rdoc +0 -29
  143. data/qed/05_machine/02_prioritized_engines.rdoc +0 -34
  144. data/qed/applique/malt.rb +0 -13
  145. data/qed/samples/data.yml +0 -4
  146. data/qed/samples/output-erb.txt +0 -1
  147. data/qed/samples/output-liquid.txt +0 -1
  148. data/qed/samples/output-mustache.txt +0 -1
  149. data/qed/samples/output-radius.txt +0 -1
  150. data/qed/samples/sample.erb +0 -1
  151. data/qed/samples/sample.liquid +0 -1
  152. data/qed/samples/sample.markdown +0 -308
  153. data/qed/samples/sample.mustache +0 -1
  154. data/qed/samples/sample.radius +0 -1
  155. data/qed/samples/sample.rdoc +0 -8
@@ -4,43 +4,66 @@ module Malt::Engine
4
4
 
5
5
  # Markaby
6
6
  #
7
- # http://markaby.rubyforge.org/
7
+ # @see http://markaby.rubyforge.org/
8
8
  #
9
+ # Markaby doesn't support template caching b/c the the initializer
10
+ # takes the local variable settings.
11
+ #
9
12
  class Markaby < Abstract
10
13
 
11
- default :markaby, :mab
14
+ default :markaby, :mab
15
+ register :rbml, :builder
12
16
 
13
17
  #
14
- #def intermediate(params)
15
- # text = params[:text]
16
- # eval("lambda{ #{text} }")
17
- #end
18
+ def render(params={}, &content)
19
+ into = parameters(params, :to) || :html
18
20
 
19
- #
20
- def render(params, &yld)
21
- into = params[:to]
22
21
  case into
23
- when :html, nil
24
- render_html(params, &yld)
22
+ when :html, :xml, :xhtml
23
+ prepare_engine(params, &content).to_s
25
24
  else
26
- super(params, &yld)
25
+ super(params, &content)
27
26
  end
28
27
  end
29
28
 
29
+ # TODO: Prefix support ?
30
+
30
31
  #
31
- def render_html(params={}, &yld)
32
- text = params[:text]
33
- file = params[:file]
34
- data = params[:data]
35
- data = make_hash(data, &yld)
36
- builder = ::Markaby::Builder.new(data)
37
- builder.instance_eval(text).to_s
32
+ def prepare_engine(params={}, &content)
33
+ prefix, text, file, scope, locals, prefix = parameters(params, :prefix, :text, :file, :scope, :locals)
34
+
35
+ file = file || "(markaby)"
36
+
37
+ if prefix
38
+ raise NotImplmentedError, "Markaby doesn't support prefix templates."
39
+ #scope, locals = scope_and_locals(data, &content)
40
+ #scope, locals = split_data(data)
41
+
42
+ scope ||= Object.new
43
+ locals ||= {}
44
+
45
+ mab = ::Markaby::Builder.new(locals) #, scope)
46
+
47
+ code = %{
48
+ lambda do |#{prefix}|
49
+ #{text}
50
+ end
51
+ }
52
+
53
+ eval(code, scope.to_binding, file).call(mab)
54
+ else
55
+ scope, locals = make_external(scope, locals, &content)
56
+
57
+ mab = ::Markaby::Builder.new(locals, scope)
58
+ mab.instance_eval(text, file)
59
+ mab
60
+ end
38
61
  end
39
62
 
40
- private
63
+ private
41
64
 
42
65
  # Load Markaby library if not already loaded.
43
- def initialize_engine
66
+ def require_engine
44
67
  return if defined? ::Markaby
45
68
  require_library 'markaby'
46
69
  end
@@ -48,4 +71,3 @@ module Malt::Engine
48
71
  end
49
72
 
50
73
  end
51
-
@@ -0,0 +1,89 @@
1
+ require 'malt/engines/abstract'
2
+
3
+ module Malt::Engine
4
+
5
+ # Redcarpet Markdown implementation.
6
+ #
7
+ # http://maruku.rubyforge.org/
8
+ #
9
+ class Maruku < Abstract
10
+
11
+ register :markdown, :md
12
+
13
+ # Convert Markdown text to HTML text.
14
+ #
15
+ # @param [Hash] params
16
+ #
17
+ # @option params [String] :text
18
+ # Template text
19
+ #
20
+ # @option params [String,Symbol] :to ('html')
21
+ # Type or file extension to convert template into.
22
+ #
23
+ def render(params={})
24
+ into, text, part = parameters(params, :to, :text, :partial)
25
+
26
+ engine = prepare_engine(params)
27
+
28
+ case into
29
+ when :html, nil
30
+ if part
31
+ engine.to_html
32
+ else
33
+ engine.to_html_document
34
+ end
35
+ when :latex #, :pdf
36
+ if part
37
+ engine.to_latex
38
+ else
39
+ engine.to_latex_document
40
+ end
41
+ else
42
+ super(params)
43
+ end
44
+ end
45
+
46
+ # Convert Markdown text to intermediate object.
47
+ #
48
+ # @param [Hash] params
49
+ # A hash containing the Markdown extensions which the parser
50
+ # will identify. The following extensions are accepted.
51
+ #
52
+ # @option params [Symbol] :on_error
53
+ # If :raise, then raise error.
54
+ #
55
+ def create_engine(params={})
56
+ text = parameters(params, :text)
57
+ opts = engine_options(params)
58
+
59
+ ::Maruku.new(text, opts)
60
+ end
61
+
62
+ private
63
+
64
+ # Supported engine options.
65
+ #
66
+ # @todo Add more options.
67
+ #
68
+ # @see http://maruku.rubyforge.org/exd.html
69
+ #
70
+ ENGINE_OPTION_NAMES = %w{
71
+ math_enabled
72
+ }
73
+
74
+ # Load rdoc makup library if not already loaded.
75
+ def require_engine
76
+ return if defined? ::Maruku
77
+ require_library 'maruku'
78
+ end
79
+
80
+ #
81
+ def engine_option_names
82
+ ENGINE_OPTION_NAMES
83
+ end
84
+
85
+ end
86
+
87
+ end
88
+
89
+
@@ -4,35 +4,41 @@ module Malt::Engine
4
4
 
5
5
  # Mustache engine.
6
6
  #
7
- # http://github.com/defunkt/mustache
7
+ # @see http://github.com/defunkt/mustache
8
8
  #
9
9
  class Mustache < Abstract
10
10
 
11
11
  register :mustache
12
12
 
13
- ## Convert Markdown text to intermediate object.
14
- #def intermediate(params)
15
- # text = params[:text]
16
- # ???
17
- #end
18
-
19
13
  #
20
- def render(params={}, &yld) #file, db, &yld)
21
- text = params[:text]
22
- data = params[:data]
14
+ def render(params={}, &content) #file, db, &content)
15
+ text, scope, locals = parameters(params, :text, :scope, :locals)
16
+
17
+ locals = make_hash(scope, locals, &content)
23
18
 
24
- data = make_hash(data, &yld)
19
+ # convert symbol keys to strings w/o rewriting the hash
20
+ symbol_keys = locals.keys.select{ |k| Symbol === k }
21
+ symbol_keys.each do |k|
22
+ locals[k.to_s] = locals[k]
23
+ locals.delete(k)
24
+ end
25
25
 
26
26
  #engine = intermediate(params)
27
27
  #engine.render(data)
28
- ::Mustache.render(text, data)
28
+
29
+ ::Mustache.render(text, locals)
29
30
  end
30
31
 
32
+ #
33
+ #def intermediate(params)
34
+ # text = parameters(params, :text)
35
+ # ???
36
+ #end
31
37
 
32
- private
38
+ private
33
39
 
34
40
  # Load rdoc makup library if not already loaded.
35
- def initialize_engine
41
+ def require_engine
36
42
  return if defined? ::Mustache
37
43
  require_library 'mustache'
38
44
  end
@@ -0,0 +1,89 @@
1
+ require 'malt/engines/abstract'
2
+
3
+ module Malt::Engine
4
+
5
+ # Nokogiri builder-like templates.
6
+ #
7
+ # @see http://nokogiri.org/
8
+
9
+ class Nokogiri < Abstract
10
+
11
+ default :nokogiri
12
+ register :rbml, :builder
13
+
14
+ #
15
+ DOCUMENT_HEADER_XML = /^<\?xml version=\"1\.0\"\?>\n?/
16
+
17
+ #
18
+ DOCUMENT_HEADER_HTML = /^<\!DOCTYPE html PUBLIC \".*?\">/
19
+
20
+ #
21
+ def render(params={}, &content)
22
+ into = parameters(params, :to) || :html
23
+
24
+ case into.to_sym
25
+ when :html
26
+ prepare_engine(params, &content).to_html.sub(DOCUMENT_HEADER_HTML,'')
27
+ when :xml, :xhtml
28
+ prepare_engine(params, &content).to_xml.sub(DOCUMENT_HEADER_XML,'')
29
+ else
30
+ super(params, &content)
31
+ end
32
+ end
33
+
34
+ #
35
+ def prepare_engine(params={}, &content)
36
+ text, file, scope, locals = parameters(params, :text, :file, :scope, :locals)
37
+
38
+ scope, locals = make_external(scope, locals, &content)
39
+
40
+ engine = create_engine(params)
41
+
42
+ locals.each do |k,v|
43
+ engine.instance_eval("@#{k} = v")
44
+ end
45
+
46
+ scope.instance_variables.each do |k|
47
+ next if k == "@target"
48
+ v = scope.instance_variable_get(k)
49
+ engine.instance_eval("#{k} = v")
50
+ end
51
+
52
+ engine.instance_eval(text, file || inspect)
53
+
54
+ engine
55
+ end
56
+
57
+ # Nokogiri engine cannot be cached as it keeps a copy the
58
+ # rendering internally. (Unless there is a way to clear it?)
59
+ #
60
+ def create_engine(params={})
61
+ into = parameters(params, :to) || :html
62
+
63
+ opts = engine_options(params)
64
+
65
+ #cached(into, opts) do
66
+ case into
67
+ when :html
68
+ ::Nokogiri::HTML::Builder.new(opts)
69
+ else
70
+ ::Nokogiri::XML::Builder.new(opts)
71
+ end
72
+ #end
73
+ end
74
+
75
+ private
76
+
77
+ # Load Nokogiri library if not already loaded.
78
+ def require_engine
79
+ return if defined? ::Nokogiri
80
+ require_library 'nokogiri'
81
+
82
+ ::Nokogiri::XML::Builder.class_eval do
83
+ undef_method :p
84
+ end
85
+ end
86
+
87
+ end
88
+
89
+ end
@@ -11,69 +11,107 @@ module Malt::Engine
11
11
  default :radius
12
12
 
13
13
  #
14
- def render(params, &yld)
15
- text = params[:text]
16
- data = params[:data]
17
- into = params[:to]
14
+ def render(params={}, &content)
15
+ into, text = parameters(params, :to, :text)
16
+
18
17
  case into
19
18
  when :html, :xml, nil
20
- data = make_context(data, &yld)
21
- opts = engine_options(params)
22
- parser = ::Radius::Parser.new(data, opts)
19
+ context = prepare_engine(params, &content)
20
+ options = engine_options(params)
21
+
22
+ parser = ::Radius::Parser.new(context, options)
23
23
  parser.parse(text)
24
24
  else
25
- super(params, &yld)
25
+ super(params, &content)
26
26
  end
27
27
  end
28
28
 
29
- private
29
+ #
30
+ def prepare_engine(params={}, &content)
31
+ scope, locals = parameters(params, :scope, :locals)
32
+
33
+ locals ||= {}
34
+
35
+ # convert string keys to symbols w/o rewriting the hash
36
+ string_keys = locals.keys.select{ |k| String === k }
37
+ string_keys.each do |k|
38
+ locals[k.to_sym] = locals[k]
39
+ locals.delete(k)
40
+ end
41
+
42
+ make_context(scope, locals, &content)
43
+ end
44
+
45
+ private
30
46
 
31
47
  # Load Radius library if not already loaded.
32
- def initialize_engine
48
+ def require_engine
33
49
  return if defined? ::Radius
34
50
  require_library 'radius'
35
51
  end
36
52
 
37
53
  # Radius templates have a very special data source.
38
- def make_context(data, &yld)
39
- case data
40
- when Hash
41
- context = make_context_from_hash(data, &yld)
54
+ def make_context(scope, locals, &content)
55
+ case scope
56
+ when nil
57
+ context = make_context_from_hash(locals, &content)
58
+ when Binding
59
+ context = make_context_from_binding(scope, locals, &content)
42
60
  else
43
- if data.respond_to(:to_hash)
44
- data = data.to_hash
45
- context = make_context_from_hash(data, &yld)
46
- else
47
- context = make_context_from_object(data, &yld)
61
+ context = make_context_from_object(scope, locals, &content)
62
+ end
63
+ context
64
+ end
65
+
66
+ #
67
+ def make_context_from_binding(scope, locals, &content)
68
+ context_class = Class.new(::Radius::Context)
69
+ context_class.class_eval do
70
+ define_method :tag_missing do |tag, attr|
71
+ if locals.key?(tag.to_sym)
72
+ locals[tag.to_sym]
73
+ else
74
+ scope.eval(tag)
75
+ end
48
76
  end
49
77
  end
78
+ context = context_class.new
79
+ context.define_tag("content") do
80
+ content ? content.call : ''
81
+ end
50
82
  context
51
83
  end
52
84
 
53
85
  #
54
- def make_context_from_object(db, &yld)
55
- context = Class.new(::Radius::Context).new
56
- db = make_object(db)
57
- (class << context; self; end).class_eval do
86
+ def make_context_from_object(scope, locals, &content)
87
+ context_class = Class.new(::Radius::Context)
88
+ context_class.class_eval do
58
89
  define_method :tag_missing do |tag, attr|
59
- db.__send__(tag) # any way to support attr as args?
90
+ if locals.key?(tag.to_sym)
91
+ locals[tag.to_sym]
92
+ else
93
+ scope.__send__(tag) # any way to support attr as args?
94
+ end
60
95
  end
61
96
  end
62
- context.define_tag("yield") do
63
- yld.call
97
+ context = context_class.new
98
+ context.define_tag("content") do
99
+ content ? content.call : ''
64
100
  end
65
101
  context
66
102
  end
67
103
 
68
104
  #
69
- def make_context_from_hash(data, &yld)
70
- context = Class.new(::Radius::Context).new
71
- #data = make_hash(data)
72
- data.each do |tag, value|
73
- context.define_tag(tag){ value }
105
+ def make_context_from_hash(locals, &content)
106
+ context_class = Class.new(::Radius::Context)
107
+ context_class.class_eval do
108
+ define_method :tag_missing do |tag, attr|
109
+ locals[tag.to_sym]
110
+ end
74
111
  end
75
- context.define_tag("yield") do
76
- yld.call
112
+ context = context_class.new
113
+ context.define_tag("content") do
114
+ content ? content.call : ''
77
115
  end
78
116
  context
79
117
  end
@@ -81,7 +119,7 @@ module Malt::Engine
81
119
  #
82
120
  def engine_options(params)
83
121
  opts = {}
84
- opts[:tag_prefix] = params[:tag_prefix] || settings[:tag_prefix]
122
+ opts[:tag_prefix] = params[:tag_prefix] || settings[:tag_prefix] #|| 'r'
85
123
  opts
86
124
  end
87
125