Skip to content

Commit

Permalink
html beauti
Browse files Browse the repository at this point in the history
  • Loading branch information
sebyx07 committed Aug 10, 2024
1 parent c0d89ca commit fa9bcf9
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 2 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ plain '<div>Inline html</div>'.html_safe
render partial: 'shared/navbar'
```

### (Optional) Nicely Format the HTML for source inspection

File: `config/environments/development.rb` or `config/environments/test.rb`
```ruby
config.middleware.use Ruby2html::HtmlBeautifierMiddleware
```

#### Or use your current .erb views

### In your ApplicationController
Expand Down
2 changes: 1 addition & 1 deletion app/views/home/rb_files.html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

div @value

Expand Down
2 changes: 2 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,6 @@

# Apply autocorrection by RuboCop to files generated by `bin/rails generate`.
# config.generators.apply_rubocop_autocorrect_after_generate!

config.middleware.use Ruby2html::HtmlBeautifierMiddleware
end
26 changes: 26 additions & 0 deletions lib/gem/ruby2html/html_beautifier_middleware.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

require 'htmlbeautifier'

module Ruby2html
class HtmlBeautifierMiddleware
def initialize(app)
@app = app
end

def call(env)
status, headers, body = @app.call(env)

if headers['Content-Type']&.include?('text/html')
new_body = []
body.each do |chunk|
new_body << HtmlBeautifier.beautify(chunk)
end
headers['Content-Length'] = new_body.map(&:bytesize).sum.to_s
body = new_body
end

[status, headers, body]
end
end
end
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.3.3'
VERSION = '1.4.0'
end
2 changes: 2 additions & 0 deletions ruby2html.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ Gem::Specification.new do |spec|
end
end
spec.require_paths = ['lib/gem']

spec.add_dependency 'htmlbeautifier', '>= 1.4'
end

0 comments on commit fa9bcf9

Please sign in to comment.