erb-formatter 0.2.0 → 0.3.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: 923478a0d01b0942755e41fca1f84a656020383d2de0249210462274b8427a1c
4
- data.tar.gz: 8b3304851134b98f48cbf7e716274770f3fb71e3e3121a1a706928fe063eb9c3
3
+ metadata.gz: 74ce05292fc99dcfeb80526b22aa762325c40589640086886190cf40d534facd
4
+ data.tar.gz: faecec389f0cd92c85e1300d9c32920ac76f508e0fc37b7c2e6a5449b5d7db35
5
5
  SHA512:
6
- metadata.gz: 384b0ff6f0bc9fb286191f54721e2b21c7c6dc30c27fc6d80a2ec6f85e600877aa53b83f27bd0b447bb978f74e1410a22971e66a91b921b14223454fbd745896
7
- data.tar.gz: e2506d5249e000ae0c6318b9baa4f6236e3d35a9a8bab069d43a812a710545f1b2c018f2871b72537c6294abea1f5670832404c9e71b4809b4af7256601e06e8
6
+ metadata.gz: 13dfae2c05ecbb7f283ed1e76e1239dbad2923f1b0bc63b5d67e8de229ea3dfd97fe11d3d51734a2a33382a0d5a566dd16e8b2d8c73cc334eb8e424b685a7e3b
7
+ data.tar.gz: 64b9b47e1a6dd2c324c8167daf9dafcc0126950e0ae509088e19abc9ffb51b14bfd21cefb98aaf538e2c4a46bf5073fbab90c187e02e7831aa32024d55529ca1
data/exe/erb-format CHANGED
@@ -1,69 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require 'optparse'
4
-
5
- write, filename, read_stdin, code = nil
6
-
7
- OptionParser.new do |parser|
8
- parser.banner = "Usage: #{$0} FILENAME... --write"
9
-
10
- parser.on("-w", "--[no-]write", "Write file") do |value|
11
- write = value
12
- end
13
-
14
- parser.on("--stdin-filename FILEPATH", "Set the stdin filename (implies --stdin)") do |value|
15
- filename = value
16
- read_stdin = true
17
- end
18
-
19
- parser.on("--[no-]stdin", "Read the file from stdin") do |value|
20
- if read_stdin == true && value == false
21
- abort "Can't set stdin filename and not use stdin at the same time"
22
- end
23
-
24
- read_stdin = value
25
- filename ||= '-'
26
- end
27
-
28
- parser.on("--[no-]debug", "Enable debug mode") do |value|
29
- $DEBUG = value
30
- end
31
-
32
- parser.on("-h", "--help", "Prints this help") do
33
- puts parser
34
- exit
35
- end
36
- end.parse!(ARGV)
37
-
38
- abort "Can't read both stdin and a list of files" if read_stdin && !ARGV.empty?
39
-
40
- # If multiple files are provided assume `--write` and
41
- # execute on each of them.
42
- if ARGV.size > 1
43
- ARGV.each do |arg|
44
- warn "==> Formatting #{arg}..."
45
- system __FILE__, arg, *[
46
- ('--write' if write)
47
- ].compact or exit(1)
48
- end
49
- exit
50
- end
51
-
52
- require 'erb/formatter'
53
-
54
- filename ||= ARGV.first
55
- code = read_stdin ? $stdin.read : File.read(filename)
56
- ignore = ERB::Formatter::IgnoreList.new
57
-
58
- if ignore.should_ignore_file? filename
59
- print code unless write
60
- else
61
- html = ERB::Formatter.format(code, filename: filename)
62
-
63
- if write
64
- File.write(filename, html)
65
- else
66
- puts html
67
- end
68
- end
3
+ require 'erb/formatter/command_line'
69
4
 
5
+ ERB::Formatter::CommandLine.new(ARGV).run
data/exe/erb-formatter ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'erb/formatter/command_line'
4
+
5
+ ERB::Formatter::CommandLine.new(ARGV).run
@@ -0,0 +1,81 @@
1
+
2
+ require 'erb/formatter'
3
+ require 'optparse'
4
+
5
+ class ERB::Formatter::CommandLine
6
+
7
+ attr_reader :write, :filename, :read_stdin
8
+
9
+ def initialize(argv, stdin: $stdin)
10
+ @argv = argv.dup
11
+ @stdin = stdin
12
+
13
+ @write, @filename, @read_stdin, @code = nil
14
+
15
+ OptionParser.new do |parser|
16
+ parser.banner = "Usage: #{$0} FILENAME... --write"
17
+
18
+ parser.on("-w", "--[no-]write", "Write file") do |value|
19
+ @write = value
20
+ end
21
+
22
+ parser.on("--stdin-filename FILEPATH", "Set the stdin filename (implies --stdin)") do |value|
23
+ @filename = value
24
+ @read_stdin = true
25
+ end
26
+
27
+ parser.on("--[no-]stdin", "Read the file from stdin") do |value|
28
+ if read_stdin == true && value == false
29
+ abort "Can't set stdin filename and not use stdin at the same time"
30
+ end
31
+
32
+ @read_stdin = value
33
+ @filename ||= '-'
34
+ end
35
+
36
+ parser.on("--[no-]debug", "Enable debug mode") do |value|
37
+ $DEBUG = value
38
+ end
39
+
40
+ parser.on("-h", "--help", "Prints this help") do
41
+ puts parser
42
+ exit
43
+ end
44
+ end.parse!(@argv)
45
+ end
46
+
47
+ def ignore_list
48
+ @ignore_list ||= ERB::Formatter::IgnoreList.new
49
+ end
50
+
51
+ def ignore?(filename)
52
+
53
+ end
54
+
55
+ def run
56
+ if read_stdin
57
+ abort "Can't read both stdin and a list of files" unless @argv.empty?
58
+ files = [
59
+ [@filename, @stdin.read]
60
+ ]
61
+ else
62
+ files = @argv.map do |filename|
63
+ [filename, File.read(filename)]
64
+ end
65
+ end
66
+
67
+ files.each do |(filename, code)|
68
+ if ignore_list.should_ignore_file? filename
69
+ print code unless write
70
+ else
71
+ html = ERB::Formatter.format(code, filename: filename)
72
+
73
+ if write
74
+ File.write(filename, html)
75
+ else
76
+ puts html
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
data/lib/erb/formatter.rb CHANGED
@@ -1,16 +1,15 @@
1
1
  # frozen_string_literal: false
