fortitude 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +26 -2
  3. data/CHANGES.md +29 -0
  4. data/lib/fortitude.rb +6 -92
  5. data/lib/fortitude/doctypes.rb +6 -4
  6. data/lib/fortitude/doctypes/base.rb +6 -2
  7. data/lib/fortitude/doctypes/html4.rb +2 -1
  8. data/lib/fortitude/doctypes/html4_frameset.rb +2 -0
  9. data/lib/fortitude/doctypes/html4_strict.rb +1 -0
  10. data/lib/fortitude/doctypes/html4_tags_frameset.rb +3 -2
  11. data/lib/fortitude/doctypes/html4_tags_strict.rb +3 -2
  12. data/lib/fortitude/doctypes/html4_tags_transitional.rb +3 -2
  13. data/lib/fortitude/doctypes/html4_transitional.rb +1 -0
  14. data/lib/fortitude/doctypes/html5.rb +3 -2
  15. data/lib/fortitude/doctypes/unknown_doctype.rb +1 -0
  16. data/lib/fortitude/doctypes/xhtml10.rb +2 -1
  17. data/lib/fortitude/doctypes/xhtml10_frameset.rb +1 -0
  18. data/lib/fortitude/doctypes/xhtml10_strict.rb +1 -0
  19. data/lib/fortitude/doctypes/xhtml10_transitional.rb +1 -0
  20. data/lib/fortitude/doctypes/xhtml11.rb +2 -1
  21. data/lib/fortitude/{fortitude_ruby_ext.rb → extensions/fortitude_ruby_ext.rb} +16 -11
  22. data/lib/fortitude/extensions/native_extensions.rb +33 -0
  23. data/lib/fortitude/{assign_locals_from_template.rb.smpl → method_templates/assign_locals_from_template.rb.smpl} +0 -0
  24. data/lib/fortitude/{need_assignment_template.rb.smpl → method_templates/need_assignment_template.rb.smpl} +0 -0
  25. data/lib/fortitude/{need_method_template.rb.smpl → method_templates/need_method_template.rb.smpl} +0 -0
  26. data/lib/fortitude/method_templates/simple_template.rb +50 -0
  27. data/lib/fortitude/{tag_method_template.rb.smpl → method_templates/tag_method_template.rb.smpl} +0 -0
  28. data/lib/fortitude/{text_method_template.rb.smpl → method_templates/text_method_template.rb.smpl} +0 -0
  29. data/lib/fortitude/rails.rb +26 -0
  30. data/lib/fortitude/rails/railtie.rb +256 -0
  31. data/lib/fortitude/rails/widget_methods.rb +0 -4
  32. data/lib/fortitude/rendering_context.rb +2 -2
  33. data/lib/fortitude/support/assigns_proxy.rb +77 -0
  34. data/lib/fortitude/support/class_inheritable_attributes.rb +98 -0
  35. data/lib/fortitude/support/instance_variable_set.rb +76 -0
  36. data/lib/fortitude/support/staticized_method.rb +87 -0
  37. data/lib/fortitude/tags/partial_tag_placeholder.rb +19 -0
  38. data/lib/fortitude/tags/tag.rb +189 -0
  39. data/lib/fortitude/tags/tag_return_value.rb +13 -0
  40. data/lib/fortitude/tags/tag_store.rb +53 -0
  41. data/lib/fortitude/tags/tag_support.rb +51 -0
  42. data/lib/fortitude/tags/tags_module.rb +16 -0
  43. data/lib/fortitude/tilt.rb +17 -0
  44. data/lib/fortitude/tilt/fortitude_template.rb +13 -13
  45. data/lib/fortitude/version.rb +1 -1
  46. data/lib/fortitude/widget.rb +36 -886
  47. data/lib/fortitude/widget/around_content.rb +53 -0
  48. data/lib/fortitude/widget/capturing.rb +37 -0
  49. data/lib/fortitude/widget/content.rb +51 -0
  50. data/lib/fortitude/widget/doctypes.rb +53 -0
  51. data/lib/fortitude/widget/helpers.rb +62 -0
  52. data/lib/fortitude/widget/integration.rb +76 -0
  53. data/lib/fortitude/widget/localization.rb +63 -0
  54. data/lib/fortitude/widget/modules_and_subclasses.rb +58 -0
  55. data/lib/fortitude/widget/needs.rb +164 -0
  56. data/lib/fortitude/widget/non_rails_widget_methods.rb +13 -0
  57. data/lib/fortitude/widget/rendering.rb +93 -0
  58. data/lib/fortitude/widget/start_and_end_comments.rb +69 -0
  59. data/lib/fortitude/widget/staticization.rb +50 -0
  60. data/lib/fortitude/widget/tag_like_methods.rb +102 -0
  61. data/lib/fortitude/widget/tags.rb +55 -0
  62. data/lib/fortitude/widget/temporary_overrides.rb +28 -0
  63. data/lib/fortitude/widget/widget_class_inheritable_attributes.rb +35 -0
  64. data/lib/fortitude/widgets.rb +12 -0
  65. data/lib/fortitude_jruby_native_ext.jar +0 -0
  66. data/spec/helpers/system_helpers.rb +3 -3
  67. data/spec/system/attribute_rules_system_spec.rb +9 -9
  68. data/spec/system/doctypes_system_spec.rb +15 -0
  69. data/spec/system/escaping_system_spec.rb +2 -2
  70. data/spec/system/formatting_system_spec.rb +2 -2
  71. data/spec/system/id_uniqueness_system_spec.rb +5 -5
  72. data/spec/system/method_precedence_system_spec.rb +1 -1
  73. data/spec/system/needs_system_spec.rb +13 -0
  74. data/spec/system/setting_inheritance_system_spec.rb +20 -20
  75. data/spec/system/tag_rendering_system_spec.rb +27 -20
  76. data/spec/system/tag_rules_system_spec.rb +1 -1
  77. data/spec/system/tag_updating_system_spec.rb +3 -3
  78. data/spec/system/tilt_system_spec.rb +4 -0
  79. data/spec/system/void_tags_system_spec.rb +44 -5
  80. metadata +42 -21
  81. data/lib/fortitude/assigns_proxy.rb +0 -75
  82. data/lib/fortitude/class_inheritable_attributes.rb +0 -96
  83. data/lib/fortitude/instance_variable_set.rb +0 -74
  84. data/lib/fortitude/non_rails_widget_methods.rb +0 -15
  85. data/lib/fortitude/partial_tag_placeholder.rb +0 -17
  86. data/lib/fortitude/railtie.rb +0 -254
  87. data/lib/fortitude/simple_template.rb +0 -45
  88. data/lib/fortitude/staticized_method.rb +0 -85
  89. data/lib/fortitude/tag.rb +0 -162
  90. data/lib/fortitude/tag_return_value.rb +0 -11
  91. data/lib/fortitude/tag_store.rb +0 -48
  92. data/lib/fortitude/tag_support.rb +0 -48
  93. data/lib/fortitude/tags_module.rb +0 -14
