htmlbeautifier 1.3.1 → 1.4.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
- SHA1:
3
- metadata.gz: b9b315f8ad4b0cf0028ab69db1bdae35dd00c4a4
4
- data.tar.gz: b7a5c639356777eca4411779b489382111547a99
2
+ SHA256:
3
+ metadata.gz: 2cd83943c1a2aa479dd33fb87de1cdb14aa9dd462760c9d20b25c93cbc86f079
4
+ data.tar.gz: e1aa440e4c79f663cfacb1946c95c15094e77d636b938bc7f33a4cd04ebd9969
5
5
  SHA512:
6
- metadata.gz: f3f3ff9b529fca273e945ecd9a0463673d967160dc12068604a6764e2df2efab6c69da563b217f874fdb93bcc3c816a10a58ebafa6a17f1fbc4b74790597b169
7
- data.tar.gz: b3ff2f097c7d4904538f14995ca2071b3aa27fc63cf057e04c0e43fb4eba42b9d7f76f72f61be78c35fd321df6523e86571d2a6aa3a2a03bbbe55c2a9410fa8a
6
+ metadata.gz: 4d427b44b461d784f4cc3e19f0c1aef81153f6c821b7bb956d78b75d8710be862a13f0765a1940612dc3da8359bb04a03d8c39898356b30d43000e5b5979e599
7
+ data.tar.gz: 4c8873331ebb4a02ede609d548caed30fe612e7a5d113733bdd68090881e3424c7fd94aa810191a5bb20751113056c1e36752f81f8d0c0df2bdeb56e7a7ad9ea
data/bin/htmlbeautifier CHANGED
@@ -1,11 +1,14 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
2
4
  require "htmlbeautifier"
3
5
  require "optparse"
4
6
  require "fileutils"
7
+ require "stringio"
5
8
 
6
9
  def beautify(name, input, output, options)
7
10
  output.puts HtmlBeautifier.beautify(input, options)
8
- rescue => e
11
+ rescue StandardError => e
9
12
  raise "Error parsing #{name}: #{e}"
10
13
  end
11
14
 
@@ -14,18 +17,18 @@ executable = File.basename(__FILE__)
14
17
  options = { indent: " " }
15
18
  parser = OptionParser.new do |opts|
16
19
  opts.banner = "Usage: #{executable} [options] [file ...]"
17
- opts.separator <<END
20
+ opts.separator <<~STRING
18
21
 
19
- #{executable} has two modes of operation:
22
+ #{executable} has two modes of operation:
20
23
 
21
- 1. If no files are listed, it will read from standard input and write to
22
- standard output.
23
- 2. If files are listed, it will modify each file in place, overwriting it
24
- with the beautified output.
24
+ 1. If no files are listed, it will read from standard input and write to
25
+ standard output.
26
+ 2. If files are listed, it will modify each file in place, overwriting it
27
+ with the beautified output.
25
28
 
26
- The following options are available:
29
+ The following options are available:
27
30
 
28
- END
31
+ STRING
29
32
  opts.on(
30
33
  "-t", "--tab-stops NUMBER", Integer,
31
34
  "Set number of spaces per indent (default #{options[:tab_stops]})"
@@ -56,6 +59,20 @@ END
56
59
  ) do |num|
57
60
  options[:keep_blank_lines] = num
58
61
  end
62
+ opts.on(
63
+ "-l", "--lint-only",
64
+ "Lint only, error on files which would be modified",
65
+ "This is not available when reading from standard input"
66
+ ) do |num|
67
+ options[:lint_only] = num
68
+ end
69
+ opts.on(
70
+ "-v", "--version",
71
+ "Display version and exit"
72
+ ) do
73
+ puts HtmlBeautifier::VERSION::STRING
74
+ exit
75
+ end
59
76
  opts.on(
60
77
  "-h", "--help",
61
78
  "Display this help message and exit"
@@ -64,16 +81,34 @@ END
64
81
  exit
65
82
  end
66
83
  end
84
+
67
85
  parser.parse!
68
86
 
69
87
  if ARGV.any?
88
+ failures = []
70
89
  ARGV.each do |path|
71
90
  input = File.read(path)
72
- temppath = path + ".tmp"
73
- File.open(temppath, "w") do |output|
91
+ if options[:lint_only]
92
+ output = StringIO.new
74
93
  beautify path, input, output, options
94
+ failures << path unless input == output.string
95
+ else
96
+ temppath = "#{path}.tmp"
97
+ File.open(temppath, "w") do |file|
98
+ beautify path, input, file, options
99
+ end
100
+ FileUtils.mv temppath, path
75
101
  end
76
- FileUtils.mv temppath, path
102
+ end
103
+ unless failures.empty?
104
+ relative_paths = failures.map { |path|
105
+ Pathname.new(path).relative_path_from Dir.pwd
106
+ }
107
+ $stderr.puts [
108
+ "Lint failed - files would be modified:",
109
+ *relative_paths
110
+ ].join("\n")
111
+ exit 1
77
112
  end
78
113
  else
79
114
  beautify "standard input", $stdin.read, $stdout, options
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "htmlbeautifier/parser"
2
4
  require "htmlbeautifier/ruby_indenter"
3
5
 
@@ -8,7 +10,7 @@ module HtmlBeautifier
8
10
  initial_level: 0,
9
11
  stop_on_errors: false,
10
12
  keep_blank_lines: 0
11
- }
13
+ }.freeze
12
14
 