2
2
 
3
- # @debug = true
3
+ require 'pp'
4
4
  require "erb"
5
- require "cgi"
6
5
  require "ripper"
7
6
  require 'securerandom'
8
7
  require 'strscan'
9
- require 'pp'
10
8
  require 'stringio'
11
9
 
12
10
  class ERB::Formatter
13
- VERSION = "0.2.0"
11
+ VERSION = "0.3.0"
12
+
14
13
  autoload :IgnoreList, 'erb/formatter/ignore_list'
15
14
 
16
15
  class Error < StandardError; end
@@ -31,9 +30,11 @@ class ERB::Formatter
31
30
  ERB_END = %r{(<%-?)\s*(end)\s*(-?%>)}
32
31
  ERB_ELSE = %r{(<%-?)\s*(else|elsif\b.*)\s*(-?%>)}
33
32
 
33
+ TAG_NAME = /[a-z0-9_:-]+/
34
+ TAG_NAME_ONLY = /\A#{TAG_NAME}\z/
34
35
  HTML_ATTR = %r{\s+#{SINGLE_QUOTE_ATTR}|\s+#{DOUBLE_QUOTE_ATTR}|\s+#{UNQUOTED_ATTR}|\s+#{ATTR_NAME}}m
35
- HTML_TAG_OPEN = %r{<(\w+)((?:#{HTML_ATTR})*)(\s*?)(/>|>)}m
36
- HTML_TAG_CLOSE = %r{</\s*(\w+)\s*>}
36
+ HTML_TAG_OPEN = %r{<(#{TAG_NAME})((?:#{HTML_ATTR})*)(\s*?)(/>|>)}m
37
+ HTML_TAG_CLOSE = %r{</\s*(#{TAG_NAME})\s*>}
37
38
 
38
39
  SELF_CLOSING_TAG = /\A(area|base|br|col|command|embed|hr|img|input|keygen|link|menuitem|meta|param|source|track|wbr)\z/i
39
40
 
@@ -291,7 +292,7 @@ class ERB::Formatter
291
292
  erb_pre_pos = 0
292
293
  until erb_scanner.eos?
293
294
  if erb_scanner.scan_until(erb_tags_regexp)
294
- p PRE_MATCH: [erb_pre_pos, '..', erb_scanner.pre_match]
295
+ p PRE_MATCH: [erb_pre_pos, '..', erb_scanner.pre_match] if @debug
295
296
  erb_pre_match = erb_scanner.pre_match
296
297
  erb_pre_match = erb_pre_match[erb_pre_pos..]
297
298
  erb_pre_pos = erb_scanner.pos
@@ -341,7 +342,7 @@ class ERB::Formatter
341
342
  self.pre_pos = scanner.charpos
342
343
 
343
344
  # Don't accept `name= "value"` attributes
344
- raise "Bad attribute, please fix spaces after the equal sign." if BAD_ATTR.match? pre_match
345
+ raise "Bad attribute, please fix spaces after the equal sign:\n#{pre_match}" if BAD_ATTR.match? pre_match
345
346
 
346
347
  format_erb_tags(pre_match) if pre_match
347
348
 
@@ -355,7 +356,7 @@ class ERB::Formatter
355
356
  elsif matched.match(HTML_TAG_OPEN)
356
357
  _, tag_name, tag_attrs, _, tag_closing = *scanner.captures
357
358
 
358
- raise "Unknown tag #{tag_name.inspect}" unless tag_name.match?(/\A[a-z0-9]+\z/)
359
+ raise "Unknown tag #{tag_name.inspect}" unless tag_name.match?(TAG_NAME_ONLY)
359
360
 
360
361
  tag_self_closing = tag_closing == '/>' || SELF_CLOSING_TAG.match?(tag_name)
361
362
  tag_attrs.strip!
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.2.0
4
+ version: 0.3.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-19 00:00:00.000000000 Z
11
+ date: 2022-04-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rufo
@@ -29,6 +29,7 @@ email:
29
29
  - elia@schito.me
30
30
  executables:
31
31
  - erb-format
32
+ - erb-formatter
32
33
  extensions: []
33
34
  extra_rdoc_files: []
34
35
  files:
@@ -39,7 +40,9 @@ files:
39
40
  - Rakefile
40
41
  - erb-formatter.gemspec
41
42
  - exe/erb-format
43
+ - exe/erb-formatter
42
44
  - lib/erb/formatter.rb
45
+ - lib/erb/formatter/command_line.rb
43
46
  - lib/erb/formatter/ignore_list.rb
44
47
  - lib/erb/formatter/version.rb
45
48
  - sig/erb/formatter.rbs