ruby2html 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: acd9834eee45bda437a281566e66300e5e5645dddb45850d571d506c27a94501
4
- data.tar.gz: ebe9a74c98f952e049335357fda1e9268f7126fbb090cbf7bf4e8b4e1fbedc1f
3
+ metadata.gz: 8e43d3eb304652b48e727067ce4361d654936cc0258589efd82adf55136a687d
4
+ data.tar.gz: 25900c6ed9f1a441f3f5a2b6d457e13caa5bfca3db99a7573daa89af92351544
5
5
  SHA512:
6
- metadata.gz: 4dc38bb961568de810742b8e8c2a8c9231abcbc0244bf0334350b3949eef3e1d81d2f907ab2565bc1acc6b0b6751ccabdc2c04b9855996e72e0ef0c117eaf8fa
7
- data.tar.gz: 5e200ab5fd3f5a0941c7d7a32e5729e5a9f249d01158b1906a2797d0b0e8309bf403e0aaf5bd5a8416ef9cf42a8d56c0150bc2a9d5f0ca63bfe987ba8c777dca
6
+ metadata.gz: 214487bc59cf8ebc35698f7c16ffca8add3d59dfb52142ef1273e5c1dc67d8351fad286f1b417bfd6e9913ae3c06be8e3bba06c81b9670a5a5561cb47e1ba683
7
+ data.tar.gz: 430e623e958a739a33cdfd16d9d0039ff3354f1461cfe8d7db0a2418ce6fbff4a0352e37a67227e36058088c9c26fac0ffd2aaad50c3842a1ea71a536133610f
data/README.md CHANGED
@@ -25,18 +25,6 @@ Or install it yourself as:
25
25
 
26
26
  ## 🎨 Usage
27
27
 
28
- ### In your ApplicationController
29
-
30
- File: `app/controllers/application_controller.rb`
31
-
32
- ```ruby
33
- # frozen_string_literal: true
34
-
35
- class ApplicationController < ActionController::Base
36
- include Ruby2html::RailsHelper
37
- end
38
- ```
39
-
40
28
  ### In your views
41
29
 
42
30
  File: `app/views/your_view.html.rb`
@@ -63,6 +51,18 @@ render partial: 'shared/navbar'
63
51
 
64
52
  #### Or use your current .erb views
65
53
 
54
+ ### In your ApplicationController
55
+
56
+ File: `app/controllers/application_controller.rb`
57
+
58
+ ```ruby
59
+ # frozen_string_literal: true
60
+
61
+ class ApplicationController < ActionController::Base
62
+ include Ruby2html::RailsHelper
63
+ end
64
+ ```
65
+
66
66
  File: `app/views/your_view.html.erb`
67
67
 
68
68
  Replace your ERB with beautiful Ruby code:
@@ -97,18 +97,18 @@ Replace your ERB with beautiful Ruby code:
97
97
  ruby 3.3.3 (2024-06-12 revision f1c7b6f435) +YJIT [x86_64-linux]
98
98
  Warming up --------------------------------------
99
99
  GET /benchmark/html (ERB)
100
- 1.000 i/100ms
100
+ 2.000 i/100ms
101
101
  GET /benchmark/ruby (Ruby2html)
102
102
  1.000 i/100ms
103
103
  Calculating -------------------------------------
104
104
  GET /benchmark/html (ERB)
105
- 20.163 (±19.8%) i/s - 95.000 in 5.062853s
105
+ 20.989 (±19.1%) i/s - 102.000 in 5.077353s
106
106
  GET /benchmark/ruby (Ruby2html)
107
- 19.36215.5%) i/s - 92.000 in 5.006433s
107
+ 20.43819.6%) i/s - 97.000 in 5.010249s
108
108
 
109
109
  Comparison:
110
- GET /benchmark/html (ERB): 20.2 i/s
111
- GET /benchmark/ruby (Ruby2html): 19.4 i/s - same-ish: difference falls within error
110
+ GET /benchmark/html (ERB): 21.0 i/s
111
+ GET /benchmark/ruby (Ruby2html): 20.4 i/s - same-ish: difference falls within error
112
112
  ```
113
113
 
114
114
  ### With ViewComponents
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ h1 'first component'
4
+
5
+ div do
6
+ h2 'A subheading'
7
+ end
8
+
9
+ p @item
@@ -2,7 +2,7 @@
2
2
 
3
3
  class SecondComponent < ApplicationComponent
4
4
  def call
5
- html(self) do
5
+ html do
6
6
  h1 class: 'my-class' do
7
7
  plain 'Second Component'
8
8
  end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ h1 'Third component'
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ThirdComponent < ApplicationComponent
4
+ def initialize
5
+ @item = 'Hello, World!'
6
+ end
7
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ h1 'Benchmark: Ruby2html'
4
+
5
+ h2 'User Statistics'
6
+ ul do
7
+ li { plain "Total Users: #{@complex_data[:stats][:total_users]}" }
8
+ li { plain "Average Orders per User: #{@complex_data[:stats][:average_orders_per_user]}" }
9
+ li { plain "Most Expensive Item: #{@complex_data[:stats][:most_expensive_item]}" }
10
+ li { plain "Most Popular Country: #{@complex_data[:stats][:most_popular_country]}" }
11
+ end
12
+
13
+ h2 'User List'
14
+ @complex_data[:users].each do |user|
15
+ div class: 'user-card' do
16
+ h3 user[:name]
17
+ p { plain "Email: #{user[:email]}" }
18
+ p { plain "Address: #{user[:address][:street]}, #{user[:address][:city]}, #{user[:address][:country]}" }
19
+
20
+ h4 'Orders'
21
+ user[:orders].each do |order|
22
+ div class: 'order' do
23
+ p { plain "Order ID: #{order[:id]}" }
24
+ p { plain "Total: $#{order[:total]}" }
25
+ ul do
26
+ order[:items].each do |item|
27
+ li { plain "#{item[:name]} - $#{item[:price]} (Quantity: #{item[:quantity]})" }
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -2,6 +2,7 @@
2
2
 
3
3
  <%= render FirstComponent.new %>
4
4
  <%= render SecondComponent.new %>
5
+ <%= render ThirdComponent.new %>
5
6
 
6
7
  <%=
7
8
  html(self) do
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ h1 'RbFiles'
4
+ h1 'ok'
5
+ h1 'ok'
6
+
7
+ div @value
8
+
9
+ link_to 'Home', root_url
10
+ render partial: 'shared/navbar'
@@ -2,8 +2,8 @@
2
2
 
3
3
  module Ruby2html
4
4
  module ComponentHelper
5
- def html(context, &block)
6
- Ruby2html::Render.new(context, &block).render.html_safe
5
+ def html(&block)
6
+ Ruby2html::Render.new(self, &block).render.html_safe
7
7
  end
8
8
  end
9
9
  end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ruby2html
4
+ class TemplateHandler
5
+ class_attribute :default_format
6
+ self.default_format = :html
7
+
8
+ def self.call(template, source)
9
+ new.call(template, source)
10
+ end
11
+
12
+ def call(_template, source)
13
+ <<-RUBY
14
+ Ruby2html::Render.new(self) do
15
+ #{source}
16
+ end.render
17
+ RUBY
18
+ end
19
+ end
20
+
21
+ class Railtie < Rails::Railtie
22
+ initializer 'ruby2html.initializer' do
23
+ Rails.autoloaders.main.ignore(
24
+ Rails.root.join('app/views/**/*.html.rb'),
25
+ Rails.root.join('app/components/**/*.html.rb')
26
+ )
27
+ end
28
+ end
29
+ end
30
+
31
+ ActionView::Template.register_template_handler :rb, Ruby2html::TemplateHandler if defined? ActionView::Template
@@ -35,7 +35,12 @@ module Ruby2html
35
35
  return plain @context.render(*args, **options, &block)
36
36
  end
37
37
  instance_exec(&@root)
38
- @output.string
38
+ result = @output.string
39
+ if defined?(ActiveSupport)
40
+ result = ActiveSupport::SafeBuffer.new(result)
41
+ end
42
+
43
+ result
39
44
  end
40
45
 
41
46
  HTML5_TAGS.each do |tag|
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Ruby2html
4
- VERSION = '1.1.0'
4
+ VERSION = '1.2.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby2html
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - sebi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-08-08 00:00:00.000000000 Z
11
+ date: 2024-08-09 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Ruby2HTML empowers developers to write view logic in pure Ruby, seamlessly
14
14
  converting it into clean, well-formatted HTML. Enhance your templating workflow,
@@ -33,9 +33,11 @@ files:
33
33
  - app/channels/application_cable/channel.rb
34
34
  - app/channels/application_cable/connection.rb
35
35
  - app/components/application_component.rb
36
- - app/components/first_component.html.erb
36
+ - app/components/first_component.html.rb
37
37
  - app/components/first_component.rb
38
38
  - app/components/second_component.rb
39
+ - app/components/third_component.rb
40
+ - app/components/third_component/third_component.html.rb
39
41
  - app/controllers/application_controller.rb
40
42
  - app/controllers/benchmark_controller.rb
41
43
  - app/controllers/concerns/.keep
@@ -46,8 +48,9 @@ files:
46
48
  - app/models/application_record.rb
47
49
  - app/models/concerns/.keep
48
50
  - app/views/benchmark/normal_html.html.erb
49
- - app/views/benchmark/ruby_2html.html.erb
51
+ - app/views/benchmark/ruby_2html.html.rb
50
52
  - app/views/home/index.html.erb
53
+ - app/views/home/rb_files.html.rb
51
54
  - app/views/layouts/application.html.erb
52
55
  - app/views/layouts/mailer.html.erb
53
56
  - app/views/layouts/mailer.text.erb
@@ -79,6 +82,7 @@ files:
79
82
  - lib/gem/ruby2html.rb
80
83
  - lib/gem/ruby2html/component_helper.rb
81
84
  - lib/gem/ruby2html/rails_helper.rb
85
+ - lib/gem/ruby2html/railtie.rb
82
86
  - lib/gem/ruby2html/render.rb
83
87
  - lib/gem/ruby2html/version.rb
84
88
  - lib/tasks/.keep
@@ -1,9 +0,0 @@
1
- <%= html(self) do
2
- h1 "first component"
3
-
4
- div do
5
- h2 "A subheading"
6
- end
7
-
8
- p @item
9
- end %>
@@ -1,35 +0,0 @@
1
- <%=
2
- html(self) do
3
- h1 'Benchmark: Ruby2html'
4
-
5
- h2 'User Statistics'
6
- ul do
7
- li { plain "Total Users: #{@complex_data[:stats][:total_users]}" }
8
- li { plain "Average Orders per User: #{@complex_data[:stats][:average_orders_per_user]}" }
9
- li { plain "Most Expensive Item: #{@complex_data[:stats][:most_expensive_item]}" }
10
- li { plain "Most Popular Country: #{@complex_data[:stats][:most_popular_country]}" }
11
- end
12
-
13
- h2 'User List'
14
- @complex_data[:users].each do |user|
15
- div class: 'user-card' do
16
- h3 user[:name]
17
- p { plain "Email: #{user[:email]}" }
18
- p { plain "Address: #{user[:address][:street]}, #{user[:address][:city]}, #{user[:address][:country]}" }
19
-
20
- h4 'Orders'
21
- user[:orders].each do |order|
22
- div class: 'order' do
23
- p { plain "Order ID: #{order[:id]}" }
24
- p { plain "Total: $#{order[:total]}" }
25
- ul do
26
- order[:items].each do |item|
27
- li { plain "#{item[:name]} - $#{item[:price]} (Quantity: #{item[:quantity]})" }
28
- end
29
- end
30
- end
31
- end
32
- end
33
- end
34
- end
35
- %>