13
15
  def initialize(output, options = {})
14
16
  options = DEFAULT_OPTIONS.merge(options)
@@ -27,6 +29,7 @@ module HtmlBeautifier
27
29
 
28
30
  def error(text)
29
31
  return unless @stop_on_errors
32
+
30
33
  raise text
31
34
  end
32
35
 
@@ -40,9 +43,10 @@ module HtmlBeautifier
40
43
  end
41
44
 
42
45
  def emit(*strings)
46
+ strings_join = strings.join("")
43
47
  @output << "\n" if @new_line && !@empty
44
- @output << (@tab * @level) if @new_line
45
- @output << strings.join("")
48
+ @output << (@tab * @level) if @new_line && !strings_join.strip.empty?
49
+ @output << strings_join
46
50
  @new_line = false
47
51
  @empty = false
48
52
  end
@@ -87,42 +91,42 @@ module HtmlBeautifier
87
91
  new_line
88
92
  end
89
93
 
90
- def standalone_element(e)
91
- emit e
92
- new_line if e =~ %r{^<br[^\w]}
94
+ def standalone_element(elem)
95
+ emit elem
96
+ new_line if elem =~ %r{^<br[^\w]}
93
97
  end
94
98
 
95
- def close_element(e)
99
+ def close_element(elem)
96
100
  outdent
97
- emit e
101
+ emit elem
98
102
  end
99
103
 
100
- def close_block_element(e)
101
- close_element e
104
+ def close_block_element(elem)
105
+ close_element elem
102
106
  new_line
103
107
  end
104
108
 
105
- def open_element(e)
106
- emit e
109
+ def open_element(elem)
110
+ emit elem
107
111
  indent
108
112
  end
109
113
 
110
- def open_block_element(e)
114
+ def open_block_element(elem)
111
115
  new_line
112
- open_element e
116
+ open_element elem
113
117
  end
114
118
 
115
- def close_ie_cc(e)
119
+ def close_ie_cc(elem)
116
120
  if @ie_cc_levels.empty?
117
121
  error "Unclosed conditional comment"
118
122
  else
119
123
  @level = @ie_cc_levels.pop
120
124
  end
121
- emit e
125
+ emit elem
122
126
  end
123
127
 
124
- def open_ie_cc(e)
125
- emit e
128
+ def open_ie_cc(elem)
129
+ emit elem
126
130
  @ie_cc_levels.push @level
127
131
  indent
128
132
  end
@@ -130,12 +134,10 @@ module HtmlBeautifier
130
134
  def new_lines(*content)
131
135
  blank_lines = content.first.scan(%r{\n}).count - 1
132
136
  blank_lines = [blank_lines, @keep_blank_lines].min
133
- @output << "\n" * blank_lines
137
+ @output << ("\n" * blank_lines)
134
138
  new_line
135
139
  end
136
140
 
137
- def text(t)
138
- emit t
139
- end
141
+ alias_method :text, :emit
140
142
  end
141
143
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "htmlbeautifier/parser"
2
4
 
3
5
  module HtmlBeautifier
@@ -35,20 +37,21 @@ module HtmlBeautifier
35
37
  :preformatted_block],
