nanoc-tidy.rb 0.3.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 377f965bc7630e3d0208011340dedea0c8e1463d17755d2ced834a5a2b10cec1
4
- data.tar.gz: 2cac200122ecebbfcc9b0b28a8264bc83a0706ed8cdc4fdb4524e121aa977510
3
+ metadata.gz: 5998921c6f4be39dbe3f20c9d248cc4d8939b905a2a08549689699aeaf6e12a4
4
+ data.tar.gz: bb4d0a22b665bac952dad499ef906d81ea85c98d2ef6a9e7027c2d0eeb2a1338
5
5
  SHA512:
6
- metadata.gz: '0159259928a18678becc9df9ada135133f8fbd917e15a479cc81d97d42cc8cb70ffc1df1a9aac1e5828dbb5af83e36163920c9ca288873185c14fc41eb541bb2'
7
- data.tar.gz: 64592fabd2529dfa1b28f6c6f423702a0c38ae0dd72edcb029cd57818235533f4d750b78ad4667bf426a0560432c70e31193693c191133edc345d769150cf51b
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,65 +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
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
10
22
 
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
+ 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
23
30
 
24
- def run(content, options = {})
25
- file = temporary_file_for(content)
26
- tidy file, self.class.default_options.merge(options)
27
- end
31
+ private
28
32
 
29
- private
33
+ def default_argv
34
+ self.class.default_argv
35
+ end
30
36
 
31
- def tidy(file, options)
32
- system tidy_exe, "-modify", "-quiet", *tidy_args(options), file.path
33
- if $?.success?
34
- File.read(file.path).tap { file.tap(&:unlink).close }
35
- else
36
- raise Error, "tidy exited unsuccessfully (exit code: #{$?.exitstatus})", []
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)
37
42
  end
38
- end
39
43
 
40
- def tidy_args(options)
41
- options.each_with_object([]) do |(key, value), ary|
42
- if value.equal?(true)
43
- ary << key
44
- else
45
- 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
46
49
  end
47
50
  end
48
- end
49
51
 
50
- def tidy_exe
51
- case
52
- when system("which tidy > /dev/null 2>&1") then "tidy"
53
- when system("which tidy5 > /dev/null 2>&1") then "tidy5"
54
- else raise Error, "unable to find a tidy executable on $PATH"
52
+ def tmpdir
53
+ File.join(Dir.getwd, "tmp", "nanoc-tidy.rb")
55
54
  end
56
55
  end
57
-
58
- def temporary_file_for(content)
59
- dir = File.join(Dir.getwd, "tmp", "nanoc-tidy.rb")
60
- mkdir_p(dir) unless Dir.exist?(dir)
61
- file = Tempfile.new(File.basename(item.identifier.to_s), dir)
62
- file.write(content)
63
- file.tap(&:flush)
64
- end
65
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.3.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.3.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-25 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