sixarm_ruby_markdown_table_of_contents 2.1.0 → 2.4.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: da33b215d110a63b5f9153ad7d73a8ccf5199d9b09ade074b33dfc1a9bca5f48
4
- data.tar.gz: '0923bbf3632d8f484468d267dd96b956219c16d17b0817f9f45359824ae0f337'
3
+ metadata.gz: a54c2cd830e7b301beaa467b0a930205dadfe364d7c64d9022a09e4000fbe857
4
+ data.tar.gz: fcc89b66c629c83e85f5102c7aa098b4fda4b354cfec55202181da0003c50b8a
5
5
  SHA512:
6
- metadata.gz: cd847cd1308214a890b7f5bb9ce05dd67e4091c58de95c0104931dc0a07a4918f66d3ec9497ef03bc9596c9e8bf78497914dedf58cf51c6a8fc2ded71aa7a87e
7
- data.tar.gz: da896a88acfa9dbc7d8ebb61c92b797311b727d16723364995a5dd43ed73ad6e3fd19045a51867d83b11ef3bf7dbebe1155cafe199b509c76a5c3c5816b2f312
6
+ metadata.gz: 653b33b8a22c8cff112f32df430dc9a77a612b3afde05e0abcf6a46d199e76ff598852bd8f4167f235c0fb82fdde28fd84bb479574690217796212e4fc979d8f
7
+ data.tar.gz: 97f3563d4639c0d81fbdfa1c21b19a14f886b971b6512e4b76ce35dec96363310cae1e35416dfd5c0aa69199c8518e65158946be037a9e4f60bd91f5d186748c
checksums.yaml.gz.sig CHANGED
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -1,11 +1,15 @@
1
1
  #!/usr/bin/env ruby
2
2
  require "sixarm_ruby_markdown_table_of_contents"
3
3
  begin
4
- markdown_file_path = ARGV.shift
5
- if nil == markdown_file_path then raise ArgumentError.new("This command needs an argument that is a markdown file path.") end
6
- file = Markdown::File.new(markdown_file_path)
7
- if nil == markdown_file_path then raise ArgumentError.new("This command needs a markdown file path that is a valid file.") end
8
- file.rewrite_toc
4
+ settings = SixArm::Markdown::TOC::Settings.new
5
+ settings.option_parsing
6
+ generator = SixArm::Markdown::TOC::Generator.new(settings: settings)
7
+ ARGV.each{|markdown_file_path|
8
+ file = SixArm::Markdown::File.new(markdown_file_path)
9
+ input = file.slurp
10
+ output = generator.rewrite_toc(input)
11
+ file.rewrite(output)
12
+ }
9
13
  exit 0
10
14
  rescue
11
15
  STDERR.puts "Error! " + $!.message
@@ -5,9 +5,7 @@ Please see README
5
5
 
6
6
  require "ostruct"
7
7
  require "sixarm_ruby_file_rewrite"
8
+ require "sixarm_ruby_markdown"
8
9
 