36
38
  [%r{<#{HTML_VOID_ELEMENTS}(?: #{ELEMENT_CONTENT})?/?>}om,
37
39
  :standalone_element],
38
- [%r{<\w+(?: #{ELEMENT_CONTENT})?/>}om,
39
- :standalone_element],
40
40
  [%r{</#{HTML_BLOCK_ELEMENTS}>}om,
41
41
  :close_block_element],
42
42
  [%r{<#{HTML_BLOCK_ELEMENTS}(?: #{ELEMENT_CONTENT})?>}om,
43
43
  :open_block_element],
44
44
  [%r{</#{ELEMENT_CONTENT}>}om,
45
45
  :close_element],
46
- [%r{<#{ELEMENT_CONTENT}>}om,
46
+ [%r{<#{ELEMENT_CONTENT}[^/]>}om,
47
47
  :open_element],
48
+ [%r{<\w+(?: #{ELEMENT_CONTENT})?/>}om,
49
+ :standalone_element],
48
50
  [%r{(\s*\r?\n\s*)+}om,
49
51
  :new_lines],
50
52
  [%r{[^<\n]+},
51
- :text]]
53
+ :text]
54
+ ].freeze
52
55
 
53
56
  def initialize
54
57
  super do |p|
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "strscan"
2
4
 
3
5
  module HtmlBeautifier
@@ -29,13 +31,15 @@ module HtmlBeautifier
29
31
  def dispatch(receiver)
30
32
  _, method = @maps.find { |pattern, _| @scanner.scan(pattern) }
31
33
  raise "Unmatched sequence" unless method
34
+
32
35
  receiver.__send__(method, *extract_params(@scanner))
33
- rescue => ex
34
- raise "#{ex.message} on line #{source_line_number}"
36
+ rescue StandardError => e
37
+ raise "#{e.message} on line #{source_line_number}"
35
38
  end
36
39
 
37
40
  def extract_params(scanner)
38
41
  return [scanner[0]] unless scanner[1]
42
+
39
43
  params = []
40
44
  i = 1
41
45
  while scanner[i]
@@ -1,10 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module HtmlBeautifier
2
4
  class RubyIndenter
3
- INDENT_KEYWORDS = %w[ if elsif else unless while until begin for ]
4
- OUTDENT_KEYWORDS = %w[ elsif else end ]
5
- RUBY_INDENT = %r{
5
+ INDENT_KEYWORDS = %w[if elsif else unless while until begin for].freeze
6
+ OUTDENT_KEYWORDS = %w[elsif else end].freeze
7
+ RUBY_INDENT = %r{
6
8
  ^ ( #{INDENT_KEYWORDS.join("|")} )\b
7
- | \b ( do | \{ ) ( \s* \| [^\|]+ \| )? $
9
+ | \b ( do | \{ ) ( \s* \| [^|]+ \| )? $
8
10
  }xo
9
11
  RUBY_OUTDENT = %r{ ^ ( #{OUTDENT_KEYWORDS.join("|")} | \} ) \b }xo
10
12
 
@@ -1,8 +1,10 @@
1
- module HtmlBeautifier #:nodoc:
2
- module VERSION #:nodoc:
1
+ # frozen_string_literal: true
2
+
3
+ module HtmlBeautifier # :nodoc:
4
+ module VERSION # :nodoc:
3
5
  MAJOR = 1
4
- MINOR = 3
5
- TINY = 1
6
+ MINOR = 4
7
+ TINY = 0
6
8
 
7
9
  STRING = [MAJOR, MINOR, TINY].join(".")
8
10
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "htmlbeautifier/builder"
2
4
  require "htmlbeautifier/html_parser"
3
5
  require "htmlbeautifier/version"
@@ -22,7 +24,7 @@ module HtmlBeautifier
22
24
  if options[:tab_stops]
23
25
  options[:indent] = " " * options[:tab_stops]
24
26
  end
25
- "".tap { |output|
27
+ String.new.tap { |output|
26
28
  HtmlParser.new.scan html.to_s, Builder.new(output, options)
27
29
  }
28
30
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: htmlbeautifier
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.1
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Battley
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-04 00:00:00.000000000 Z
11
+ date: 2021-11-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: '13'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: '13'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rspec
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -44,14 +44,42 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: 0.30.0
47
+ version: '1'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: 0.30.0
54
+ version: '1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop-rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop-rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.6'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.6'
55
83
  description: A normaliser/beautifier for HTML that also understands embedded Ruby.
56
84
  email: pbattley@gmail.com
57
85
  executables:
@@ -80,15 +108,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
80
108
  requirements:
81
109
  - - ">="
82
110
  - !ruby/object:Gem::Version
83
- version: 1.9.2
111
+ version: 2.6.0
84
112
  required_rubygems_version: !ruby/object:Gem::Requirement
85
113
  requirements:
86
114
  - - ">="
87
115
  - !ruby/object:Gem::Version
88
116
  version: '0'
89
117
  requirements: []
90
- rubyforge_project:
91
- rubygems_version: 2.4.5
118
+ rubygems_version: 3.2.22
92
119
  signing_key:
93
120
  specification_version: 4
94
121
  summary: HTML/ERB beautifier