nanoc-tidy.rb 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 82bb7fbdab23c96d587126887440424d6952c7e083fafb48385d1daad72459ab
4
+ data.tar.gz: 72960c21694c0a722fac6c6a3c09b2b78250fc3033dac85b6c11ed838f001726
5
+ SHA512:
6
+ metadata.gz: 48d64352f129d1593b0e6f787a53a15cf3dc72b1700d7dc82d8c60aaf6b8895345f54f612305d67f1868a95adfd3cdecd544343b91844adc1bab47dbdd4c0d59
7
+ data.tar.gz: feb6516507e522d664dfb9194efb2897f0bdea0bf9515276e3be84ee27ab493f4281d16c723048c88e9033d203f532af449bae803912bdc14b6f31c6e716b2ea
data/.bundle/config ADDED
@@ -0,0 +1,2 @@
1
+ ---
2
+ BUNDLE_PATH: ".gems"
@@ -0,0 +1,24 @@
1
+ name: nanoc-tidy.rb
2
+
3
+ on:
4
+ push:
5
+ branches: [ main ]
6
+ pull_request:
7
+ branches: [ main ]
8
+
9
+ jobs:
10
+ specs:
11
+ strategy:
12
+ fail-fast: false
13
+ matrix:
14
+ os: [ubuntu-latest]
15
+ ruby: [3.1, 3.2]
16
+ runs-on: ${{ matrix.os }}
17
+ steps:
18
+ - uses: actions/checkout@v2
19
+ - uses: ruby/setup-ruby@v1
20
+ with:
21
+ ruby-version: ${{ matrix.ruby }}
22
+ - run: sudo apt-get install tidy
23
+ - run: bundle install
24
+ - run: rake
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ .gems/
2
+ *.lock
data/.projectile ADDED
@@ -0,0 +1,6 @@
1
+ -.gems/
2
+ +.github/
3
+ +lib/
4
+ +test/
5
+ +.bundle/
6
+ +.
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "https://rubygems.org"
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,15 @@
1
+ Copyright (C) 2023 by 0x1eef <0x1eef@protonmail.com>
2
+
3
+ Permission to use, copy, modify, and/or distribute this
4
+ software for any purpose with or without fee is hereby
5
+ granted.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS
8
+ ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
9
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
10
+ EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
11
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
12
+ RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
14
+ ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
15
+ OF THIS SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,68 @@
1
+ ## About
2
+
3
+ nanoc-tidy.rb is a
4
+ [nanoc](https://nanoc.app)
5
+ filter that utilizes
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.
11
+
12
+ ## Examples
13
+
14
+ __Defaults__
15
+
16
+ The following is an example with the
17
+ [default options](https://0x1eef.github.io/x/nanoc-tidy.rb/Nanoc/Tidy/Filter#default_options-class_method)
18
+ being used:
19
+
20
+ ``` ruby
21
+ # Rules
22
+ require "nanoc-tidy"
23
+ compile "/index.html.erb" do
24
+ filter(:erb)
25
+ filter(:tidy)
26
+ write("/index.html")
27
+ end
28
+ ```
29
+
30
+ __Options__
31
+
32
+ The following example forwards command-line options to
33
+ [tidy-html5](https://github.com/htacg/tidy-html5):
34
+
35
+ ```ruby
36
+ # Rules
37
+ require "nanoc-tidy"
38
+ compile "/index.html.erb" do
39
+ filter(:erb)
40
+ filter(:tidy, "-upper" => true)
41
+ write("/index.html")
42
+ end
43
+ ```
44
+
45
+ ## Sources
46
+
47
+ * [Source code (GitHub)](https://github.com/0x1eef/nanoc-tidy.rb)
48
+ * [Source code (GitLab)](https://gitlab.com/0x1eef/nanoc-tidy.rb)
49
+
50
+ ## <a id='install'>Install</a>
51
+
52
+ nanoc-tidy.rb is distributed as a RubyGem through its git repositories. <br>
53
+ [GitHub](https://github.com/0x1eef/nanoc-tidy.rb),
54
+ and
55
+ [GitLab](https://gitlab.com/0x1eef/nanoc-tidy.rb)
56
+ are available as sources.
57
+
58
+ **Gemfile**
59
+
60
+ ```ruby
61
+ gem "nanoc-tidy.rb", github: "0x1eef/nanoc-tidy.rb"
62
+ ```
63
+
64
+ ## License
65
+
66
+ [BSD Zero Clause](https://choosealicense.com/licenses/0bsd/).
67
+ <br>
68
+ See [LICENSE](./LICENSE).
data/Rakefile.rb ADDED
@@ -0,0 +1,8 @@
1
+ require "rake/testtask"
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.test_files = FileList['test/*_test.rb']
5
+ t.verbose = true
6
+ t.warning = false
7
+ end
8
+ task default: :test
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
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
+ # @return [{"-wrap" => 120, "-indent" => true}]
13
+ # Returns the default options forwarded as command-line
14
+ # arguments to tidy-html5.
15
+ def self.default_options
16
+ @default_options ||= {"-wrap" => 120, "-indent" => true}
17
+ end
18
+
19
+ def run(content, options = {})
20
+ file = temporary_file_for(content)
21
+ tidy file, self.class.default_options.merge(options)
22
+ end
23
+
24
+ private
25
+
26
+ def tidy(file, options)
27
+ system "tidy", "-modify", "-quiet", *tidy_args(options), file.path
28
+ if $?.success?
29
+ File.read(file.path).tap { file.tap(&:unlink).close }
30
+ else
31
+ raise Error, "tidy exited unsuccessfully (exit code: #{$?.exitstatus})", []
32
+ end
33
+ end
34
+
35
+ def tidy_args(options)
36
+ options.each_with_object([]) do |(key, value), ary|
37
+ if value.equal?(true)
38
+ ary << key
39
+ else
40
+ ary.concat [key, value.to_s]
41
+ end
42
+ end
43
+ end
44
+
45
+ def temporary_file_for(content)
46
+ dir = File.join(Dir.getwd, "tmp", "nanoc-tidy.rb")
47
+ mkdir_p(dir) unless Dir.exist?(dir)
48
+ file = Tempfile.new(File.basename(item.identifier.to_s), dir)
49
+ file.write(content)
50
+ file.tap(&:flush)
51
+ end
52
+ end
@@ -0,0 +1,5 @@
1
+ module Nanoc
2
+ module Tidy
3
+ VERSION = "0.2.0"
4
+ end
5
+ end
data/lib/nanoc/tidy.rb ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "nanoc"
4
+ module Nanoc::Tidy
5
+ require_relative "tidy/version"
6
+ require_relative "tidy/filter"
7
+ end
data/lib/nanoc-tidy.rb ADDED
@@ -0,0 +1,2 @@
1
+ # frozen_string_literal: true
2
+ require_relative "nanoc/tidy"
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "./lib/nanoc/tidy/version"
4
+ Gem::Specification.new do |gem|
5
+ gem.name = "nanoc-tidy.rb"
6
+ gem.authors = ["0x1eef"]
7
+ gem.email = ["0x1eef@protonmail.com"]
8
+ gem.homepage = "https://github.com/0x1eef/nanoc-tidy.rb#readme"
9
+ gem.version = Nanoc::Tidy::VERSION
10
+ gem.licenses = ["0BSD"]
11
+ gem.files = `git ls-files`.split($/)
12
+ gem.require_paths = ["lib"]
13
+ gem.summary = "nanoc-tidy.rb integrates tidy-html5 into nanoc."
14
+ gem.description = gem.summary
15
+ gem.add_development_dependency "yard", "~> 0.9"
16
+ gem.add_development_dependency "redcarpet", "~> 3.5"
17
+ gem.add_development_dependency "test-unit", "~> 3.6"
18
+ gem.add_development_dependency "standard", "~> 1.13"
19
+ gem.add_development_dependency "nanoc", "~> 4.12"
20
+ gem.add_development_dependency "rack", "~> 3.0"
21
+ gem.add_development_dependency "rackup", "~> 2.1"
22
+ end
@@ -0,0 +1,11 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>1.html</title>
5
+ </head>
6
+ <body>
7
+ <p>
8
+ foo
9
+ </p>
10
+ </body>
11
+ </html>
@@ -0,0 +1,9 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>1.html</title>
5
+ </head>
6
+ <body>
7
+ <p>foo</p>
8
+ </body>
9
+ </html>
@@ -0,0 +1,9 @@
1
+ <!DOCTYPE html>
2
+ <HTML>
3
+ <HEAD>
4
+ <TITLE>1.html</TITLE>
5
+ </HEAD>
6
+ <BODY>
7
+ <P>foo</P>
8
+ </BODY>
9
+ </HTML>
data/test/setup.rb ADDED
@@ -0,0 +1,3 @@
1
+ require "bundler/setup"
2
+ require "test/unit"
3
+ require "nanoc-tidy"
@@ -0,0 +1,41 @@
1
+ require_relative "setup"
2
+
3
+ class FilterTest < Test::Unit::TestCase
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")
8
+ end
9
+
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
+
15
+ end
16
+
17
+ private
18
+
19
+ def filter_for(basename)
20
+ @filter ||= begin
21
+ filter = Nanoc::Tidy::Filter.new
22
+ filter.extend module_for(basename)
23
+ end
24
+ end
25
+
26
+ def module_for(basename)
27
+ Module.new do
28
+ define_method(:item) do
29
+ OpenStruct.new(identifier: basename)
30
+ end
31
+ end
32
+ end
33
+
34
+ def read_result(basename)
35
+ File.binread File.join("test", "results", basename)
36
+ end
37
+
38
+ def html
39
+ File.binread "./test/fixtures/fixture.html"
40
+ end
41
+ end
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nanoc-tidy.rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - '0x1eef'
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-12-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: yard
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: redcarpet
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: test-unit
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: standard
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.13'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.13'
69
+ - !ruby/object:Gem::Dependency
70
+ name: nanoc
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '4.12'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '4.12'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rack
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rackup
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '2.1'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '2.1'
111
+ description: nanoc-tidy.rb integrates tidy-html5 into nanoc.
112
+ email:
113
+ - 0x1eef@protonmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".bundle/config"
119
+ - ".github/workflows/tests.yml"
120
+ - ".gitignore"
121
+ - ".projectile"
122
+ - Gemfile
123
+ - LICENSE
124
+ - README.md
125
+ - Rakefile.rb
126
+ - lib/nanoc-tidy.rb
127
+ - lib/nanoc/tidy.rb
128
+ - lib/nanoc/tidy/filter.rb
129
+ - lib/nanoc/tidy/version.rb
130
+ - nanoc-tidy.rb.gemspec
131
+ - test/fixtures/fixture.html
132
+ - test/results/default_options.html
133
+ - test/results/upper_option.html
134
+ - test/setup.rb
135
+ - test/tidy_filter_test.rb
136
+ homepage: https://github.com/0x1eef/nanoc-tidy.rb#readme
137
+ licenses:
138
+ - 0BSD
139
+ metadata: {}
140
+ post_install_message:
141
+ rdoc_options: []
142
+ require_paths:
143
+ - lib
144
+ required_ruby_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ required_rubygems_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ requirements: []
155
+ rubygems_version: 3.4.10
156
+ signing_key:
157
+ specification_version: 4
158
+ summary: nanoc-tidy.rb integrates tidy-html5 into nanoc.
159
+ test_files: []