nanoc-tidy.rb 0.6.1 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ee29f7af6d3dbaf443aa2e35e52e696f184481641a493a29292d110760776c07
4
- data.tar.gz: c42f3696900ea751574072d35a0005ed4dd2442cde77640e7a56a23f9794a500
3
+ metadata.gz: baa9bd78c6f407c7050903ec46fe89c19a702dc788eb556438078d43f10c2adf
4
+ data.tar.gz: 5bd8b9fd26eb7eb0a6831eaee86d0da38e02ceee4589cfbdef4881f29f52b6a7
5
5
  SHA512:
6
- metadata.gz: bb1ad99a6c41a010f04d96483aac6cec646b153aeaaa0f827e2c2f176e5d0890435021a301e434600ab37623e5b824db75ce41826bde6b864a2da76a8e5be4ee
7
- data.tar.gz: 648fa0abae522cf95225adc508991a7f5698cc4573e3d1cafd56a211fec6376a460c800142521bfe1ca4d400657b2b48fd96cf0074fdcaf2999a35d88259479f
6
+ metadata.gz: 3acb6eb29a332c6013cdaa878f54327abfc96ec87fbd36c8a439afe153469b724713cea1a77572ab02b069c24e6f3dac25c1b05e00aeac146e304f9637c6766e
7
+ data.tar.gz: 83f346f2273873f02015065cc8701d8693e6cfd8a26686a767789a9dcd2f4cd46b084b452b928807758141ac4e3f312afb0f80d63bc6f355cdbd1663db9ab03d
data/Rakefile.rb CHANGED
@@ -1,5 +1,11 @@
1
1
  require "rake/testtask"
2
2
 
3
+ desc "Run CI tasks"
4
+ task :ci do
5
+ sh "rake test"
6
+ sh "bundle exec rubocop"
7
+ end
8
+
3
9
  Rake::TestTask.new do |t|
4
10
  t.test_files = FileList['test/*_test.rb']
5
11
  t.verbose = true
@@ -4,6 +4,7 @@ module Nanoc::Tidy
4
4
  class Filter < Nanoc::Filter
5
5
  require "fileutils"
6
6
  require_relative "spawn"
7
+
7
8
  include Spawn
8
9
  include FileUtils
9
10
 
@@ -22,13 +23,27 @@ module Nanoc::Tidy
22
23
  @default_argv ||= ["-wrap", "120", "-indent"]
23
24
  end
24
25
 
26
+ ##
27
+ # Runs the filter
28
+ #
29
+ # @param [String] content
30
+ # HTML content
31
+ #
32
+ # @param [Hash] options
33
+ # Filter options
34
+ #
35
+ # @return [String]
36
+ # Returns HTML content (modified)
25
37
  def run(content, options = {})
26
- path = temporary_file(content).path
38
+ file = temporary_file(
39
+ File.basename(item.identifier.to_s),
40
+ content
41
+ )
27
42
  spawn tidy,
28
- [*default_argv, *(options[:argv] || []), "-modify", path],
29
- err: File.join(tmpdir, "stderr"),
30
- out: File.join(tmpdir, "stdout")
31
- File.read(path).tap { rm(path) }
43
+ [*default_argv, *(options[:argv] || []), "-modify", file.path]
44
+ File.read(file.path)
45
+ ensure
46
+ file&.unlink
32
47
  end
33
48
 
34
49
  private
@@ -37,23 +52,25 @@ module Nanoc::Tidy
37
52
  self.class.default_argv
38
53
  end
39
54
 
40
- def temporary_file(content)
41
- mkdir_p(tmpdir)
42
- file = Tempfile.new(File.basename(item.identifier.to_s), tmpdir)
43
- file.write(content)
44
- file.tap(&:flush)
55
+ def temporary_file(basename, content)
56
+ tempname = [
57
+ ".nanoc.tidy.#{basename}.#{object_id}",
58
+ SecureRandom.alphanumeric(3)
59
+ ]
60
+ Tempfile.new(tempname).tap do
61
+ _1.write(content)
62
+ _1.flush
63
+ end
45
64
  end
46
65
 
47
66
  def tidy
48
- case
49
- when system("which tidy > /dev/null 2>&1") then "tidy"
50
- when system("which tidy5 > /dev/null 2>&1") then "tidy5"
51
- else nil
67
+ if system("which tidy > /dev/null 2>&1")
68
+ "tidy"
69
+ elsif system("which tidy5 > /dev/null 2>&1")
70
+ "tidy5"
71
+ else
72
+ raise Nanoc::Tidy::Error, "tidy executable not found on $PATH"
52
73
  end
53
74
  end
54
-
55
- def tmpdir
56
- File.join(Dir.getwd, "tmp", "tidy-html5")
57
- end
58
75
  end
59
76
  end
@@ -1,8 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Nanoc::Tidy
2
4
  module Spawn
3
- require "securerandom"
4
5
  require "fileutils"
5
- Error = Class.new(RuntimeError)
6
6
 
7
7
  ##
8
8
  # Spawns a process
@@ -13,20 +13,10 @@ module Nanoc::Tidy
13
13
  # @param [Array<String>] argv
14
14
  # An array of command line arguments