9
- module Markdown; end
10
-
11
- require_relative "sixarm_ruby_markdown_table_of_contents/markdown/headline"
12
- require_relative "sixarm_ruby_markdown_table_of_contents/markdown/file"
13
- require_relative "sixarm_ruby_markdown_table_of_contents/markdown/string"
10
+ require_relative "sixarm_ruby_markdown_table_of_contents/sixarm/markdown/toc/generator"
11
+ require_relative "sixarm_ruby_markdown_table_of_contents/sixarm/markdown/toc/settings"
@@ -0,0 +1,41 @@
1
+ # -*- coding: utf-8 -*-
2
+ =begin rdoc
3
+ =end
4
+
5
+ module SixArm; module Markdown; module TOC; end; end; end
6
+
7
+ class SixArm::Markdown::TOC::Generator
8
+
9
+ attr_accessor :settings
10
+
11
+ def initialize(
12
+ settings: SixArm::Markdown::TOC::Settings.new
13
+ )
14
+ @settings = settings
15
+ end
16
+
17
+ def headline_to_markdown(headline)
18
+ settings.line_prefix + (" " * (headline.level - 2)) + settings.bullet + " " + headline.link + "\n"
19
+ end
20
+
21
+ def scan_headlines(markdown_string)
22
+ headlines = []
23
+ markdown_string.scan(settings.headline_regexp).each{|markers, text|
24
+ level = markers.length
25
+ if level >= settings.headline_minimum_level && level <= settings.headline_maximum_level
26
+ headlines << SixArm::Markdown::Headline.new(level: level, text: text)
27
+ end
28
+ }
29
+ return headlines
30
+ end
31
+
32
+ def create_toc(markdown_string)
33
+ return scan_headlines(markdown_string).map{|headline| headline_to_markdown(headline)}.join
34
+ end
35
+
36
+ def rewrite_toc(markdown_string)
37
+ m = markdown_string.match(settings.contents_regexp)
38
+ return m ? m.pre_match + create_toc(markdown_string) + m.post_match : nil
39
+ end
40
+
41
+ end
@@ -0,0 +1,104 @@
1
+ # -*- coding: utf-8 -*-
2
+ =begin rdoc
3
+ =end
4
+
5
+ require "optparse"
6
+
7
+ module SixArm; module Markdown; module TOC; end; end; end
8
+
9
+ class SixArm::Markdown::TOC:: Settings
10
+
11
+ DefaultBullet = "*"
12
+ DefaultLinePrefix = ""
13
+ DefaultHeadlineMinimumLevel = 2
14
+ DefaultHeadlineMaximumLevel = 6
15
+ DefaultHeadlineRegexp = /^(#+) +(.+)\n/
16
+ DefaultContentsRegexp = /(^ *\* \[.*?\]\(#.*?\) *\n)+/m
17
+
18
+ attr_accessor \
19
+ :bullet,
20
+ :line_prefix,
21
+ :headline_minimum_level,
22
+ :headline_maximum_level,
23
+ :headline_regexp,
24
+ :contents_regexp
25
+
26
+ def initialize(
27
+ bullet: DefaultBullet,
28
+ line_prefix: DefaultLinePrefix,
29
+ headline_minimum_level: DefaultHeadlineMinimumLevel,
30
+ headline_maximum_level: DefaultHeadlineMaximumLevel,
31
+ headline_regexp: DefaultHeadlineRegexp,
32
+ contents_regexp: DefaultContentsRegexp
33
+ )
34
+ self.headline_regexp = headline_regexp
35
+ self.contents_regexp = contents_regexp
36
+ self.bullet = bullet
37
+ self.headline_minimum_level = headline_minimum_level
38
+ self.headline_maximum_level = headline_maximum_level
39
+ self.line_prefix = line_prefix
40
+ end
41
+
42
+ def option_parsing
43
+
44
+ ::OptionParser.new do |o|
45
+
46
+ o.banner = "Usage: markdown-table-of-contents [options]"
47
+ o.separator ""
48
+ o.separator "Options:"
49
+
50
+ o.on("-bBULLET", "--bullet BULLET", String,
51
+ "Set the bullet text",
52
+ "(default is \"#{DefaultBullet}\" and other typical choices are \"+\" and \"-\")"
53
+ ) do |x|
54
+ self.bullet = x
55
+ end
56
+
57
+ o.on("-lLINE_PREFIX", "--line-prefix LINE_PREFIX", String,
58
+ "Set the line prefiix text",
59
+ "(default is \"#{DefaultLinePrefix}\" and other typical choices are \" \" and \"\t\")"
60
+ ) do |x|
61
+ self.line_prefix = x
62
+ end
63
+
64
+ o.on("--min NUMBER", Integer,
65
+ "Set the headline minimum level",
66
+ "(default is \"#{DefaultHeadlineMinimumLevel}\" which means use H#{DefaultHeadlineMinimumLevel} and higher)"
67
+ ) do |x|
68
+ #self.headline_minimum_level = x
69
+ end
70
+
71
+ o.on("--max NUMBER", Integer,
72
+ "Set the headline maxmium level",
73
+ "(default is \"#{DefaultHeadlineMaximumLevel}\" which means use up to H#{DefaultHeadlineMaximumLevel})"
74
+ ) do |x|
75
+ self.headline_maximum_level = x
76
+ end
77
+
78
+ o.on("--headline-regexp REGEXP", Regexp,
79
+ "Set the headline regular expression",
80
+ "(default is #{DefaultHeadlineRegexp})"
81
+ ) do |x|
82
+ self.headline_regexp = x
83
+ end
84
+
85
+ o.on("--contents-regexp REGEXP", Regexp,
86
+ "Set the contents regular expression",
87
+ "(default is #{DefaultContentsRegexp})"
88
+ ) do |x|
89
+ self.contents_regexp = x
90
+ end
91
+
92
+ o.on_tail("-h", "--help", "Show this message") do
93
+ #puts "Help is TODO" #TODO
94
+ end
95
+
96
+ o.on_tail("--version", "Show version") do
97
+ #puts "Version is TODO" #TODO
98
+ end
99
+
100
+ end.parse!
101
+
102
+ end
103
+
104
+ end
@@ -0,0 +1,138 @@
1
+ # -*- coding: utf-8 -*-
2
+ require "sixarm_ruby_markdown_table_of_contents_test"
3
+
4
+ describe SixArm::Markdown::TOC::Generator do
5
+
6
+ include Let
7
+
8
+ let(:default_settings){
9
+ SixArm::Markdown::TOC::Settings.new
10
+ }
11
+
12
+ let(:custom_settings){
13
+ SixArm::Markdown::TOC::Settings.new(
14
+ bullet: custom_bullet,
15
+ line_prefix: custom_line_prefix,
16
+ headline_minimum_level: custom_headline_minimum_level,
17
+ headline_maximum_level: custom_headline_maximum_level,
18
+ )
19
+ }
20
+
21
+ let(:generator_with_default_settings) { SixArm::Markdown::TOC::Generator.new(settings: default_settings) }
22
+ let(:generator_with_custom_settings) { SixArm::Markdown::TOC::Generator.new(settings: custom_settings) }
23
+
24
+ let(:s) { markdown_input_as_markdown_string }
25
+ let(:headline){ SixArm::Markdown::Headline.new(level: 3, text: "Hello World") }
26
+
27
+ describe "#headline_to_markdown" do
28
+
29
+ describe "with default settings" do
30
+
31
+ it "return the headline, with a default line prefix ``, with a two-space indent because the headline is level 3, with a default bullet `*`" do
32
+ g = generator_with_default_settings
33
+ actual = g.headline_to_markdown(headline)
34
+ expect = " * [Hello World](#hello-world)\n"
35
+ expect(actual).must_equal(expect)
36
+ end
37
+
38
+ end
39
+
40
+ describe "with custom settings" do
41
+
42
+ it "return the headline, with a custom line prefix `!`, with a two-space indent because the headline is level 3, with a custom bullet `+`" do
43
+ g = generator_with_custom_settings
44
+ actual = g.headline_to_markdown(headline)
45
+ expect = "! + [Hello World](#hello-world)\n"
46
+ expect(actual).must_equal(expect)
47
+ end
48
+
49
+ end
50
+
51
+ end
52
+
53
+ describe "#scan_headlines" do
54
+
55
+ describe "with default settings" do
56
+
57
+ it "return all headlines" do
58
+ g = generator_with_default_settings
59
+ actual = g.scan_headlines(s)
60
+ expect = markdown_default_output_as_headlines
61
+ expect(actual).must_equal(expect)
62
+ end
63
+
64
+ end
65
+
66
+ describe "with custom settings" do
67
+
68
+ it "return the headlines that have a level greater than than or equal to the settings headline minimum level" do
69
+ g = generator_with_custom_settings
70
+ actual = g.scan_headlines(s)
71
+ expect = markdown_custom_output_as_headlines
72
+ expect(actual).must_equal(expect)
73
+ end
74
+
75
+ it "return the headlines that have a level less than or equal to the settings headline maximum level" do
76
+ g = generator_with_custom_settings
77
+ actual = g.scan_headlines(s)
78
+ expect = markdown_custom_output_as_headlines
79
+ expect(actual).must_equal(expect)
80
+ end
81
+
82
+ end
83
+
84
+ end
85
+
86
+ describe "#create_toc" do
87
+
88
+ describe "with default settings" do
89
+
90
+ it "return the toc" do
91
+ g = generator_with_default_settings
92
+ actual = g.create_toc(s)
93
+ expect = markdown_default_output_toc_as_markdown_string
94
+ expect(actual).must_equal(expect)
95
+ end
96
+
97
+ end
98
+
99
+ describe "with custom settings" do
100
+
101
+ it "return the toc" do
102
+ g = generator_with_custom_settings
103
+ actual = g.create_toc(s)
104
+ expect = markdown_custom_output_toc_as_markdown_string
105
+ expect(actual).must_equal(expect)
106
+ end
107
+
108
+ end
109
+
110
+ end
111
+
112
+ describe "#rewrite_toc" do
113
+
114
+ describe "with default settings" do
115
+
116
+ it "rewrite" do
117
+ g = generator_with_default_settings
118
+ actual = g.rewrite_toc(s)
119
+ expect = markdown_default_output_as_markdown_string
120
+ expect(actual).must_equal(expect)
121
+ end
122
+
123
+ end
124
+
125
+ describe "with custom settings" do
126
+
127
+ it "rewrite" do
128
+ g = generator_with_custom_settings
129
+ expect = markdown_custom_output_as_markdown_string
130
+ expect(g.rewrite_toc(s)).must_equal(expect)
131
+ end
132
+
133
+ end
134
+
135
+ end
136
+
137
+
138
+ end
@@ -0,0 +1,187 @@
1
+ # -*- coding: utf-8 -*-
2
+ require "sixarm_ruby_markdown_table_of_contents_test"
3
+
4
+ describe SixArm::Markdown::TOC::Settings do
5
+
6
+ include Let
7
+
8
+ let(:settings){ SixArm::Markdown::TOC::Settings.new }
9
+
10
+ def argv(*strings)
11
+ ARGV.clear.append(*strings)
12
+ end
13
+
14
+ describe "#initialize" do
15
+
16
+ describe "with defaults" do
17
+
18
+ it "return default headline regexp" do
19
+ x = SixArm::Markdown::TOC::Settings::DefaultHeadlineRegexp
20
+ expect(SixArm::Markdown::TOC::Settings.new.headline_regexp).must_equal x
21
+ end
22
+
23
+ it "return default contents regexp" do
24
+ x = SixArm::Markdown::TOC::Settings::DefaultContentsRegexp
25
+ expect(SixArm::Markdown::TOC::Settings.new.contents_regexp).must_equal x
26
+ end
27
+
28
+ it "return default bullet" do
29
+ x = SixArm::Markdown::TOC::Settings::DefaultBullet
30
+ expect(SixArm::Markdown::TOC::Settings.new.bullet).must_equal x
31
+ end
32
+
33
+ it "return default headline minimum level" do
34
+ x = SixArm::Markdown::TOC::Settings::DefaultHeadlineMinimumLevel
35
+ expect(SixArm::Markdown::TOC::Settings.new.headline_minimum_level).must_equal x
36
+ end
37
+
38
+ it "return default headline maximum level" do
39
+ x = SixArm::Markdown::TOC::Settings::DefaultHeadlineMaximumLevel
40
+ expect(SixArm::Markdown::TOC::Settings.new.headline_maximum_level).must_equal x
41
+ end
42
+
43
+ it "return default line_prefix" do
44
+ x = SixArm::Markdown::TOC::Settings::DefaultLinePrefix
45
+ expect(SixArm::Markdown::TOC::Settings.new.line_prefix).must_equal x
46
+ end
47
+
48
+ end
49
+
50
+ describe "with options" do
51
+
52
+ it "return custom headline regexp" do
53
+ x = /x/
54
+ expect(SixArm::Markdown::TOC::Settings.new(headline_regexp: x).headline_regexp).must_equal x
55
+ end
56
+
57
+ it "return custom contents regexp" do
58
+ x = /x/
59
+ expect(SixArm::Markdown::TOC::Settings.new(contents_regexp: x).contents_regexp).must_equal x
60
+ end
61
+
62
+ it "return custom bullet" do
63
+ x = custom_bullet
64
+ expect(SixArm::Markdown::TOC::Settings.new(bullet: x).bullet).must_equal x
65
+ end
66
+
67
+ it "return custom line_prefix" do
68
+ x = custom_line_prefix
69
+ expect(SixArm::Markdown::TOC::Settings.new(line_prefix: x).line_prefix).must_equal x
70
+ end
71
+
72
+ it "return custom headline minimum level" do
73
+ x = custom_headline_minimum_level
74
+ expect(SixArm::Markdown::TOC::Settings.new(headline_minimum_level: x).headline_minimum_level).must_equal x
75
+ end
76
+
77
+ it "return custom headline maximum level" do
78
+ x = custom_headline_maximum_level
79
+ expect(SixArm::Markdown::TOC::Settings.new(headline_maximum_level: x).headline_maximum_level).must_equal x
80
+ end
81
+
82
+ end
83
+
84
+ end
85
+
86
+ describe "#option_parsing" do
87
+
88
+ describe "bullet" do
89
+
90
+ before do
91
+ @x = custom_bullet
92
+ end
93
+
94
+ it "parse short" do
95
+ argv("-b", @x)
96
+ settings.option_parsing
97
+ expect(settings.bullet).must_equal @x
98
+ end
99
+
100
+ it "parse long" do
101
+ argv("--bullet", @x)
102
+ settings.option_parsing
103
+ expect(settings.bullet).must_equal @x
104
+ end
105
+
106
+ end
107
+
108
+ describe "line prefix" do
109
+
110
+ before do
111
+ @x = custom_line_prefix
112
+ end
113
+
114
+ it "parse short" do
115
+ argv("-l", @x)
116
+ settings.option_parsing
117
+ expect(settings.line_prefix).must_equal @x
118
+ end
119
+
120
+ it "parse long" do
121
+ argv("--line-prefix", @x)
122
+ settings.option_parsing
123
+ expect(settings.line_prefix).must_equal @x
124
+ end
125
+
126
+ end
127
+
128
+ describe "headline minimum level" do
129
+
130
+ before do
131
+ @x = custom_headline_minimum_level
132
+ end
133
+
134
+ it "parse" do
135
+ argv("--min", @x.to_s)
136
+ settings.option_parsing
137
+ #expect(settings.headline_minimum_level).must_equal @x
138
+ end
139
+
140
+ end
141
+
142
+ describe "headline maxmimum level" do
143
+
144
+ before do
145
+ @x = custom_headline_maximum_level
146
+ end
147
+
148
+ it "parse" do
149
+ argv("--max", @x.to_s)
150
+ settings.option_parsing
151
+ expect(settings.headline_maximum_level).must_equal @x
152
+ end
153
+
154
+ end
155
+
156
+ describe "help" do
157
+
158
+ it "parse short" do
159
+ argv("-h")
160
+ settings.option_parsing #TODO remove when the line below works
161
+ #proc { settings.option_parsing }.must_output "hello"
162
+ end
163
+
164
+ it "parse long" do
165
+ argv("--help")
166
+ proc { settings.option_parsing }#.must_output "hello" #TODO
167
+ end
168
+
169
+ end
170
+
171
+ describe "version" do
172
+
173
+ it "parse short" do
174
+ argv("-v")
175
+ proc { settings.option_parsing }#.must_output /^\d+\.\d+\.\d+/ #TODO
176
+ end
177
+
178
+ it "parse long" do
179
+ argv("--version")
180
+ proc { settings.option_parsing }#.must_output /^\d+\.\d+\.\d+/ #TODO
181
+ end
182
+
183
+ end
184
+
185
+ end
186
+
187
+ end
data/test/support/let.rb CHANGED
@@ -12,29 +12,9 @@ module Let
12
12
  * [](#)
13
13
 
14
14
  ## Alpha
15
-
16
- Lorem ipsum
17
-
18
15
  ### Bravo
19
-
20
- Lorem ipsum
21
-
22
16
  #### Charlie
23
-
24
- Lorem ipsum
25
-
26
- ## Delta
27
-
28
- Lorem ipsum
29
-
30
- ### Echo
31
-
32
- Lorem ipsum
33
-
34
- #### Foxtrot
35
-
36
- Lorem ipsum
37
-
17
+ ##### Delta
38
18
  XXX
39
19
  }
40
20
 
@@ -45,7 +25,7 @@ module Let
45
25
  XXX
46
26
  }
47
27
 
48
- let(:markdown_input_toc_as_string){
28
+ let(:markdown_input_toc_match_as_string){
49
29
  <<~XXX
50
30
  * [](#)
51
31
  XXX
@@ -55,96 +35,97 @@ module Let
55
35
  <<~XXX
56
36
 
57
37
  ## Alpha
58
-
59
- Lorem ipsum
60
-
61
38
  ### Bravo
62
-
63
- Lorem ipsum
64
-
65
39
  #### Charlie
66
-
67
- Lorem ipsum
68
-
69
- ## Delta
70
-
71
- Lorem ipsum
72
-
73
- ### Echo
74
-
75
- Lorem ipsum
76
-
77
- #### Foxtrot
78
-
79
- Lorem ipsum
80
-
40
+ ##### Delta
81
41
  XXX
82
42
  }
83
43
 
84
44
 
85
- let(:markdown_output_as_string){
45
+ let(:markdown_default_output_as_string){
86
46
  <<~XXX
87
47
  # Example
88
48
 
89
49
  * [Alpha](#alpha)
90
50
  * [Bravo](#bravo)
91
51
  * [Charlie](#charlie)
92
- * [Delta](#delta)
93
- * [Echo](#echo)
94
- * [Foxtrot](#foxtrot)
52
+ * [Delta](#delta)
95
53
 
96
54
  ## Alpha
97
-
98
- Lorem ipsum
99
-
100
55
  ### Bravo
101
-
102
- Lorem ipsum
103
-
104
56
  #### Charlie
57
+ ##### Delta
58
+ XXX
59
+ }
105
60
 
106
- Lorem ipsum
107
-
108
- ## Delta
109
-
110
- Lorem ipsum
111
-
112
- ### Echo
113
-
114
- Lorem ipsum
115
-
116
- #### Foxtrot
61
+ let(:markdown_custom_output_as_string){
62
+ <<~XXX
63
+ # Example
117
64
 
118
- Lorem ipsum
65
+ ! + [Bravo](#bravo)
66
+ ! + [Charlie](#charlie)
119
67
 
68
+ ## Alpha
69
+ ### Bravo
70
+ #### Charlie
71
+ ##### Delta
120
72
  XXX
121
73
  }
122
74
 
123
- let(:markdown_output_toc_as_string){
75
+ let(:markdown_default_output_toc_as_string){
124
76
  <<~XXX
125
77
  * [Alpha](#alpha)
126
78
  * [Bravo](#bravo)
127
79
  * [Charlie](#charlie)
128
- * [Delta](#delta)
129
- * [Echo](#echo)
130
- * [Foxtrot](#foxtrot)
80
+ * [Delta](#delta)
81
+ XXX
82
+ }
83
+
84
+ let(:markdown_custom_output_toc_as_string){
85
+ <<~XXX
86
+ ! + [Bravo](#bravo)
87
+ ! + [Charlie](#charlie)
131
88
  XXX
132
89
  }
133
90
 
134
91
  let(:markdown_input_as_markdown_string){
135
- Markdown::String.new(markdown_input_as_string)
92
+ SixArm::Markdown::String.new(markdown_input_as_string)
136
93
  }
137
94
 
138
- let(:markdown_input_toc_as_markdown_string){
139
- Markdown::String.new(markdown_input_toc_as_string)
95
+ let(:markdown_input_toc_match_as_markdown_string){
96
+ SixArm::Markdown::String.new(markdown_input_toc_match_as_string)
140
97
  }
141
98
 
142
- let(:markdown_output_as_markdown_string){
143
- Markdown::String.new(markdown_output_as_string)
99
+ let(:markdown_default_output_as_headlines){[
100
+ SixArm::Markdown::Headline.new(level: 2, text: "Alpha"),
101
+ SixArm::Markdown::Headline.new(level: 3, text: "Bravo"),
102
+ SixArm::Markdown::Headline.new(level: 4, text: "Charlie"),
103
+ SixArm::Markdown::Headline.new(level: 5, text: "Delta"),
104
+ ]}
105
+
106
+ let(:markdown_custom_output_as_headlines){[
107
+ SixArm::Markdown::Headline.new(level: 3, text: "Bravo"),
108
+ SixArm::Markdown::Headline.new(level: 4, text: "Charlie"),
109
+ ]}
110
+
111
+ let(:markdown_default_output_as_markdown_string){
112
+ SixArm::Markdown::String.new(markdown_default_output_as_string)
113
+ }
114
+
115
+ let(:markdown_default_output_toc_as_markdown_string){
116
+ SixArm::Markdown::String.new(markdown_default_output_toc_as_string)
144
117
  }
145
118
 
146
- let(:markdown_output_toc_as_markdown_string){
147
- Markdown::String.new(markdown_output_toc_as_string)
119
+ let(:markdown_custom_output_as_markdown_string){
120
+ SixArm::Markdown::String.new(markdown_custom_output_as_string)
121
+ }
122
+ let(:markdown_custom_output_toc_as_markdown_string){
123
+ SixArm::Markdown::String.new(markdown_custom_output_toc_as_string)
148
124
  }
149
125
 
126
+ let(:custom_bullet){ "+" }
127
+ let(:custom_line_prefix){ "!" }
128
+ let(:custom_headline_minimum_level){ 3 }
129
+ let(:custom_headline_maximum_level){ 4 }
130
+
150
131
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sixarm_ruby_markdown_table_of_contents
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - SixArm
@@ -39,8 +39,28 @@ cert_chain:
39
39
  tzpk/VnZXj7Ek/earx+N/Z+Wtnl2xENm5IF8SFPeI1HFa9NH47pqtxF1YKpNIEVc
40
40
  2xa2BNHSePe7tys/2hbmZuyMu8X5ERmovsabSXB3a+YwtJh5c2jhA21wF7986s0q
41
41
  -----END CERTIFICATE-----
42
- date: 2018-02-06 00:00:00.000000000 Z
42
+ date: 2018-02-12 00:00:00.000000000 Z
43
43
  dependencies:
44
+ - !ruby/object:Gem::Dependency
45
+ name: sixarm_ruby_markdown
46
+ requirement: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '2'
51
+ - - "<"
52
+ - !ruby/object:Gem::Version
53
+ version: '3'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '2'
61
+ - - "<"
62
+ - !ruby/object:Gem::Version
63
+ version: '3'
44
64
  - !ruby/object:Gem::Dependency
45
65
  name: sixarm_ruby_file_rewrite
46
66
  requirement: !ruby/object:Gem::Requirement
@@ -171,13 +191,11 @@ files:
171
191
  - Rakefile
172
192
  - bin/markdown-table-of-contents
173
193
  - lib/sixarm_ruby_markdown_table_of_contents.rb
174
- - lib/sixarm_ruby_markdown_table_of_contents/markdown/file.rb
175
- - lib/sixarm_ruby_markdown_table_of_contents/markdown/headline.rb
176
- - lib/sixarm_ruby_markdown_table_of_contents/markdown/string.rb
194
+ - lib/sixarm_ruby_markdown_table_of_contents/sixarm/markdown/toc/generator.rb
195
+ - lib/sixarm_ruby_markdown_table_of_contents/sixarm/markdown/toc/settings.rb
177
196
  - test/sixarm_ruby_markdown_table_of_contents_test.rb
178
- - test/sixarm_ruby_markdown_table_of_contents_test/markdown/file_test.rb
179
- - test/sixarm_ruby_markdown_table_of_contents_test/markdown/headline_test.rb
180
- - test/sixarm_ruby_markdown_table_of_contents_test/markdown/string_test.rb
197
+ - test/sixarm_ruby_markdown_table_of_contents_test/sixarm/markdown/toc/generator_test.rb
198
+ - test/sixarm_ruby_markdown_table_of_contents_test/sixarm/markdown/toc/settings_test.rb
181
199
  - test/support/let.rb
182
200
  homepage: http://sixarm.com/
183
201
  licenses:
@@ -201,7 +219,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
201
219
  requirements:
202
220
  - - ">="
203
221
  - !ruby/object:Gem::Version
204
- version: '0'
222
+ version: '2.2'
205
223
  required_rubygems_version: !ruby/object:Gem::Requirement
206
224
  requirements:
207
225
  - - ">="
@@ -215,7 +233,6 @@ specification_version: 4
215
233
  summary: SixArm.com → Ruby → Mardown table of contents
216
234
  test_files:
217
235
  - test/sixarm_ruby_markdown_table_of_contents_test.rb
218
- - test/sixarm_ruby_markdown_table_of_contents_test/markdown/file_test.rb
219
- - test/sixarm_ruby_markdown_table_of_contents_test/markdown/headline_test.rb
220
- - test/sixarm_ruby_markdown_table_of_contents_test/markdown/string_test.rb
236
+ - test/sixarm_ruby_markdown_table_of_contents_test/sixarm/markdown/toc/generator_test.rb
237
+ - test/sixarm_ruby_markdown_table_of_contents_test/sixarm/markdown/toc/settings_test.rb
221
238
  - test/support/let.rb
metadata.gz.sig CHANGED
Binary file
@@ -1,16 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- =begin rdoc
3
- Markdown::File#refresh_toc
4
- =end
5
-
6
- class Markdown::File < ::File
7
-
8
- def slurp
9
- Markdown::String.new(read.scrub)
10
- end
11
-
12
- def rewrite_toc
13
- rewrite(slurp.refresh_toc)
14
- end
15
-
16
- end
@@ -1,24 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- =begin rdoc
3
- Headline
4
- =end
5
-
6
- class Headline < OpenStruct
7
-
8
- def to_markdown
9
- indent + "* " + link + "\n"
10
- end
11
-
12
- def indent
13
- level ? (" " * (level - 2)) : ""
14
- end
15
-
16
- def link
17
- "[#{text}](##{anchor})"
18
- end
19
-
20
- def anchor
21
- text.downcase.gsub(/\W+/,'-')
22
- end
23
-
24
- end
@@ -1,27 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- =begin rdoc
3
- Markdown::String#refresh_toc
4
- =end
5
-
6
- module Markdown
7
-
8
- class String < ::String
9
-
10
- def generate_toc
11
- return scan(/^(##+) +(.+)\n/).map{|level, text|
12
- Headline.new(level: level.length, text: text).to_markdown
13
- }.join
14
- end
15
-
16
- def match_toc
17
- /(^ *\* \[.*?\]\(#.*?\) *\n)+/m
18
- end
19
-
20
- def refresh_toc
21
- m = match(match_toc)
22
- return m ? m.pre_match + generate_toc + m.post_match : nil
23
- end
24
-
25
- end
26
-
27
- end
@@ -1,54 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- require "sixarm_ruby_markdown_table_of_contents_test"
3
-
4
- describe Markdown::File do
5
-
6
- include Let
7
- let(:dir){ File.expand_path File.dirname(__FILE__) }
8
- let(:file_path){ dir + "tempfile"}
9
- let(:text){ "foo" }
10
-
11
- describe "#slurp" do
12
-
13
- before do
14
- file = Markdown::File.new(file_path, "w")
15
- file.write(text)
16
- file.close
17
- end
18
-
19
- it "read and scrub, and return a Markdown::String" do
20
- file = Markdown::File.new(file_path)
21
- s = file.slurp
22
- expect(s).must_equal text
23
- expect(s).must_be_kind_of Markdown::String
24
- end
25
-
26
- after do
27
- File.delete(file_path)
28
- end
29
-
30
- end
31
-
32
- describe "#rewrite_toc" do
33
-
34
- before do
35
- file = Markdown::File.new(file_path, "w")
36
- file.write markdown_input_as_markdown_string
37
- file.close
38
- end
39
-
40
- it "rewrite" do
41
- file = Markdown::File.new(file_path)
42
- file.rewrite_toc
43
- file = Markdown::File.new(file_path)
44
- expect(file.slurp).must_equal(markdown_output_as_markdown_string)
45
- file.close
46
- end
47
-
48
- after do
49
- File.delete(file_path)
50
- end
51
-
52
- end
53
-
54
- end
@@ -1,60 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- require "sixarm_ruby_markdown_table_of_contents_test"
3
-
4
- describe Headline do
5
-
6
- include Let
7
-
8
- let(:level){ 3 }
9
- let(:text){ "Hello World" }
10
- let(:headline){ Headline.new(level: level, text: text) }
11
-
12
- describe "#text" do
13
-
14
- it "return text" do
15
- expect(headline.text).must_equal "Hello World"
16
- end
17
-
18
- end
19
-
20
- describe "#level" do
21
-
22
- it "return level" do
23
- expect(headline.text).must_equal "Hello World"
24
- end
25
-
26
- end
27
-
28
- describe "#indent" do
29
-
30
- it "return indent, which is two spaces per level greater than H2" do
31
- expect(headline.indent).must_equal " "
32
- end
33
-
34
- end
35
-
36
- describe "#anchor" do
37
-
38
- it "return anchor, which is the text as lowercase, and replacing every run of non-word characters with a dash" do
39
- expect(headline.anchor).must_equal "hello-world"
40
- end
41
-
42
- end
43
-
44
- describe "#link" do
45
-
46
- it "return link, which is the Markdown formatting of the text and anchor" do
47
- expect(headline.link).must_equal "[Hello World](#hello-world)"
48
- end
49
-
50
- end
51
-
52
- describe "#to_markdown" do
53
-
54
- it "return the headline table of contents entry, which is a bullet link, and ending newline" do
55
- expect(headline.to_markdown).must_equal " * [Hello World](#hello-world)\n"
56
- end
57
-
58
- end
59
-
60
- end
@@ -1,34 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- require "sixarm_ruby_markdown_table_of_contents_test"
3
-
4
- describe Markdown::String do
5
-
6
- include Let
7
-
8
- let(:s) { markdown_input_as_markdown_string }
9
-
10
- describe "#generate_toc" do
11
-
12
- it "return the toc" do
13
- expect(s.generate_toc).must_equal(markdown_output_toc_as_markdown_string)
14
- end
15
-
16
- end
17
-
18
- describe "#match_toc" do
19
-
20
- it "match on the first occurance of lines that look like a table of contents" do
21
- expect(s.match_toc).must_equal /(^ *\* \[.*?\]\(#.*?\) *\n)+/m
22
- end
23
-
24
- end
25
-
26
- describe "#refresh_toc" do
27
-
28
- it "refresh" do
29
- expect(s.refresh_toc).must_equal(markdown_output_as_markdown_string)
30
- end
31
-
32
- end
33
-
34
- end