From e13bd782a960c0f1c8d8f759a4e890f048160d99 Mon Sep 17 00:00:00 2001 From: Wyatt Kirby Date: Mon, 19 Nov 2018 10:50:46 -0800 Subject: [PATCH 1/4] Update to pass HTML beautifier options along --- .gitignore | 1 + jekyll-tidy.gemspec | 1 + lib/jekyll/tidy.rb | 5 ++++- test/fixtures/clean_tabs.html | 15 +++++++++++++++ test/test_tidy.rb | 16 ++++++++++++++++ 5 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/clean_tabs.html diff --git a/.gitignore b/.gitignore index 31cafb5..b184ff3 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ .bundle .config .yardoc +.byebug_history Gemfile.lock InstalledFiles _yardoc diff --git a/jekyll-tidy.gemspec b/jekyll-tidy.gemspec index 2e932b3..47714da 100644 --- a/jekyll-tidy.gemspec +++ b/jekyll-tidy.gemspec @@ -25,4 +25,5 @@ Gem::Specification.new do |spec| spec.add_development_dependency "bundler", "~> 1.14" spec.add_development_dependency "rake", "~> 10.0" spec.add_development_dependency "minitest", "~> 5.0" + spec.add_development_dependency "byebug", "~> 10.0" end diff --git a/lib/jekyll/tidy.rb b/lib/jekyll/tidy.rb index e591ea6..5b84e89 100644 --- a/lib/jekyll/tidy.rb +++ b/lib/jekyll/tidy.rb @@ -3,6 +3,7 @@ require "jekyll" require "htmlbeautifier" require "htmlcompressor" +require "byebug" module Jekyll module Tidy @@ -19,7 +20,9 @@ def output_clean(output) if compress_output? return HtmlCompressor::Compressor.new.compress output else - return HtmlBeautifier.beautify output + opts = jekyll_tidy_config.key?('html_beautifier') ? jekyll_tidy_config['html_beautifier'] : {} + opts = opts.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo} + return HtmlBeautifier.beautify output, opts end end diff --git a/test/fixtures/clean_tabs.html b/test/fixtures/clean_tabs.html new file mode 100644 index 0000000..2b02779 --- /dev/null +++ b/test/fixtures/clean_tabs.html @@ -0,0 +1,15 @@ + + + Dirty Html + + +

This is a dirty file

+

It has indentation all over the place

+

With a mix of tabs and spaces

+ + + + \ No newline at end of file diff --git a/test/test_tidy.rb b/test/test_tidy.rb index c90ef0a..775f45a 100644 --- a/test/test_tidy.rb +++ b/test/test_tidy.rb @@ -36,6 +36,22 @@ def test_outputs_compressed_html assert_equal expected, actual end + def test_uses_html_beautifier_options + setup_fixtures({ + "jekyll_tidy" => { + "html_beautifier" => { + "indent": "\t" + } + } + }) + + dirty_html = load_fixture("dirty.html") + expected = load_fixture("clean_tabs.html") + actual = Tidy.output_clean(dirty_html) + + assert_equal expected, actual + end + def test_matches_file_globs_correctly setup_fixtures({ "jekyll_tidy" => { From f5e7a12b2b2ba69b414ed57e4d83a8b5a0783e3d Mon Sep 17 00:00:00 2001 From: Wyatt Kirby Date: Mon, 19 Nov 2018 10:54:13 -0800 Subject: [PATCH 2/4] Update README --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index f6964d1..9305d0d 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,7 @@ gems: * `exclude` — an array of files to exclude from tidying. * `ignore_env` — a `JEKYLL_ENV` string on which to skip tidying entirely. * `compress_html` — a flag for whether or not to compress the HTML output + * `html_beautifier` — options to pass to HTML beautifier. ```yaml jekyll_tidy: @@ -94,6 +95,17 @@ $ JEKYLL_ENV=development jekyll serve will skip all tidying. +### html_beautifier (default: {}) + +The `html_beautifier` option will pass a hash of options on to the underlying HTML Beautifier class. + +Available options are: + +- `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. + ## Development After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. From a3eba709166c9962ac17e9ff0b56058d7b50f833 Mon Sep 17 00:00:00 2001 From: Wyatt Kirby Date: Mon, 19 Nov 2018 10:56:02 -0800 Subject: [PATCH 3/4] Update README with yaml example --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 9305d0d..d231a21 100644 --- a/README.md +++ b/README.md @@ -99,6 +99,15 @@ will skip all tidying. The `html_beautifier` option will pass a hash of options on to the underlying HTML Beautifier class. +These can be configured like so: + +```yaml +jekyll_tidy: + html_beautifier: + indent: "\t" + initial_level: 1 +``` + Available options are: - `indent` — what to indent with (`" "`, `"\t"` etc.), default `" "` From 939443d7762d9bc2019f5e91b539dfe2674bb1df Mon Sep 17 00:00:00 2001 From: Ashwin Maroli Date: Mon, 19 Nov 2018 18:30:47 -0800 Subject: [PATCH 4/4] Update lib/jekyll/tidy.rb Co-Authored-By: wkirby --- lib/jekyll/tidy.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jekyll/tidy.rb b/lib/jekyll/tidy.rb index 5b84e89..917d439 100644 --- a/lib/jekyll/tidy.rb +++ b/lib/jekyll/tidy.rb @@ -20,7 +20,7 @@ def output_clean(output) if compress_output? return HtmlCompressor::Compressor.new.compress output else - opts = jekyll_tidy_config.key?('html_beautifier') ? jekyll_tidy_config['html_beautifier'] : {} + opts = jekyll_tidy_config['html_beautifier'] || {} opts = opts.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo} return HtmlBeautifier.beautify output, opts end