@@ -0,0 +1,164 @@
1
+ require 'active_support'
2
+ require 'active_support/concern'
3
+
4
+ require 'fortitude/method_templates/simple_template'
5
+ require 'fortitude/support/assigns_proxy'
6
+
7
+ module Fortitude
8
+ class Widget
9
+ module Needs
10
+ extend ActiveSupport::Concern
11
+
12
+ # INTERNAL USE ONLY
13
+ REQUIRED_NEED = Object.new
14
+ # INTERNAL USE ONLY
15
+ NOT_PRESENT_NEED = Object.new
16
+
17
+ # INTERNAL USE ONLY
18
+ included do
19
+ attr_reader :_fortitude_default_assigns
20
+ end
21
+
22
+ module ClassMethods
23
+ # PUBLIC API
24
+ def needs(*names)
25
+ previous_needs = needs_as_hash
26
+ return previous_needs if names.length == 0
27
+
28
+ @this_class_needs ||= { }
29
+
30
+ with_defaults_raw = { }
31
+ with_defaults_raw = names.pop if names[-1] && names[-1].kind_of?(Hash)
32
+
33
+ names = names.map { |n| n.to_s.strip.downcase.to_sym }
34
+ with_defaults = { }
35
+ with_defaults_raw.each { |k,v| with_defaults[k.to_s.strip.downcase.to_sym] = v }
36
+
37
+ bad_names = names.select { |n| ! is_valid_ruby_method_name?(n.to_s) }
38
+ raise ArgumentError, "Needs in a Fortitude widget class must be valid Ruby method names; these are not: #{bad_names.inspect}" if bad_names.length > 0
39
+
40
+ names.each do |name|
41
+ @this_class_needs[name] = REQUIRED_NEED
42
+ end
43
+
44
+ with_defaults.each do |name, default_value|
45
+ @this_class_needs[name] = default_value.freeze
46
+ end
47
+
48
+ rebuild_needs!(:need_declared)
49
+
50
+ needs_as_hash
51
+ end
52
+
53
+ # INTERNAL USE ONLY
54
+ def is_valid_ruby_method_name?(s)
55
+ s.to_s =~ /^[A-Za-z_][A-Za-z0-9_]*[\?\!]?$/
56
+ end
57
+
58
+ # INTERNAL USE ONLY
59
+ def needs_as_hash
60
+ @_fortitude_needs_as_hash ||= begin
61
+ out = { }
62
+ out = superclass.needs_as_hash if superclass.respond_to?(:needs_as_hash)
63
+ out.merge(@this_class_needs || { })
64
+ end
65
+ end
66
+
67
+ # INTERNAL USE ONLY
68
+ def rebuild_needs!(why, klass = self)
69
+ rebuilding(:needs, why, klass) do
70
+ @_fortitude_needs_as_hash = nil
71
+ rebuild_my_needs_methods!
72
+ direct_subclasses.each { |s| s.rebuild_needs!(why, klass) }
73
+ end
74
+ end
75
+
76
+ # PUBLIC API
77
+ def extract_needed_assigns_from(input)
78
+ input = input.with_indifferent_access
79
+
80
+ out = { }
81
+ needs_as_hash.keys.each do |name|
82
+ out[name] = input[name] if input.has_key?(name)
83
+ end
84
+ out
85
+ end
86
+
87
+ # INTERNAL USE ONLY
88
+ STANDARD_INSTANCE_VARIABLE_PREFIX = "_fortitude_assign_"
89
+
90
+ # INTERNAL USE ONLY
91
+ def instance_variable_name_for_need(need_name)
92
+ effective_name = need_name.to_s
93
+ effective_name.gsub!("!", "_fortitude_bang")
94
+ effective_name.gsub!("?", "_fortitude_question")
95
+ "@" + (use_instance_variables_for_assigns ? "" : STANDARD_INSTANCE_VARIABLE_PREFIX) + effective_name
96
+ end
97
+
98
+ # INTERNAL USE ONLY
99
+ def rebuild_my_needs_methods!
100
+ n = needs_as_hash
101
+
102
+ needs_text = n.map do |need, default_value|
103
+ Fortitude::MethodTemplates::SimpleTemplate.template('need_assignment_template').result(:extra_assigns => extra_assigns,
104
+ :need => need, :has_default => (default_value != REQUIRED_NEED),
105
+ :ivar_name => instance_variable_name_for_need(need)
106
+ )
107
+ end.join("\n\n")
108
+
109
+ assign_locals_from_text = Fortitude::MethodTemplates::SimpleTemplate.template('assign_locals_from_template').result(
110
+ :extra_assigns => extra_assigns, :needs_text => needs_text)
111
+ class_eval(assign_locals_from_text)
112
+
113
+ n.each do |need, default_value|
114
+ text = Fortitude::MethodTemplates::SimpleTemplate.template('need_method_template').result(
115
+ :need => need, :ivar_name => instance_variable_name_for_need(need),
116
+ :debug => self.debug)
117
+ needs_module.module_eval(text)
118
+ end
119
+ end
120
+ private :rebuild_my_needs_methods!
121
+ end
122
+
123
+ # PUBLIC API
124
+ def shared_variables
125
+ @_fortitude_rendering_context.instance_variable_set
126
+ end
127
+
128
+ # INTERNAL USE ONLY
129
+ def instance_variable_name_for_need(need)
130
+ self.class.instance_variable_name_for_need(need)
131
+ end
132
+
133
+ # INTERNAL USE ONLY
134
+ def needs_as_hash
135
+ @_fortitude_needs_as_hash ||= self.class.needs_as_hash
136
+ end
137
+
138
+ # PUBLIC API
139
+ def assigns
140
+ @_fortitude_assigns_proxy ||= begin
141
+ keys = needs_as_hash.keys
142
+ keys |= (@_fortitude_raw_assigns.keys.map(&:to_sym)) if self.class.extra_assigns == :use
143
+
144
+ Fortitude::Support::AssignsProxy.new(self, keys)
145
+ end
146
+ end
147
+
148
+ # INTERNAL USE ONLY
149
+ def widget_extra_assigns
150
+ (@_fortitude_extra_assigns || { })
151
+ end
152
+
153
+ # INTERNAL USE ONLY
154
+ def transfer_shared_variables(*args, &block)
155
+ if self.class.implicit_shared_variable_access
156
+ @_fortitude_rendering_context.instance_variable_set.with_instance_variable_copying(self, *args, &block)
157
+ else
158
+ block.call(*args)
159
+ end
160
+ end
161
+ private :transfer_shared_variables
162
+ end
163
+ end
164
+ end
@@ -0,0 +1,13 @@
1
+ require 'active_support'
2
+
3
+ module Fortitude
4
+ class Widget
5
+ module NonRailsWidgetMethods
6
+ extend ActiveSupport::Concern
7
+
8
+ def widget_locale
9
+ nil
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,93 @@
1
+ require 'active_support'
2
+ require 'active_support/concern'
3
+
4
+ require 'fortitude/tags/partial_tag_placeholder'
5
+
6
+ module Fortitude
7
+ class Widget
8
+ module Rendering
9
+ extend ActiveSupport::Concern
10
+
11
+ # PUBLIC API
12
+ def render(*args, &block)
13
+ call_through = lambda do
14
+ @_fortitude_rendering_context.record_widget(args) do
15
+ tag_rawtext(invoke_helper(:render, *args, &block))
16
+ end
17
+ end
18
+
19
+ if self.class.enforce_element_nesting_rules && args[0].kind_of?(Hash) && args[0].has_key?(:partial)
20
+ @_fortitude_rendering_context.record_tag(self, Fortitude::Tags::PartialTagPlaceholder.instance, &call_through)
21
+ else
22
+ call_through.call
23
+ end
24
+ end
25
+
26
+ # PUBLIC API
27
+ def to_html(rendering_context)
28
+ @_fortitude_rendering_context = rendering_context
29
+ @_fortitude_output_buffer_holder = rendering_context.output_buffer_holder
30
+
31
+ block = lambda { |*args| @_fortitude_rendering_context.yield_from_widget(*args) }
32
+
33
+ rendering_context.record_widget(self) do
34
+ begin
35
+ run_content(&block)
36
+ ensure
37
+ @_fortitude_rendering_context = nil
38
+ end
39
+ end
40
+ end
41
+
42
+ # PUBLIC API
43
+ def rendering_context
44
+ @_fortitude_rendering_context
45
+ end
46
+
47
+ # PUBLIC API
48
+ def widget(w)
49
+ w.to_html(@_fortitude_rendering_context)
50
+ end
51
+
52
+ # PUBLIC API
53
+ def output_buffer
54
+ @_fortitude_output_buffer_holder.output_buffer
55
+ end
56
+
57
+ # PUBLIC API
58
+ def initialize(assigns = { })
59
+ assign_locals_from(assigns)
60
+ end
61
+
62
+ # INTERNAL USE ONLY
63
+ def _fortitude_new_buffer
64
+ _fortitude_class_for_new_buffer.new
65
+ end
66
+ private :_fortitude_new_buffer
67
+
68
+ POTENTIAL_NEW_BUFFER_CLASSES = %w{ActionView::OutputBuffer ActiveSupport::SafeBuffer String}
69
+
70
+ # INTERNAL USE ONLY
71
+ def _fortitude_class_for_new_buffer
72
+ @_fortitude_class_for_new_buffer ||= begin
73
+ out = nil
74
+ POTENTIAL_NEW_BUFFER_CLASSES.each do |class_name|
75
+ klass = eval(class_name) rescue nil
76
+ if klass
77
+ out = klass
78
+ break
79
+ end
80
+ end
81
+ raise "Huh? NONE of the following classes appear to be defined?!? #{POTENTIAL_NEW_BUFFER_CLASSES.inspect}" unless out
82
+ out
83
+ end
84
+ end
85
+ private :_fortitude_class_for_new_buffer
86
+
87
+ # PUBLIC API
88
+ def yield_from_widget(*args)
89
+ @_fortitude_rendering_context.yield_from_widget(*args)
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,69 @@
1
+ require 'active_support'
2
+ require 'active_support/concern'
3
+
4
+ module Fortitude
5
+ class Widget
6
+ module StartAndEndComments
7
+ extend ActiveSupport::Concern
8
+
9
+ # INTERNAL USE ONLY
10
+ def widget_nesting_depth
11
+ @_fortitude_widget_nesting_depth ||= @_fortitude_rendering_context.current_widget_depth
12
+ end
13
+ private :widget_nesting_depth
14
+
15
+ MAX_START_COMMENT_VALUE_STRING_LENGTH = 100
16
+ START_COMMENT_VALUE_STRING_TOO_LONG_ELLIPSIS = "...".freeze
17
+ MAX_ASSIGNS_LENGTH_BEFORE_MULTIPLE_LINES = 200
18
+ START_COMMENT_EXTRA_INDENT_FOR_NEXT_LINE = " " * 5
19
+
20
+ # INTERNAL USE ONLY
21
+ def start_and_end_comments
22
+ if self.class.start_and_end_comments
23
+ fo = self.class.format_output
24
+
25
+ comment_text = "BEGIN #{self.class.name} depth #{widget_nesting_depth}"
26
+
27
+ assign_keys = assigns.keys
28
+ if assign_keys.length > 0
29
+
30
+ assign_text = assign_keys.map do |assign|
31
+ value = assigns[assign]
32
+ out = ":#{assign} => "
33
+ out << "(DEFAULT) " if assigns.is_default?(assign)
34
+
35
+ value_string = if value.respond_to?(:to_fortitude_comment_string) then value.to_fortitude_comment_string else value.inspect end
36
+ if value_string.length > MAX_START_COMMENT_VALUE_STRING_LENGTH
37
+ value_string = value_string[0..(MAX_START_COMMENT_VALUE_STRING_LENGTH - START_COMMENT_VALUE_STRING_TOO_LONG_ELLIPSIS.length)] + START_COMMENT_VALUE_STRING_TOO_LONG_ELLIPSIS
38
+ end
39
+ out << value_string
40
+ out
41
+ end
42
+
43
+ total_length = assign_text.map(&:length).inject(0, &:+)
44
+ if total_length > MAX_ASSIGNS_LENGTH_BEFORE_MULTIPLE_LINES
45
+ newline_and_indent = "\n#{@_fortitude_rendering_context.current_indent}"
46
+ newline_and_extra_indent = newline_and_indent + START_COMMENT_EXTRA_INDENT_FOR_NEXT_LINE
47
+
48
+ comment_text << ":"
49
+ assign_text.each do |at|
50
+ comment_text << newline_and_extra_indent
51
+ comment_text << at
52
+ end
53
+ comment_text << newline_and_indent
54
+ else
55
+ comment_text << ": "
56
+ comment_text << assign_text.join(", ")
57
+ end
58
+ end
59
+ tag_comment comment_text
60
+ yield
61
+ tag_comment "END #{self.class.name} depth #{widget_nesting_depth}"
62
+ else
63
+ yield
64
+ end
65
+ end
66
+ private :start_and_end_comments
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,50 @@
1
+ require 'active_support'
2
+ require 'active_support/concern'
3
+
4
+ require 'fortitude/support/staticized_method'
5
+
6
+ module Fortitude
7
+ class Widget
8
+ module Staticization
9
+ extend ActiveSupport::Concern
10
+
11
+ module ClassMethods
12
+ # PUBLIC API
13
+ def static(*method_names)
14
+ options = method_names.extract_options!
15
+
16
+ method_names.each do |method_name|
17
+ method_name = method_name.to_sym
18
+ staticized_method = Fortitude::Support::StaticizedMethod.new(self, method_name, options)
19
+ staticized_method.create_method!
20
+ end
21
+ end
22
+ end
23
+
24
+ METHODS_TO_DISABLE_WHEN_STATIC = [ :assigns, :shared_variables ]
25
+
26
+ # INTERNAL USE ONLY
27
+ def with_staticness_enforced(static_method_name, &block)
28
+ methods_to_disable = METHODS_TO_DISABLE_WHEN_STATIC + self.class.needs_as_hash.keys
29
+ metaclass = (class << self; self; end)
30
+
31
+ methods_to_disable.each do |method_name|
32
+ metaclass.class_eval do
33
+ alias_method "_static_disabled_#{method_name}", method_name
34
+ define_method(method_name) { raise Fortitude::Errors::DynamicAccessFromStaticMethod.new(self, static_method_name, method_name) }
35
+ end
36
+ end
37
+
38
+ begin
39
+ block.call
40
+ ensure
41
+ methods_to_disable.each do |method_name|
42
+ metaclass.class_eval do
43
+ alias_method method_name, "_static_disabled_#{method_name}"
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,102 @@
1
+ require 'active_support'
2
+ require 'active_support/concern'
3
+
4
+ module Fortitude
5
+ class Widget
6
+ module TagLikeMethods
7
+ extend ActiveSupport::Concern
8
+
9
+ # From http://www.w3.org/TR/html5/syntax.html#comments:
10
+ #
11
+ # Comments must start with the four character sequence U+003C LESS-THAN SIGN, U+0021 EXCLAMATION MARK,
12
+ # U+002D HYPHEN-MINUS, U+002D HYPHEN-MINUS (<!--). Following this sequence, the comment may have text,
13
+ # with the additional restriction that the text must not start with a single ">" (U+003E) character,
14
+ # nor start with a U+002D HYPHEN-MINUS character (-) followed by a ">" (U+003E) character, nor contain
15
+ # two consecutive U+002D HYPHEN-MINUS characters (--), nor end with a U+002D HYPHEN-MINUS character (-).
16
+ # Finally, the comment must be ended by the three character sequence U+002D HYPHEN-MINUS, U+002D HYPHEN-MINUS,
17
+ # U+003E GREATER-THAN SIGN (-->).
18
+
19
+ # INTERNAL USE ONLY
20
+ def comment_escape(string)
21
+ string = "_#{string}" if string =~ /^\s*(>|->)/
22
+ string = string.gsub("--", "- - ") if string =~ /\-\-/ # don't gsub if it doesn't match to avoid generating garbage
23
+ string = "#{string}_" if string =~ /\-\s*$/i
24
+ string
25
+ end
26
+ private :comment_escape
27
+
28
+ # PUBLIC API
29
+ def tag_comment(s)
30
+ fo = self.class.format_output
31
+ @_fortitude_rendering_context.needs_newline! if fo
32
+ raise ArgumentError, "You cannot pass a block to a comment" if block_given?
33
+ tag_rawtext "<!-- "
34
+ tag_rawtext comment_escape(s)
35
+ tag_rawtext " -->"
36
+ @_fortitude_rendering_context.needs_newline! if fo
37
+ end
38
+
39
+ # PUBLIC API
40
+ def tag_javascript(content = nil, &block)
41
+ args = if content.kind_of?(Hash)
42
+ [ self.class.doctype.default_javascript_tag_attributes.merge(content) ]
43
+ elsif content
44
+ if block
45
+ raise ArgumentError, "You can't supply JavaScript content both via text and a block"
46
+ else
47
+ block = lambda { tag_rawtext content }
48
+ [ self.class.doctype.default_javascript_tag_attributes.dup ]
49
+ end
50
+ else
51
+ [ self.class.doctype.default_javascript_tag_attributes.dup ]
52
+ end
53
+
54
+ actual_block = block
55
+ if self.class.doctype.needs_cdata_in_javascript_tag?
56
+ actual_block = lambda do
57
+ tag_rawtext "\n//#{CDATA_START}\n"
58
+ block.call
59
+ tag_rawtext "\n//#{CDATA_END}\n"
60
+ end
61
+ end
62
+
63
+ @_fortitude_rendering_context.with_indenting_disabled do
64
+ script(*args, &actual_block)
65
+ end
66
+ end
67
+
68
+ %w{comment javascript}.each do |non_tag_method|
69
+ alias_method non_tag_method, "tag_#{non_tag_method}"
70
+ end
71
+
72
+ CDATA_START = "<![CDATA[".freeze
73
+ CDATA_END = "]]>".freeze
74
+
75
+ # PUBLIC API
76
+ def cdata(s = nil, &block)
77
+ if s
78
+ raise ArgumentError, "You can only pass literal text or a block, not both" if block
79
+
80
+ components = s.split("]]>")
81
+
82
+ if components.length > 1
83
+ components.each_with_index do |s, i|
84
+ this_component = s
85
+ this_component = ">#{this_component}" if i > 0
86
+ this_component = "#{this_component}]]" if i < (components.length - 1)
87
+ cdata(this_component)
88
+ end
89
+ else
90
+ tag_rawtext CDATA_START
91
+ tag_rawtext s
92
+ tag_rawtext CDATA_END
93
+ end
94
+ else
95
+ tag_rawtext CDATA_START
96
+ yield
97
+ tag_rawtext CDATA_END
98
+ end
99
+ end
100
+ end
101
+ end
102
+ end