Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
name: CI

on:
push:
branches:
- '**'
pull_request:
branches:
- '**'
schedule:
- cron: '0 4 1 * *'
# Run workflow manually
workflow_dispatch:

jobs:
rubocop:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: '3.2'

- name: Bundler
run: bundle install

- name: Rubocop
run: bin/rubocop

rspec:
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
ruby:
- '3.4'
- '3.3'
- '3.2'
- 'head'
# - 'jruby'
- 'truffleruby'

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true

- name: Run RSpec
run: bin/rspec
20 changes: 18 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
*.gem
# Ignore bundler config.
.bundle/

# Ignore Gemfile.lock
Gemfile.lock
/tmp

# Ignore builded gem
*.gem

# Ignore temp files
tmp/

# Ignore Ruby version
.ruby-version

# Ignore MacOS files
.DS_Store

# Ignore coverage files
coverage/
4 changes: 1 addition & 3 deletions .rspec
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
--color
--require spec_helper
-I lib
--warnings
65 changes: 52 additions & 13 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,57 @@
inherit_mode:
merge:
- Exclude

require:
- standard
---
plugins:
- rubocop-performance
- rubocop-rake

inherit_gem:
standard: config/base.yml
- rubocop-rspec

AllCops:
NewCops: disable
SuggestExtensions: false
NewCops: enable
TargetRubyVersion: 3.2
Exclude:
- '**/*.gemspec'
- '**/Rakefile'
- bin/*
- exe/*

Gemspec/RequireMFA:
Enabled: false

#########
# STYLE #
#########

Style/Documentation:
Enabled: false

Style/StringLiterals:
EnforcedStyle: double_quotes

Style/RegexpLiteral:
EnforcedStyle: percent_r

Style/BlockDelimiters:
EnforcedStyle: semantic

Style/FormatString:
EnforcedStyle: percent

Style/FormatStringToken:
EnforcedStyle: unannotated

Style/NumericPredicate:
EnforcedStyle: comparison

Style/TrailingCommaInArrayLiteral:
EnforcedStyleForMultiline: consistent_comma

Style/Alias:
EnforcedStyle: prefer_alias_method


#########
# RSPEC #
#########

RSpec/ExampleLength:
Enabled: false

RSpec/MultipleExpectations:
Enabled: false
12 changes: 12 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
# frozen_string_literal: true

source "https://rubygems.org"

gemspec

# Dev libs
gem "rake"
gem "rspec"
gem "simplecov"

# Dev tools / linter
gem "rubocop", require: false
gem "rubocop-performance", require: false
gem "rubocop-rake", require: false
gem "rubocop-rspec", require: false
10 changes: 6 additions & 4 deletions COPYING.txt → LICENSE
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
The MIT License (MIT)

Copyright (c) 2007-2015 Paul Battley

Permission is hereby granted, free of charge, to any person obtaining a copy
Expand All @@ -7,13 +9,13 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
19 changes: 5 additions & 14 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
require "rspec/core/rake_task"

desc "Run the specs."
RSpec::Core::RakeTask.new do |t|
t.pattern = "spec/**/*_spec.rb"
t.verbose = false
end
# frozen_string_literal: true

task :default => [:spec]

if Gem.loaded_specs.key?('rubocop')
require 'rubocop/rake_task'
RuboCop::RakeTask.new
require "bundler/gem_tasks"
require "rspec/core/rake_task"

task(:default).prerequisites << task(:rubocop)
end
RSpec::Core::RakeTask.new(:spec)
task default: :spec
16 changes: 16 additions & 0 deletions bin/rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

#
# This file was generated by Bundler.
#
# The application 'rake' is installed as part of a gem, and
# this file is here to facilitate running it.
#

ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)

require "rubygems"
require "bundler/setup"

load Gem.bin_path("rake", "rake")
16 changes: 16 additions & 0 deletions bin/rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

#
# This file was generated by Bundler.
#
# The application 'rspec' is installed as part of a gem, and
# this file is here to facilitate running it.
#

ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)

require "rubygems"
require "bundler/setup"

load Gem.bin_path("rspec-core", "rspec")
16 changes: 16 additions & 0 deletions bin/rubocop
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

