malt 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. data/History.rdoc +13 -0
  2. data/License.txt +204 -0
  3. data/README.rdoc +68 -0
  4. data/bin/malt +4 -0
  5. data/features/consistent_rendering.feature +36 -0
  6. data/features/samples/sample.erb +1 -0
  7. data/features/samples/sample.erubis +1 -0
  8. data/features/samples/sample.liquid +1 -0
  9. data/features/samples/sample.mustache +1 -0
  10. data/features/samples/sample.radius +1 -0
  11. data/features/step_definitions/engine_steps.rb +49 -0
  12. data/features/support/loadpath.rb +1 -0
  13. data/features/support/sample_class.rb +8 -0
  14. data/lib/malt.rb +79 -0
  15. data/lib/malt/core_ext.rb +31 -0
  16. data/lib/malt/engines.rb +10 -0
  17. data/lib/malt/engines/abstract.rb +151 -0
  18. data/lib/malt/engines/bluecloth.rb +39 -0
  19. data/lib/malt/engines/erb.rb +84 -0
  20. data/lib/malt/engines/erubis.rb +65 -0
  21. data/lib/malt/engines/haml.rb +68 -0
  22. data/lib/malt/engines/kramdown.rb +48 -0
  23. data/lib/malt/engines/less.rb +49 -0
  24. data/lib/malt/engines/liquid.rb +40 -0
  25. data/lib/malt/engines/radius.rb +90 -0
  26. data/lib/malt/engines/rdiscount.rb +49 -0
  27. data/lib/malt/engines/rdoc.rb +46 -0
  28. data/lib/malt/engines/redcloth.rb +42 -0
  29. data/lib/malt/engines/rtals.rb +46 -0
  30. data/lib/malt/engines/ruby.rb +36 -0
  31. data/lib/malt/engines/sass.rb +50 -0
  32. data/lib/malt/engines/tenjin.rb +61 -0
  33. data/lib/malt/formats.rb +10 -0
  34. data/lib/malt/formats/abstract.rb +195 -0
  35. data/lib/malt/formats/css.rb +34 -0
  36. data/lib/malt/formats/erb.rb +102 -0
  37. data/lib/malt/formats/haml.rb +53 -0
  38. data/lib/malt/formats/html.rb +29 -0
  39. data/lib/malt/formats/latex.rb +47 -0
  40. data/lib/malt/formats/less.rb +51 -0
  41. data/lib/malt/formats/liquid.rb +53 -0
  42. data/lib/malt/formats/markdown.rb +83 -0
  43. data/lib/malt/formats/pdf.rb +29 -0
  44. data/lib/malt/formats/radius.rb +47 -0
  45. data/lib/malt/formats/rdoc.rb +43 -0
  46. data/lib/malt/formats/rtals.rb +46 -0
  47. data/lib/malt/formats/ruby.rb +71 -0
  48. data/lib/malt/formats/sass.rb +56 -0
  49. data/lib/malt/formats/tenjin.rb +50 -0
  50. data/lib/malt/formats/text.rb +54 -0
  51. data/lib/malt/formats/textile.rb +59 -0
  52. data/lib/malt/formats/yaml.rb +50 -0
  53. data/lib/malt/kernel.rb +31 -0
  54. data/lib/malt/meta/data.rb +26 -0
  55. data/lib/malt/meta/gemfile +17 -0
  56. data/lib/malt/meta/profile +21 -0
  57. data/meta/data.rb +26 -0
  58. data/meta/gemfile +17 -0
  59. data/meta/profile +21 -0
  60. data/qed/01_overview.rdoc +44 -0
  61. data/qed/applique/malt.rb +12 -0
  62. metadata +283 -0
