Replace filters/tidy.rb with nanoc-tidy.rb (rubygem)

This commit is contained in:
0x1eef 2023-11-07 01:59:45 -03:00
parent de67edadff
commit 74eae72495
4 changed files with 10 additions and 48 deletions

View file

@ -9,6 +9,7 @@ gem "nanoc", "~> 4.12"
# nanoc filters
gem "nanoc-gzip.rb", github: "0x1eef/nanoc-gzip.rb", tag: "v0.2.1"
gem "nanoc-webpack.rb", github: "0x1eef/nanoc-webpack.rb", tag: "v0.2.0"
gem "nanoc-tidy.rb", github: "0x1eef/nanoc-tidy.rb", tag: "v0.1.3"
gem "rainpress", "~> 1.0"
gem "sass", "~> 3.7"

View file

@ -10,6 +10,13 @@ GIT
set (= 1.0.2)
stringio (= 3.0.1)
GIT
remote: https://github.com/0x1eef/nanoc-tidy.rb.git
revision: 013380d5583a6eb9cb563ce2aa3abd7db0498787
tag: v0.1.3
specs:
nanoc-tidy.rb (0.1.3)
GIT
remote: https://github.com/0x1eef/nanoc-webpack.rb.git
revision: 5c0630138fa8188c37ed85d3708d59ca85d50921
@ -168,6 +175,7 @@ DEPENDENCIES
memoize (~> 1.3)
nanoc (~> 4.12)
nanoc-gzip.rb!
nanoc-tidy.rb!
nanoc-webpack.rb!
paint (~> 2.3)
rainpress (~> 1.0)

1
Rules
View file

@ -4,6 +4,7 @@
require "ryo"
require "nanoc-gzip"
require "nanoc-webpack"
require "nanoc-tidy"
locales = %w[ar en]
slugs = Ryo.from(

View file

@ -1,48 +0,0 @@
# 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