erb-formatter 0.1.0 → 0.2.1
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/CHANGELOG.md +1 -5
- 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 +32 -15
- metadata +2 -3
- data/Gemfile.lock +0 -23
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5673c7c32218808dc8ab3b9a2e7df06036b99135fe3f0735c82d498feb83e0e4
|
4
|
+
data.tar.gz: e4e2a94113a06512eb4ddb8bcdc8408c5488d53b77de5d4813e21d97f5dd7c75
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f6a3681798995f41e9295db63ec9c0c2fd948d43ccfd4cefd26a83003baeb67fd9c409b7ad8e0904dfe8adc8b1102a18552902e4ed62453c198f6ac4fdbcfa31
|
7
|
+
data.tar.gz: 5d6e3957f8817519b44baeba7e6ccfbbac3e2baef7ae8479cb016020f454e7df08fe5f43bd72cbc97950eac833b604393b9414760b82cf78ec6dfed85fab5723
|
data/CHANGELOG.md
CHANGED
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,6 +1,6 @@
|
|
1
|
-
# frozen_string_literal:
|
1
|
+
# frozen_string_literal: false
|
2
2
|
|
3
|
-
#
|
3
|
+
# @debug = true
|
4
4
|
require "erb"
|
5
5
|
require "cgi"
|
6
6
|
require "ripper"
|
@@ -10,7 +10,7 @@ require 'pp'
|
|
10
10
|
require 'stringio'
|
11
11
|
|
12
12
|
class ERB::Formatter
|
13
|
-
VERSION = "0.1
|
13
|
+
VERSION = "0.2.1"
|
14
14
|
autoload :IgnoreList, 'erb/formatter/ignore_list'
|
15
15
|
|
16
16
|
class Error < StandardError; end
|
@@ -62,14 +62,15 @@ class ERB::Formatter
|
|
62
62
|
new(source, filename: filename).html
|
63
63
|
end
|
64
64
|
|
65
|
-
def initialize(source, line_width: 80, filename: nil)
|
65
|
+
def initialize(source, line_width: 80, filename: nil, debug: $DEBUG)
|
66
66
|
@original_source = source
|
67
67
|
@filename = filename || '(erb)'
|
68
68
|
@line_width = line_width
|
69
69
|
@source = source.dup
|
70
70
|
@html = +""
|
71
|
+
@debug = debug
|
71
72
|
|
72
|
-
html.extend DebugShovel if
|
73
|
+
html.extend DebugShovel if @debug
|
73
74
|
|
74
75
|
@tag_stack = []
|
75
76
|
@pre_pos = 0
|
@@ -168,13 +169,13 @@ class ERB::Formatter
|
|
168
169
|
|
169
170
|
def tag_stack_push(tag_name, code)
|
170
171
|
tag_stack << [tag_name, code]
|
171
|
-
p PUSH: tag_stack if
|
172
|
+
p PUSH: tag_stack if @debug
|
172
173
|
end
|
173
174
|
|
174
175
|
def tag_stack_pop(tag_name, code)
|
175
176
|
if tag_name == tag_stack.last&.first
|
176
177
|
tag_stack.pop
|
177
|
-
p POP_: tag_stack if
|
178
|
+
p POP_: tag_stack if @debug
|
178
179
|
else
|
179
180
|
raise "Unmatched close tag, tried with #{[tag_name, code]}, but #{tag_stack.last} was on the stack"
|
180
181
|
end
|
@@ -201,6 +202,7 @@ class ERB::Formatter
|
|
201
202
|
end
|
202
203
|
|
203
204
|
def format_text(text)
|
205
|
+
p format_text: text if @debug
|
204
206
|
starting_space = text.match?(/\A\s/)
|
205
207
|
|
206
208
|
final_newlines_count = text.match(/(\s*)\z/m).captures.last.count("\n")
|
@@ -210,14 +212,23 @@ class ERB::Formatter
|
|
210
212
|
|
211
213
|
text = text.gsub(/\s+/m, ' ').strip
|
212
214
|
|
213
|
-
offset =
|
215
|
+
offset = indented("").size
|
216
|
+
# Restore full line width if there are less than 40 columns available
|
217
|
+
offset = 0 if (line_width - offset) <= 40
|
218
|
+
available_width = line_width - offset
|
219
|
+
|
214
220
|
lines = []
|
215
221
|
|
216
222
|
until text.empty?
|
217
|
-
|
223
|
+
if text.size >= available_width
|
224
|
+
last_space_index = text[0..available_width].rindex(' ')
|
225
|
+
lines << text.slice!(0..last_space_index)
|
226
|
+
else
|
227
|
+
lines << text.slice!(0..-1)
|
228
|
+
end
|
218
229
|
offset = 0
|
219
230
|
end
|
220
|
-
|
231
|
+
p lines: lines if @debug
|
221
232
|
html << lines.shift.strip unless starting_space
|
222
233
|
lines.each do |line|
|
223
234
|
html << indented(line)
|
@@ -253,7 +264,7 @@ class ERB::Formatter
|
|
253
264
|
code += "\nend" unless ERB_OPEN_BLOCK["#{code}\nend"]
|
254
265
|
code += "\n}" unless ERB_OPEN_BLOCK["#{code}\n}"]
|
255
266
|
end
|
256
|
-
p RUBY_IN_: code if
|
267
|
+
p RUBY_IN_: code if @debug
|
257
268
|
|
258
269
|
offset = tag_stack.size * 2
|
259
270
|
if defined? Rubocop
|
@@ -264,12 +275,13 @@ class ERB::Formatter
|
|
264
275
|
|
265
276
|
lines = code.strip.lines
|
266
277
|
lines = lines[0...-1] if autoclose
|
267
|
-
code = lines.map { indented(
|
268
|
-
p RUBY_OUT: code if
|
278
|
+
code = lines.map { |l| indented(l) }.join.strip
|
279
|
+
p RUBY_OUT: code if @debug
|
269
280
|
code
|
270
281
|
end
|
271
282
|
|
272
283
|
def format_erb_tags(string)
|
284
|
+
p format_erb_tags: string if @debug
|
273
285
|
if %w[style script].include?(tag_stack.last&.first)
|
274
286
|
html << string.rstrip
|
275
287
|
return
|
@@ -279,6 +291,7 @@ class ERB::Formatter
|
|
279
291
|
erb_pre_pos = 0
|
280
292
|
until erb_scanner.eos?
|
281
293
|
if erb_scanner.scan_until(erb_tags_regexp)
|
294
|
+
p PRE_MATCH: [erb_pre_pos, '..', erb_scanner.pre_match] if @debug
|
282
295
|
erb_pre_match = erb_scanner.pre_match
|
283
296
|
erb_pre_match = erb_pre_match[erb_pre_pos..]
|
284
297
|
erb_pre_pos = erb_scanner.pos
|
@@ -308,6 +321,7 @@ class ERB::Formatter
|
|
308
321
|
html << (erb_pre_match.match?(/\s+\z/) ? indented(full_erb_tag) : full_erb_tag)
|
309
322
|
end
|
310
323
|
else
|
324
|
+
p ERB_REST: erb_scanner.rest if @debug
|
311
325
|
rest = erb_scanner.rest.to_s
|
312
326
|
format_text(rest)
|
313
327
|
erb_scanner.terminate
|
@@ -319,10 +333,12 @@ class ERB::Formatter
|
|
319
333
|
scanner = StringScanner.new(source)
|
320
334
|
|
321
335
|
until scanner.eos?
|
322
|
-
|
323
336
|
if matched = scanner.scan_until(tags_regexp)
|
337
|
+
p format_pre_match: [pre_pos, '..', scanner.pre_match[pre_pos..]] if @debug
|
324
338
|
pre_match = scanner.pre_match[pre_pos..]
|
325
|
-
|
339
|
+
p POS: pre_pos...scanner.pos, advanced: source[pre_pos...scanner.pos] if @debug
|
340
|
+
p MATCHED: matched if @debug
|
341
|
+
self.pre_pos = scanner.charpos
|
326
342
|
|
327
343
|
# Don't accept `name= "value"` attributes
|
328
344
|
raise "Bad attribute, please fix spaces after the equal sign." if BAD_ATTR.match? pre_match
|
@@ -352,6 +368,7 @@ class ERB::Formatter
|
|
352
368
|
raise "Unrecognized content: #{matched.inspect}"
|
353
369
|
end
|
354
370
|
else
|
371
|
+
p format_rest: scanner.rest if @debug
|
355
372
|
format_erb_tags(scanner.rest.to_s)
|
356
373
|
scanner.terminate
|
357
374
|
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.1
|
4
|
+
version: 0.2.1
|
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-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rufo
|
@@ -34,7 +34,6 @@ extra_rdoc_files: []
|
|
34
34
|
files:
|
35
35
|
- CHANGELOG.md
|
36
36
|
- Gemfile
|
37
|
-
- Gemfile.lock
|
38
37
|
- LICENSE.txt
|
39
38
|
- README.md
|
40
39
|
- Rakefile
|
data/Gemfile.lock
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: .
|
3
|
-
specs:
|
4
|
-
erb-formatter (0.1.0)
|
5
|
-
|
6
|
-
GEM
|
7
|
-
remote: https://rubygems.org/
|
8
|
-
specs:
|
9
|
-
minitest (5.15.0)
|
10
|
-
rake (13.0.6)
|
11
|
-
rufo (0.13.0)
|
12
|
-
|
13
|
-
PLATFORMS
|
14
|
-
arm64-darwin-21
|
15
|
-
|
16
|
-
DEPENDENCIES
|
17
|
-
erb-formatter!
|
18
|
-
minitest (~> 5.0)
|
19
|
-
rake (~> 13.0)
|
20
|
-
rufo
|
21
|
-
|
22
|
-
BUNDLED WITH
|
23
|
-
2.3.7
|