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/README.md b/README.md index f6964d1..d231a21 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,26 @@ $ 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. + +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 `" "` +- `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. 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..917d439 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['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" => {