Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/html beautifier opts #18

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
.bundle
.config
.yardoc
.byebug_history
Gemfile.lock
InstalledFiles
_yardoc
Expand Down
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions jekyll-tidy.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 4 additions & 1 deletion lib/jekyll/tidy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "jekyll"
require "htmlbeautifier"
require "htmlcompressor"
require "byebug"

module Jekyll
module Tidy
Expand All @@ -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

Expand Down
15 changes: 15 additions & 0 deletions test/fixtures/clean_tabs.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<html>
<head>
<title>Dirty Html</title>
</head>
<body>
<h1>This is a dirty file</h1>
<p>It has indentation all over the place</p>
<h3>With a mix of tabs and spaces</h3>
<ul>
<li>and its markup</li>
<li>is just bad</li>
</ul>
<!-- WITH COMMENTS! -->
</body>
</html>
16 changes: 16 additions & 0 deletions test/test_tidy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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" => {
Expand Down