tocer 8.2.1 → 9.0.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: de08def15c7da9f0e9f3fc337decee184eec685edd94395d5047741a923c0983
4
- data.tar.gz: 40b60a857bce2e4e5ab1b7a5c416b8040b644ae7edad35c2ef6554bd8f8f52de
3
+ metadata.gz: a3c9b3dae9b9556720306f6c34cc8f516f447e3cca5e461949c4f417c24e9e7f
4
+ data.tar.gz: c41d9489fc0f358d580e7e002fda5ab52cca4bd169000de3a9aa0f76edd5381e
5
5
  SHA512:
6
- metadata.gz: 9451bde0737ece398e55010f999010389bef5bb5d57ae9a314aa4d3004178962c4c8d5924432282d6144e1b037dfe5a8506aab812787bee35ee68ee2c0c44683
7
- data.tar.gz: f64b8781b9acb5ed881125026705ff809d31e7f757cb2c5d50288155f6c3c2f59b8d0015600067661449d52373db51534519c3eaa7b1307dc5623a3b8373bfca
6
+ metadata.gz: 57c8ebbfdc73807bf818b4f871e5aea7631c2a8b1be6739ee677a670ee52ff7541bc4070785b3ef09f29e79d8c3727d83650020ec19d3e93e9d44b83ff399a5c
7
+ data.tar.gz: 3137ddfc17abba4c07ce2cbce0558809cb8c2eb0eb04b5ed40d3953c454683bc86d292375d8dc19c04f8a06b7421a095090a9ddf880d9a12645b617c3a29a8df
Binary file
data.tar.gz.sig CHANGED
Binary file
data/README.md CHANGED
@@ -22,6 +22,7 @@ Markdown files.
22
22
  - [Usage](#usage)
23
23
  - [Command Line Interface (CLI)](#command-line-interface-cli)
24
24
  - [Customization](#customization)
25
+ - [Rake](#rake)
25
26
  - [Tests](#tests)
26
27
  - [Versioning](#versioning)
27
28
  - [Code of Conduct](#code-of-conduct)
@@ -109,7 +110,7 @@ Feel free to take this default configuration, modify, and save as your own custo
109
110
  The `configuration.yml` file can be configured as follows:
110
111
 
111
112
  - `label`: The header label for the table of contents. Default: "# Table of Contents".
112
- - `includes`: The list of included files. Default: "*.md".
113
+ - `includes`: The list of included files. Default: "\*.md".
113
114
 
114
115
  There are multiple ways the include list can be defined. Here are some examples:
115
116
 
@@ -132,6 +133,20 @@ There are multiple ways the include list can be defined. Here are some examples:
132
133
  :includes:
133
134
  - "**/*.md"
134
135
 
136
+ ### Rake
137
+
138
+ You can add Rake support by adding the following to your `Rakefile`:
139
+
140
+ begin
141
+ require "tocer/rake/setup"
142
+ rescue LoadError => error
143
+ puts error.message
144
+ end
145
+
146
+ Once configured, the following tasks will be available (i.e. `bundle exec rake -T`):
147
+
148
+ rake toc[label,includes] # Add/Update Table of Contents (README)
149
+
135
150
  ## Tests
136
151
 
137
152
  To test, run:
@@ -1,10 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "tocer/identity"
4
+ require "tocer/configuration"
4
5
  require "tocer/elements/comment_block"
5
6
  require "tocer/parsers/header"
6
7
  require "tocer/transformers/link"
7
8
  require "tocer/transformers/text"
9
+ require "tocer/transformers/finder"
8
10
  require "tocer/builder"
9
11
  require "tocer/writer"
10
12
  require "tocer/runner"
@@ -11,22 +11,14 @@ module Tocer
11
11
 
12
12
  def_delegators :comment_block, :start_index, :finish_index, :prependable?
13
13
 
14
- def self.transformer header
15
- if header.match?(/\[.+\]\(.+\)/)
16
- Transformers::Link.new header
17
- else
18
- Transformers::Text.new header
19
- end
20
- end
21
-
22
14
  def initialize label: "## Table of Contents", comment_block: Elements::CommentBlock.new
23
15
  @label = label
24
16
  @comment_block = comment_block
25
- @url_count = Hash.new { |hash, key| hash[key] = 0 }
17
+ @url_count = Hash.new 0
26
18
  @code_block = false
27
19
  end
28
20
 
29
- def build lines
21
+ def call lines
30
22
  return "" if headers(lines).empty?
31
23
 
32
24
  [
@@ -42,23 +34,8 @@ module Tocer
42
34
  attr_reader :label, :comment_block, :url_count
43
35
  attr_accessor :code_block
44
36
 
45
- def toggle_code_block line
46
- return unless line.start_with? CODE_BLOCK_PUNCTUATION
47
-
48
- self.code_block = !code_block
49
- end
50
-
51
- def url_suffix url
52
- count = url_count[url]
53
- count.zero? ? "" : count
54
- end
55
-
56
- def transform header
57
- transformer = self.class.transformer header
58
- url = transformer.url
59
- link = transformer.transform url_suffix: url_suffix(url)
60
- url_count[url] += 1
61
- link
37
+ def links lines
38
+ headers(lines).map { |markdown| transform markdown }
62
39
  end
63
40
 
64
41
  def headers lines
@@ -68,8 +45,23 @@ module Tocer
68
45
  end
69
46
  end
70
47
 
71
- def links lines
72
- headers(lines).map { |header| transform header }
48
+ def toggle_code_block line
49
+ return unless line.start_with? CODE_BLOCK_PUNCTUATION
50
+
51
+ self.code_block = !code_block
52
+ end
53
+
54
+ def transform markdown
55
+ Transformers::Finder.call(markdown).then do |transformer|
56
+ url = transformer.url
57
+ link = transformer.call url_suffix: url_suffix(url)
58
+ url_count[url] += 1
59
+ link
60
+ end
61
+ end
62
+
63
+ def url_suffix url
64
+ url_count[url].then { |count| count.zero? ? "" : count }
73
65
  end
74
66
  end
75
67
  end
@@ -11,17 +11,9 @@ module Tocer
11
11
 
12
12
  package_name Identity.version_label
13
13
 
14
- def self.configuration
15
- Runcom::Config.new Identity.name,
16
- defaults: {
17
- label: "## Table of Contents",
18
- includes: ["README.md"]
19
- }
20
- end
21
-
22
14
  def initialize args = [], options = {}, config = {}
23
15
  super args, options, config
24
- @configuration = self.class.configuration
16
+ @configuration = Tocer::Configuration.default
25
17
  rescue Runcom::Errors::Base => error
26
18
  abort error.message
27
19
  end
@@ -32,12 +24,12 @@ module Tocer
32
24
  aliases: "-l",
33
25
  desc: "Label",
34
26
  type: :string,
35
- default: configuration.to_h.fetch(:label)
27
+ default: Tocer::Configuration.default.to_h.fetch(:label)
36
28
  method_option :includes,
37
29
  aliases: "-i",
38
30
  desc: "File include list",
39
31
  type: :array,
40
- default: configuration.to_h.fetch(:includes)
32
+ default: Tocer::Configuration.default.to_h.fetch(:includes)
41
33
  # :reek:TooManyStatements
42
34
  def generate path = "."
43
35
  updated_configuration = configuration.merge label: options.label, includes: options.includes
@@ -46,7 +38,7 @@ module Tocer
46
38
 
47
39
  return if files.empty?
48
40
 
49
- runner.run
41
+ runner.call
50
42
 
51
43
  say "Processed table of contents for:"
52
44
  files.each { |file| say " #{file}" }
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "runcom"
4
+
5
+ module Tocer
6
+ module Configuration
7
+ def self.default
8
+ Runcom::Config.new Identity.name,
9
+ defaults: {
10
+ label: "## Table of Contents",
11
+ includes: ["README.md"]
12
+ }
13
+ end
14
+ end
15
+ end
@@ -12,7 +12,7 @@ module Tocer
12
12
  end
13
13
 
14
14
  def self.version
15
- "8.2.1"
15
+ "9.0.0"
16
16
  end
17
17
 
18
18
  def self.version_label
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "tocer/rake/tasks"
4
+ Tocer::Rake::Tasks.setup
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rake"
4
+
5
+ module Tocer
6
+ module Rake
7
+ class Tasks
8
+ include ::Rake::DSL
9
+
10
+ def self.setup
11
+ new.install
12
+ end
13
+
14
+ def initialize configuration: Tocer::Configuration.default, runner: Runner
15
+ @configuration = configuration
16
+ @runner = runner
17
+ end
18
+
19
+ def install
20
+ desc "Add/Update Table of Contents (README)"
21
+ task :toc, %i[label includes] do |_task, arguments|
22
+ inputs = {label: arguments[:label], includes: arguments[:includes]}.compact
23
+ updated_configuration = configuration.merge inputs
24
+ runner.new(configuration: updated_configuration.to_h).call
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ attr_reader :configuration, :runner
31
+ end
32
+ end
33
+ end
@@ -17,8 +17,8 @@ module Tocer
17
17
  Pathname.glob(%(#{path}/{#{includes.join ","}})).select(&:file?)
18
18
  end
19
19
 
20
- def run
21
- files.each { |file| writer.new(file, label: configuration.fetch(:label)).write }
20
+ def call
21
+ files.each { |file| writer.new(file, label: configuration.fetch(:label)).call }
22
22
  end
23
23
 
24
24
  private
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tocer
4
+ module Transformers
5
+ # Finds appropriate header transformer for matching pattern.
6
+ class Finder
7
+ TRANSFORMERS = {
8
+ /\[.+\]\(.+\)/ => Transformers::Link,
9
+ /.*/ => Transformers::Text
10
+ }.freeze
11
+
12
+ def self.call markdown
13
+ TRANSFORMERS.find { |pattern, transformer| break transformer if pattern.match? markdown }
14
+ .then { |transformer| transformer.new markdown }
15
+ end
16
+ end
17
+ end
18
+ end
@@ -8,46 +8,49 @@ module Tocer
8
8
  class Link
9
9
  using Refinements::Arrays
10
10
 
11
- def initialize text, header: Parsers::Header
12
- @header = header.new text
11
+ def initialize text, parser: Parsers::Header
12
+ @parser = parser.new text
13
13
  end
14
14
 
15
15
  def label
16
- header.content.gsub embedded_link, embedded_link_label
16
+ parser.content.gsub embedded_link, embedded_link_label
17
17
  end
18
18
 
19
19
  def url
20
20
  label.downcase.gsub(/\s/, "-").gsub(/[^\w\-]+/, "")
21
21
  end
22
22
 
23
- def transform url_suffix: ""
24
- modified_url = [url, url_suffix.to_s].compress.join "-"
25
- "#{indented_bullet}[#{label}](##{modified_url})"
23
+ def call url_suffix: ""
24
+ "#{indented_bullet}[#{label}](##{computed_url url_suffix})"
26
25
  end
27
26
 
28
27
  private
29
28
 
30
- attr_reader :header
29
+ attr_reader :parser
31
30
 
32
- def embedded_link_label
33
- header.content[/\[(.*)\]/, 1]
34
- end
35
-
36
- def embedded_link_url
37
- header.content[/\((.*)\)/, 1]
31
+ def computed_url suffix = ""
32
+ [url, suffix.to_s].compress.join "-"
38
33
  end
39
34
 
40
35
  def embedded_link
41
36
  "[#{embedded_link_label}](#{embedded_link_url})"
42
37
  end
43
38
 
44
- def prefix_to_spaces
45
- Array.new(header.prefix.length, " ").join
39
+ def embedded_link_label
40
+ parser.content[/\[(.*)\]/, 1]
41
+ end
42
+
43
+ def embedded_link_url
44
+ parser.content[/\((.*)\)/, 1]
46
45
  end
47
46
 
48
47
  def indented_bullet
49
48
  prefix_to_spaces.gsub(/\s{2}$/, "- ")
50
49
  end
50
+
51
+ def prefix_to_spaces
52
+ Array.new(parser.prefix.length, " ").join
53
+ end
51
54
  end
52
55
  end
53
56
  end
@@ -8,34 +8,37 @@ module Tocer
8
8
  class Text
9
9
  using Refinements::Arrays
10
10
 
11
- def initialize text, header: Parsers::Header
12
- @header = header.new text
11
+ def initialize text, parser: Parsers::Header
12
+ @parser = parser.new text
13
13
  end
14
14
 
15
15
  def label
16
- header.content
16
+ parser.content
17
17
  end
18
18
 
19
19
  def url
20
20
  label.downcase.gsub(/\s/, "-").gsub(/[^\w\-]+/, "")
21
21
  end
22
22
 
23
- def transform url_suffix: ""
24
- modified_url = [url, url_suffix.to_s].compress.join "-"
25
- "#{indented_bullet}[#{label}](##{modified_url})"
23
+ def call url_suffix: ""
24
+ "#{indented_bullet}[#{label}](##{computed_url url_suffix})"
26
25
  end
27
26
 
28
27
  private
29
28
 
30
- attr_reader :header
29
+ attr_reader :parser
31
30
 
32
- def prefix_to_spaces
33
- Array.new(header.prefix.length, " ").join
31
+ def computed_url suffix = ""
32
+ [url, suffix.to_s].compress.join "-"
34
33
  end
35
34
 
36
35
  def indented_bullet
37
36
  prefix_to_spaces.gsub(/\s{2}$/, "- ")
38
37
  end
38
+
39
+ def prefix_to_spaces
40
+ Array.new(parser.prefix.length, " ").join
41
+ end
39
42
  end
40
43
  end
41
44
  end
@@ -18,7 +18,7 @@ module Tocer
18
18
  @builder = builder
19
19
  end
20
20
 
21
- def write
21
+ def call
22
22
  lines = File.readlines file_path
23
23
  body = builder.prependable?(lines) ? prepend(lines) : replace(lines)
24
24
  File.open(file_path, "w") { |file| file.write body }
@@ -29,7 +29,7 @@ module Tocer
29
29
  attr_reader :file_path, :builder
30
30
 
31
31
  def content lines
32
- builder.build lines
32
+ builder.call lines
33
33
  end
34
34
 
35
35
  def replace lines
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tocer
3
3
  version: !ruby/object:Gem::Version
4
- version: 8.2.1
4
+ version: 9.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brooke Kuhlmann
@@ -28,7 +28,7 @@ cert_chain:
28
28
  dKvURM+1PwDCzC5tvRwjhUJIizau6+MtkFCvJHmaAj1aZL3odcPejHj5Hxt/0CUW
29
29
  y84=
30
30
  -----END CERTIFICATE-----
31
- date: 2019-05-02 00:00:00.000000000 Z
31
+ date: 2019-06-01 00:00:00.000000000 Z
32
32
  dependencies:
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: refinements
@@ -50,14 +50,14 @@ dependencies:
50
50
  requirements:
51
51
  - - "~>"
52
52
  - !ruby/object:Gem::Version
53
- version: '4.0'
53
+ version: '5.0'
54
54
  type: :runtime
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
57
57
  requirements:
58
58
  - - "~>"
59
59
  - !ruby/object:Gem::Version
60
- version: '4.0'
60
+ version: '5.0'
61
61
  - !ruby/object:Gem::Dependency
62
62
  name: thor
63
63
  requirement: !ruby/object:Gem::Requirement
@@ -128,34 +128,6 @@ dependencies:
128
128
  - - "~>"
129
129
  - !ruby/object:Gem::Version
130
130
  version: '0.1'
131
- - !ruby/object:Gem::Dependency
132
- name: gemsmith
133
- requirement: !ruby/object:Gem::Requirement
134
- requirements:
135
- - - "~>"
136
- - !ruby/object:Gem::Version
137
- version: '13.0'
138
- type: :development
139
- prerelease: false
140
- version_requirements: !ruby/object:Gem::Requirement
141
- requirements:
142
- - - "~>"
143
- - !ruby/object:Gem::Version
144
- version: '13.0'
145
- - !ruby/object:Gem::Dependency
146
- name: git-cop
147
- requirement: !ruby/object:Gem::Requirement
148
- requirements:
149
- - - "~>"
150
- - !ruby/object:Gem::Version
151
- version: '3.0'
152
- type: :development
153
- prerelease: false
154
- version_requirements: !ruby/object:Gem::Requirement
155
- requirements:
156
- - - "~>"
157
- - !ruby/object:Gem::Version
158
- version: '3.0'
159
131
  - !ruby/object:Gem::Dependency
160
132
  name: guard-rspec
161
133
  requirement: !ruby/object:Gem::Requirement
@@ -232,14 +204,14 @@ dependencies:
232
204
  requirements:
233
205
  - - "~>"
234
206
  - !ruby/object:Gem::Version
235
- version: '5.0'
207
+ version: '5.4'
236
208
  type: :development
237
209
  prerelease: false
238
210
  version_requirements: !ruby/object:Gem::Requirement
239
211
  requirements:
240
212
  - - "~>"
241
213
  - !ruby/object:Gem::Version
242
- version: '5.0'
214
+ version: '5.4'
243
215
  - !ruby/object:Gem::Dependency
244
216
  name: rspec
245
217
  requirement: !ruby/object:Gem::Requirement
@@ -260,42 +232,42 @@ dependencies:
260
232
  requirements:
261
233
  - - "~>"
262
234
  - !ruby/object:Gem::Version
263
- version: '0.67'
235
+ version: '0.69'
264
236
  type: :development
265
237
  prerelease: false
266
238
  version_requirements: !ruby/object:Gem::Requirement
267
239
  requirements:
268
240
  - - "~>"
269
241
  - !ruby/object:Gem::Version
270
- version: '0.67'
242
+ version: '0.69'
271
243
  - !ruby/object:Gem::Dependency
272
244
  name: rubocop-performance
273
245
  requirement: !ruby/object:Gem::Requirement
274
246
  requirements:
275
247
  - - "~>"
276
248
  - !ruby/object:Gem::Version
277
- version: '1.1'
249
+ version: '1.3'
278
250
  type: :development
279
251
  prerelease: false
280
252
  version_requirements: !ruby/object:Gem::Requirement
281
253
  requirements:
282
254
  - - "~>"
283
255
  - !ruby/object:Gem::Version
284
- version: '1.1'
256
+ version: '1.3'
285
257
  - !ruby/object:Gem::Dependency
286
258
  name: rubocop-rspec
287
259
  requirement: !ruby/object:Gem::Requirement
288
260
  requirements:
289
261
  - - "~>"
290
262
  - !ruby/object:Gem::Version
291
- version: '1.30'
263
+ version: '1.33'
292
264
  type: :development
293
265
  prerelease: false
294
266
  version_requirements: !ruby/object:Gem::Requirement
295
267
  requirements:
296
268
  - - "~>"
297
269
  - !ruby/object:Gem::Version
298
- version: '1.30'
270
+ version: '1.33'
299
271
  - !ruby/object:Gem::Dependency
300
272
  name: simplecov
301
273
  requirement: !ruby/object:Gem::Requirement
@@ -340,10 +312,14 @@ files:
340
312
  - lib/tocer.rb
341
313
  - lib/tocer/builder.rb
342
314
  - lib/tocer/cli.rb
315
+ - lib/tocer/configuration.rb
343
316
  - lib/tocer/elements/comment_block.rb
344
317
  - lib/tocer/identity.rb
345
318
  - lib/tocer/parsers/header.rb
319
+ - lib/tocer/rake/setup.rb
320
+ - lib/tocer/rake/tasks.rb
346
321
  - lib/tocer/runner.rb
322
+ - lib/tocer/transformers/finder.rb
347
323
  - lib/tocer/transformers/link.rb
348
324
  - lib/tocer/transformers/text.rb
349
325
  - lib/tocer/writer.rb
metadata.gz.sig CHANGED
Binary file