devcenter-parser 1.4.0 → 1.4.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 +4 -4
- data/Rakefile +9 -0
- data/lib/devcenter-parser/header_id_generator.rb +20 -8
- data/lib/devcenter-parser/version.rb +1 -1
- data/test/header_id_generator_test.rb +15 -3
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 51f55582e8c324f21c5d8f0995579fb2bf238344
|
4
|
+
data.tar.gz: fdc5d451987e9e746001ec43717e7d5aca2120e1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 995dbe71fc3e5c43ca0a063020b2cf54c00502cf1a6a8c56c9f711b3df1ac88776bbfdd5e1d59fe268cbb23c276a2e56375f9ade142b949c61820045cd36a49c
|
7
|
+
data.tar.gz: 6ada25e661ceba84df62f401948c457cccc2c66b56927dbcd281d57129c907ac523adae6f8aedd1d29500ccd08f9626df0c4eb501f4ceb019ad8bccc65b352f0
|
data/Rakefile
ADDED
@@ -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
|
-
|
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
|
-
|
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
|
-
|
30
|
-
|
31
|
-
|
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
|
|
@@ -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
|
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.
|
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
|