better_html 1.0.0 → 1.0.1

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
  SHA1:
3
- metadata.gz: 16bc95c4f55d720a177e9849dea00e7d83c66455
4
- data.tar.gz: 376b80e51f2379ce6c5640e834de1c62aa990ae5
3
+ metadata.gz: 24abf8276750c88363603b5b3900aa6c5ca253aa
4
+ data.tar.gz: bc8e69fb9ff82006af0b94f50f150200ec904ece
5
5
  SHA512:
6
- metadata.gz: bf07d75143cfde43414bb3e8a5c2520b527f047d0c796c7ff97e60aa2d23d0bc03ea1f06c72eab5db5f4c10cdb01eccf0c6abe585b0ae394dc662731599e296b
7
- data.tar.gz: b7dd6bfc56b0a36d773dabbe13a7ad922bc9c5cd322f4a240ba8d9064c8753ec4efca3fb5b1a7c63d36291b60dd48ec142521b3af98811f3a017d5d62f90ab97
6
+ metadata.gz: 1d990f65159106d1d1794c6dd9d810ced2ab3e5c79c518a9b8165235c1d5ea1919334f8bc7d9787d460ecaab7059af2d8a63fbb7721909610b43920efe2d1943
7
+ data.tar.gz: ed23dc14085fd3fa583a9309339f73af41bc85e8f3a6dd1a61c7febf2d03ca575136c87544b545d5fdb0864505a262fd1eb838920d69e4f26a1e309d3feac339
@@ -66,10 +66,10 @@ module BetterHtml
66
66
  token = add_token(:erb_end, pos, pos + 2)
67
67
  end
68
68
 
69
- def add_token(type, start, stop, line = nil, column = nil)
69
+ def add_token(type, start, stop)
70
70
  token = Token.new(
71
71
  type: type,
72
- loc: Location.new(@document, start, stop - 1, line, column)
72
+ loc: Location.new(@document, start, stop - 1)
73
73
  )
74
74
  @tokens << token
75
75
  token
@@ -22,8 +22,8 @@ module BetterHtml
22
22
  end
23
23
 
24
24
  def add_text(text)
25
- @parser.parse(text) do |type, start, stop, line, column|
26
- add_token(type, start, stop, line, column)
25
+ @parser.parse(text) do |type, start, stop, _line, _column|
26
+ add_token(type, start, stop)
27
27
  end
28
28
  end
29
29
  end
@@ -62,8 +62,8 @@ module BetterHtml
62
62
  end
63
63
 
64
64
  def add_text(text)
65
- @parser.parse(text) do |type, start, stop, line, column|
66
- add_token(type, start: start, stop: stop, line: line, column: column)
65
+ @parser.parse(text) do |type, start, stop, _line, _column|
66
+ add_token(type, start: start, stop: stop)
67
67
  end
68
68
  end
69
69
 
@@ -84,10 +84,10 @@ module BetterHtml
84
84
  add_token(:lodash_end, start: pos, stop: pos + 2)
85
85
  end
86
86
 
87
- def add_token(type, start: nil, stop: nil, line: nil, column: nil)
87
+ def add_token(type, start: nil, stop: nil)
88
88
  token = Token.new(
89
89
  type: type,
90
- loc: Location.new(@document, start, stop-1, line, column)
90
+ loc: Location.new(@document, start, stop-1)
91
91
  )
92
92
  @tokens << token
93
93
  token
@@ -3,7 +3,7 @@ module BetterHtml
3
3
  class Location
4
4
  attr_accessor :start, :stop
5
5
 
6
- def initialize(document, start, stop, line = nil, column = nil)
6
+ def initialize(document, start, stop)
7
7
  raise ArgumentError, "start location #{start} is out of range for document of size #{document.size}" if start > document.size
8
8
  raise ArgumentError, "stop location #{stop} is out of range for document of size #{document.size}" if stop > document.size
9
9
  raise ArgumentError, "end of range must be greater than start of range (#{stop} < #{start})" if stop < start
@@ -11,28 +11,46 @@ module BetterHtml
11
11
  @document = document
12
12
  @start = start
13
13
  @stop = stop
14
- @line = line
15
- @column = column
16
14
  end
17
15
 
18
16
  def range
19
17
  Range.new(start, stop)
