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 +4 -4
- data/README.md +19 -18
- data/lib/nanoc/tidy/filter.rb +42 -77
- data/lib/nanoc/tidy/spawn.rb +19 -0
- data/lib/nanoc/tidy/version.rb +1 -1
- data/lib/nanoc/tidy.rb +4 -4
- data/test/tidy_filter_test.rb +6 -7
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5998921c6f4be39dbe3f20c9d248cc4d8939b905a2a08549689699aeaf6e12a4
|
4
|
+
data.tar.gz: bb4d0a22b665bac952dad499ef906d81ea85c98d2ef6a9e7027c2d0eeb2a1338
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
5
|
+
filter that integrates
|
6
6
|
[tidy-html5](https://github.com/htacg/tidy-html5)
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
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
|
17
|
-
[default options](https://0x1eef.github.io/x/nanoc-tidy.rb/Nanoc/Tidy/Filter#
|
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
|
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"
|
42
|
+
filter(:tidy, argv: ["-upper"])
|
42
43
|
write("/index.html")
|
43
44
|
end
|
44
45
|
```
|
45
46
|
|
46
|
-
##
|
47
|
+
## <a id='install'>Install</a>
|
47
48
|
|
48
|
-
|
49
|
-
* [Source code (GitLab)](https://gitlab.com/0x1eef/nanoc-tidy.rb)
|
49
|
+
**Rubygems.org**
|
50
50
|
|
51
|
-
|
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
|
54
|
-
[
|
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
|
|
data/lib/nanoc/tidy/filter.rb
CHANGED
@@ -1,91 +1,56 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
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
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
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
|
-
|
43
|
-
|
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
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
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
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
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
|
-
|
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
|
data/lib/nanoc/tidy/version.rb
CHANGED
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.
|
10
|
-
# @return (see Nanoc::Tidy::Filter.
|
11
|
-
def self.
|
12
|
-
Filter.
|
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
|
data/test/tidy_filter_test.rb
CHANGED
@@ -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"
|
6
|
-
assert_equal
|
7
|
-
|
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"
|
12
|
-
assert_equal
|
13
|
-
|
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
|
+
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-
|
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
|