erb-formatter 0.1.1 → 0.2.2
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 +1 -1
- data/exe/erb-format +7 -3
- data/lib/erb/formatter/ignore_list.rb +2 -2
- data/lib/erb/formatter.rb +35 -19
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b90b39bdcbed3837f51229bc9c840b0bd44d61fa48eace4e2c7b32aedcb90e3c
|
4
|
+
data.tar.gz: d42ecb4464e92ceb46b9d266647db19c88540b369acb8585bec5800b4f0d6617
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7e7ba4c9ad276e45a2ad557000fe565ed66bc9205a7aabaf432d5a99dd693758c73a7975fbe0a770fe0becd9f66be294d0f2bf168b1249b3fd4d416e20770611
|
7
|
+
data.tar.gz: 0fed7945792aa4ad161c6b784796703c00b0aef7c69c61f4d77d4cfedbfae71ee76b1d2b7eeb106e6f3ec633031878dff5fd033605d7e61e0875ce1a3902b30d
|
data/README.md
CHANGED
@@ -113,7 +113,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
113
113
|
|
114
114
|
## Contributing
|
115
115
|
|
116
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
116
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/nebulab/erb-formatter.
|
117
117
|
|
118
118
|
## License
|
119
119
|
|
data/exe/erb-format
CHANGED
@@ -25,6 +25,10 @@ OptionParser.new do |parser|
|
|
25
25
|
filename ||= '-'
|
26
26
|
end
|
27
27
|
|
28
|
+
parser.on("--[no-]debug", "Enable debug mode") do |value|
|
29
|
+
$DEBUG = value
|
30
|
+
end
|
31
|
+
|
28
32
|
parser.on("-h", "--help", "Prints this help") do
|
29
33
|
puts parser
|
30
34
|
exit
|
@@ -36,9 +40,9 @@ abort "Can't read both stdin and a list of files" if read_stdin && !ARGV.empty?
|
|
36
40
|
# If multiple files are provided assume `--write` and
|
37
41
|
# execute on each of them.
|
38
42
|
if ARGV.size > 1
|
39
|
-
ARGV.each do
|
40
|
-
warn "==> Formatting #{
|
41
|
-
system __FILE__,
|
43
|
+
ARGV.each do |arg|
|
44
|
+
warn "==> Formatting #{arg}..."
|
45
|
+
system __FILE__, arg, *[
|
42
46
|
('--write' if write)
|
43
47
|
].compact or exit(1)
|
44
48
|
end
|
@@ -7,8 +7,8 @@ class ERB::Formatter::IgnoreList
|
|
7
7
|
|
8
8
|
def should_ignore_file?(path)
|
9
9
|
path = File.expand_path(path, @base_dir)
|
10
|
-
@ignore_list.any? do
|
11
|
-
File.fnmatch? File.expand_path(
|
10
|
+
@ignore_list.any? do |line|
|
11
|
+
File.fnmatch? File.expand_path(line.chomp, @base_dir), path
|
12
12
|
end
|
13
13
|
end
|
14
14
|
end
|
data/lib/erb/formatter.rb
CHANGED
@@ -1,16 +1,13 @@
|
|
1
1
|
# frozen_string_literal: false
|
2
2
|
|
3
|
-
# $DEBUG = true
|
4
3
|
require "erb"
|
5
|
-
require "cgi"
|
6
4
|
require "ripper"
|
7
5
|
require 'securerandom'
|
8
6
|
require 'strscan'
|
9
|
-
require 'pp'
|
10
7
|
require 'stringio'
|
11
8
|
|
12
9
|
class ERB::Formatter
|
13
|
-
VERSION = "0.
|
10
|
+
VERSION = "0.2.2"
|
14
11
|
autoload :IgnoreList, 'erb/formatter/ignore_list'
|
15
12
|
|
16
13
|
class Error < StandardError; end
|
@@ -31,9 +28,11 @@ class ERB::Formatter
|
|
31
28
|
ERB_END = %r{(<%-?)\s*(end)\s*(-?%>)}
|
32
29
|
ERB_ELSE = %r{(<%-?)\s*(else|elsif\b.*)\s*(-?%>)}
|
33
30
|
|
31
|
+
TAG_NAME = /[a-z0-9_:-]+/
|
32
|
+
TAG_NAME_ONLY = /\A#{TAG_NAME}\z/
|
34
33
|
HTML_ATTR = %r{\s+#{SINGLE_QUOTE_ATTR}|\s+#{DOUBLE_QUOTE_ATTR}|\s+#{UNQUOTED_ATTR}|\s+#{ATTR_NAME}}m
|
35
|
-
HTML_TAG_OPEN = %r{<(
|
36
|
-
HTML_TAG_CLOSE = %r{</\s*(
|
34
|
+
HTML_TAG_OPEN = %r{<(#{TAG_NAME})((?:#{HTML_ATTR})*)(\s*?)(/>|>)}m
|
35
|
+
HTML_TAG_CLOSE = %r{</\s*(#{TAG_NAME})\s*>}
|
37
36
|
|
38
37
|
SELF_CLOSING_TAG = /\A(area|base|br|col|command|embed|hr|img|input|keygen|link|menuitem|meta|param|source|track|wbr)\z/i
|
39
38
|
|
@@ -62,14 +61,15 @@ class ERB::Formatter
|
|
62
61
|
new(source, filename: filename).html
|
63
62
|
end
|
64
63
|
|
65
|
-
def initialize(source, line_width: 80, filename: nil)
|
64
|
+
def initialize(source, line_width: 80, filename: nil, debug: $DEBUG)
|
66
65
|
@original_source = source
|
67
66
|
@filename = filename || '(erb)'
|
68
67
|
@line_width = line_width
|
69
68
|
@source = source.dup
|
70
69
|
@html = +""
|
70
|
+
@debug = debug
|
71
71
|
|
72
|
-
html.extend DebugShovel if
|
72
|
+
html.extend DebugShovel if @debug
|
73
73
|
|
74
74
|
@tag_stack = []
|
75
75
|
@pre_pos = 0
|
@@ -168,13 +168,13 @@ class ERB::Formatter
|
|
168
168
|
|
169
169
|
def tag_stack_push(tag_name, code)
|
170
170
|
tag_stack << [tag_name, code]
|
171
|
-
p PUSH: tag_stack if
|
171
|
+
p PUSH: tag_stack if @debug
|
172
172
|
end
|
173
173
|
|
174
174
|
def tag_stack_pop(tag_name, code)
|
175
175
|
if tag_name == tag_stack.last&.first
|
176
176
|
tag_stack.pop
|
177
|
-
p POP_: tag_stack if
|
177
|
+
p POP_: tag_stack if @debug
|
178
178
|
else
|
179
179
|
raise "Unmatched close tag, tried with #{[tag_name, code]}, but #{tag_stack.last} was on the stack"
|
180
180
|
end
|
@@ -201,6 +201,7 @@ class ERB::Formatter
|
|
201
201
|
end
|
202
202
|
|
203
203
|
def format_text(text)
|
204
|
+
p format_text: text if @debug
|
204
205
|
starting_space = text.match?(/\A\s/)
|
205
206
|
|
206
207
|
final_newlines_count = text.match(/(\s*)\z/m).captures.last.count("\n")
|
@@ -210,14 +211,23 @@ class ERB::Formatter
|
|
210
211
|
|
211
212
|
text = text.gsub(/\s+/m, ' ').strip
|
212
213
|
|
213
|
-
offset =
|
214
|
+
offset = indented("").size
|
215
|
+
# Restore full line width if there are less than 40 columns available
|
216
|
+
offset = 0 if (line_width - offset) <= 40
|
217
|
+
available_width = line_width - offset
|
218
|
+
|
214
219
|
lines = []
|
215
220
|
|
216
221
|
until text.empty?
|
217
|
-
|
222
|
+
if text.size >= available_width
|
223
|
+
last_space_index = text[0..available_width].rindex(' ')
|
224
|
+
lines << text.slice!(0..last_space_index)
|
225
|
+
else
|
226
|
+
lines << text.slice!(0..-1)
|
227
|
+
end
|
218
228
|
offset = 0
|
219
229
|
end
|
220
|
-
|
230
|
+
p lines: lines if @debug
|
221
231
|
html << lines.shift.strip unless starting_space
|
222
232
|
lines.each do |line|
|
223
233
|
html << indented(line)
|
@@ -253,7 +263,7 @@ class ERB::Formatter
|
|
253
263
|
code += "\nend" unless ERB_OPEN_BLOCK["#{code}\nend"]
|
254
264
|
code += "\n}" unless ERB_OPEN_BLOCK["#{code}\n}"]
|
255
265
|
end
|
256
|
-
p RUBY_IN_: code if
|
266
|
+
p RUBY_IN_: code if @debug
|
257
267
|
|
258
268
|
offset = tag_stack.size * 2
|
259
269
|
if defined? Rubocop
|
@@ -264,12 +274,13 @@ class ERB::Formatter
|
|
264
274
|
|
265
275
|
lines = code.strip.lines
|
266
276
|
lines = lines[0...-1] if autoclose
|
267
|
-
code = lines.map { indented(
|
268
|
-
p RUBY_OUT: code if
|
277
|
+
code = lines.map { |l| indented(l) }.join.strip
|
278
|
+
p RUBY_OUT: code if @debug
|
269
279
|
code
|
270
280
|
end
|
271
281
|
|
272
282
|
def format_erb_tags(string)
|
283
|
+
p format_erb_tags: string if @debug
|
273
284
|
if %w[style script].include?(tag_stack.last&.first)
|
274
285
|
html << string.rstrip
|
275
286
|
return
|
@@ -279,6 +290,7 @@ class ERB::Formatter
|
|
279
290
|
erb_pre_pos = 0
|
280
291
|
until erb_scanner.eos?
|
281
292
|
if erb_scanner.scan_until(erb_tags_regexp)
|
293
|
+
p PRE_MATCH: [erb_pre_pos, '..', erb_scanner.pre_match] if @debug
|
282
294
|
erb_pre_match = erb_scanner.pre_match
|
283
295
|
erb_pre_match = erb_pre_match[erb_pre_pos..]
|
284
296
|
erb_pre_pos = erb_scanner.pos
|
@@ -308,6 +320,7 @@ class ERB::Formatter
|
|
308
320
|
html << (erb_pre_match.match?(/\s+\z/) ? indented(full_erb_tag) : full_erb_tag)
|
309
321
|
end
|
310
322
|
else
|
323
|
+
p ERB_REST: erb_scanner.rest if @debug
|
311
324
|
rest = erb_scanner.rest.to_s
|
312
325
|
format_text(rest)
|
313
326
|
erb_scanner.terminate
|
@@ -319,10 +332,12 @@ class ERB::Formatter
|
|
319
332
|
scanner = StringScanner.new(source)
|
320
333
|
|
321
334
|
until scanner.eos?
|
322
|
-
|
323
335
|
if matched = scanner.scan_until(tags_regexp)
|
336
|
+
p format_pre_match: [pre_pos, '..', scanner.pre_match[pre_pos..]] if @debug
|
324
337
|
pre_match = scanner.pre_match[pre_pos..]
|
325
|
-
|
338
|
+
p POS: pre_pos...scanner.pos, advanced: source[pre_pos...scanner.pos] if @debug
|
339
|
+
p MATCHED: matched if @debug
|
340
|
+
self.pre_pos = scanner.charpos
|
326
341
|
|
327
342
|
# Don't accept `name= "value"` attributes
|
328
343
|
raise "Bad attribute, please fix spaces after the equal sign." if BAD_ATTR.match? pre_match
|
@@ -339,7 +354,7 @@ class ERB::Formatter
|
|
339
354
|
elsif matched.match(HTML_TAG_OPEN)
|
340
355
|
_, tag_name, tag_attrs, _, tag_closing = *scanner.captures
|
341
356
|
|
342
|
-
raise "Unknown tag #{tag_name.inspect}" unless tag_name.match?(
|
357
|
+
raise "Unknown tag #{tag_name.inspect}" unless tag_name.match?(TAG_NAME_ONLY)
|
343
358
|
|
344
359
|
tag_self_closing = tag_closing == '/>' || SELF_CLOSING_TAG.match?(tag_name)
|
345
360
|
tag_attrs.strip!
|
@@ -352,6 +367,7 @@ class ERB::Formatter
|
|
352
367
|
raise "Unrecognized content: #{matched.inspect}"
|
353
368
|
end
|
354
369
|
else
|
370
|
+
p format_rest: scanner.rest if @debug
|
355
371
|
format_erb_tags(scanner.rest.to_s)
|
356
372
|
scanner.terminate
|
357
373
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: erb-formatter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Elia Schito
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-03-
|
11
|
+
date: 2022-03-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rufo
|