20
18
  end
21
19
 
20
+ def line_range
21
+ Range.new(start_line, stop_line)
22
+ end
23
+
22
24
  def source
23
25
  @document[range]
24
26
  end
25
27
 
28
+ def start_line
29
+ @start_line ||= calculate_line(start)
30
+ end
31
+
26
32
  def line
27
- @line ||= calculate_line
33
+ start_line
34
+ end
35
+
36
+ def stop_line
37
+ @stop_line ||= calculate_line(stop)
38
+ end
39
+
40
+ def start_column
41
+ @start_column ||= calculate_column(start)
28
42
  end
29
43
 
30
44
  def column
31
- @column ||= calculate_column
45
+ start_column
46
+ end
47
+
48
+ def stop_column
49
+ @stop_column ||= calculate_column(stop)
32
50
  end
33
51
 
34
52
  def line_source_with_underline
35
- line_content = extract_line(line: line)
53
+ line_content = extract_line(line: start_line)
36
54
  spaces = line_content.scan(/\A\s*/).first
37
55
  column_without_spaces = [column - spaces.length, 0].max
38
56
  underscore_length = [[stop - start + 1, line_content.length - column_without_spaces].min, 1].max
@@ -41,14 +59,14 @@ module BetterHtml
41
59
 
42
60
  private
43
61
 
44
- def calculate_line
45
- return 1 if start == 0
46
- @document[0..start-1].scan("\n").count + 1
62
+ def calculate_line(pos)
63
+ return 1 if pos == 0
64
+ @document[0..pos-1].scan("\n").count + 1
47
65
  end
48
66
 
49
- def calculate_column
50
- return 0 if start == 0
51
- @document[0..start-1]&.split("\n", -1)&.last&.length || 0
67
+ def calculate_column(pos)
68
+ return 0 if pos == 0
69
+ @document[0..pos-1]&.split("\n", -1)&.last&.length || 0
52
70
  end
53
71
 
54
72
  def extract_line(line:)
@@ -1,3 +1,3 @@
1
1
  module BetterHtml
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
@@ -95,7 +95,7 @@ module BetterHtml
95
95
 
96
96
  assert_attributes ({ type: :text, loc: { line: 1, source: 'before ' } }), scanner.tokens[0]
97
97
  assert_attributes ({ type: :erb_begin, loc: { line: 1, source: '<%' } }), scanner.tokens[1]
98
- assert_attributes ({ type: :code, loc: { line: 1, source: " multi\nline " } }), scanner.tokens[2]
98
+ assert_attributes ({ type: :code, loc: { line: 1, start_line: 1, stop_line: 2, source: " multi\nline " } }), scanner.tokens[2]
99
99
  assert_attributes ({ type: :erb_end, loc: { line: 2, source: "%>" } }), scanner.tokens[3]
100
100
  assert_attributes ({ type: :text, loc: { line: 2, source: " after" } }), scanner.tokens[4]
101
101
  end
@@ -25,12 +25,14 @@ module BetterHtml
25
25
  assert_equal "end of range must be greater than start of range (2 < 5)", e.message
26
26
  end
27
27
 
28
- test "location without line and column" do
29
- loc = Location.new("foo\nbar\nbaz", 9, 9)
30
-
31
- assert_equal "a", loc.source
32
- assert_equal 3, loc.line
33
- assert_equal 1, loc.column
28
+ test "location calulates start and stop line and column" do
29
+ loc = Location.new("foo\nbar\nbaz", 5, 9)
30
+
31
+ assert_equal "ar\nba", loc.source
32
+ assert_equal 2, loc.start_line
33
+ assert_equal 1, loc.start_column
34
+ assert_equal 3, loc.stop_line
35
+ assert_equal 1, loc.stop_column
34
36
  end
35
37
 
36
38
  test "line_source_with_underline" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: better_html
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Francois Chagnon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-12-07 00:00:00.000000000 Z
11
+ date: 2018-01-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ast
@@ -228,7 +228,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
228
228
  version: '0'
229
229
  requirements: []
230
230
  rubyforge_project:
231
- rubygems_version: 2.5.2.1
231
+ rubygems_version: 2.6.14
232
232
  signing_key:
233
233
  specification_version: 4
234
234
  summary: Better HTML for Rails.