Skip to content

Commit

Permalink
support .rb files
Browse files Browse the repository at this point in the history
  • Loading branch information
sebyx07 committed Aug 8, 2024
1 parent 1aa0846 commit 4e50c9d
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 23 deletions.
46 changes: 34 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,32 @@ Or install it yourself as:

## 🎨 Usage

### In your views

File: `app/views/your_view.html.rb`

```ruby
div class: 'container' do
h1 'Welcome to Ruby2html! 🎉', class: 'main-title', 'data-controller': 'welcome'
link_to 'Home Sweet Home 🏠', root_path, class: 'btn btn-primary', 'data-turbo': false

@items.each do |item|
h2 class: 'item-title', id: "item-#{item[:id]}" do
item[:title]
end
p class: 'item-description' do
item[:description]
end
end
end

plain '<div>Inline html</div>'.html_safe

render partial: 'shared/navbar'
```

#### Or use your current .erb views

### In your ApplicationController

File: `app/controllers/application_controller.rb`
Expand All @@ -37,28 +63,24 @@ class ApplicationController < ActionController::Base
end
```

### In your views

File: `app/views/your_view.html.erb`

Replace your ERB with beautiful Ruby code:

```erb
<%=
html(self) do
h1 class: 'main-title', 'data-controller': 'welcome' do
plain "Welcome to Ruby2html! 🎉"
end
h1 "Welcome to Ruby2html! 🎉", class: 'main-title', 'data-controller': 'welcome'
div id: 'content', class: 'container' do
link_to 'Home Sweet Home 🏠', root_path, class: 'btn btn-primary', 'data-turbo': false
end
@items.each do |item|
h2 class: 'item-title', id: "item-#{item[:id]}" do
plain item[:title]
item[:title]
end
p class: 'item-description' do
plain item[:description]
item[:description]
end
end
Expand Down Expand Up @@ -118,10 +140,10 @@ class GreetingComponent < ApplicationComponent
def call
html(self) do
h1 class: 'greeting', 'data-user': @name do
plain "Hello, #{@name}! 👋"
"Hello, #{@name}! 👋"
end
p class: 'welcome-message' do
plain 'Welcome to the wonderful world of Ruby2html!'
'Welcome to the wonderful world of Ruby2html!'
end
end
end
Expand Down Expand Up @@ -177,13 +199,13 @@ class FirstComponent < ApplicationComponent
def call
html(self) do
h1 id: 'first-component-title' do
plain 'first component'
'first component'
end
div class: 'content-wrapper' do
h2 'A subheading'
end
p class: 'greeting-text', 'data-testid': 'greeting' do
plain @item
@item
end
end
end
Expand All @@ -199,7 +221,7 @@ class SecondComponent < ApplicationComponent
def call
html(self) do
h1 class: 'my-class', id: 'second-component-title', 'data-controller': 'second' do
plain 'second component'
'second component'
end
link_to 'Home', root_path, class: 'nav-link', 'data-turbo-frame': false
end
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ def index
}
]
end

def rb_files
@value = 'value'
end
end
3 changes: 2 additions & 1 deletion app/views/home/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
<%=
html(self) do
h1(id: 4) { plain "ok" }
h1(id: 4) { "Inside" }
h2 "Inside 2", id: '44'
h1 "ok"
h1 "ok"
h1 "ok"
Expand Down
10 changes: 10 additions & 0 deletions app/views/home/rb_files.html.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

h1 'RbFiles'
h1 'ok'
h1 'ok'

div @value

link_to 'Home', root_url
render partial: 'shared/navbar'
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
get '/benchmark/html', to: 'benchmark#normal_html'
get '/benchmark/ruby', to: 'benchmark#ruby_2html'

get '/rb_files', to: 'home#rb_files'

# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

# Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.
Expand Down
22 changes: 22 additions & 0 deletions lib/gem/ruby2html/railtie.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

module Ruby2html
class TemplateHandler
class_attribute :default_format
self.default_format = :html

def self.call(template, source)
new.call(template, source)
end

def call(_template, source)
<<-RUBY
Ruby2html::Render.new(self) do
#{source}
end.render
RUBY
end
end
end

ActionView::Template.register_template_handler :rb, Ruby2html::TemplateHandler if defined? ActionView::Template
19 changes: 10 additions & 9 deletions lib/gem/ruby2html/render.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@ def render(*args, **options, &block)
end

HTML5_TAGS.each do |tag|
define_method(tag) do |*args, &block|
html!(tag, *args, &block)
define_method(tag) do |*args, **options, &block|
html!(tag, *args, **options, &block)
end
end

def html!(name, *args, &block)
attributes = args.first.is_a?(Hash) ? args.shift : {}
content = args.first.to_s
def html!(name, *args, **options, &block)
content = args.first.is_a?(String) ? args.shift : nil
attributes = options

tag_content = StringIO.new
tag_content << '<'
tag_content << name
Expand All @@ -61,10 +62,10 @@ def html!(name, *args, &block)
prev_output = @current_output
nested_content = StringIO.new
@current_output = nested_content
instance_exec(&block)
block_result = yield
@current_output = prev_output
tag_content << nested_content.string
else
tag_content << (block_result.is_a?(String) ? escape_html(block_result) : nested_content.string)
elsif content
tag_content << escape_html(content)
end

Expand Down Expand Up @@ -105,7 +106,7 @@ def respond_to_missing?(method_name, include_private = false)
define_method(method) do |*args, &block|
plain @context.send(method, *args, &block)
end
end
end if defined?(ActionView)

def attributes_to_s(attributes)
return '' if attributes.empty?
Expand Down
2 changes: 1 addition & 1 deletion lib/gem/ruby2html/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Ruby2html
VERSION = '1.0.0'
VERSION = '1.1.0'
end
5 changes: 5 additions & 0 deletions spec/features/home_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@
visit root_path
expect(page).to have_content('Hello')
end

it 'can visit rb_files' do
visit rb_files_path
expect(page).to have_content('RbFiles')
end
end

0 comments on commit 4e50c9d

Please sign in to comment.