mdtoc 0.1.4 → 0.1.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4b54718ab7b2f7bbc8e90ae473318895665986e0a7408674670db03294f12d6e
4
- data.tar.gz: f1b251a78ba55942f8c85d82b99f614d3b9368541b7dcb1f144d1094e293d01f
3
+ metadata.gz: f092d7c28f783773c0c48be2f48ec708b986cb2bc059a5312420a15dd7e57878
4
+ data.tar.gz: c1af35b25f4c5c9596c232572b158074574ba16b154c72de1ad84c22d189ec40
5
5
  SHA512:
6
- metadata.gz: 247816469ed9798906c93e6ca0c58bd4b863d379795d9f776b71fc6c7d3835d7eb4ee890372a408ee04981658336a5f924156d85456e66d90793e0d58b7bd9e2
7
- data.tar.gz: 2c10991bfa7f4b95896d468f803bae94b44899fc695c1113bd6fa6d7d90c784f4303939a7c2637ed70cd6335bf51f66688d6c932b03a6c0d284f5060cc009646
6
+ metadata.gz: 0f20e8dbf0ce1d8ccc67ede27cdb15c8a6253d01e35c44780194b8322ccd3281e8f5c9fb8a3d311fcd4f91236988e1379cb639315711f87549f6dbd4fed44402
7
+ data.tar.gz: fa16fbf938973290b2724a8d1215aca328260813592083f2ea7adf9a6503c1d981702594260ab38969de05b13e78477e60ad56e201f1b7e754aa9759666899f8
data/lib/mdtoc.rb CHANGED
@@ -1,4 +1,4 @@
1
- # typed: true
1
+ # typed: strict
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'optparse'
data/lib/mdtoc/cli.rb CHANGED
@@ -1,4 +1,4 @@
1
- # typed: true
1
+ # typed: strict
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'optparse'
@@ -14,7 +14,7 @@ module Mdtoc
14
14
  raise ArgumentError, "Header depth must be >= 0, but was #{depth}"
15
15
  end
16
16
  @depth = depth
17
- @label = label.strip.gsub(/\s+/, ' ')
17
+ @label = normalize_label(label)
18
18
  @url = url
19
19
  end
20
20
 
@@ -28,13 +28,20 @@ module Mdtoc
28
28
  def top_level?(relative_to_depth)
29
29
  @depth == relative_to_depth
30
30
  end
31
+
32
+ private
33
+
34
+ def normalize_label(label)
35
+ label = label.strip.tr("\t\n\r", '') # Remove whitespace characters other than spaces.
36
+ label.gsub(/\[(.*)\]\(.*\)/, '\1') # Remove links
37
+ end
31
38
  end
32
39
 
33
40
  class HeaderWithFragment < Header
34
41
  sig { params(depth: Integer, label: String, url: String).void }
35
42
  def initialize(depth, label, url)
36
- url = "#{url}##{label.strip.downcase.tr(' ', '-').gsub(/[^\w\-]/, '')}"
37
43
  super
44
+ @url += "##{@label.downcase.tr(' ', '-').gsub(/[^\w\-]/, '')}"
38
45
  end
39
46
  end
40
47
  end
@@ -1,4 +1,4 @@
1
- # typed: true
1
+ # typed: strict
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'sorbet-runtime'
data/lib/mdtoc/node.rb CHANGED
@@ -1,4 +1,4 @@
1
- # typed: true
1
+ # typed: strict
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'pathname'
@@ -45,33 +45,33 @@ module Mdtoc
45
45
  def label
46
46
  File.basename(@path, File.extname(@path)).gsub(/_+/, ' ').gsub(/\s+/, ' ').capitalize
47
47
  end
48
- end
49
48
 
50
- class DirNode < Node
51
- sig { override.returns(T::Array[Mdtoc::Markdown::Header]) }
52
- def headers
53
- readme_path = T.let(nil, T.nilable(String))
54
- child_headers = Dir
55
- .each_child(@path)
56
- .reject { |path| readme_path = File.join(@path, path) if path.casecmp?('readme.md') }
57
- .sort!
58
- .flat_map { |path| Node.for_path(File.join(@path, path), @depth + 1).headers }
59
- return child_headers unless readme_path
49
+ class DirNode < Node
50
+ sig { override.returns(T::Array[Mdtoc::Markdown::Header]) }
51
+ def headers
52
+ readme_path = T.let(nil, T.nilable(String))
53
+ child_headers = Dir
54
+ .each_child(@path)
55
+ .reject { |path| readme_path = File.join(@path, path) if path.casecmp?('readme.md') }
56
+ .sort!
57
+ .flat_map { |path| Node.for_path(File.join(@path, path), @depth + 1).headers }
58
+ return child_headers unless readme_path
60
59
 
61
- # Include the headers from the README at the beginning.
62
- readme_headers = FileNode.new(readme_path, @depth).headers
63
- readme_headers.push(*child_headers)
60
+ # Include the headers from the README at the beginning.
61
+ readme_headers = FileNode.new(readme_path, @depth).headers
62
+ readme_headers + child_headers
63
+ end
64
64
  end
65
- end
66
65
 
67
- class FileNode < Node
68
- sig { override.returns(T::Array[Mdtoc::Markdown::Header]) }
69
- def headers
70
- parser = Markdown::Parser.new(@depth, @path)
71
- headers = parser.headers(File.foreach(@path))
72
- return headers if headers[0]&.top_level?(@depth)
66
+ class FileNode < Node
67
+ sig { override.returns(T::Array[Mdtoc::Markdown::Header]) }
68
+ def headers
69
+ parser = Markdown::Parser.new(@depth, @path)
70
+ headers = parser.headers(File.foreach(@path))
71
+ return headers if headers[0]&.top_level?(@depth)
73
72
 
74
- headers.unshift(Mdtoc::Markdown::Header.new(@depth, label, @path))
73
+ headers.unshift(Mdtoc::Markdown::Header.new(@depth, label, @path))
74
+ end
75
75
  end
76
76
  end
77
77
  end
data/lib/mdtoc/version.rb CHANGED
@@ -1,6 +1,6 @@
1
- # typed: true
1
+ # typed: strict
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module Mdtoc
5
- VERSION = '0.1.4'
5
+ VERSION = '0.1.5'
6
6
  end
data/lib/mdtoc/writer.rb CHANGED
@@ -1,4 +1,4 @@
1
- # typed: true
1
+ # typed: strict
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module Mdtoc
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mdtoc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - andornaut
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-11-12 00:00:00.000000000 Z
11
+ date: 2021-02-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
@@ -159,8 +159,13 @@ description: |
159
159
 
160
160
  ## Example Rakefile
161
161
 
162
- Run the [rake](https://github.com/ruby/rake) "mdtoc" task to update a table of contents.
163
- See [andornaut/til](https://github.com/andornaut/til) for an example.
162
+ Create a `Rakefile` with the contents below, then run
163
+ [`rake`](https://github.com/ruby/rake) to:
164
+
165
+ * `git pull`
166
+ * `git add` any *.md files
167
+ * Run `mdtoc` to update the generated table of contents in the ./README.md file
168
+ * Git commit and push any changes
164
169
 
165
170
  ```
166
171
  task default: %w[mdtoc]
@@ -183,6 +188,8 @@ description: |
183
188
  end
184
189
  ```
185
190
 
191
+ See [andornaut/til](https://github.com/andornaut/til/blob/master/Rakefile) for an example.
192
+
186
193
  ## Development
187
194
 
188
195
  ### Installation