devcenter-parser 1.4.0 → 1.4.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
  !binary "U0hBMQ==":
3
- metadata.gz: 731e954adb885153b1d9f5cecb351e93af1c249d
4
- data.tar.gz: bc8bca7b5de1f10ff4400b43da93c93b299bf352
3
+ metadata.gz: 51f55582e8c324f21c5d8f0995579fb2bf238344
4
+ data.tar.gz: fdc5d451987e9e746001ec43717e7d5aca2120e1
5
5
  SHA512:
6
- metadata.gz: dbfa585bb7106530d36e94166f845acd49c96c952fab64a1a96e89eb0fc3d76772ff6537357047388d40596e264132d96eaa7d2c1a7f479c6d8f70676317b4da
7
- data.tar.gz: 443d781a6d537f9612f8b8dcb4809a8d766f31c362f9826ecccb4c56892f11654a89a54c7461027405c5598afbe4ad25b012f3eeea41f1a67b40e2f901310627
6
+ metadata.gz: 995dbe71fc3e5c43ca0a063020b2cf54c00502cf1a6a8c56c9f711b3df1ac88776bbfdd5e1d59fe268cbb23c276a2e56375f9ade142b949c61820045cd36a49c
7
+ data.tar.gz: 6ada25e661ceba84df62f401948c457cccc2c66b56927dbcd281d57129c907ac523adae6f8aedd1d29500ccd08f9626df0c4eb501f4ceb019ad8bccc65b352f0
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << "test"
5
+ t.test_files = FileList['test/*_test.rb']
6
+ t.verbose = true
7
+ end
8
+
9
+ task :default => :test
@@ -14,22 +14,34 @@ class HeaderIdGenerator
14
14
  @nodes_ids = @header_nodes.inject({}){ |hash, node| hash[node] = nil; hash }
15
15
 
16
16
  add_default_ids
17
- prepend_parents_on_conflicts
18
- append_numbers_on_conflicts
19
-
17
+ resolve_conflicts
20
18
  @nodes_ids.each{ |node, id| node['id'] = id }
21
19
  end
22
20
 
23
21
  private
24
22
 
23
+ def resolve_conflicts
24
+ return if conflicts(@nodes_ids).empty?
25
+
26
+ # { h4-node -> [h3-node, h2-node] }
27
+ @parents = @nodes_ids.keys.inject({}){ |hash, node| hash[node] = parent_header_nodes(node); hash }
28
+
29
+ prepend_parents_on_conflicts
30
+ append_numbers_on_conflicts
31
+ end
32
+
25
33
  # Parent != DOM nesting, but in the context of the content <h2></h2> ... <h3></h3>
26
34
 
27
- def prepend_parents_on_conflicts
35
+ # Prepend parents recursively, one level at a time, until there are no conflicts or no changes in the result
36
+ def prepend_parents_on_conflicts(parent_index = 0)
37
+ original = @nodes_ids.dup
28
38
  conflicts(@nodes_ids).each do |node, id|
29
- parent_contents = parent_header_nodes(node).map{ |parent_node| parent_node.content }
30
- content = (parent_contents + [node.content]).join(' ')
31
- @nodes_ids[node] = subheader_id(content.to_s)
39
+ if parent = @parents[node][parent_index]
40
+ new_id = subheader_id("#{subheader_id(parent.content)} #{@nodes_ids[node]}")
41
+ @nodes_ids[node] = new_id
42
+ end
32
43
  end
44
+ prepend_parents_on_conflicts(parent_index + 1) if original != @nodes_ids
33
45
  end
34
46
 
35
47
  def parent_header_nodes(node)
@@ -41,7 +53,7 @@ class HeaderIdGenerator
41
53
  # "h4" -> ["h2", "h3"]
42
54
  def parent_tags(tag)
43
55
  level = tag.gsub('h','').to_i
44
- (2..level-1).map{ |n| "h#{n}" }
56
+ (2..level-1).map{ |n| "h#{n}" }.reverse
45
57
  end
46
58
 
47
59
 
@@ -1,3 +1,3 @@
1
1
  module DevcenterParser
2
- VERSION = '1.4.0'
2
+ VERSION = '1.4.1'
3
3
  end
@@ -22,19 +22,31 @@ describe 'HeaderIdGeneratorTest' do
22
22
 
23
23
 
24
24
  describe 'ensures that there are not collisions between ids in subheaders' do
25
- it 'by prepending the id of the previous H2 if possible' do
25
+ it 'by prepending the id of the previous headers as long as necessary' do
26
26
  html = <<-HTML
27
27
  <h2>A</h2>
28
28
  <h3>B</h3>
29
29
  <h4>Z</h4>
30
+
30
31
  <h2>C</h2>
31
32
  <h3>B</h3>
32
33
  <h4>Z</h4>
34
+
35
+ <h2>D</h2>
36
+ <h3>X</h3>
37
+ <h4>Z</h4>
38
+ <h3>Y</h3>
39
+ <h4>Z</h4>
40
+
33
41
  HTML
34
42
  result = result(html)
35
- %w{ a c }.each{ |id| assert_id(result, 'h2', id) }
36
- %w{ a-b c-b }.each{ |id| assert_id(result, 'h3', id) }
43
+ %w{ a c d }.each{ |id| assert_id(result, 'h2', id) }
44
+ %w{ a-b c-b x y }.each{ |id| assert_id(result, 'h3', id) }
37
45
  %w{ a-b-z c-b-z }.each{ |id| assert_id(result, 'h4', id) }
46
+
47
+ %w{ d }.each{ |id| assert_id(result, 'h2', id) }
48
+ %w{ x y }.each{ |id| assert_id(result, 'h3', id) }
49
+ %w{ x-z y-z }.each{ |id| assert_id(result, 'h4', id) }
38
50
  end
39
51
 
40
52
  it 'by appending numbers for those subheaders with same nesting level and parent header name' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: devcenter-parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Heroku
@@ -90,6 +90,7 @@ files:
90
90
  - Gemfile
91
91
  - LICENSE
92
92
  - README.md
93
+ - Rakefile
93
94
  - devcenter-parser.gemspec
94
95
  - lib/devcenter-parser.rb
95
96
  - lib/devcenter-parser/github_parser.rb