erb-formatter 0.1.1 → 0.2.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
2
  SHA256:
3
- metadata.gz: ab97e4adbe2cdfa30f5dc353679d8d50ca4d26ace14a192748cceb7c6592ec55
4
- data.tar.gz: b47ebce98595b833ca99418d9ce9d331ccf72e6286fdf138d74e429e65d1ac51
3
+ metadata.gz: 923478a0d01b0942755e41fca1f84a656020383d2de0249210462274b8427a1c
4
+ data.tar.gz: 8b3304851134b98f48cbf7e716274770f3fb71e3e3121a1a706928fe063eb9c3
5
5
  SHA512:
6
- metadata.gz: cbc94f9055deb521afb0842c591a26b1d540b92ae8a77b2c4a6e4d4dc95ad34c899cd0dbb5a682c168fa92a172548dfd2cbe1ea47df4fadf37cd8795f94c2b49
7
- data.tar.gz: 56bdbdce6955b6bd0e8bb3ea1fbd45cc4874189ce138d1238e483b5620f533eee2b69c3f17300b38b839b317fbe6eff8d4b734ee241bce50bdb23466e244c103
6
+ metadata.gz: 384b0ff6f0bc9fb286191f54721e2b21c7c6dc30c27fc6d80a2ec6f85e600877aa53b83f27bd0b447bb978f74e1410a22971e66a91b921b14223454fbd745896
7
+ data.tar.gz: e2506d5249e000ae0c6318b9baa4f6236e3d35a9a8bab069d43a812a710545f1b2c018f2871b72537c6294abea1f5670832404c9e71b4809b4af7256601e06e8
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/elia/erb-formatter.
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 #{_1}..."
41
- system __FILE__, _1, *[
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(_1.chomp, @base_dir), 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
1
  # frozen_string_literal: false
2
2
 
3
- # $DEBUG = true
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.1"
13
+ VERSION = "0.2.0"
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 $DEBUG
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 $DEBUG
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 $DEBUG
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 = (starting_space ? indented("") : "").size
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
- lines << text.slice!(0..text.rindex(' ', -(line_width - offset)))
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 $DEBUG
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(_1) }.join.strip
268
- p RUBY_OUT: code if $DEBUG
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]
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
- self.pre_pos = scanner.pos
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.1
4
+ version: 0.2.0
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-15 00:00:00.000000000 Z
11
+ date: 2022-03-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rufo