@@ -0,0 +1,48 @@
1
+ require 'malt/engines/abstract'
2
+
3
+ module Malt::Engines
4
+
5
+ # Discount Markdown implementation.
6
+ #
7
+ # http://github.com/rtomayko/rdiscount
8
+ #
9
+ # The +:smart+ and +:filter_html+ options can be set true
10
+ # to enable those flags on the underlying RDiscount object.
11
+ class Kramdown < Abstract
12
+
13
+ register :markdown, :md
14
+
15
+ # Convert Markdown text to HTML text.
16
+ def render(params)
17
+ text = params[:text]
18
+ format = params[:format]
19
+ case format
20
+ when :html, nil
21
+ intermediate(params).to_html
22
+ when :latex
23
+ intermediate(params).to_latex
24
+ else
25
+ super(params)
26
+ end
27
+ end
28
+
29
+ # Convert Markdown text to intermediate object.
30
+ def intermediate(params)
31
+ text = params[:text]
32
+ ::Kramdown::Document.new(text)
33
+ end
34
+
35
+
36
+ private
37
+
38
+ # Load rdoc makup library if not already loaded.
39
+ def initialize_engine
40
+ return if defined? ::Kramdown
41
+ require_library 'kramdown'
42
+ end
43
+
44
+ end
45
+
46
+ end
47
+
48
+
@@ -0,0 +1,49 @@
1
+ require 'malt/engines/abstract'
2
+
3
+ module Malt::Engines
4
+
5
+ # LESS
6
+ #
7
+ # http://lesscss.org/
8
+ #
9
+ # LESS is an extension of CSS. You can write LESS code just like you would write CSS,
10
+ # except you need to compile it to CSS. That's what this class is for.
11
+ class Less < Abstract
12
+
13
+ default :less
14
+
15
+ #
16
+ def render(params)
17
+ text = params[:text]
18
+ format = params[:format]
19
+ case format
20
+ when :css, nil
21
+ intermediate(params).to_css
22
+ else
23
+ super(params)
24
+ end
25
+ end
26
+
27
+ #
28
+ def intermediate(params)
29
+ text = params[:text]
30
+ ::Less::Engine.new(text)
31
+ end
32
+
33
+ #
34
+ #def compile(text, file)
35
+ # intermediate # ??
36
+ #end
37
+
38
+ private
39
+
40
+ # Load Less library if not already loaded.
41
+ def initialize_engine
42
+ return if defined? ::Less::Engine
43
+ require_library 'less'
44
+ end
45
+
46
+ end
47
+
48
+ end
49
+
@@ -0,0 +1,40 @@
1
+ require 'malt/engines/abstract'
2
+
3
+ module Malt::Engines
4
+
5
+ # Liquid
6
+ #
7
+ # http://liquid.rubyforge.org/
8
+ #
9
+ class Liquid < Abstract
10
+
11
+ default :liquid
12
+
13
+ #
14
+ def intermediate(params)
15
+ text = params[:text]
16
+ ::Liquid::Template.parse(text)
17
+ end
18
+
19
+ #
20
+ def render(params={}, &yld) #file, db, &yld)
21
+ text = params[:text]
22
+ data = params[:data]
23
+ data = make_hash(data, &yld)
24
+ data = data.rekey{ |k| k.to_s }
25
+ engine = intermediate(params)
26
+ engine.render(data)
27
+ end
28
+
29
+ private
30
+
31
+ # Load Liquid library if not already loaded.
32
+ def initialize_engine
33
+ return if defined? ::Liquid::Template
34
+ require_library 'liquid'
35
+ end
36
+
37
+ end
38
+
39
+ end
40
+
@@ -0,0 +1,90 @@
1
+ require 'malt/engines/abstract'
2
+
3
+ module Malt::Engines
4
+
5
+ # Radius Template
6
+ #
7
+ # http://github.com/jlong/radius/
8
+ #
9
+ class Radius < Abstract
10
+
11
+ default :radius
12
+
13
+ #
14
+ def render(params, &yld)
15
+ text = params[:text]
16
+ data = params[:data]
17
+ format = params[:format]
18
+ case format
19
+ when :html, :xml, nil
20
+ data = make_context(data, &yld)
21
+ opts = engine_options(params)
22
+ parser = ::Radius::Parser.new(data, opts)
23
+ parser.parse(text)
24
+ else
25
+ super(params, &yld)
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ # Load Radius library if not already loaded.
32
+ def initialize_engine
33
+ return if defined? ::Radius
34
+ require_library 'radius'
35
+ end
36
+
37
+ # 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)
42
+ 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)
48
+ end
49
+ end
50
+ context
51
+ end
52
+
53
+ #
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
58
+ define_method :tag_missing do |tag, attr|
59
+ db.__send__(tag) # any way to support attr as args?
60
+ end
61
+ end
62
+ context.define_tag("yield") do
63
+ yld.call
64
+ end
65
+ context
66
+ end
67
+
68
+ #
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 }
74
+ end
75
+ context.define_tag("yield") do
76
+ yld.call
77
+ end
78
+ context
79
+ end
80
+
81
+ #
82
+ def engine_options(params)
83
+ opts = {}
84
+ opts[:tag_prefix] = params[:tag_prefix] || settings[:tag_prefix]
85
+ opts
86
+ end
87
+
88
+ end
89
+
90
+ end
@@ -0,0 +1,49 @@
1
+ require 'malt/engines/abstract'
2
+ require 'malt/formats/rdoc'
3
+ require 'malt/formats/html'
4
+
5
+ module Malt::Engines
6
+
7
+ # Discount Markdown implementation.
8
+ #
9
+ # http://github.com/rtomayko/rdiscount
10
+ #
11
+ # The +:smart+ and +:filter_html+ options can be set true
12
+ # to enable those flags on the underlying RDiscount object.
13
+ class RDiscount < Abstract
14
+
15
+ default :markdown, :md
16
+
17
+ # Convert Markdown text to HTML text.
18
+ def render(params)
19
+ case params[:format]
20
+ when :html, nil
21
+ intermediate(params).to_html
22
+ else
23
+ super(params)
24
+ end
25
+ end
26
+
27
+ # Convert Markdown text to intermediate engine object.
28
+ def intermediate(params)
29
+ text = params[:text]
30
+ ::RDiscount.new(text, *flags)
31
+ end
32
+
33
+ private
34
+
35
+ # Load rdoc makup library if not already loaded.
36
+ def initialize_engine
37
+ return if defined? ::RDiscount
38
+ require_library 'rdiscount'
39
+ end
40
+
41
+ #
42
+ def flags(params={})
43
+ [:smart, :filter_html].select{ |flag| params[flag] || settings[flag] }
44
+ end
45
+
46
+ end
47
+
48
+ end
49
+
@@ -0,0 +1,46 @@
1
+ require 'malt/engines/abstract'
2
+
3
+ module Malt::Engines
4
+
5
+ # RDoc template.
6
+ #
7
+ # http://rdoc.rubyforge.org/
8
+ #
9
+ # It's suggested that your program require 'rdoc/markup' and
10
+ # 'rdoc/markup/to_html' at load time when using this template
11
+ # engine.
12
+ class RDoc < Abstract
13
+
14
+ default :rdoc
15
+
16
+ # Convert rdoc text to html.
17
+ def render(params)
18
+ text = params[:text]
19
+ format = params[:format]
20
+ case format
21
+ when :html, nil
22
+ html_engine.convert(text).to_s
23
+ else
24
+ super(params)
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ # Load rdoc makup library if not already loaded.
31
+ def initialize_engine
32
+ return if defined?(::RDoc::Markup)
33
+ require 'rubygems' # hack
34
+ require_library 'rdoc/markup'
35
+ require_library 'rdoc/markup/to_html'
36
+ end
37
+
38
+ #
39
+ def html_engine
40
+ @html_engine ||= ::RDoc::Markup::ToHtml.new
41
+ end
42
+
43
+ end
44
+
45
+ end
46
+
@@ -0,0 +1,42 @@
1
+ require 'malt/engines/abstract'
2
+
3
+ module Malt::Engines
4
+
5
+ #
6
+ class RedCloth < Abstract
7
+
8
+ default :tt, :textile
9
+
10
+ # Convert textile text to html.
11
+ #
12
+ # params:
13
+ #
14
+ # :format => Symbol of the format to render [:html]
15
+ #
16
+ def render(params={})
17
+ case params[:format]
18
+ when :html, nil
19
+ intermediate(params).to_html
20
+ else
21
+ super(params)
22
+ end
23
+ end
24
+
25
+ #
26
+ def intermediate(params={})
27
+ text = params[:text]
28
+ ::RedCloth.new(text)
29
+ end
30
+
31
+ private
32
+
33
+ # Load redcloth library if not already loaded.
34
+ def initialize_engine
35
+ return if defined? ::RedCloth
36
+ require_library 'redcloth'
37
+ end
38
+
39
+ end
40
+
41
+ end
42
+
@@ -0,0 +1,46 @@
1
+ require 'malt/engines/abstract'
2
+
3
+ module Malt::Engines
4
+
5
+ # RTALS
6
+ #
7
+ # http://github.com/rubyworks/rtals
8
+ #
9
+ class RTALS < Abstract
10
+
11
+ default :rtal
12
+
13
+ #
14
+ def render(params, &yld)
15
+ text = params[:text]
16
+ file = params[:file]
17
+ data = params[:data]
18
+ format = params[:format]
19
+
20
+ case format
21
+ when :html, :xml, nil
22
+ data = make_binding(data, &yld)
23
+ intermediate(params).compile(data).to_s
24
+ else
25
+ super(params, &yld)
26
+ end
27
+ end
28
+
29
+ #
30
+ def intermediate(params)
31
+ text = params[:text]
32
+ ::RTAL.new(text)
33
+ end
34
+
35
+ private
36
+
37
+ # Load Haml library if not already loaded.
38
+ def initialize_engine
39
+ return if defined? ::RTAL
40
+ require_library 'rtals'
41
+ end
42
+
43
+ end
44
+
45
+ end
46
+
@@ -0,0 +1,36 @@
1
+ require 'malt/engines/abstract'
2
+
3
+ module Malt::Engines
4
+
5
+ # Ruby as a template engine.
6
+ #
7
+ # http://
8
+ #
9
+ class Ruby < Abstract
10
+
11
+ default :rb
12
+
13
+ #
14
+ def render(params={}, &yld)
15
+ text = params[:text]
16
+ file = params[:file]
17
+ data = params[:data]
18
+ data = make_binding(data, &yld)
19
+ eval(text, data, file)
20
+ end
21
+
22
+ # Ruby compiles to Ruby. How odd. ;)
23
+ def compile(text, file)
24
+ text
25
+ end
26
+
27
+ private
28
+
29
+ #
30
+ def initialize_engine
31
+ end
32
+
33
+ end
34
+
35
+ end
36
+