nanoc-tidy.rb 0.4.0 → 0.5.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: d805396fd479848ce7e69377255fc4b646dd34bf35f4453337d79c3c2560bdce
4
- data.tar.gz: '08f58910fda43532c3ab1161036cdabf810067a84dc1847211f07315e433dca5'
3
+ metadata.gz: 5998921c6f4be39dbe3f20c9d248cc4d8939b905a2a08549689699aeaf6e12a4
4
+ data.tar.gz: bb4d0a22b665bac952dad499ef906d81ea85c98d2ef6a9e7027c2d0eeb2a1338
5
5
  SHA512:
6
- metadata.gz: 74c898f367043cf746d9d4a5b292d261b13ce551071232558215b9ea86573127590330534c960da49252a1ccd533008a7e9d89d133f4d9bd2b70b5391e038efc
7
- data.tar.gz: 200c914a37b4fa14b350be967643dc3a5d97f95e7eb57169724b3450805f4462de03a66e593ebcbb0fad5f171a86103c78ac2335cd5857b006b1b26d33c14b10
6
+ metadata.gz: b0d63eed8bd9e8955977420462ef0498ea5dac27f656ddcfb2cf4c1fbd7574d59590a13af1dfe81d416ade6061559f59c247b04727d9a5688b076ae760d7d28f
7
+ data.tar.gz: 8dfd5a2914ac5b3a3385c92bb65c05de74191f03b6951bb0e6667c6eafef8b9783282f8e7f28475c39343a74b3fea179f2a72846d10f911008346ae5b5e1cd43
data/README.md CHANGED
@@ -2,19 +2,20 @@
2
2
 
3
3
  nanoc-tidy.rb is a
