tocer 3.1.1 → 3.2.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
  SHA1:
3
- metadata.gz: bdb2805ce0350fce832d1be825451d10055c67bd
4
- data.tar.gz: bdfdf2cd1ee2a94b4d49618d58e6571a23e392d7
3
+ metadata.gz: 5b7d0d42ffc60ae31c41a13f98571d2aa60b692b
4
+ data.tar.gz: 2b3ee230d78cb78aaf77664bb2c3e1f0dae61fb5
5
5
  SHA512:
6
- metadata.gz: 76f95274217283cd4d8269cbfbff4123f6a303bdf0a49e95dc1bcf66315318f6ebafa43206d8ced740e1316af9252348120e27dc69710d4629db1ffcccce0208
7
- data.tar.gz: 3136586c43d3bced44276603c337aa52a803ec5b4c28f46fca94b5c7749c4b16067e660b68d3020a69e705ecb4f3f8c9ae7bae890a7344f685d621fb257d235b
6
+ metadata.gz: f6cb3b159d8a724a0f6384ae1611d77cd1466b6655be90e1f9ad6f32fea7b4501604b29af09996f921dce886c3382583fe9d6629ccc5fe1b263301d83bd9bb74
7
+ data.tar.gz: 3b081110b49be864e15c4c4ba06c5596bba1c890e75634e448221aa77c61f91ab416861099f1fb7d5b7e7bd5906273a067d662ff7523845d369b33a23a9ec23e
@@ -1 +1 @@
1
- G_.�Ls��wOE}��}��sޓ�oK� �a㨨�1����ͪq�ר&������cm��>���<W�1�eAO�D&�kknku(�*I#CT��?p����MU$������%�ʰix���\��9,ώ�8�O_�Sa��}l\p�j��>����enfM��v,�#���-�
1
+ ������}=IGuy]wz����8$E�������x]�� �,�k&�p�* u
data.tar.gz.sig CHANGED
@@ -1,4 +1,2 @@
1
- b�^� ��7oi3� r���r��J 0R��]�O��S��� �H���'��4ŅK~VN,������=�����&{�D,b��R��
2
- ����a-�v�B ܑ�_09J
3
-
4
- ��J�*�>�~x �+�{E�@
1
+ [���i
2
+ b�����7�EsU�S����0#4��S�]��F�%fd�i�g��M+X�׫c�[���B�.���_d�#�@�� i�y���3�G�@�Ntb*�O
data/README.md CHANGED
@@ -35,8 +35,11 @@ Markdown files.
35
35
  - Supports Markdown ATX-style headers. Example: `# Header`.
36
36
  - Does not support header suffixes. Example: `# Header #`.
37
37
  - Does not support header prefixes without spaces. Example: `#Header`.
38
+ - Supports table of contents generation for single or multiple files.
39
+ - Supports custom label. Default: "# Table of Contents".
40
+ - Supports whitelist filtering. Default: ".md".
38
41
  - Prepends table of contents to Markdown documents that don't have table of contents.
39
- - Replaces/updates Markdown documents that have existing table of contents.
42
+ - Rebuilds Markdown documents that have existing table of contents.
40
43
 
41
44
  # Requirements
42
45
 
@@ -62,22 +65,30 @@ For an insecure install, type the following (not recommended):
62
65
 
63
66
  ## Command Line Interface (CLI)
64
67
 
65
- From the command line, type: `tocer help`
68
+ From the command line, type: `tocer --help`
66
69
 
67
70
  tocer -c, [--config] # Manage gem configuration.
68
71
  tocer -g, [--generate=PATH] # Generate table of contents.
69
72
  tocer -h, [--help=COMMAND] # Show this message or get help for a command.
70
73
  tocer -v, [--version] # Show gem version.
71
74
 
72
- To add Tocer support, add the following at the correct position within your Markdown files:
75
+ For specific `--generate` options, run `tocer --help --generate` to see the following:
76
+
77
+ -l, [--label=LABEL] # Label
78
+ # Default: # Table of Contents
79
+ -w, [--whitelist=one two three] # File whitelist
80
+ # Default: [".md"]
81
+
82
+ To generate the table of contents at a specific position within your Markdown files, add the
83
+ following lines to your file(s) prior to generation:
73
84
 
74
85
  ```
75
86
  <!-- Tocer[start] -->
76
87
  <!-- Tocer[finish] -->
77
88
  ```
78
89
 
79
- Alternatively, you can run `tocer -g <file_path>` on a file that does not have Tocer support and it
80
- will prepend the above to your file, complete with an auto-generated table of contents.
90
+ Alternatively, you can run `tocer -g <directory>` on files that do not have Tocer support and it
91
+ will prepend the above to your file(s), complete with an auto-generated table of contents.
81
92
 
82
93
  In the case that Tocer has already auto-generated a table of contents for a Markdown file, the
83
94
  existing table of contents has become stale, or placement of the table of contents has changed you
@@ -88,20 +99,40 @@ can re-run Tocer on that file to auto-update it with new table of contents.
88
99
  If desired, this gem supports global customization via the `~/.tocerrc` file. Order of precedence is
89
100
  determined in the following order (with the last one taking top priority):
90
101
 
91
- 0. Global `~/.tocerrc`.
92
- 0. CLI option. Example: `tocer --generate README.md --label "Custom Label"`
102
+ 0. Global: `~/.tocerrc`.
103
+ 0. Local: `<project_root>/.tocerrc`.
104
+ 0. CLI: `tocer --generate . --label "## Custom Label" --whitelist README.md`
93
105
 
94
- Any setting provided to the CLI during runtime would trump the global setting. The global setting is
106
+ Any settings provided to the CLI during runtime will trump the global setting. The global setting is
95
107
  the weakest of all but great for situations where custom settings should be applied to *all*
96
108
  projects.
97
109
 
98
110
  The `~/.tocerrc` uses the following default settings:
99
111
 
100
112
  :label: "# Table of Contents"
113
+ :whitelist: [".md"]
101
114
 
102
115
  Each `~/.tocerrc` setting can be configured as follows:
103
116
 
104
117
  - `label`: The header label for the table of contents. Default: "# Table of Contents".
118
+ - `whitelist`: The list of *included* files. Default: ".md".
119
+
120
+ There are multiple ways the *whitelist* can be defined. Here are some examples:
121
+
122
+ # Use an empty array to include *all* files.
123
+ :whitelist: []
124
+
125
+ # Use an array of wildcards for groups of files with similar extensions:
126
+ :whitelist:
127
+ - .md
128
+ - .mkd
129
+ - .markdown
130
+
131
+ # Use a mix of wild cards and relative paths to customized as necessary:
132
+ :whitelist:
133
+ - README.md
134
+ - docs/*.md
135
+ - .markdown
105
136
 
106
137
  # Tests
107
138
 
@@ -7,4 +7,5 @@ require "tocer/transformers/link"
7
7
  require "tocer/transformers/text"
8
8
  require "tocer/builder"
9
9
  require "tocer/writer"
10
+ require "tocer/runner"
10
11
  require "tocer/cli"
@@ -3,15 +3,21 @@
3
3
  module Tocer
4
4
  # Builds a table of contents for a Markdown document.
5
5
  class Builder
6
+ CODE_BLOCK_PUNCTUATION = "```"
7
+
6
8
  def initialize lines, label: "# Table of Contents", comment_block: Elements::CommentBlock
7
9
  @lines = lines
8
10
  @label = label
9
11
  @comment_block = comment_block.new
10
12
  @url_count = Hash.new { |hash, key| hash[key] = 0 }
13
+ @code_block = false
11
14
  end
12
15
 
13
16
  def headers
14
- lines.select { |line| line.start_with? Parsers::Header.punctuation }
17
+ lines.select do |line|
18
+ toggle_code_block line
19
+ line.start_with?(Parsers::Header::PUNCTUATION) && !code_block
20
+ end
15
21
  end
16
22
 
17
23
  def build
@@ -28,6 +34,12 @@ module Tocer
28
34
  private
29
35
 
30
36
  attr_reader :lines, :label, :comment_block, :url_count
37
+ attr_accessor :code_block
38
+
39
+ def toggle_code_block line
40
+ return unless line.start_with? CODE_BLOCK_PUNCTUATION
41
+ self.code_block = !code_block
42
+ end
31
43
 
32
44
  def acquire_transfomer header
33
45
  if header =~ /\[.+\]\(.+\)/
@@ -15,7 +15,8 @@ module Tocer
15
15
 
16
16
  def self.configuration
17
17
  Runcom::Configuration.new file_name: Identity.file_name, defaults: {
18
- label: "# Table of Contents"
18
+ label: "# Table of Contents",
19
+ whitelist: %w[.md]
19
20
  }
20
21
  end
21
22
 
@@ -25,10 +26,26 @@ module Tocer
25
26
 
26
27
  desc "-g, [--generate=PATH]", "Generate table of contents."
27
28
  map %w[-g --generate] => :generate
28
- method_option :label, aliases: "-l", desc: "Label", type: :string, default: configuration.to_h.fetch(:label)
29
+ method_option :label,
30
+ aliases: "-l",
31
+ desc: "Label",
32
+ type: :string,
33
+ default: configuration.to_h.fetch(:label)
34
+ method_option :whitelist,
35
+ aliases: "-w",
36
+ desc: "File whitelist",
37
+ type: :array,
38
+ default: configuration.to_h.fetch(:whitelist)
29
39
  def generate path
30
- Writer.new(path, label: options.label).write
31
- say "Generated table of contents: #{path}."
40
+ configuration = build_configuration path, options.label, options.whitelist
41
+ runner = build_runner path, configuration
42
+ files = runner.files
43
+
44
+ runner.run
45
+ return if files.empty?
46
+
47
+ say "Processed table of contents for:"
48
+ files.each { |file| say " #{file}" }
32
49
  end
33
50
 
34
51
  desc "-c, [--config]", %(Manage gem configuration ("#{configuration.computed_path}").)
@@ -64,9 +81,21 @@ module Tocer
64
81
 
65
82
  private
66
83
 
67
- def compute_label label
68
- configured_label = self.class.configuration.to_h.fetch :label
69
- label == configured_label ? label : configured_label
84
+ def build_configuration path, label, whitelist
85
+ if Pathname(path).file?
86
+ warn "[DEPRECATION]: File paths are deprecated, use directory paths instead."
87
+ self.class.configuration.merge label: label, whitelist: [path]
88
+ else
89
+ self.class.configuration.merge label: label, whitelist: whitelist
90
+ end
91
+ end
92
+
93
+ def build_runner path, configuration
94
+ if Pathname(path).file?
95
+ Runner.new ".", configuration: configuration
96
+ else
97
+ Runner.new path, configuration: configuration
98
+ end
70
99
  end
71
100
  end
72
101
  end
@@ -12,7 +12,7 @@ module Tocer
12
12
  end
13
13
 
14
14
  def self.version
15
- "3.1.1"
15
+ "3.2.0"
16
16
  end
17
17
 
18
18
  def self.version_label
@@ -4,16 +4,14 @@ module Tocer
4
4
  module Parsers
5
5
  # Represents a Markdown header.
6
6
  class Header
7
- def self.punctuation
8
- "#"
9
- end
7
+ PUNCTUATION = "#"
10
8
 
11
9
  def initialize markdown
12
10
  @markdown = markdown
13
11
  end
14
12
 
15
13
  def prefix
16
- String markdown[/#{self.class.punctuation}{1,}/]
14
+ String markdown[/#{PUNCTUATION}{1,}/]
17
15
  end
18
16
 
19
17
  def content
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "pathname"
4
+
5
+ module Tocer
6
+ # Generates/updates Table of Contents for files in given path.
7
+ class Runner
8
+ def initialize path = "", configuration: {}, writer: Writer
9
+ @path = Pathname path
10
+ @configuration = configuration
11
+ @writer = writer
12
+ end
13
+
14
+ def files
15
+ return [] unless path.exist?
16
+ glob = whitelist.empty? ? %(#{path}/**/*) : %(#{path}/**/*{#{whitelist.join ","}})
17
+ Pathname.glob(glob).select(&:file?)
18
+ end
19
+
20
+ def run
21
+ files.each { |file| writer.new(file, label: configuration.fetch(:label)).write }
22
+ end
23
+
24
+ private
25
+
26
+ attr_reader :path, :configuration, :writer
27
+
28
+ def whitelist
29
+ Array configuration.fetch(:whitelist)
30
+ end
31
+ end
32
+ end
@@ -46,7 +46,7 @@ module Tocer
46
46
  end
47
47
 
48
48
  def prepend_toc
49
- content(file_lines) << file_lines.join
49
+ content(file_lines).dup << file_lines.join
50
50
  end
51
51
  end
52
52
  end
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: 3.1.1
4
+ version: 3.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brooke Kuhlmann
@@ -30,7 +30,7 @@ cert_chain:
30
30
  n/LUZ1dKhIHzfKx1B4+TEIefArObGfkLIDM8+Dq1RX7TF1k81Men7iu4MgE9bYBn
31
31
  3dE+xI3FdB5gWcdWxdtgRCmWjtXeYYyb4z6NQQ==
32
32
  -----END CERTIFICATE-----
33
- date: 2016-11-13 00:00:00.000000000 Z
33
+ date: 2016-12-03 00:00:00.000000000 Z
34
34
  dependencies:
35
35
  - !ruby/object:Gem::Dependency
36
36
  name: thor
@@ -108,14 +108,14 @@ dependencies:
108
108
  requirements:
109
109
  - - "~>"
110
110
  - !ruby/object:Gem::Version
111
- version: '8.0'
111
+ version: '8.1'
112
112
  type: :development
113
113
  prerelease: false
114
114
  version_requirements: !ruby/object:Gem::Requirement
115
115
  requirements:
116
116
  - - "~>"
117
117
  - !ruby/object:Gem::Version
118
- version: '8.0'
118
+ version: '8.1'
119
119
  - !ruby/object:Gem::Dependency
120
120
  name: pry
121
121
  requirement: !ruby/object:Gem::Requirement
@@ -320,6 +320,7 @@ files:
320
320
  - lib/tocer/elements/comment_block.rb
321
321
  - lib/tocer/identity.rb
322
322
  - lib/tocer/parsers/header.rb
323
+ - lib/tocer/runner.rb
323
324
  - lib/tocer/transformers/link.rb
324
325
  - lib/tocer/transformers/text.rb
325
326
  - lib/tocer/writer.rb
metadata.gz.sig CHANGED
@@ -1,4 +1 @@
1
- �����WdL��Tq :�E��f�[ޫמּ�Jk��A��?�wa2x(��(��|��� ��=�H����1=����$� Ɗ�f��t�Uk7�d��s���6]�1
2
- �IRʰ��UF�G�i�$��[�;��d�R}Mn�íԽ��u�|���Օ6{Ht�N�(.l
3
- Yb
4
- �с�b���HZ��*�����p>�&�hxC�=m���%�
1
+ N��𴢘>N�^5&!&(�p�4MW�\G