danger-toc 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop_todo.yml +3 -1
- data/CHANGELOG.md +4 -0
- data/RELEASING.md +2 -2
- data/lib/toc/config.rb +11 -2
- data/lib/toc/constructors.rb +16 -0
- data/lib/toc/constructors/github_constructor.rb +17 -0
- data/lib/toc/constructors/kramdown_constructor.rb +42 -0
- data/lib/toc/gem_version.rb +1 -1
- data/lib/toc/markdown_file.rb +2 -2
- data/spec/config_spec.rb +20 -0
- data/spec/constructors_spec.rb +23 -0
- data/spec/fixtures/markdown_file/one_section_with_underscore_in_name.md +11 -0
- data/spec/markdown_file/with_underscore_in_section_name_spec.rb +32 -0
- metadata +11 -3
- data/lib/toc/constructor.rb +0 -40
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8d6f07178a26d9c1cc6267388640e6f4c21ad15b
|
4
|
+
data.tar.gz: 6cd82b36ca80b580f402cbbd2a31b5a319dfb9e3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f41167916836adaea70857524473e43654cff465430046b6342a9a77049294c834c2be9f33d5ad0a5610a164dff0611c0a8cced4763985655450d923befcef13
|
7
|
+
data.tar.gz: 6713504e51a68041100b89c05e1210294172cd725032f5628ce51aa1b1e2539bc06f59ab0054a5d2f0c79a522847293a6b513616ccf4897a80d55828925f8659
|
data/.rubocop_todo.yml
CHANGED
@@ -27,7 +27,9 @@ Style/Documentation:
|
|
27
27
|
- 'spec/**/*'
|
28
28
|
- 'test/**/*'
|
29
29
|
- 'lib/toc/config.rb'
|
30
|
-
- 'lib/toc/
|
30
|
+
- 'lib/toc/constructors.rb'
|
31
|
+
- 'lib/toc/constructors/github_constructor.rb'
|
32
|
+
- 'lib/toc/constructors/kramdown_constructor.rb'
|
31
33
|
- 'lib/toc/extractor.rb'
|
32
34
|
- 'lib/toc/markdown_file.rb'
|
33
35
|
- 'lib/toc/plugin.rb'
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
## Changelog
|
2
2
|
|
3
|
+
### 0.1.2 (2018/04/02)
|
4
|
+
|
5
|
+
* [#14](https://github.com/dblock/danger-toc/pull/14): Configure "Table of Contents" format with `toc.format`. Allow values: github (by default) and kramdown - [@mr-dxdy](https://github.com/mr-dxdy).
|
6
|
+
|
3
7
|
### 0.1.1 (2018/03/27)
|
4
8
|
|
5
9
|
* [#3](https://github.com/dblock/danger-toc/issues/3): Configure "Table of Contents" header with `toc.header` - [@dblock](https://github.com/dblock).
|
data/RELEASING.md
CHANGED
@@ -31,7 +31,7 @@ Commit your changes.
|
|
31
31
|
|
32
32
|
```
|
33
33
|
git add CHANGELOG.md lib/toc/gem_version.rb
|
34
|
-
git commit -m "Preparing for release 0.2.2."
|
34
|
+
git commit -m "Preparing for release, 0.2.2."
|
35
35
|
git push origin master
|
36
36
|
```
|
37
37
|
|
@@ -61,7 +61,7 @@ Increment the third version number in [lib/toc/gem_version.rb](lib/toc/gem_versi
|
|
61
61
|
Commit your changes.
|
62
62
|
|
63
63
|
```
|
64
|
-
git add CHANGELOG.md lib/
|
64
|
+
git add CHANGELOG.md lib/toc/gem_version.rb
|
65
65
|
git commit -m "Preparing for next development iteration, 0.2.3."
|
66
66
|
git push origin master
|
67
67
|
```
|
data/lib/toc/config.rb
CHANGED
@@ -3,8 +3,8 @@ module Danger
|
|
3
3
|
module Config
|
4
4
|
extend self
|
5
5
|
|
6
|
-
attr_accessor :files
|
7
|
-
|
6
|
+
attr_accessor :files, :header
|
7
|
+
attr_writer :format
|
8
8
|
|
9
9
|
# Files to process
|
10
10
|
def files=(value)
|
@@ -16,9 +16,18 @@ module Danger
|
|
16
16
|
@header = value
|
17
17
|
end
|
18
18
|
|
19
|
+
def format
|
20
|
+
@format ||= default_format
|
21
|
+
end
|
22
|
+
|
23
|
+
def default_format
|
24
|
+
:github
|
25
|
+
end
|
26
|
+
|
19
27
|
def reset
|
20
28
|
self.files = ['README.md']
|
21
29
|
self.header = 'Table of Contents'
|
30
|
+
self.format = default_format
|
22
31
|
end
|
23
32
|
|
24
33
|
reset
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require_relative 'constructors/kramdown_constructor'
|
2
|
+
require_relative 'constructors/github_constructor'
|
3
|
+
|
4
|
+
module Danger
|
5
|
+
module Toc
|
6
|
+
module Constructors
|
7
|
+
def self.get(name)
|
8
|
+
const_get "#{name.to_s.camelize}Constructor"
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.current
|
12
|
+
get Danger::Toc.config.format
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Danger
|
2
|
+
module Toc
|
3
|
+
module Constructors
|
4
|
+
class GithubConstructor < KramdownConstructor
|
5
|
+
PUNCTUATION_REGEXP = /[^\p{Word}\- ]/u
|
6
|
+
|
7
|
+
def basic_generate_id(str)
|
8
|
+
# Get source code from https://github.com/jch/html-pipeline/blob/master/lib/html/pipeline/toc_filter.rb#L38
|
9
|
+
id = str.downcase
|
10
|
+
id.gsub!(PUNCTUATION_REGEXP, '') # remove punctuation
|
11
|
+
id.tr!(' ', '-') # replace spaces with dash
|
12
|
+
id
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'kramdown/converter'
|
2
|
+
|
3
|
+
module Danger
|
4
|
+
module Toc
|
5
|
+
module Constructors
|
6
|
+
class KramdownConstructor < Kramdown::Converter::Toc
|
7
|
+
def flatten(el)
|
8
|
+
return [] unless el.type == :toc
|
9
|
+
result = []
|
10
|
+
if el.value
|
11
|
+
result << {
|
12
|
+
id: el.attr[:id],
|
13
|
+
text: el.value.options[:raw_text],
|
14
|
+
depth: el.value.options[:level]
|
15
|
+
}
|
16
|
+
end
|
17
|
+
if el.children
|
18
|
+
el.children.each do |child|
|
19
|
+
result.concat(flatten(child))
|
20
|
+
end
|
21
|
+
end
|
22
|
+
result
|
23
|
+
end
|
24
|
+
|
25
|
+
def convert(el)
|
26
|
+
toc = flatten(super(el))
|
27
|
+
has_toc = false
|
28
|
+
headers = []
|
29
|
+
toc.each do |line|
|
30
|
+
if !has_toc && line[:text] == Danger::Toc.config.header
|
31
|
+
headers = [] # drop any headers prior to TOC
|
32
|
+
has_toc = true
|
33
|
+
else
|
34
|
+
headers << line
|
35
|
+
end
|
36
|
+
end
|
37
|
+
headers
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/lib/toc/gem_version.rb
CHANGED
data/lib/toc/markdown_file.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'active_support/core_ext/string/inflections'
|
2
2
|
|
3
3
|
require_relative 'extractor'
|
4
|
-
require_relative '
|
4
|
+
require_relative 'constructors'
|
5
5
|
|
6
6
|
module Danger
|
7
7
|
module Toc
|
@@ -60,7 +60,7 @@ module Danger
|
|
60
60
|
@toc = md.split("\n")[toc_start, toc_end - toc_start - 1].reject(&:empty?) if @has_toc
|
61
61
|
|
62
62
|
# construct toc
|
63
|
-
@headers = Danger::Toc::
|
63
|
+
@headers = Danger::Toc::Constructors.current.convert(doc.root).first
|
64
64
|
end
|
65
65
|
|
66
66
|
def reduce!
|
data/spec/config_spec.rb
CHANGED
@@ -42,5 +42,25 @@ describe Danger::Toc::Config do
|
|
42
42
|
end
|
43
43
|
end
|
44
44
|
end
|
45
|
+
|
46
|
+
describe 'format' do
|
47
|
+
context 'default' do
|
48
|
+
it 'assumes github' do
|
49
|
+
expect(Danger::Toc.config.format).to eq(:github)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context 'custom value' do
|
54
|
+
before do
|
55
|
+
Danger::Toc.configure do |config|
|
56
|
+
config.format = :kramdown
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'is set' do
|
61
|
+
expect(Danger::Toc.config.format).to eq(:kramdown)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
45
65
|
end
|
46
66
|
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require File.expand_path('../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
describe Danger::Toc::Constructors do
|
4
|
+
describe '.get' do
|
5
|
+
it 'should return constructor for kramdown' do
|
6
|
+
expect(
|
7
|
+
described_class.get(:kramdown)
|
8
|
+
).to eq(Danger::Toc::Constructors::KramdownConstructor)
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'should return constructor for github' do
|
12
|
+
expect(
|
13
|
+
described_class.get(:github)
|
14
|
+
).to eq(Danger::Toc::Constructors::GithubConstructor)
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should return error if not found constructor' do
|
18
|
+
expect do
|
19
|
+
described_class.get :unknown
|
20
|
+
end.to raise_error(NameError)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Danger::Toc::MarkdownFile do
|
4
|
+
describe 'with a underscore in section name' do
|
5
|
+
let(:filename) do
|
6
|
+
File.expand_path('../../fixtures/markdown_file/one_section_with_underscore_in_name.md', __FILE__)
|
7
|
+
end
|
8
|
+
|
9
|
+
subject do
|
10
|
+
Danger::Toc::MarkdownFile.new(filename)
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'exists?' do
|
14
|
+
expect(subject.exists?).to be true
|
15
|
+
end
|
16
|
+
it 'has_toc?' do
|
17
|
+
expect(subject.has_toc?).to be true
|
18
|
+
end
|
19
|
+
it 'toc' do
|
20
|
+
expect(subject.toc).to eq(['- [What_is_this?](#what_is_this)'])
|
21
|
+
end
|
22
|
+
it 'headers' do
|
23
|
+
expect(subject.headers).to eq([{ depth: 0, id: 'what_is_this', text: 'What_is_this?' }])
|
24
|
+
end
|
25
|
+
it 'good?' do
|
26
|
+
expect(subject.good?).to be true
|
27
|
+
end
|
28
|
+
it 'bad?' do
|
29
|
+
expect(subject.bad?).to be false
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: danger-toc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- dblock
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-04-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -204,23 +204,28 @@ files:
|
|
204
204
|
- lib/danger_plugin.rb
|
205
205
|
- lib/danger_toc.rb
|
206
206
|
- lib/toc/config.rb
|
207
|
-
- lib/toc/
|
207
|
+
- lib/toc/constructors.rb
|
208
|
+
- lib/toc/constructors/github_constructor.rb
|
209
|
+
- lib/toc/constructors/kramdown_constructor.rb
|
208
210
|
- lib/toc/extractor.rb
|
209
211
|
- lib/toc/gem_version.rb
|
210
212
|
- lib/toc/markdown_file.rb
|
211
213
|
- lib/toc/plugin.rb
|
212
214
|
- spec/config_spec.rb
|
215
|
+
- spec/constructors_spec.rb
|
213
216
|
- spec/fixtures/markdown_file/one_section.md
|
214
217
|
- spec/fixtures/markdown_file/one_section_with_custom_toc.md
|
215
218
|
- spec/fixtures/markdown_file/one_section_with_invalid_toc.md
|
216
219
|
- spec/fixtures/markdown_file/one_section_with_toc.md
|
217
220
|
- spec/fixtures/markdown_file/one_section_with_toc_level.md
|
221
|
+
- spec/fixtures/markdown_file/one_section_with_underscore_in_name.md
|
218
222
|
- spec/fixtures/markdown_file/with_quoted_example.md
|
219
223
|
- spec/markdown_file/danger_toc_readme_spec.rb
|
220
224
|
- spec/markdown_file/one_section_spec.rb
|
221
225
|
- spec/markdown_file/one_section_with_toc_level_spec.rb
|
222
226
|
- spec/markdown_file/one_section_with_toc_spec.rb
|
223
227
|
- spec/markdown_file/with_quoted_example_spec.rb
|
228
|
+
- spec/markdown_file/with_underscore_in_section_name_spec.rb
|
224
229
|
- spec/spec_helper.rb
|
225
230
|
- spec/toc_spec.rb
|
226
231
|
homepage: https://github.com/dblock/danger-toc
|
@@ -249,16 +254,19 @@ specification_version: 4
|
|
249
254
|
summary: A danger.systems plugin for your markdown TOC.
|
250
255
|
test_files:
|
251
256
|
- spec/config_spec.rb
|
257
|
+
- spec/constructors_spec.rb
|
252
258
|
- spec/fixtures/markdown_file/one_section.md
|
253
259
|
- spec/fixtures/markdown_file/one_section_with_custom_toc.md
|
254
260
|
- spec/fixtures/markdown_file/one_section_with_invalid_toc.md
|
255
261
|
- spec/fixtures/markdown_file/one_section_with_toc.md
|
256
262
|
- spec/fixtures/markdown_file/one_section_with_toc_level.md
|
263
|
+
- spec/fixtures/markdown_file/one_section_with_underscore_in_name.md
|
257
264
|
- spec/fixtures/markdown_file/with_quoted_example.md
|
258
265
|
- spec/markdown_file/danger_toc_readme_spec.rb
|
259
266
|
- spec/markdown_file/one_section_spec.rb
|
260
267
|
- spec/markdown_file/one_section_with_toc_level_spec.rb
|
261
268
|
- spec/markdown_file/one_section_with_toc_spec.rb
|
262
269
|
- spec/markdown_file/with_quoted_example_spec.rb
|
270
|
+
- spec/markdown_file/with_underscore_in_section_name_spec.rb
|
263
271
|
- spec/spec_helper.rb
|
264
272
|
- spec/toc_spec.rb
|
data/lib/toc/constructor.rb
DELETED
@@ -1,40 +0,0 @@
|
|
1
|
-
require 'kramdown/converter'
|
2
|
-
|
3
|
-
module Danger
|
4
|
-
module Toc
|
5
|
-
class Constructor < Kramdown::Converter::Toc
|
6
|
-
def flatten(el)
|
7
|
-
return [] unless el.type == :toc
|
8
|
-
result = []
|
9
|
-
if el.value
|
10
|
-
result << {
|
11
|
-
id: el.attr[:id],
|
12
|
-
text: el.value.options[:raw_text],
|
13
|
-
depth: el.value.options[:level]
|
14
|
-
}
|
15
|
-
end
|
16
|
-
if el.children
|
17
|
-
el.children.each do |child|
|
18
|
-
result.concat(flatten(child))
|
19
|
-
end
|
20
|
-
end
|
21
|
-
result
|
22
|
-
end
|
23
|
-
|
24
|
-
def convert(el)
|
25
|
-
toc = flatten(super(el))
|
26
|
-
has_toc = false
|
27
|
-
headers = []
|
28
|
-
toc.each do |line|
|
29
|
-
if !has_toc && line[:text] == Danger::Toc.config.header
|
30
|
-
headers = [] # drop any headers prior to TOC
|
31
|
-
has_toc = true
|
32
|
-
else
|
33
|
-
headers << line
|
34
|
-
end
|
35
|
-
end
|
36
|
-
headers
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|