#
# This file was generated by Bundler.
#
# The application 'rubocop' is installed as part of a gem, and
# this file is here to facilitate running it.
#

ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)

require "rubygems"
require "bundler/setup"

load Gem.bin_path("rubocop", "rubocop")
File renamed without changes.
38 changes: 18 additions & 20 deletions htmlbeautifier.gemspec
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
require File.expand_path("../lib/htmlbeautifier/version", __FILE__)
# frozen_string_literal: true

spec = Gem::Specification.new do |s|
s.name = "htmlbeautifier"
s.version = HtmlBeautifier::VERSION::STRING
s.summary = "HTML/ERB beautifier"
s.description = "A normaliser/beautifier for HTML that also understands embedded Ruby."
s.author = "Paul Battley"
s.email = "[email protected]"
s.homepage = "http://github.com/threedaymonk/htmlbeautifier"
s.license = "MIT"
require_relative "lib/html_beautifier/version"

s.files = %w(Rakefile README.md) + Dir.glob("{bin,test,lib}/**/*")
s.executables = Dir["bin/**"].map { |f| File.basename(f) }
Gem::Specification.new do |s|
s.name = "htmlbeautifier"
s.version = HtmlBeautifier::VERSION::STRING
s.platform = Gem::Platform::RUBY
s.author = "Paul Battley"
s.email = "[email protected]"
s.homepage = "http://github.com/threedaymonk/htmlbeautifier"
s.summary = "HTML/ERB beautifier"
s.description = "A normaliser/beautifier for HTML that also understands embedded Ruby."
s.license = "MIT"

s.require_paths = ["lib"]
s.required_ruby_version = ">= 3.2.0"

s.required_ruby_version = '>= 2.6.0'
s.files = Dir["README.md", "LICENSE", "lib/**/*.rb", "exe/*"]

s.add_development_dependency "rake", "~> 13"
s.add_development_dependency "rspec", "~> 3"
s.add_development_dependency "standard", "~> 1.33"
s.add_development_dependency "rubocop-rspec", "~> 2"
s.add_development_dependency "rubocop-rake", "~> 0.6"
end
s.bindir = "exe"
s.executables = ["htmlbeautifier"]

s.add_dependency "zeitwerk"
end
37 changes: 37 additions & 0 deletions lib/html_beautifier.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

# require ruby dependencies
require "strscan"

# require external dependencies
require "zeitwerk"

# load zeitwerk
Zeitwerk::Loader.for_gem.tap do |loader|
loader.ignore("#{__dir__}/htmlbeautifier.rb")
loader.setup
end

module HtmlBeautifier
#
# Returns a beautified HTML/HTML+ERB document as a String.
# html must be an object that responds to +#to_s+.
#
# Available options are:
# tab_stops - an integer for the number of spaces to indent, default 2.
# Deprecated: see indent.
# indent - what to indent with (" ", "\t" etc.), default " "
# stop_on_errors - raise an exception on a badly-formed document. Default
# is false, i.e. continue to process the rest of the document.
# initial_level - The entire output will be indented by this number of steps.
# Default is 0.
# keep_blank_lines - an integer for the number of consecutive empty lines
# to keep in output.
#
def self.beautify(html, options = {})
options[:indent] = " " * options[:tab_stops] if options[:tab_stops]
(+"").tap do |output|
HtmlParser.new.scan(html.to_s, Builder.new(output, options))
end
end
end
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
# frozen_string_literal: true

require "htmlbeautifier/parser"
require "htmlbeautifier/ruby_indenter"

module HtmlBeautifier
class Builder
class Builder # rubocop:disable Metrics/ClassLength
DEFAULT_OPTIONS = {
indent: " ",
initial_level: 0,
Expand Down Expand Up @@ -43,7 +40,7 @@ def outdent
end

def emit(*strings)
strings_join = strings.join("")
strings_join = strings.join
@output << "\n" if @new_line && !@empty
@output << (@tab * @level) if @new_line && !strings_join.strip.empty?
@output << strings_join
Expand Down Expand Up @@ -93,7 +90,7 @@ def preformatted_block(opening, content, closing)

def standalone_element(elem)
emit elem
new_line if elem =~ %r{^<br[^\w]}
new_line if %r{^<br[^\w]}.match?(elem)
end

def close_element(elem)
Expand Down
Loading