pivotal-erector 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. data/README.txt +81 -0
  2. data/VERSION.yml +4 -0
  3. data/bin/erect +7 -0
  4. data/lib/erector/erect.rb +148 -0
  5. data/lib/erector/erected.rb +63 -0
  6. data/lib/erector/extensions/object.rb +18 -0
  7. data/lib/erector/indenting.rb +36 -0
  8. data/lib/erector/rails/extensions/action_controller/1.2.5/action_controller.rb +17 -0
  9. data/lib/erector/rails/extensions/action_controller/2.2.0/action_controller.rb +26 -0
  10. data/lib/erector/rails/extensions/action_controller.rb +8 -0
  11. data/lib/erector/rails/extensions/action_view.rb +21 -0
  12. data/lib/erector/rails/extensions/widget/1.2.5/widget.rb +18 -0
  13. data/lib/erector/rails/extensions/widget/2.2.0/widget.rb +36 -0
  14. data/lib/erector/rails/extensions/widget.rb +117 -0
  15. data/lib/erector/rails/supported_rails_versions.rb +14 -0
  16. data/lib/erector/rails/template_handlers/1.2.5/action_view_template_handler.rb +32 -0
  17. data/lib/erector/rails/template_handlers/2.0.0/action_view_template_handler.rb +36 -0
  18. data/lib/erector/rails/template_handlers/2.1.0/action_view_template_handler.rb +31 -0
  19. data/lib/erector/rails/template_handlers/2.2.0/action_view_template_handler.rb +46 -0
  20. data/lib/erector/rails/template_handlers/action_view_template_handler.rb +14 -0
  21. data/lib/erector/rails.rb +6 -0
  22. data/lib/erector/raw_string.rb +8 -0
  23. data/lib/erector/rhtml.treetop +156 -0
  24. data/lib/erector/unicode.rb +18185 -0
  25. data/lib/erector/unicode_builder.rb +67 -0
  26. data/lib/erector/version.rb +10 -0
  27. data/lib/erector/widget.rb +510 -0
  28. data/lib/erector/widgets/table.rb +96 -0
  29. data/lib/erector/widgets.rb +2 -0
  30. data/lib/erector.rb +16 -0
  31. data/spec/core_spec_suite.rb +3 -0
  32. data/spec/erect/erect_spec.rb +145 -0
  33. data/spec/erect/erected_spec.rb +80 -0
  34. data/spec/erect/rhtml_parser_spec.rb +318 -0
  35. data/spec/erector/indentation_spec.rb +136 -0
  36. data/spec/erector/unicode_builder_spec.rb +75 -0
  37. data/spec/erector/widget_spec.rb +657 -0
  38. data/spec/erector/widgets/table_spec.rb +99 -0
  39. data/spec/rails_spec_suite.rb +3 -0
  40. data/spec/spec_helper.rb +54 -0
  41. data/spec/spec_suite.rb +45 -0
  42. metadata +118 -0
@@ -0,0 +1,36 @@
1
+ module ActionView #:nodoc:
2
+ module TemplateHandlers #:nodoc:
3
+ class Erector < TemplateHandler
4
+ def self.line_offset
5
+ 2
6
+ end
7
+
8
+ attr_reader :view
9
+ def initialize(view)
10
+ @view = view
11
+ end
12
+
13
+ def compilable?
14
+ true
15
+ end
16
+ undef :compile
17
+
18
+ ActionView::Base.register_template_handler :rb, ActionView::TemplateHandlers::Erector
19
+
20
+ def render(template, local_assigns)
21
+ relative_path_parts = view.first_render.split('/')
22
+ file_name = relative_path_parts.last
23
+ require_dependency(view.template_file_path)
24
+
25
+ dot_rb = /\.rb$/
26
+ widget_class = relative_path_parts.inject(Views) do |mod, node|
27
+ mod.const_get(node.gsub(dot_rb, '').gsub(".html", "").camelize)
28
+ end
29
+ render_method = view.is_partial_template? ? 'render_partial' : 'render'
30
+ widget = widget_class.new(view, view.assigns)
31
+ widget.to_s(render_method)
32
+ end
33
+ end
34
+ end
35
+ end
36
+
@@ -0,0 +1,31 @@
1
+ module ActionView #:nodoc:
2
+ module TemplateHandlers #:nodoc:
3
+ class Erector < TemplateHandler
4
+ include Compilable
5
+ def self.line_offset
6
+ 2
7
+ end
8
+
9
+ ActionView::Template.instance_eval do
10
+ register_template_handler :rb, ActionView::TemplateHandlers::Erector
11
+ end
12
+
13
+ def compile(template)
14
+ relative_path_parts = template.path.split('/')
15
+
16
+ is_partial = relative_path_parts.last =~ /^_/
17
+ require_dependency File.expand_path(template.filename)
18
+
19
+ widget_class_parts = relative_path_parts.inject(['Views']) do |class_parts, node|
20
+ class_parts << node.gsub(/^_/, "").gsub(/(\.html)?\.rb$/, '').camelize
21
+ class_parts
22
+ end
23
+ widget_class_name = widget_class_parts.join("::")
24
+ render_method = is_partial ? 'render_partial' : 'render'
25
+
26
+ erb_template = "<% #{widget_class_name}.new(self, controller.assigns, _erbout).#{render_method} %>"
27
+ ::ERB.new(erb_template, nil, ActionView::Base.erb_trim_mode).src
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,46 @@
1
+ module ActionView #:nodoc:
2
+ module TemplateHandlers #:nodoc:
3
+ class Erector < TemplateHandler
4
+ include Compilable
5
+ def self.line_offset
6
+ 2
7
+ end
8
+
9
+ ActionView::Template.instance_eval do
10
+ register_template_handler :rb, ActionView::TemplateHandlers::Erector
11
+ end
12
+
13
+ def compile(template)
14
+ relative_path_parts = template.path.split('/')
15
+
16
+ is_partial = relative_path_parts.last =~ /^_/
17
+ require_dependency File.expand_path(template.filename)
18
+
19
+ widget_class_parts = relative_path_parts.inject(['Views']) do |class_parts, node|
20
+ class_parts << node.gsub(/^_/, "").gsub(/(\.html)?\.rb$/, '').camelize
21
+ class_parts
22
+ end
23
+ widget_class_name = widget_class_parts.join("::")
24
+ render_method = is_partial ? 'render_partial' : 'render'
25
+
26
+ erb_template = <<-ERB
27
+ <%
28
+ assigns = instance_variables.inject({}) do |hash, name|
29
+ hash[name.sub('@', "")] = instance_variable_get(name)
30
+ hash
31
+ end
32
+
33
+ widget = #{widget_class_name}.new(self, assigns, output_buffer)
34
+ widget.#{render_method}
35
+ %>
36
+ ERB
37
+ ::ERB.new(
38
+ erb_template,
39
+ nil,
40
+ ::ActionView::TemplateHandlers::ERB.erb_trim_mode,
41
+ "@output_buffer"
42
+ ).src
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,14 @@
1
+ dir = File.dirname(__FILE__)
2
+ if ActionView.const_defined?(:TemplateHandlers)
3
+ if ::ActionView::TemplateHandlers::const_defined?(:Compilable)
4
+ if ActionView.const_defined?(:TemplateHandlers) && ::ActionView::TemplateHandlers::ERB.respond_to?(:erb_trim_mode)
5
+ require File.expand_path("#{dir}/2.2.0/action_view_template_handler")
6
+ else
7
+ require File.expand_path("#{dir}/2.1.0/action_view_template_handler")
8
+ end
9
+ else
10
+ require File.expand_path("#{dir}/2.0.0/action_view_template_handler")
11
+ end
12
+ else
13
+ require File.expand_path("#{dir}/1.2.5/action_view_template_handler")
14
+ end
@@ -0,0 +1,6 @@
1
+ dir = File.dirname(__FILE__)
2
+ require "action_controller"
3
+ require "#{dir}/rails/extensions/widget"
4
+ require "#{dir}/rails/extensions/action_controller"
5
+ require "#{dir}/rails/extensions/action_view"
6
+ require "#{dir}/rails/template_handlers/action_view_template_handler"
@@ -0,0 +1,8 @@
1
+ module Erector
2
+ # A string that has a special type so Erector knows to render it directly, not HTML-escaped
3
+ class RawString < String
4
+ def html_escape
5
+ self
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,156 @@
1
+ grammar Rhtml
2
+
3
+ rule doc
4
+ space node space x:doc? <Erector::Indenting> {
5
+ def convert
6
+ if x.empty?
7
+ node.convert
8
+ else
9
+ node.convert + x.convert
10
+ end
11
+ end
12
+ }
13
+ end
14
+
15
+ rule node
16
+ hprintlet / printlet / scriptlet / doctype / self_closing_tag / imgtag / closetag / opentag / text
17
+ end
18
+
19
+ rule scriptlet
20
+ '<%' space code space '%>' <Erector::Indenting> {
21
+ def convert
22
+ text = code.text_value.strip
23
+ if text =~ /\bdo( |.*|)?$/
24
+ line_in text
25
+ elsif text == "end"
26
+ line_out text
27
+ else
28
+ line text
29
+ end
30
+ end
31
+ }
32
+ end
33
+
34
+ rule printlet
35
+ '<%=' space code space '%>' <Erector::Indenting> {
36
+ def convert
37
+ line "rawtext #{code.convert}"
38
+ end
39
+ }
40
+ end
41
+
42
+ rule hprintlet
43
+ '<%=' space 'h' ' '+ code space '%>' <Erector::Indenting> {
44
+ def convert
45
+ line "text #{code.convert}"
46
+ end
47
+ }
48
+ end
49
+
50
+ rule code
51
+ (('%' !'>') / [^%])* <Erector::Indenting> {
52
+ def convert
53
+ code = text_value.strip
54
+ # matches a word, followed by either a word, a string, or a symbol
55
+ result = code.gsub(/^(\w+) ([\w:"'].*)$/, '\1(\2)')
56
+
57
+ # Convert yield, for layouts
58
+ #result.gsub!(/^yield$/, '@content')
59
+
60
+ result
61
+ end
62
+ }
63
+ end
64
+
65
+ rule doctype
66
+ '<!DOCTYPE' [^>]* '>' <Erector::Indenting> {
67
+ def convert
68
+ line "rawtext '#{text_value}'"
69
+ end
70
+ }
71
+ end
72
+
73
+ rule tagname
74
+ [A-Za-z0-9_:-]+
75
+ end
76
+
77
+ rule self_closing_tag
78
+ '<' tag_name:tagname attrs:attributes? space '/>' <Erector::Indenting> {
79
+ def convert
80
+ line "#{tag_name.text_value}#{attrs.blank? ? "" : attrs.convert}"
81
+ end
82
+ }
83
+ end
84
+
85
+ rule opentag
86
+ '<' tag_name:tagname attrs:attributes? space '>' <Erector::Indenting> {
87
+ def convert
88
+ line_in "#{tag_name.text_value}#{attrs.blank? ? "" : attrs.convert} do"
89
+ end
90
+ }
91
+ end
92
+
93
+ rule imgtag
94
+ '<' tag_name:'img' attrs:attributes? space '>' <Erector::Indenting> {
95
+ def convert
96
+ line "#{tag_name.text_value}#{attrs.blank? ? "" : attrs.convert}"
97
+ end
98
+ }
99
+ end
100
+
101
+ rule closetag
102
+ '</' tag_name:tagname '>' <Erector::Indenting> {
103
+ def convert
104
+ line_out "end"
105
+ end
106
+ }
107
+ end
108
+
109
+ rule text
110
+ (([<>] !(tagname / [/%!])) / [^<>])+ <Erector::Indenting> {
111
+ def convert
112
+ stripped = text_value.strip
113
+ if stripped.blank?
114
+ ""
115
+ else
116
+ line "text '#{text_value.strip.html_unescape}'"
117
+ end
118
+ end
119
+ }
120
+ end
121
+
122
+ rule attributes
123
+ first:attribute rest:attributes* {
124
+ def convert
125
+ " " + first.convert +
126
+ if rest.blank?
127
+ ""
128
+ else
129
+ ",#{rest.elements.first.convert}" # this is hacky -- is there a better way?
130
+ end
131
+ end
132
+ }
133
+ end
134
+
135
+ rule attribute
136
+ space n:(tagname) space '=' space v:quoted space {
137
+ def convert
138
+ attr_name = (n.text_value =~ /[-:]/) ? "'#{n.text_value}'" : ":#{n.text_value}"
139
+ "#{attr_name} => '#{v.value.html_unescape.escape_single_quotes}'"
140
+ end
141
+ }
142
+ end
143
+
144
+ rule quoted
145
+ (('"' val:([^"]*) '"') / ('\'' val:([^']*) '\'')) {
146
+ def value
147
+ val.text_value
148
+ end
149
+ }
150
+ end
151
+
152
+ rule space
153
+ [ \n\t]*
154
+ end
155
+
156
+ end