tocer 5.0.0 → 6.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
  SHA1:
3
- metadata.gz: c3a5d9b3cbfbd48fe9e33a63d6bb64f835b41d29
4
- data.tar.gz: 4bf1614edeb7da8332a07551446b0d3b9203f889
3
+ metadata.gz: cc14839d2577710ed5cf36a50119597ed809581f
4
+ data.tar.gz: 6e0e455468f5ba1f2c9d71f1db42702ab44a7343
5
5
  SHA512:
6
- metadata.gz: 10030e97ee6a0f6935483700653a3ac795b7905dfb3a8da3a61d0222a694248260ca6c02ca38923bf1a50d57eab92ba213bf6565de075b82973bea719eab51e0
7
- data.tar.gz: 00f5d32a47dc85d51323bf50f1af64761728071d212e653acd73e6da2a852e8da28ae78831e9f7a96e2110fbd41ae80d3953161d6fd31021020177f280462201
6
+ metadata.gz: 0ee34bfa069b8aef9c0a6ce4fea96cb4f98890476e8d44dec4fb6e644eb186dccf6581a32a794789b792568a7720ffaa4f4209a43bc2fda2c52162832935fc01
7
+ data.tar.gz: 8c6c61f73ad832cc5f904dc1dbbd66ab2203ca1dcf0aef492e522445bb7a67b598bcc5a2265c37eb3282459bf18ae6c9186a5b13718125f8d771d96344285513
data/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
  [![Code Climate GPA](https://codeclimate.com/github/bkuhlmann/tocer.svg)](https://codeclimate.com/github/bkuhlmann/tocer)
5
5
  [![Code Climate Coverage](https://codeclimate.com/github/bkuhlmann/tocer/coverage.svg)](https://codeclimate.com/github/bkuhlmann/tocer)
6
6
  [![Gemnasium Status](https://gemnasium.com/bkuhlmann/tocer.svg)](https://gemnasium.com/bkuhlmann/tocer)
7
- [![Travis CI Status](https://secure.travis-ci.org/bkuhlmann/tocer.svg)](https://travis-ci.org/bkuhlmann/tocer)
7
+ [![Circle CI Status](https://circleci.com/gh/bkuhlmann/tocer.svg?style=svg)](https://circleci.com/gh/bkuhlmann/tocer)
8
8
  [![Patreon](https://img.shields.io/badge/patreon-donate-brightgreen.svg)](https://www.patreon.com/bkuhlmann)
9
9
 
10
10
  Tocer (a.k.a. Table of Contenter) is a command line interface for generating table of contents for
@@ -12,41 +12,41 @@ Markdown files.
12
12
 
13
13
  <!-- Tocer[start]: Auto-generated, don't remove. -->
14
14
 
15
- # Table of Contents
16
-
17
- - [Features](#features)
18
- - [Requirements](#requirements)
19
- - [Setup](#setup)
20
- - [Usage](#usage)
21
- - [Command Line Interface (CLI)](#command-line-interface-cli)
22
- - [Customization](#customization)
23
- - [Tests](#tests)
24
- - [Versioning](#versioning)
25
- - [Code of Conduct](#code-of-conduct)
26
- - [Contributions](#contributions)
27
- - [License](#license)
28
- - [History](#history)
29
- - [Credits](#credits)
15
+ ## Table of Contents
16
+
17
+ - [Features](#features)
18
+ - [Requirements](#requirements)
19
+ - [Setup](#setup)
20
+ - [Usage](#usage)
21
+ - [Command Line Interface (CLI)](#command-line-interface-cli)
22
+ - [Customization](#customization)
23
+ - [Tests](#tests)
24
+ - [Versioning](#versioning)
25
+ - [Code of Conduct](#code-of-conduct)
26
+ - [Contributions](#contributions)
27
+ - [License](#license)
28
+ - [History](#history)
29
+ - [Credits](#credits)
30
30
 
31
31
  <!-- Tocer[finish]: Auto-generated, don't remove. -->
32
32
 
33
- # Features
33
+ ## Features
34
34
 
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
38
  - Supports table of contents generation for single or multiple files.
39
- - Supports custom label. Default: "# Table of Contents".
39
+ - Supports custom label. Default: "## Table of Contents".
40
40
  - Supports whitelist filtering. Default: "README.md".
41
41
  - Prepends table of contents to Markdown documents that don't have table of contents.
42
42
  - Rebuilds Markdown documents that have existing table of contents.
43
43
 
44
- # Requirements
44
+ ## Requirements
45
45
 
46
46
  0. A UNIX-based system.
47
47
  0. [Ruby 2.4.x](https://www.ruby-lang.org).
48
48
 
49
- # Setup
49
+ ## Setup
50
50
 
51
51
  For a secure install, type the following (recommended):
52
52
 
@@ -61,9 +61,9 @@ For an insecure install, type the following (not recommended):
61
61
 
62
62
  gem install tocer
63
63
 
64
- # Usage
64
+ ## Usage
65
65
 
66
- ## Command Line Interface (CLI)
66
+ ### Command Line Interface (CLI)
67
67
 
68
68
  From the command line, type: `tocer --help`
69
69
 
@@ -95,24 +95,24 @@ In the case that Tocer has already auto-generated a table of contents for a Mark
95
95
  existing table of contents has become stale, or placement of the table of contents has changed you
96
96
  can re-run Tocer on that file to auto-update it with new table of contents.
97
97
 
98
- ## Customization
98
+ ### Customization
99
99
 
100
- If desired, this gem supports global customization via the `~/.tocerrc` file. Order of precedence is
101
- determined in the following order (with the last one taking top priority):
100
+ This gem can be configured via a global configuration:
102
101
 
103
- 0. Global: `~/.tocerrc`.
104
- 0. Local: `<project_root>/.tocerrc`.
105
- 0. CLI: `tocer --generate . --label "## Custom Label" --whitelist CHANGES.md`
102
+ ~/.config/tocer/configuration.yml
106
103
 
107
- Settings provided to the CLI during runtime will trump the global setting. The global settings are
108
- weakest of all but great for situations where custom settings should be applied to *all* projects.
104
+ It can also be configured via [XDG environment variables](https://github.com/bkuhlmann/runcom#xdg)
105
+ as provided by the [Runcom](https://github.com/bkuhlmann/runcom) gem.
109
106
 
110
- The `~/.tocerrc` file uses the following default settings:
107
+ The default configuration is as follows:
111
108
 
112
- :label: "# Table of Contents"
109
+ :label: "## Table of Contents"
113
110
  :whitelist: ["README.md"]
114
111
 
115
- Each `~/.tocerrc` setting can be configured as follows:
112
+ Feel free to take this default configuration, modify, and save as your own custom
113
+ `configuration.yml`.
114
+
115
+ The `configuration.yml` file can be configured as follows:
116
116
 
117
117
  - `label`: The header label for the table of contents. Default: "# Table of Contents".
118
118
  - `whitelist`: The list of *included* files. Default: "*.md".
@@ -138,13 +138,13 @@ There are multiple ways the *whitelist* can be defined. Here are some examples:
138
138
  :whitelist:
139
139
  - **/*.md
140
140
 
141
- # Tests
141
+ ## Tests
142
142
 
143
143
  To test, run:
144
144
 
145
145
  bundle exec rake
146
146
 
147
- # Versioning
147
+ ## Versioning
148
148
 
149
149
  Read [Semantic Versioning](http://semver.org) for details. Briefly, it means:
150
150
 
@@ -152,26 +152,26 @@ Read [Semantic Versioning](http://semver.org) for details. Briefly, it means:
152
152
  - Minor (x.Y.z) - Incremented for new, backwards compatible, public API enhancements/fixes.
153
153
  - Patch (x.y.Z) - Incremented for small, backwards compatible, bug fixes.
154
154
 
155
- # Code of Conduct
155
+ ## Code of Conduct
156
156
 
157
157
  Please note that this project is released with a [CODE OF CONDUCT](CODE_OF_CONDUCT.md). By
158
158
  participating in this project you agree to abide by its terms.
159
159
 
160
- # Contributions
160
+ ## Contributions
161
161
 
162
162
  Read [CONTRIBUTING](CONTRIBUTING.md) for details.
163
163
 
164
- # License
164
+ ## License
165
165
 
166
166
  Copyright (c) 2015 [Alchemists](https://www.alchemists.io).
167
167
  Read [LICENSE](LICENSE.md) for details.
168
168
 
169
- # History
169
+ ## History
170
170
 
171
171
  Read [CHANGES](CHANGES.md) for details.
172
172
  Built with [Gemsmith](https://github.com/bkuhlmann/gemsmith).
173
173
 
174
- # Credits
174
+ ## Credits
175
175
 
176
176
  Developed by [Brooke Kuhlmann](https://www.alchemists.io) at
177
177
  [Alchemists](https://www.alchemists.io).
data/lib/tocer/builder.rb CHANGED
@@ -1,11 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "forwardable"
4
+
3
5
  module Tocer
4
6
  # Builds a table of contents for a Markdown document.
5
- # :reek:TooManyInstanceVariables
6
7
  class Builder
8
+ extend Forwardable
9
+
7
10
  CODE_BLOCK_PUNCTUATION = "```"
8
11
 
12
+ def_delegators :comment_block, :start_index, :finish_index, :prependable?
13
+
9
14
  def self.transformer header
10
15
  if header.match?(/\[.+\]\(.+\)/)
11
16
  Transformers::Link.new header
@@ -14,35 +19,27 @@ module Tocer
14
19
  end
15
20
  end
16
21
 
17
- def initialize lines, label: "# Table of Contents", comment_block: Elements::CommentBlock
18
- @lines = lines
22
+ def initialize label: "## Table of Contents", comment_block: Elements::CommentBlock.new
19
23
  @label = label
20
- @comment_block = comment_block.new
24
+ @comment_block = comment_block
21
25
  @url_count = Hash.new { |hash, key| hash[key] = 0 }
22
26
  @code_block = false
23
27
  end
24
28
 
25
- def headers
26
- lines.select do |line|
27
- toggle_code_block line
28
- line.start_with?(Parsers::Header::PUNCTUATION) && !code_block
29
- end
30
- end
31
-
32
- def build
33
- return "" if headers.empty?
29
+ def build lines
30
+ return "" if headers(lines).empty?
34
31
 
35
32
  [
36
- "#{comment_block.start}\n\n",
33
+ "#{comment_block.start_tag}\n\n",
37
34
  "#{label}\n\n",
38
- headers_as_links.join("\n"),
39
- "\n\n#{comment_block.finish}\n\n"
35
+ links(lines).join("\n"),
36
+ "\n\n#{comment_block.finish_tag}\n"
40
37
  ].join
41
38
  end
42
39
 
43
40
  private
44
41
 
45
- attr_reader :lines, :label, :comment_block, :url_count
42
+ attr_reader :label, :comment_block, :url_count
46
43
  attr_accessor :code_block
47
44
 
48
45
  def toggle_code_block line
@@ -63,8 +60,15 @@ module Tocer
63
60
  link
64
61
  end
65
62
 
66
- def headers_as_links
67
- headers.map { |header| transform header }
63
+ def headers lines
64
+ lines.select do |line|
65
+ toggle_code_block line
66
+ line.start_with?(Parsers::Header::PUNCTUATION) && !code_block
67
+ end
68
+ end
69
+
70
+ def links lines
71
+ headers(lines).map { |header| transform header }
68
72
  end
69
73
  end
70
74
  end
data/lib/tocer/cli.rb CHANGED
@@ -14,8 +14,8 @@ module Tocer
14
14
  package_name Identity.version_label
15
15
 
16
16
  def self.configuration
17
- Runcom::Configuration.new file_name: Identity.file_name, defaults: {
18
- label: "# Table of Contents",
17
+ Runcom::Configuration.new project_name: Identity.name, defaults: {
18
+ label: "## Table of Contents",
19
19
  whitelist: ["README.md"]
20
20
  }
21
21
  end
@@ -50,7 +50,7 @@ module Tocer
50
50
  files.each { |file| say " #{file}" }
51
51
  end
52
52
 
53
- desc "-c, [--config]", %(Manage gem configuration ("#{configuration.computed_path}").)
53
+ desc "-c, [--config]", "Manage gem configuration."
54
54
  map %w[-c --config] => :config
55
55
  method_option :edit,
56
56
  aliases: "-e",
@@ -61,10 +61,11 @@ module Tocer
61
61
  desc: "Print gem configuration.",
62
62
  type: :boolean, default: false
63
63
  def config
64
- path = self.class.configuration.computed_path
64
+ path = self.class.configuration.path
65
65
 
66
66
  if options.edit? then `#{editor} #{path}`
67
- elsif options.info? then say(path)
67
+ elsif options.info?
68
+ path ? say(path) : say("Configuration doesn't exist.")
68
69
  else help(:config)
69
70
  end
70
71
  end
@@ -17,22 +17,26 @@ module Tocer
17
17
  @message = message
18
18
  end
19
19
 
20
- def start
21
- comment start_id, message
22
- end
23
-
24
20
  def start_index lines
25
21
  self.class.index lines, start_id
26
22
  end
27
23
 
28
- def finish
29
- comment finish_id, message
24
+ def start_tag
25
+ comment start_id, message
30
26
  end
31
27
 
32
28
  def finish_index lines
33
29
  self.class.index lines, finish_id
34
30
  end
35
31
 
32
+ def finish_tag
33
+ comment finish_id, message
34
+ end
35
+
36
+ def prependable? lines
37
+ start_index(lines).zero? && finish_index(lines).zero?
38
+ end
39
+
36
40
  private
37
41
 
38
42
  attr_reader :start_id, :finish_id, :message
@@ -12,15 +12,11 @@ module Tocer
12
12
  end
13
13
 
14
14
  def self.version
15
- "5.0.0"
15
+ "6.0.0"
16
16
  end
17
17
 
18
18
  def self.version_label
19
19
  "#{label} #{version}"
20
20
  end
21
-
22
- def self.file_name
23
- ".#{name}rc"
24
- end
25
21
  end
26
22
  end
data/lib/tocer/writer.rb CHANGED
@@ -2,63 +2,50 @@
2
2
 
3
3
  module Tocer
4
4
  # Writes table of contents to a Markdown document.
5
- # :reek:TooManyInstanceVariables
6
5
  class Writer
7
- # rubocop:disable Metrics/ParameterLists
8
- def initialize file_path,
9
- label: "# Table of Contents",
10
- builder: Builder,
11
- comment_block: Elements::CommentBlock
6
+ def self.add start_index:, old_lines:, new_lines:
7
+ computed_new_lines = start_index.zero? ? new_lines : new_lines + "\n"
8
+ old_lines.insert start_index, *computed_new_lines
9
+ end
10
+
11
+ def self.remove start_index, finish_index, lines
12
+ range = (start_index - 1)..finish_index
13
+ lines.reject.with_index { |_, index| range.include? index }
14
+ end
12
15
 
16
+ def initialize file_path, label: "## Table of Contents", builder: Builder.new(label: label)
13
17
  @file_path = file_path
14
- @file_lines = File.open(file_path, &:to_a)
15
- @label = label
16
18
  @builder = builder
17
- setup_indexes comment_block.new, @file_lines
18
19
  end
19
20
 
20
21
  def write
21
- body = start_index.zero? ? prepend_toc : replace_toc
22
+ lines = File.readlines file_path
23
+ body = builder.prependable?(lines) ? unshift(lines) : replace(lines)
22
24
  File.open(file_path, "w") { |file| file.write body }
23
25
  end
24
26
 
25
27
  private
26
28
 
27
- attr_reader :file_path,
28
- :file_lines,
29
- :label,
30
- :start_index,
31
- :finish_index,
32
- :builder,
33
- :comment_block
34
-
35
- def setup_indexes comment_block, lines
36
- @start_index = comment_block.start_index lines
37
- @finish_index = comment_block.finish_index lines
38
- end
29
+ attr_reader :file_path, :builder
39
30
 
40
31
  def content lines
41
- builder.new(lines, label: label).build
42
- end
43
-
44
- def remove_toc lines
45
- toc_range = ((start_index - 1)..finish_index)
46
- lines.reject.with_index { |_, index| toc_range.include? index }
47
- end
48
-
49
- def add_toc old_lines, new_lines
50
- old_lines.insert start_index, new_lines
32
+ builder.build lines
51
33
  end
52
34
 
53
- def replace_toc
54
- old_lines = remove_toc file_lines
55
- new_lines = content file_lines[finish_index, file_lines.length]
35
+ def replace lines
36
+ start_index = builder.start_index lines
37
+ finish_index = builder.finish_index lines
38
+ klass = self.class
56
39
 
57
- add_toc(old_lines, new_lines).join
40
+ klass.add(
41
+ start_index: start_index,
42
+ old_lines: klass.remove(start_index, finish_index, lines),
43
+ new_lines: content(lines[finish_index, lines.length])
44
+ ).join
58
45
  end
59
46
 
60
- def prepend_toc
61
- content(file_lines).dup << file_lines.join
47
+ def unshift lines
48
+ content(lines) + "\n" + lines.join
62
49
  end
63
50
  end
64
51
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tocer
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.0.0
4
+ version: 6.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brooke Kuhlmann
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-07 00:00:00.000000000 Z
11
+ date: 2017-06-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '0.6'
61
+ version: '1.1'
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '0.6'
68
+ version: '1.1'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rake
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -80,20 +80,6 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '12.0'
83
- - !ruby/object:Gem::Dependency
84
- name: gemsmith
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - "~>"
88
- - !ruby/object:Gem::Version
89
- version: '9.4'
90
- type: :development
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - "~>"
95
- - !ruby/object:Gem::Version
96
- version: '9.4'
97
83
  - !ruby/object:Gem::Dependency
98
84
  name: pry
99
85
  requirement: !ruby/object:Gem::Requirement
@@ -240,28 +226,28 @@ dependencies:
240
226
  requirements:
241
227
  - - "~>"
242
228
  - !ruby/object:Gem::Version
243
- version: '4.6'
229
+ version: '4.7'
244
230
  type: :development
245
231
  prerelease: false
246
232
  version_requirements: !ruby/object:Gem::Requirement
247
233
  requirements:
248
234
  - - "~>"
249
235
  - !ruby/object:Gem::Version
250
- version: '4.6'
236
+ version: '4.7'
251
237
  - !ruby/object:Gem::Dependency
252
238
  name: rubocop
253
239
  requirement: !ruby/object:Gem::Requirement
254
240
  requirements:
255
241
  - - "~>"
256
242
  - !ruby/object:Gem::Version
257
- version: '0.48'
243
+ version: '0.49'
258
244
  type: :development
259
245
  prerelease: false
260
246
  version_requirements: !ruby/object:Gem::Requirement
261
247
  requirements:
262
248
  - - "~>"
263
249
  - !ruby/object:Gem::Version
264
- version: '0.48'
250
+ version: '0.49'
265
251
  - !ruby/object:Gem::Dependency
266
252
  name: codeclimate-test-reporter
267
253
  requirement: !ruby/object:Gem::Requirement