honkster-erector 0.8.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.txt +116 -0
- data/VERSION.yml +4 -0
- data/bin/erector +14 -0
- data/lib/erector.rb +34 -0
- data/lib/erector/abstract_widget.rb +172 -0
- data/lib/erector/after_initialize.rb +34 -0
- data/lib/erector/caching.rb +93 -0
- data/lib/erector/convenience.rb +58 -0
- data/lib/erector/dependencies.rb +24 -0
- data/lib/erector/dependency.rb +30 -0
- data/lib/erector/erect/erect.rb +160 -0
- data/lib/erector/erect/erected.rb +75 -0
- data/lib/erector/erect/indenting.rb +36 -0
- data/lib/erector/erect/rhtml.treetop +233 -0
- data/lib/erector/errors.rb +12 -0
- data/lib/erector/extensions/hash.rb +21 -0
- data/lib/erector/extensions/object.rb +18 -0
- data/lib/erector/externals.rb +97 -0
- data/lib/erector/html.rb +352 -0
- data/lib/erector/inline.rb +37 -0
- data/lib/erector/jquery.rb +36 -0
- data/lib/erector/mixin.rb +12 -0
- data/lib/erector/needs.rb +94 -0
- data/lib/erector/output.rb +117 -0
- data/lib/erector/rails.rb +27 -0
- data/lib/erector/rails/extensions/action_controller.rb +16 -0
- data/lib/erector/rails/extensions/rails_helpers.rb +159 -0
- data/lib/erector/rails/extensions/rails_widget.rb +126 -0
- data/lib/erector/rails/rails_form_builder.rb +24 -0
- data/lib/erector/rails/rails_version.rb +6 -0
- data/lib/erector/rails/template_handlers/ert_handler.rb +32 -0
- data/lib/erector/rails/template_handlers/rb_handler.rb +52 -0
- data/lib/erector/raw_string.rb +8 -0
- data/lib/erector/sass.rb +22 -0
- data/lib/erector/unicode.rb +18185 -0
- data/lib/erector/unicode_builder.rb +67 -0
- data/lib/erector/version.rb +12 -0
- data/lib/erector/widget.rb +54 -0
- data/lib/erector/widgets.rb +6 -0
- data/lib/erector/widgets/environment_badge.rb +29 -0
- data/lib/erector/widgets/external_renderer.rb +51 -0
- data/lib/erector/widgets/field_table.rb +110 -0
- data/lib/erector/widgets/form.rb +30 -0
- data/lib/erector/widgets/page.rb +165 -0
- data/lib/erector/widgets/table.rb +104 -0
- data/rails/init.rb +4 -0
- data/spec/erect/erect_rails_spec.rb +114 -0
- data/spec/erect/erect_spec.rb +175 -0
- data/spec/erect/erected_spec.rb +164 -0
- data/spec/erect/rhtml_parser_spec.rb +361 -0
- data/spec/erector/caching_spec.rb +269 -0
- data/spec/erector/convenience_spec.rb +259 -0
- data/spec/erector/dependency_spec.rb +67 -0
- data/spec/erector/externals_spec.rb +236 -0
- data/spec/erector/html_spec.rb +509 -0
- data/spec/erector/indentation_spec.rb +211 -0
- data/spec/erector/inline_spec.rb +94 -0
- data/spec/erector/jquery_spec.rb +35 -0
- data/spec/erector/mixin_spec.rb +65 -0
- data/spec/erector/needs_spec.rb +120 -0
- data/spec/erector/output_spec.rb +199 -0
- data/spec/erector/sample-file.txt +1 -0
- data/spec/erector/sass_spec.rb +33 -0
- data/spec/erector/unicode_builder_spec.rb +75 -0
- data/spec/erector/widget_spec.rb +250 -0
- data/spec/erector/widgets/field_table_spec.rb +133 -0
- data/spec/erector/widgets/form_spec.rb +31 -0
- data/spec/erector/widgets/page_spec.rb +85 -0
- data/spec/erector/widgets/table_spec.rb +99 -0
- data/spec/spec_helper.rb +95 -0
- metadata +191 -0
@@ -0,0 +1,104 @@
|
|
1
|
+
begin
|
2
|
+
require "active_support/inflector"
|
3
|
+
|
4
|
+
module Erector
|
5
|
+
module Widgets #:nodoc:
|
6
|
+
|
7
|
+
|
8
|
+
# The Table widget provides the ability to render a table from a
|
9
|
+
# list of objects (one for each row).
|
10
|
+
#
|
11
|
+
# Because the default for the column titles utilizes the ActiveSupport
|
12
|
+
# Inflector#titleize method, this widget requires active_support to be loaded.
|
13
|
+
#
|
14
|
+
# class UsersTable < Erector::Widgets::Table
|
15
|
+
# column :first_name
|
16
|
+
# column :last_name
|
17
|
+
# column :email
|
18
|
+
# row_classes :even, :odd
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# widget UsersTable, :row_objects => [user_1, user_2, user_3]
|
22
|
+
class Table < Erector::Widget
|
23
|
+
ColumnDefinition = Struct.new(:id, :name, :cell_proc)
|
24
|
+
class << self
|
25
|
+
# Define a column, optionally specifying the name (the heading
|
26
|
+
# that the user sees) and a block which renders the cell given
|
27
|
+
# a row object. If the block is not specified, the cell contains
|
28
|
+
# the result of calling a method whose name is id.
|
29
|
+
#
|
30
|
+
# The name can be a string or a proc.
|
31
|
+
def column(id, name=id.to_s.titleize, &cell_proc)
|
32
|
+
cell_proc ||= proc {|object| text object.__send__(id)}
|
33
|
+
column_definitions << ColumnDefinition.new(id, name, cell_proc)
|
34
|
+
end
|
35
|
+
|
36
|
+
def column_definitions #:nodoc:
|
37
|
+
@column_definitions ||= []
|
38
|
+
end
|
39
|
+
|
40
|
+
# A list of HTML classes to apply to the rows in turn. After the
|
41
|
+
# list is exhausted, start again at the start. The most
|
42
|
+
# common use for this is to specify one class for odd rows
|
43
|
+
# and a different class for even rows.
|
44
|
+
def row_classes(*row_classes)
|
45
|
+
@row_class_list = row_classes
|
46
|
+
end
|
47
|
+
attr_reader :row_class_list
|
48
|
+
end
|
49
|
+
|
50
|
+
# The standard erector content method.
|
51
|
+
def content
|
52
|
+
table do
|
53
|
+
thead do
|
54
|
+
tr do
|
55
|
+
column_definitions.each do |column_def|
|
56
|
+
th do
|
57
|
+
if column_def.name.is_a?(Proc)
|
58
|
+
self.instance_exec(column_def.id, &column_def.name)
|
59
|
+
else
|
60
|
+
text column_def.name
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
tbody do
|
67
|
+
@row_objects.each_with_index do |object, index|
|
68
|
+
row object, index
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
protected
|
75
|
+
def row(object, index) #:nodoc:
|
76
|
+
tr(:class => row_css_class(object, index)) do
|
77
|
+
column_definitions.each do |column_def|
|
78
|
+
td do
|
79
|
+
self.instance_exec(object, &column_def.cell_proc)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
# You can override this method to provide a class for a row
|
86
|
+
# (as an alternative to calling row_classes).
|
87
|
+
def row_css_class(object, index)
|
88
|
+
cycle(index)
|
89
|
+
end
|
90
|
+
|
91
|
+
def column_definitions #:nodoc:
|
92
|
+
self.class.column_definitions
|
93
|
+
end
|
94
|
+
|
95
|
+
def cycle(index) #:nodoc:
|
96
|
+
list = self.class.row_class_list
|
97
|
+
list ? list[index % list.length] : ''
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
rescue LoadError => e
|
103
|
+
$stderr.puts "Erector::Widgets::Table requires active_support"
|
104
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
require File.expand_path("#{File.dirname(__FILE__)}/../spec_helper")
|
2
|
+
require "erector/rails"
|
3
|
+
require "rails/version"
|
4
|
+
|
5
|
+
# backport mktmpdir so this test will work on Ruby 1.8.6
|
6
|
+
unless Dir.respond_to?(:mktmpdir)
|
7
|
+
def Dir.mktmpdir(prefix_suffix=nil, tmpdir=nil)
|
8
|
+
case prefix_suffix
|
9
|
+
when nil
|
10
|
+
prefix = "d"
|
11
|
+
suffix = ""
|
12
|
+
when String
|
13
|
+
prefix = prefix_suffix
|
14
|
+
suffix = ""
|
15
|
+
when Array
|
16
|
+
prefix = prefix_suffix[0]
|
17
|
+
suffix = prefix_suffix[1]
|
18
|
+
else
|
19
|
+
raise ArgumentError, "unexpected prefix_suffix: #{prefix_suffix.inspect}"
|
20
|
+
end
|
21
|
+
tmpdir ||= Dir.tmpdir
|
22
|
+
t = Time.now.strftime("%Y%m%d")
|
23
|
+
n = nil
|
24
|
+
begin
|
25
|
+
path = "#{tmpdir}/#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}"
|
26
|
+
path << "-#{n}" if n
|
27
|
+
path << suffix
|
28
|
+
Dir.mkdir(path, 0700)
|
29
|
+
rescue Errno::EEXIST
|
30
|
+
n ||= 0
|
31
|
+
n += 1
|
32
|
+
retry
|
33
|
+
end
|
34
|
+
|
35
|
+
if block_given?
|
36
|
+
begin
|
37
|
+
yield path
|
38
|
+
ensure
|
39
|
+
FileUtils.remove_entry_secure path
|
40
|
+
end
|
41
|
+
else
|
42
|
+
path
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# Note: this is *not* inside the rails_root since we're not testing
|
48
|
+
# Erector inside a rails app. We're testing that we can use the command-line
|
49
|
+
# converter tool on a newly generated scaffold app (like we brag about in the
|
50
|
+
# user guide).
|
51
|
+
#
|
52
|
+
module Erector
|
53
|
+
describe "the Rails version" do
|
54
|
+
it "should be #{Erector::Rails::RAILS_VERSION}" do
|
55
|
+
::Rails::VERSION::STRING.should == Erector::Rails::RAILS_VERSION
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe "Erect in a Rails app" do
|
60
|
+
def run(cmd)
|
61
|
+
stderr_file = Dir.tmpdir + "/stderr.txt"
|
62
|
+
stdout = IO.popen(cmd + " 2>#{stderr_file}") do |pipe|
|
63
|
+
pipe.read
|
64
|
+
end
|
65
|
+
stderr = File.open(stderr_file) {|f| f.read}
|
66
|
+
FileUtils.rm_f(stderr_file)
|
67
|
+
if $?.exitstatus != 0
|
68
|
+
raise "Command #{cmd} failed\nDIR:\n #{Dir.getwd}\nSTDOUT:\n#{indent stdout}\nSTDERR:\n#{indent stderr}"
|
69
|
+
else
|
70
|
+
return stdout
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def indent(s)
|
75
|
+
s.gsub(/^/, ' ')
|
76
|
+
end
|
77
|
+
|
78
|
+
it "works like we say it does in the user guide" do
|
79
|
+
erector_dir = File.expand_path("#{File.dirname(__FILE__)}/../..")
|
80
|
+
|
81
|
+
# We add the paths to our vendored copy of rails to the load paths, so
|
82
|
+
# that this spec can be run without having a version of Rails (which may
|
83
|
+
# not match the version we wish to test against) installed.
|
84
|
+
rails_libs_argument = "-I'#{RAILS_LOAD_PATHS.join("':'")}'"
|
85
|
+
|
86
|
+
Dir.mktmpdir do |app_dir|
|
87
|
+
run "ruby #{rails_libs_argument} '#{VENDOR_RAILS}/railties/bin/rails' '#{app_dir}'"
|
88
|
+
|
89
|
+
FileUtils.cp_r(VENDOR_RAILS, "#{app_dir}/vendor/rails")
|
90
|
+
|
91
|
+
FileUtils.mkdir_p(app_dir + "/vendor/gems")
|
92
|
+
FileUtils.cp_r(erector_dir, "#{app_dir}/vendor/gems/erector")
|
93
|
+
|
94
|
+
FileUtils.cd(app_dir) do
|
95
|
+
run "script/generate scaffold post title:string body:text published:boolean"
|
96
|
+
|
97
|
+
# The 'erector' binary would normally have been installed through rubygems,
|
98
|
+
# providing it with a wrapper script which requires rubygems. But here we
|
99
|
+
# run it directly, so we need to require rubygems explicitly before running
|
100
|
+
# the main script.
|
101
|
+
run "ruby #{rails_libs_argument} -I'#{erector_dir}/lib' " +
|
102
|
+
"-e \"require 'rubygems'; load '#{erector_dir}/bin/erector'\" app/views/posts"
|
103
|
+
|
104
|
+
FileUtils.rm_f("app/views/posts/*.erb")
|
105
|
+
run "rake --trace db:migrate"
|
106
|
+
|
107
|
+
# run "script/server" # todo: launch in background; use mechanize or something to crawl it; then kill it
|
108
|
+
# perhaps use open4?
|
109
|
+
# open http://localhost:3000/posts
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
@@ -0,0 +1,175 @@
|
|
1
|
+
require File.expand_path("#{File.dirname(__FILE__)}/../spec_helper")
|
2
|
+
|
3
|
+
require "erector/erect/erect"
|
4
|
+
|
5
|
+
module Erector
|
6
|
+
describe Erect do
|
7
|
+
it "parses an empty command line" do
|
8
|
+
erect = Erect.new([])
|
9
|
+
erect.files.should == []
|
10
|
+
end
|
11
|
+
|
12
|
+
it "parses a command line with one filename on it" do
|
13
|
+
erect = Erect.new(["foo.html"])
|
14
|
+
erect.files.should == ["foo.html"]
|
15
|
+
end
|
16
|
+
|
17
|
+
it "parses a command line with several filenames on it" do
|
18
|
+
erect = Erect.new(["foo.html", "bar/baz.html"])
|
19
|
+
erect.files.should == ["foo.html", "bar/baz.html"]
|
20
|
+
end
|
21
|
+
|
22
|
+
it "is verbose by default, but quiet when told" do
|
23
|
+
Erect.new([]).verbose.should be_true
|
24
|
+
Erect.new(["-q"]).verbose.should be_false
|
25
|
+
end
|
26
|
+
|
27
|
+
it "sets the superclass to what you tell it" do
|
28
|
+
Erect.new([]).superklass.should == 'Erector::Widget'
|
29
|
+
Erect.new(['--superclass', 'Foo::Bar']).superklass.should == 'Foo::Bar'
|
30
|
+
end
|
31
|
+
|
32
|
+
it "sets the method name to what you tell it" do
|
33
|
+
Erect.new([]).method_name.should == 'content'
|
34
|
+
Erect.new(['--method', 'my_content']).method_name.should == 'my_content'
|
35
|
+
end
|
36
|
+
|
37
|
+
it "parses a command line with several filenames and an option on it" do
|
38
|
+
erect = Erect.new(["-q", "foo.html", "bar/baz.html"])
|
39
|
+
erect.files.should == ["foo.html", "bar/baz.html"]
|
40
|
+
end
|
41
|
+
|
42
|
+
def capturing_output
|
43
|
+
output = StringIO.new
|
44
|
+
$stdout = output
|
45
|
+
yield
|
46
|
+
output.string
|
47
|
+
ensure
|
48
|
+
$stdout = STDOUT
|
49
|
+
end
|
50
|
+
|
51
|
+
it "exits immediately from help" do
|
52
|
+
output = capturing_output do
|
53
|
+
lambda {
|
54
|
+
erect = Erect.new(["-h"])
|
55
|
+
}.should raise_error(SystemExit)
|
56
|
+
end
|
57
|
+
output.should =~ /^Usage/
|
58
|
+
end
|
59
|
+
|
60
|
+
it "exits immediately from --version" do
|
61
|
+
output = capturing_output do
|
62
|
+
lambda {
|
63
|
+
erect = Erect.new(["--version"])
|
64
|
+
}.should raise_error(SystemExit)
|
65
|
+
end
|
66
|
+
output.should == Erector::VERSION + "\n"
|
67
|
+
end
|
68
|
+
|
69
|
+
it "changes to html output" do
|
70
|
+
erect = Erect.new(["--to-html"])
|
71
|
+
erect.mode.should == :to_html
|
72
|
+
end
|
73
|
+
|
74
|
+
it "changes to html output when passed a .rb file" do
|
75
|
+
pending do
|
76
|
+
erect = Erect.new(["foo.rb"])
|
77
|
+
erect.mode.should == :to_html
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
it "fails when given both .rb and .html files" do
|
82
|
+
pending do
|
83
|
+
lambda {
|
84
|
+
erect = Erect.new(["foo.rb", "bar.html"])
|
85
|
+
}.should raise_error
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
it "returns false when there's an error during run" do
|
90
|
+
capturing_output do
|
91
|
+
Erect.new(["MISSINGFILE"]).run.should == false
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
97
|
+
|
98
|
+
describe "Erect functionally" do
|
99
|
+
|
100
|
+
attr_reader :dir, :fred_html, :wilma_rhtml, :barney_html_erb, :fred_rb
|
101
|
+
|
102
|
+
def create(file, body="hi")
|
103
|
+
File.open(file, "w") do |f|
|
104
|
+
f.puts(body)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
before :all do
|
109
|
+
@dir = Dir.tmpdir + "/#{Time.now.to_i}" + "/explode"
|
110
|
+
@fred_html = "#{dir}/fred.html"
|
111
|
+
@wilma_rhtml = "#{dir}/wilma.rhtml"
|
112
|
+
@barney_html_erb = "#{dir}/barney.html.erb"
|
113
|
+
@fred_rb = "#{dir}/fred.rb"
|
114
|
+
|
115
|
+
FileUtils.mkdir_p(dir)
|
116
|
+
create(fred_html)
|
117
|
+
create(wilma_rhtml)
|
118
|
+
create(barney_html_erb)
|
119
|
+
create(fred_rb, "class Fred < Erector::Widget\ndef content\ndiv 'dino'\nend\nend")
|
120
|
+
end
|
121
|
+
|
122
|
+
it "explodes dirs into .html etc. files when in to-rb mode" do
|
123
|
+
erect = Erect.new(["--to-erector", dir])
|
124
|
+
erect.files.sort.should == [barney_html_erb, fred_html, wilma_rhtml]
|
125
|
+
end
|
126
|
+
|
127
|
+
it "explodes dirs into .rb files when in to-html mode" do
|
128
|
+
erect = Erect.new(["--to-html", dir])
|
129
|
+
erect.files.should == [fred_rb]
|
130
|
+
end
|
131
|
+
|
132
|
+
it "outputs .rb files in the same directory as the input .html files" do
|
133
|
+
erect = Erect.new(["--to-erector", "-q", fred_html])
|
134
|
+
erect.run
|
135
|
+
File.exist?(fred_rb).should be_true
|
136
|
+
File.read(fred_rb).should include("text 'hi'")
|
137
|
+
end
|
138
|
+
|
139
|
+
it "outputs .html files in the same directory as the input .rb files" do
|
140
|
+
betty_rb = "#{dir}/betty.rb"
|
141
|
+
betty_html = "#{dir}/betty.html"
|
142
|
+
create(betty_rb, "class Betty < Erector::Widget\ndef content\ndiv 'bam bam'\nend\nend")
|
143
|
+
|
144
|
+
erect = Erect.new(["--to-html", "-q", betty_rb])
|
145
|
+
erect.run
|
146
|
+
File.exist?(betty_html).should be_true
|
147
|
+
File.read(betty_html).should == "<div>bam bam</div>\n"
|
148
|
+
end
|
149
|
+
|
150
|
+
it "outputs .html files in the given directory" do
|
151
|
+
create(fred_rb, "class Fred < Erector::Widget\ndef content\ndiv 'dino'\nend\nend")
|
152
|
+
out_dir = "#{dir}/out"
|
153
|
+
out_file = "#{out_dir}/fred.html"
|
154
|
+
|
155
|
+
Erect.new([]).output_dir.should be_nil
|
156
|
+
erect = Erect.new(["--to-html", "-o", "#{out_dir}", "-q", fred_rb])
|
157
|
+
erect.output_dir.should == out_dir
|
158
|
+
erect.run
|
159
|
+
File.exist?(out_file).should be_true
|
160
|
+
File.read(out_file).should == "<div>dino</div>\n"
|
161
|
+
end
|
162
|
+
|
163
|
+
it "skips rendering classes that aren't widgets" do
|
164
|
+
mr_slate_rb = "#{dir}/mr_slate.rb"
|
165
|
+
mr_slate_html = "#{dir}/mr_slate.html"
|
166
|
+
create(mr_slate_rb, "class MrSlate\nend")
|
167
|
+
erect = Erect.new(["-q", "--to-html", mr_slate_rb])
|
168
|
+
erect.run
|
169
|
+
File.exist?(mr_slate_html).should be_false
|
170
|
+
end
|
171
|
+
|
172
|
+
# it "properly indents lines beginning with for, unless, etc."
|
173
|
+
end
|
174
|
+
|
175
|
+
end
|
@@ -0,0 +1,164 @@
|
|
1
|
+
require File.expand_path("#{File.dirname(__FILE__)}/../spec_helper")
|
2
|
+
|
3
|
+
require "erector/erect/erect"
|
4
|
+
|
5
|
+
module Erector
|
6
|
+
describe Erected do
|
7
|
+
|
8
|
+
it "picks the right file name" do
|
9
|
+
Erected.new("foo.html.erb").filename.should == "foo.rb"
|
10
|
+
Erected.new("foo.html").filename.should == "foo.rb"
|
11
|
+
Erected.new("foo.bar.html").filename.should == "foo.rb"
|
12
|
+
Erected.new("foo_bar.html.erb").filename.should == "foo_bar.rb"
|
13
|
+
Erected.new("stuff/foo_bar.html.erb").filename.should == "stuff/foo_bar.rb"
|
14
|
+
end
|
15
|
+
|
16
|
+
it "picks a nice class name" do
|
17
|
+
Erected.new("foo.html.erb").classname.should == "Foo"
|
18
|
+
Erected.new("foo.html").classname.should == "Foo"
|
19
|
+
Erected.new("foo.bar.html").classname.should == "Foo"
|
20
|
+
Erected.new("foo_bar.html.erb").classname.should == "FooBar"
|
21
|
+
Erected.new("stuff/foo_bar.html.erb").classname.should == "FooBar"
|
22
|
+
end
|
23
|
+
|
24
|
+
it "picks an even nicer class name if it's in a views dir" do
|
25
|
+
Erected.new("app/views/stuff/foo_bar.html.erb").classname.should == "Views::Stuff::FooBar"
|
26
|
+
Erected.new("views/stuff/foo_bar.html.erb").classname.should == "Views::Stuff::FooBar"
|
27
|
+
end
|
28
|
+
|
29
|
+
it "uses Widget as the parent class" do
|
30
|
+
Erected.new("foo_bar.html").parent_class.should == "Erector::Widget"
|
31
|
+
Erected.new("foo_bar.html.erb").parent_class.should == "Erector::Widget"
|
32
|
+
Erected.new("stuff/foo_bar.html.erb").parent_class.should == "Erector::Widget"
|
33
|
+
end
|
34
|
+
|
35
|
+
it "uses Widget as the parent class if it's in a views dir" do
|
36
|
+
Erected.new("app/views/stuff/foo_bar.html.erb").parent_class.should == "Erector::Widget"
|
37
|
+
Erected.new("views/stuff/foo_bar.html.erb").parent_class.should == "Erector::Widget"
|
38
|
+
end
|
39
|
+
|
40
|
+
def convert(dir, input, output, superklass = nil, method_name = nil)
|
41
|
+
dir = Dir.tmpdir + "/#{Time.now.to_i}" + "/#{dir}"
|
42
|
+
|
43
|
+
FileUtils.mkdir_p(dir)
|
44
|
+
html = "#{dir}/dummy.html"
|
45
|
+
rb = "#{dir}/dummy.rb"
|
46
|
+
|
47
|
+
File.open(html, "w") do |f|
|
48
|
+
f.puts(input)
|
49
|
+
end
|
50
|
+
|
51
|
+
args = [ html, superklass || 'Erector::Widget', method_name || 'content' ]
|
52
|
+
@e = Erected.new(*args)
|
53
|
+
@e.convert
|
54
|
+
|
55
|
+
File.read(rb).should == output
|
56
|
+
end
|
57
|
+
|
58
|
+
it "converts a normal file" do
|
59
|
+
convert(".",
|
60
|
+
"<div>hello</div>",
|
61
|
+
"class Dummy < Erector::Widget\n" +
|
62
|
+
" def content\n" +
|
63
|
+
" div do\n" +
|
64
|
+
" text 'hello'\n" +
|
65
|
+
" end\n" +
|
66
|
+
" end\n" +
|
67
|
+
"end\n"
|
68
|
+
)
|
69
|
+
end
|
70
|
+
|
71
|
+
it "converts a views file" do
|
72
|
+
convert("app/views/foos",
|
73
|
+
"<div>hello</div>",
|
74
|
+
"class Views::Foos::Dummy < Erector::Widget\n" +
|
75
|
+
" def content\n" +
|
76
|
+
" div do\n" +
|
77
|
+
" text 'hello'\n" +
|
78
|
+
" end\n" +
|
79
|
+
" end\n" +
|
80
|
+
"end\n"
|
81
|
+
)
|
82
|
+
end
|
83
|
+
|
84
|
+
it "converts a normal file with a different superclass" do
|
85
|
+
convert(".",
|
86
|
+
"<div>hello</div>",
|
87
|
+
"class Dummy < Foo::Bar\n" +
|
88
|
+
" def content\n" +
|
89
|
+
" div do\n" +
|
90
|
+
" text 'hello'\n" +
|
91
|
+
" end\n" +
|
92
|
+
" end\n" +
|
93
|
+
"end\n",
|
94
|
+
"Foo::Bar"
|
95
|
+
)
|
96
|
+
end
|
97
|
+
|
98
|
+
it "converts a normal file with a different superclass and method name" do
|
99
|
+
convert(".",
|
100
|
+
"<div>hello</div>",
|
101
|
+
"class Dummy < Foo::Bar\n" +
|
102
|
+
" def my_content\n" +
|
103
|
+
" div do\n" +
|
104
|
+
" text 'hello'\n" +
|
105
|
+
" end\n" +
|
106
|
+
" end\n" +
|
107
|
+
"end\n",
|
108
|
+
"Foo::Bar",
|
109
|
+
'my_content'
|
110
|
+
)
|
111
|
+
end
|
112
|
+
|
113
|
+
it "ignores ERb trim markers" do
|
114
|
+
convert(".",
|
115
|
+
%{<div>
|
116
|
+
<%= 1 + 3 -%>
|
117
|
+
</div>},
|
118
|
+
%{class Dummy < Erector::Widget
|
119
|
+
def content
|
120
|
+
div do
|
121
|
+
rawtext 1 + 3
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
})
|
126
|
+
end
|
127
|
+
|
128
|
+
it "converts ERb escapes in attributes" do
|
129
|
+
convert(".",
|
130
|
+
"<div id=\"foo_<%= bar %>_baz_<%= quux %>_marph\">hello</div>",
|
131
|
+
%{class Dummy < Erector::Widget
|
132
|
+
def content
|
133
|
+
div(:id => ('foo_' + bar + '_baz_' + quux + '_marph')) do
|
134
|
+
text 'hello'
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
})
|
139
|
+
end
|
140
|
+
|
141
|
+
it "only parenthesizes ERb escapes in attributes if necessary" do
|
142
|
+
convert(".",
|
143
|
+
"<div id=\'<%= bar %>\'>hello</div>",
|
144
|
+
%{class Dummy < Erector::Widget
|
145
|
+
def content
|
146
|
+
div :id => bar do
|
147
|
+
text 'hello'
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
})
|
152
|
+
end
|
153
|
+
|
154
|
+
# todo: figure out if there is any such thing as unparsable HTML anymore
|
155
|
+
# it "raises an exception if given unparsable HTML" do
|
156
|
+
# begin
|
157
|
+
# convert(".", "<", "")
|
158
|
+
# rescue => e
|
159
|
+
# e.to_html.should include("Could not parse")
|
160
|
+
# end
|
161
|
+
# end
|
162
|
+
|
163
|
+
end
|
164
|
+
end
|