15
15
  #
16
- # @param [String] err
17
- # A path where stderr is redirected to
18
- #
19
- # @param [String] out
20
- # A path where stdout is redirected to
21
- #
22
16
  # @return [void]
23
- def spawn(exe, argv, err:, out:)
24
- hex = SecureRandom.hex
25
- err = "#{err}-ID#{hex}"
26
- out = "#{out}-ID#{hex}"
27
- Kernel.spawn(
28
- exe, *argv, { STDERR => err, STDOUT => out }
29
- )
17
+ def spawn(exe, argv, workdir: File.join(Dir.getwd, "tmp"))
18
+ logfile = File.join(workdir, ".#{Process.pid}.tidy")
19
+ Kernel.spawn(exe, *argv, {STDERR => logfile, STDOUT => logfile})
30
20
  Process.wait
31
21
  status = $?
32
22
  ##
@@ -34,24 +24,18 @@ module Nanoc::Tidy
34
24
  # * 0: no warnings, no errors
35
25
  # * 1: has warnings
36
26
  # * 2: has errors
37
- return if [0, 1].include?(status.exitstatus)
38
-
39
- msgs = [err, out].map do
40
- FileUtils.touch(_1)
41
- [_1.gsub(Dir.getwd, ''), ":", File.binread(_1)].join
42
- end.join("\n")
43
- raise Error,
44
- "#{File.basename(exe)} exited unsuccessfully " \
45
- "(" \
46
- "exit code: #{status.exitstatus}, " \
47
- "item: #{item.identifier}" \
48
- ")" \
49
- "\n#{msgs}",
50
- []
27
+ if [0, 1].include?(status.exitstatus)
28
+ status.exitstatus
29
+ else
30
+ raise Nanoc::Tidy::Error,
31
+ "#{File.basename(exe)} exited unsuccessfully\n" \
32
+ "(item: #{item.identifier})\n" \
33
+ "(exit code: #{status.exitstatus})\n" \
34
+ "output:\n#{File.binread(logfile)}\n",
35
+ []
36
+ end
51
37
  ensure
52
- [err, out]
53
- .select { File.exist?(_1) }
54
- .each { FileUtils.rm(_1) }
38
+ File.exist?(logfile) ? FileUtils.rm(logfile) : nil
55
39
  end
56
40
  end
57
41
  end
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Nanoc
2
4
  module Tidy
3
- VERSION = "0.6.1"
5
+ VERSION = "0.7.0"
4
6
  end
5
7
  end
data/lib/nanoc/tidy.rb CHANGED
@@ -2,6 +2,11 @@
2
2
 
3
3
  require "nanoc"
4
4
  module Nanoc::Tidy
5
+ ##
6
+ # Generic error
7
+ Error = Class.new(RuntimeError)
8
+
9
+ require "securerandom"
5
10
  require_relative "tidy/version"
6
11
  require_relative "tidy/filter"
7
12
 
data/lib/nanoc-tidy.rb CHANGED
@@ -1,2 +1,3 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require_relative "nanoc/tidy"
@@ -10,7 +10,7 @@ Gem::Specification.new do |gem|
10
10
  gem.licenses = ["0BSD"]
11
11
  gem.files = `git ls-files`.split($/).reject { _1.start_with?(".") }
12
12
  gem.require_paths = ["lib"]
13
- gem.summary = "nanoc-tidy.rb integrates tidy-html5 into nanoc"
13
+ gem.summary = "nanoc + tidy-html5"
14
14
  gem.description = gem.summary
15
15
  gem.add_development_dependency "yard", "~> 0.9"
16
16
  gem.add_development_dependency "redcarpet", "~> 3.5"
@@ -1,14 +1,16 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative "setup"
2
4
 
3
5
  class FilterTest < Test::Unit::TestCase
4
6
  def test_default_options
5
- options = { argv: ["--tidy-mark", "false"] }
7
+ options = {argv: ["--tidy-mark", "false"]}
6
8
  assert_equal read_result("default_options.html"),
7
9
  filter_for("fixture.html").run(html, options)
8
10
  end
9
11
 
10
12
  def test_upper_option
11
- options = { argv: ["-upper", "--tidy-mark", "false"] }
13
+ options = {argv: ["-upper", "--tidy-mark", "false"]}
12
14
  assert_equal read_result("upper_option.html"),
13
15
  filter_for("fixture.html").run(html, options)
14
16
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nanoc-tidy.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - '0x1eef'
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-04-29 00:00:00.000000000 Z
11
+ date: 2024-05-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: yard
@@ -122,7 +122,7 @@ dependencies:
122
122
  - - "~>"
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0.6'
125
- description: nanoc-tidy.rb integrates tidy-html5 into nanoc
125
+ description: nanoc + tidy-html5
126
126
  email:
127
127
  - 0x1eef@protonmail.com
128
128
  executables: []
@@ -166,5 +166,5 @@ requirements: []
166
166
  rubygems_version: 3.5.9
167
167
  signing_key:
168
168
  specification_version: 4
169
- summary: nanoc-tidy.rb integrates tidy-html5 into nanoc
169
+ summary: nanoc + tidy-html5
170
170
  test_files: []