-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #152 from ReflectsLight/tidy
Add Nanoc::Filters::Tidy
- Loading branch information
Showing
5 changed files
with
59 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# frozen_string_literal: true | ||
|
||
class Nanoc::Filters::Tidy < Nanoc::Filter | ||
require "fileutils" | ||
include FileUtils | ||
Error = Class.new(RuntimeError) | ||
|
||
identifier :tidy | ||
type text: :text | ||
|
||
def self.default_options | ||
@default_options ||= {wrap: 120, indent: true} | ||
end | ||
|
||
def run(content, options = {}) | ||
file = temporary_file_for(content) | ||
tidy file, self.class.default_options.merge(options) | ||
end | ||
|
||
private | ||
|
||
def tidy(file, options) | ||
system "tidy", "-modify", "-quiet", *tidy_args(options), file.path | ||
if $?.success? | ||
File.read(file.path).tap { file.tap(&:unlink).close } | ||
else | ||
raise Error, "tidy exited unsuccessfully (exit code: #{$?.exitstatus})", [] | ||
end | ||
end | ||
|
||
def tidy_args(options) | ||
options.each_with_object([]) do |(key, value), ary| | ||
if value.equal?(true) | ||
ary << "-#{key}" | ||
else | ||
ary.concat ["-#{key}", value.to_s] | ||
end | ||
end | ||
end | ||
|
||
def temporary_file_for(content) | ||
dir = File.join(Dir.getwd, "tmp", "htmltidy") | ||
mkdir_p(dir) unless Dir.exist?(dir) | ||
file = Tempfile.new(File.basename(item.identifier.to_s), dir) | ||
file.write(content) | ||
file.tap(&:flush) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters