erb-formatter 0.4.3 → 0.5.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a957537254444c222e81398e138e7d3bd22512b782cd3a67302ad9b1de8c12bc
4
- data.tar.gz: 23eee273e38701317b779ec455353fa801212b80b78c786d5a01b61fc54942c0
3
+ metadata.gz: 9746e0224608b37531674e86d589b087a67875791440318536b7d55ec66c8548
4
+ data.tar.gz: 73b0639da80cba0761eb5b6a58270b32db5b146cca710b446ebae61737bba385
5
5
  SHA512:
6
- metadata.gz: 05176b2dca77e2500ed5d00ce531e395cf4a11a35d38132fb7539f516d98e007480a32b690f53715a6ca79f967aa773d9698856464e24145e329c8389adad54c
7
- data.tar.gz: 2d088aa641242b2a30b8d4a21abe739cf365838950ea9c8aa9d39b395aaf3bda966f3159ee66bb03ad486e337fd6536694b064bdd834d7d138eb59518db44cfe
6
+ metadata.gz: d2658816da104a74d23952c1f59c70726c27668763ed94332914112fcd04e76c61ab95e738fea856c44e1f0bd72d0dae61d8ee7c6ec38d28abd927e91b970e56
7
+ data.tar.gz: 57a63692f5f533077df12677bbdf1dfa414ac564f897621d58fa08607e707d1af02fa9a78d7b0493e9340b27f046b2abee779758a0a6c8239e135abe3811da8b
data/README.md CHANGED
@@ -33,13 +33,18 @@ Or install it yourself as:
33
33
 
34
34
  ## Usage
35
35
 
36
+ ### From [Visual Studio Code](https://code.visualstudio.com)
37
+
38
+ Just install the [Ruby ERB::Formatter 🪜](https://marketplace.visualstudio.com/items?itemName=elia.erb-formatter) extension
39
+ and follow the setup instructions there.
40
+
36
41
  ### From the command line
37
42
 
38
43
  Update files in-place:
39
44
 
40
45
  $ erb-format app/views/**/*.html.erb --write
41
46
 
42
- or use stdin/stdout (useful for editor integrations):
47
+ or use stdin/stdout (useful for editor integrations):
43
48
 
44
49
  $ echo "<div > asdf <% if 123%> <%='foobar'%> <%end-%> </div>" | erb-format --stdin
45
50
  <div>
@@ -51,7 +56,7 @@ or use stdin/stdout (useful for editor integrations):
51
56
 
52
57
  Check out `erb-format --help` for more options.
53
58
 
54
- ### From Ruby
59
+ ### From [Ruby](https://www.ruby-lang.org/en/)
55
60
 
56
61
  ```ruby
57
62
  require 'erb/formatter'
@@ -76,7 +81,7 @@ ERB
76
81
  # </div>
77
82
  ```
78
83
 
79
- ### With `lint-staged`
84
+ ### With [`lint-staged`](https://github.com/okonet/lint-staged#readme)
80
85
 
81
86
  Add the gem to your gemfile and the following to your `package.json`:
82
87
 
@@ -87,7 +92,7 @@ Add the gem to your gemfile and the following to your `package.json`:
87
92
  }
88
93
  ```
89
94
 
90
- ### As a TextMate plugin
95
+ ### As a [TextMate](http://macromates.com) command
91
96
 
92
97
  Create a command with the following settings:
93
98
 
@@ -104,6 +109,16 @@ cd "$TM_PROJECT_DIRECTORY"
104
109
  bundle exec erb-format --stdin-filename "$TM_FILEPATH" < /dev/stdin 2> /dev/stdout
105
110
  ```
106
111
 
112
+ ### With [(Neo)VIM ALE](https://github.com/dense-analysis/ale)
113
+
114
+ Enable `erb-formatter` as a fixer in the ALE config:
115
+
116
+ ```vim
117
+ let g:ale_fixers = {
118
+ \ 'eruby': ['erb-formatter'],
119
+ \}
120
+ ```
121
+
107
122
  ## Development
108
123
 
109
124
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -10,7 +10,7 @@ class ERB::Formatter::CommandLine
10
10
  @argv = argv.dup
11
11
  @stdin = stdin
12
12
 
13
- @write, @filename, @read_stdin, @code = nil
13
+ @write, @filename, @read_stdin, @code, @single_class_per_line = nil
14
14
 
15
15
  OptionParser.new do |parser|
16
16
  parser.banner = "Usage: #{$0} FILENAME... --write"
@@ -37,6 +37,10 @@ class ERB::Formatter::CommandLine
37
37
  @width = value
38
38
  end
39
39
 
40
+ parser.on("--single-class-per-line", "Print each class on a separate line") do |value|
41
+ @single_class_per_line = value
42
+ end
43
+
40
44
  parser.on("--[no-]debug", "Enable debug mode") do |value|
41
45
  $DEBUG = value
42
46
  end
@@ -77,7 +81,7 @@ class ERB::Formatter::CommandLine
77
81
  if ignore_list.should_ignore_file? filename
78
82
  print code unless write
79
83
  else
80
- html = ERB::Formatter.new(code, filename: filename, line_width: @width || 80)
84
+ html = ERB::Formatter.new(code, filename: filename, line_width: @width || 80, single_class_per_line: @single_class_per_line)
81
85
 
82
86
  if write
83
87
  File.write(filename, html)
@@ -3,5 +3,5 @@
3
3
  require 'erb'
4
4
 
5
5
  class ERB::Formatter
6
- VERSION = "0.4.3"
6
+ VERSION = "0.5.1"
7
7
  end
data/lib/erb/formatter.rb CHANGED
@@ -1,11 +1,11 @@
1
1
  # frozen_string_literal: false
2
2
 
3
3
  require 'pp'
4
- require "erb"
5
- require "ripper"
6
- require 'securerandom'
4
+ require 'erb'
5
+ require 'yaml'
7
6
  require 'strscan'
8
7
  require 'stringio'
8
+ require 'securerandom'
9
9
  require 'erb/formatter/version'
10
10
 
11
11
  require 'syntax_tree'
@@ -56,13 +56,6 @@ class ERB::Formatter
56
56
 
57
57
  RUBOCOP_STDIN_MARKER = "===================="
58
58
 
59
- # Override the max line length to account from already indented ERB
60
- module RubocopForcedMaxLineLength
61
- def max
62
- Thread.current['RuboCop::Cop::Layout::LineLength#max'] || super
63
- end
64
- end
65
-
66
59
  module DebugShovel
67
60
  def <<(string)
68
61
  puts "ADDING: #{string.inspect} FROM:\n #{caller(1, 5).join("\n ")}"
@@ -74,13 +67,14 @@ class ERB::Formatter
74
67
  new(source, filename: filename).html
75
68
  end
76
69
 
77
- def initialize(source, line_width: 80, filename: nil, debug: $DEBUG)
70
+ def initialize(source, line_width: 80, single_class_per_line: false, filename: nil, debug: $DEBUG)
78
71
  @original_source = source
79
72
  @filename = filename || '(erb)'
80
73
  @line_width = line_width
81
74
  @source = remove_front_matter source.dup
82
75
  @html = +""
83
76
  @debug = debug
77
+ @single_class_per_line = single_class_per_line
84
78
 
85
79
  html.extend DebugShovel if @debug
86
80
 
@@ -104,10 +98,13 @@ class ERB::Formatter
104
98
  end
105
99
 
106
100
  def remove_front_matter(source)
107
- source.sub(/\A---\n[\s\S]*?\n---\n/) do |match|
108
- @front_matter = match
109
- match.gsub(/[^\n]/, ' ')
110
- end
101
+ return source unless source.start_with?("---\n")
102
+
103
+ first_body_line = YAML.parse(source).children.first.end_line + 1
104
+ lines = source.lines
105
+
106
+ @front_matter = lines[0...first_body_line].join
107
+ lines[first_body_line..].join
111
108
  end
112
109
 
113
110
  attr_accessor \
@@ -124,23 +121,48 @@ class ERB::Formatter
124
121
 
125
122
  attr_html = ""
126
123
  tag_stack_push(['attr='], attrs)
124
+
127
125
  attrs.scan(ATTR).flatten.each do |attr|
128
126
  attr.strip!
129
- full_attr = indented(attr)
130
127
  name, value = attr.split('=', 2)
131
128
 
129
+ if value.nil?
130
+ attr_html << indented("#{name}")
131
+ next
132
+ end
133
+
134
+ value_parts = value[1...-1].strip.split(/\s+/)
135
+
136
+ full_attr = indented("#{name}=#{value[0]}#{value_parts.join(" ")}#{value[-1]}")
137
+
132
138
  if full_attr.size > line_width && MULTILINE_ATTR_NAMES.include?(name) && attr.match?(QUOTED_ATTR)
133
139
  attr_html << indented("#{name}=#{value[0]}")
134
140
  tag_stack_push('attr"', value)
135
- value[1...-1].strip.split(/\s+/).each do |value_part|
136
- attr_html << indented(value_part)
141
+
142
+ if !@single_class_per_line && name == 'class'
143
+ line = value_parts.shift
144
+ value_parts.each do |value_part|
145
+ if (line.size + value_part.size + 1) <= line_width
146
+ line << " #{value_part}"
147
+ else
148
+ attr_html << indented(line)
149
+ line = value_part
150
+ end
151
+ end
152
+ attr_html << indented(line) if line
153
+ else
154
+ value_parts.each do |value_part|
155
+ attr_html << indented(value_part)
156
+ end
137
157
  end
158
+
138
159
  tag_stack_pop('attr"', value)
139
160
  attr_html << indented(value[-1])
140
161
  else
141
162
  attr_html << full_attr
142
163
  end
143
164
  end
165
+
144
166
  tag_stack_pop(['attr='], attrs)
145
167
  attr_html << indented("")
146
168
  attr_html
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.3
4
+ version: 0.5.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: 2023-03-31 00:00:00.000000000 Z
11
+ date: 2023-12-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: syntax_tree
@@ -68,7 +68,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
68
68
  - !ruby/object:Gem::Version
69
69
  version: '0'
70
70
  requirements: []
71
- rubygems_version: 3.3.7
71
+ rubygems_version: 3.3.23
72
72
  signing_key:
73
73
  specification_version: 4
74
74
  summary: Format ERB files with speed and precision.