4
4
  [nanoc](https://nanoc.app)
5
- filter that utilizes
5
+ filter that integrates
6
6
  [tidy-html5](https://github.com/htacg/tidy-html5)
7
- to nicely format, and detect / correct markup
8
- errors in HTML produced by template languages such as ERB -
9
- where misaligned indentation and other whitespace issues
10
- are often commonplace.
7
+ into nanoc. <br>
8
+ The filter can format and validate HTML produced
9
+ during the
10
+ [nanoc](https://nanoc.app)
11
+ build process.
11
12
 
12
13
  ## Examples
13
14
 
14
15
  __Defaults__
15
16
 
16
- The following is an example that uses the
17
- [default options](https://0x1eef.github.io/x/nanoc-tidy.rb/Nanoc/Tidy/Filter#default_options-class_method):
17
+ The following example uses the
18
+ [default command-line options](https://0x1eef.github.io/x/nanoc-tidy.rb/Nanoc/Tidy/Filter#default_argv-class_method):
18
19
 
19
20
  ``` ruby
20
21
  # Rules
@@ -29,7 +30,7 @@ end
29
30
 
30
31
  __Options__
31
32
 
32
- The following example forwards custom command-line options to
33
+ The following example forwards command-line options to
33
34
  [tidy-html5](https://github.com/htacg/tidy-html5):
34
35
 
35
36
  ```ruby
@@ -38,23 +39,23 @@ require "nanoc-tidy"
38
39
  compile "/index.html.erb" do
39
40
  layout("/default.*")
40
41
  filter(:erb)
41
- filter(:tidy, "-upper" => true)
42
+ filter(:tidy, argv: ["-upper"])
42
43
  write("/index.html")
43
44
  end
44
45
  ```
45
46
 
46
- ## Sources
47
+ ## <a id='install'>Install</a>
47
48
 
48
- * [Source code (GitHub)](https://github.com/0x1eef/nanoc-tidy.rb)
49
- * [Source code (GitLab)](https://gitlab.com/0x1eef/nanoc-tidy.rb)
49
+ **Rubygems.org**
50
50
 
51
- ## <a id='install'>Install</a>
51
+ nanoc-tidy.rb can be installed via rubygems.org.
52
+
53
+ gem install nanoc-tidy.rb
54
+
55
+ ## Sources
52
56
 
53
- nanoc-tidy.rb is distributed as a RubyGem through its git repositories. <br>
54
- [GitHub](https://github.com/0x1eef/nanoc-tidy.rb),
55
- and
56
- [GitLab](https://gitlab.com/0x1eef/nanoc-tidy.rb)
57
- are available as sources.
57
+ * [GitHub](https://github.com/0x1eef/nanoc-tidy.rb#readme)
58
+ * [GitLab](https://gitlab.com/0x1eef/nanoc-tidy.rb#about)
58
59
 
59
60
  ## License
60
61
 
@@ -1,91 +1,56 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class Nanoc::Tidy::Filter < Nanoc::Filter
4
- require "fileutils"
5
- include FileUtils
6
- Error = Class.new(RuntimeError)
7
-
8
- identifier :tidy
9
- type text: :text
10
-
11
- ##
12
- # @example
13
- # Nanoc::Tidy.default_options.merge!(
14
- # "-upper" => true
15
- # )
16
- #
17
- # @return [{"-wrap" => 120, "-indent" => true}]
18
- # Returns the default options forwarded as command-line
19
- # arguments to tidy-html5.
20
- def self.default_options
21
- @default_options ||= {"-wrap" => 120, "-indent" => true}
22
- end
23
-
24
- def run(content, options = {})
25
- tidy temporary_file(content),
26
- self.class.default_options.merge(options)
27
- end
28
-
29
- private
3
+ module Nanoc::Tidy
4
+ class Filter < Nanoc::Filter
5
+ require "fileutils"
6
+ require_relative "spawn"
7
+ include Spawn
8
+ include FileUtils
9
+
10
+ identifier :tidy
11
+ type text: :text
12
+
13
+ ##
14
+ # @example
15
+ # Nanoc::Tidy.default_argv.concat ["-upper"]
16
+ #
17
+ # @return [Array<String>]
18
+ # The default command line options forwarded to tidy-html5.
19
+ def self.default_argv
20
+ ["-wrap", "120", "-indent"]
21
+ end
30
22
 
31
- def temporary_file(content)
32
- dir = cwd
33
- mkdir_p(dir) unless Dir.exist?(dir)
34
- file = Tempfile.new(File.basename(item.identifier.to_s), dir)
35
- file.write(content)
36
- file.tap(&:flush)
37
- end
23
+ def run(content, options = {})
24
+ path = temporary_file(content).path
25
+ spawn tidy,
26
+ [*default_argv, *(options[:argv] || []), "-modify", path],
27
+ log: File.join(tmpdir, "tidy-html5.log")
28
+ File.read(path).tap { rm(path) }
29
+ end
38
30
 
39
- ##
40
- # tidy executable interface
31
+ private
41
32
 
42
- def tidy(file, options)
43
- Process.wait spawn(
44
- tidy_exe, "-modify", "-quiet", *tidy_args(options), file.path,
45
- spawn_options
46
- )
47
- if $?.success?
48
- File.read(file.path).tap { file.tap(&:unlink).close }
49
- else
50
- raise Error,
51
- "tidy exited unsuccessfully " \
52
- "(exit code: #{$?.exitstatus}, " \
53
- "item: #{item.identifier}, " \
54
- "log: #{log.gsub(Dir.getwd, '')[1..]})",
55
- []
33
+ def default_argv
34
+ self.class.default_argv
56
35
  end
57
- end
58
36
 
59
- def tidy_exe
60
- case
61
- when system("which tidy > /dev/null 2>&1") then "tidy"
62
- when system("which tidy5 > /dev/null 2>&1") then "tidy5"
63
- else raise Error, "unable to find a tidy executable on $PATH"
37
+ def temporary_file(content)
38
+ mkdir_p(tmpdir)
39
+ file = Tempfile.new(File.basename(item.identifier.to_s), tmpdir)
40
+ file.write(content)
41
+ file.tap(&:flush)
64
42
  end
65
- end
66
43
 
67
- def tidy_args(options)
68
- options.each_with_object([]) do |(key, value), ary|
69
- if value.equal?(true)
70
- ary << key
71
- else
72
- ary.concat [key, value.to_s]
44
+ def tidy
45
+ case
46
+ when system("which tidy > /dev/null 2>&1") then "tidy"
47
+ when system("which tidy5 > /dev/null 2>&1") then "tidy5"
48
+ else nil
73
49
  end
74
50
  end
75
- end
76
51
 
77
- ##
78
- # spawn-related methods
79
-
80
- def spawn_options
81
- { STDOUT => log, STDERR => log }
82
- end
83
-
84
- def log
85
- File.join(cwd, "tidy.log")
86
- end
87
-
88
- def cwd
89
- File.join(Dir.getwd, "tmp", "nanoc-tidy.rb")
52
+ def tmpdir
53
+ File.join(Dir.getwd, "tmp", "nanoc-tidy.rb")
54
+ end
90
55
  end
91
56
  end
@@ -0,0 +1,19 @@
1
+ module Nanoc::Tidy
2
+ module Spawn
3
+ Error = Class.new(RuntimeError)
4
+ def spawn(exe, argv, log:)
5
+ Kernel.spawn(
6
+ exe, *argv, { STDOUT => log, STDERR => log }
7
+ )
8
+ Process.wait
9
+ unless $?.success?
10
+ raise Error,
11
+ "#{File.basename(exe)} exited unsuccessfully " \
12
+ "(exit code: #{$?.exitstatus}, " \
13
+ "item: #{item.identifier}, " \
14
+ "log: #{log.gsub(Dir.getwd, '')[1..]})",
15
+ []
16
+ end
17
+ end
18
+ end
19
+ end
@@ -1,5 +1,5 @@
1
1
  module Nanoc
2
2
  module Tidy
3
- VERSION = "0.4.0"
3
+ VERSION = "0.5.0"
4
4
  end
5
5
  end
data/lib/nanoc/tidy.rb CHANGED
@@ -6,9 +6,9 @@ module Nanoc::Tidy
6
6
  require_relative "tidy/filter"
7
7
 
8
8
  ##
9
- # @example (see Nanoc::Tidy::Filter.default_options)
10
- # @return (see Nanoc::Tidy::Filter.default_options)
11
- def self.default_options
12
- Filter.default_options
9
+ # @example (see Nanoc::Tidy::Filter.default_argv)
10
+ # @return (see Nanoc::Tidy::Filter.default_argv)
11
+ def self.default_argv
12
+ Filter.default_argv
13
13
  end
14
14
  end
@@ -2,16 +2,15 @@ require_relative "setup"
2
2
 
3
3
  class FilterTest < Test::Unit::TestCase
4
4
  def test_default_options
5
- options = { "--tidy-mark" => false }
6
- assert_equal filter_for("fixture.html").run(html, options),
7
- read_result("default_options.html")
5
+ options = { argv: ["--tidy-mark", "false"] }
6
+ assert_equal read_result("default_options.html"),
7
+ filter_for("fixture.html").run(html, options)
8
8
  end
9
9
 
10
10
  def test_upper_option
11
- options = { "-upper" => true, "--tidy-mark" => false }
12
- assert_equal filter_for("fixture.html").run(html, options),
13
- read_result("upper_option.html")
14
-
11
+ options = { argv: ["-upper", "--tidy-mark", "false"] }
12
+ assert_equal read_result("upper_option.html"),
13
+ filter_for("fixture.html").run(html, options)
15
14
  end
16
15
 
17
16
  private
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.4.0
4
+ version: 0.5.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-02-26 00:00:00.000000000 Z
11
+ date: 2024-03-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: yard
@@ -122,6 +122,7 @@ files:
122
122
  - lib/nanoc-tidy.rb
123
123
  - lib/nanoc/tidy.rb
124
124
  - lib/nanoc/tidy/filter.rb
125
+ - lib/nanoc/tidy/spawn.rb
125
126
  - lib/nanoc/tidy/version.rb
126
127
  - nanoc-tidy.rb.gemspec
127
128
  - test/fixtures/fixture.html