jekyll-toc 0.3.0 → 0.4.0.rc

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
  SHA1:
3
- metadata.gz: 53c57e9121410deaeb8ae17baa0aba66d09884ff
4
- data.tar.gz: 64fa560a6fb7008f52fad57889e0e5d00fbfe739
3
+ metadata.gz: 14addb9f45e2c59684378d0a893225af44329358
4
+ data.tar.gz: 61974f038bb809635bb874991aa9d962a1759a48
5
5
  SHA512:
6
- metadata.gz: 13220d95cfcea4503fcbb1fee8b91395ed5b4623206f8f9a7258f0c888526484cef6a21131263526f7041a66ad826ac9165ae1c01f86b04cd6d490886f890eb8
7
- data.tar.gz: d92865dc291732d1eded8cb67420488786d9b0385b2216a8e523c6a0cbac699d21cccfc1d8f395dc1f1f540ac9893c7065b5b435feaa5b52271870e9cb7b8401
6
+ metadata.gz: ea6df1586db53071164ffa49bd6ef5e9cec024d690aa890838c222853e83de86e817fdd4c916618d4c50604ca3f61f3c253917cb015acb51f45787254d31e8fc
7
+ data.tar.gz: 4c5b95c5d20a28f7bacdc1effaa717d6e9466fa555c449e7e53b80882c9bd7daf17ae715fb51b79b6abb3983e9ad4cee9075e6c2bf48bb37af12950f1ba98955
@@ -7,6 +7,7 @@ rvm:
7
7
  - 2.4.1
8
8
  # gemfile is generated by appraisal
9
9
  gemfile:
10
+ - gemfiles/jekyll_3.6.gemfile
10
11
  - gemfiles/jekyll_3.5.gemfile
11
12
  - gemfiles/jekyll_3.4.gemfile
12
13
  - gemfiles/jekyll_3.3.gemfile
data/Appraisals CHANGED
@@ -1,3 +1,7 @@
1
+ appraise "jekyll-3.6" do
2
+ gem "jekyll", "3.6"
3
+ end
4
+
1
5
  appraise "jekyll-3.5" do
2
6
  gem "jekyll", "3.5.1"
3
7
  end
@@ -21,7 +25,3 @@ end
21
25
  appraise "jekyll-3" do
22
26
  gem "jekyll", "3.0.3"
23
27
  end
24
-
25
- appraise "jekyll-2" do
26
- gem "jekyll", "2.5.3"
27
- end
data/README.md CHANGED
@@ -8,20 +8,20 @@
8
8
 
9
9
  # Installation
10
10
 
11
- Add jekyll-toc plugin in your site's `Gemfile`.
11
+ Add jekyll-toc plugin in your site's `Gemfile`, and run `bundle install`.
12
12
 
13
13
  ```ruby
14
14
  gem 'jekyll-toc'
15
15
  ```
16
16
 
17
- And add the jekyll-toc to your site's `_config.yml`.
17
+ Add jekyll-toc to the `gems:` section in your site's `_config.yml`.
18
18
 
19
19
  ```yml
20
20
  gems:
21
21
  - jekyll-toc
22
22
  ```
23
23
 
24
- Set `toc: true` in your posts.
24
+ Set `toc: true` in posts for which you want the TOC to appear.
25
25
 
26
26
  ```yml
27
27
  ---
@@ -33,15 +33,14 @@ toc: true
33
33
 
34
34
  # Usage
35
35
 
36
- There are three Liquid filters available now, which all should be applied
37
- to some HTML content, e.g. the Liquid variable `content` available in
38
- Jekyll's templates.
36
+ There are three Liquid filters, which can be applied to HTML content,
37
+ e.g. the Liquid variable `content` available in Jekyll's templates.
39
38
 
40
39
  ## 1. Basic Usage
41
40
 
42
41
  ### `toc` filter
43
42
 
44
- Add `toc` filter to your site's `{{ content }}` (e.g. `_layouts/post.html`).
43
+ Add the `toc` filter to your site's `{{ content }}` (e.g. `_layouts/post.html`).
45
44
 
46
45
  ```liquid
47
46
  {{ content | toc }}
@@ -73,9 +72,9 @@ TOC itself. They are of the form:
73
72
  This is only useful when the TOC itself should be placed at some other
74
73
  location with the `toc_only` filter.
75
74
 
76
- ## Generated Table of Contents HTML
75
+ ## Generated HTML
77
76
 
78
- jekyll-toc generates Unordered List. The final output is as follows.
77
+ jekyll-toc generates an unordered list. The final output is as follows.
79
78
 
80
79
  **Heads-up**: As of v0.3, nested toc has been supported. If you update jekyll-toc from 0.2 to 0.3, please check [upgrade guide](https://github.com/toshimaru/jekyll-toc/wiki/0.3-Upgrade-Guide)
81
80
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "jekyll", "2.5.3"
5
+ gem "jekyll", "3.6"
6
6
 
7
7
  gemspec :path => "../"
@@ -12,16 +12,7 @@ module Jekyll
12
12
  end
13
13
 
14
14
  def build_toc
15
- toc = %(<ul class="section-nav">\n)
16
-
17
- min_h_num = 6
18
- @entries.each do |entry|
19
- h_num = entry[:node_name].delete('h').to_i
20
- min_h_num = [min_h_num, h_num].min
21
- end
22
- toc << build_lis(@entries, min_h_num)
23
-
24
- toc << '</ul>'
15
+ %(<ul class="section-nav">\n#{build_toc_list(@entries)}</ul>)
25
16
  end
26
17
 
27
18
  def inject_anchors_into_html
@@ -60,7 +51,8 @@ module Jekyll
60
51
  uniq: uniq,
61
52
  text: text,
62
53
  node_name: node.name,
63
- content_node: header_content
54
+ content_node: header_content,
55
+ h_num: node.name.delete('h').to_i
64
56
  }
65
57
  end
66
58
 
@@ -68,55 +60,47 @@ module Jekyll
68
60
  end
69
61
 
70
62
  # Returns the list items for entries
71
- def build_lis(entries, min_h_num)
72
- lis = ''
63
+ def build_toc_list(entries)
73
64
  i = 0
74
- while i < entries.length
65
+ toc_list = ''
66
+ min_h_num = entries.map { |e| e[:h_num] }.min
67
+
68
+ while i < entries.count
75
69
  entry = entries[i]
76
- curr_h_num = entry[:node_name].delete('h').to_i
77
- if curr_h_num == min_h_num
70
+ if entry[:h_num] == min_h_num
78
71
  # If the current entry should not be indented in the list, add the entry to the list
79
- lis << %(<li class="toc-entry toc-#{entry[:node_name]}"><a href="##{entry[:id]}#{entry[:uniq]}">#{entry[:text]}</a>)
72
+ toc_list << %(<li class="toc-entry toc-#{entry[:node_name]}"><a href="##{entry[:id]}#{entry[:uniq]}">#{entry[:text]}</a>)
80
73
  # If the next entry should be indented in the list, generate a sublist
81
- if i + 1 < entries.length
74
+ if i + 1 < entries.count
82
75
  next_entry = entries[i + 1]
83
- next_h_num = next_entry[:node_name].delete('h').to_i
84
- if next_h_num > min_h_num
85
- lis << %(\n)
86
- lis << %(<ul>\n)
87
- nest_entries = get_nest_entries(entries[i + 1, entries.length], min_h_num)
88
- lis << build_lis(nest_entries, min_h_num + 1)
89
- lis << %(</ul>\n)
90
- i += nest_entries.length
76
+ if next_entry[:h_num] > min_h_num
77
+ nest_entries = get_nest_entries(entries[i + 1, entries.count], min_h_num)
78
+ toc_list << %(\n<ul>\n#{build_toc_list(nest_entries)}</ul>\n)
79
+ i += nest_entries.count
91
80
  end
92
81
  end
93
82
  # Add the closing tag for the current entry in the list
94
- lis << %(</li>\n)
95
- elsif curr_h_num > min_h_num
83
+ toc_list << %(</li>\n)
84
+ elsif entry[:h_num] > min_h_num
96
85
  # If the current entry should be indented in the list, generate a sublist
97
- lis << %(<ul>\n)
98
- nest_entries = get_nest_entries(entries[i, entries.length], min_h_num)
99
- lis << build_lis(nest_entries, min_h_num + 1)
100
- lis << %(</ul>\n)
101
- i += nest_entries.length - 1
86
+ nest_entries = get_nest_entries(entries[i, entries.count], min_h_num)
87
+ toc_list << %(<ul>\n#{build_toc_list(nest_entries)}</ul>\n)
88
+ i += nest_entries.count - 1
102
89
  end
103
90
  i += 1
104
91
  end
105
- lis
92
+
93
+ toc_list
106
94
  end
107
95
 
108
96
  # Returns the entries in a nested list
109
97
  # The nested list starts at the first entry in entries (inclusive)
110
98
  # The nested list ends at the first entry in entries with depth min_h_num or greater (exclusive)
111
99
  def get_nest_entries(entries, min_h_num)
112
- nest_entries = []
113
- (0..(entries.length - 1)).each do |i|
114
- nest_entry = entries[i]
115
- nest_h_num = nest_entry[:node_name].delete('h').to_i
116
- break unless nest_h_num > min_h_num
117
- nest_entries.push(nest_entry)
100
+ entries.inject([]) do |nest_entries, entry|
101
+ break nest_entries if entry[:h_num] == min_h_num
102
+ nest_entries << entry
118
103
  end
119
- nest_entries
120
104
  end
121
105
  end
122
106
  end
@@ -1,3 +1,3 @@
1
1
  module JekyllToc
2
- VERSION = '0.3.0'.freeze
2
+ VERSION = '0.4.0.rc'.freeze
3
3
  end
@@ -5,18 +5,17 @@ require 'minitest/autorun'
5
5
  require 'jekyll'
6
6
  require 'jekyll-toc'
7
7
 
8
- SIMPLE_HTML = <<-HTML
9
- <h1>Simple H1</h1>
10
- <h2>Simple H2</h2>
11
- <h3>Simple H3</h3>
12
- <h4>Simple H4</h4>
13
- <h5>Simple H5</h5>
14
- <h6>Simple H6</h6>
8
+ SIMPLE_HTML = <<-HTML.freeze
9
+ <h1>Simple H1</h1>
10
+ <h2>Simple H2</h2>
11
+ <h3>Simple H3</h3>
12
+ <h4>Simple H4</h4>
13
+ <h5>Simple H5</h5>
14
+ <h6>Simple H6</h6>
15
15
  HTML
16
16
 
17
17
  module TestHelpers
18
18
  def read_html_and_create_parser
19
19
  @parser = Jekyll::TableOfContents::Parser.new(SIMPLE_HTML)
20
- assert_match(/Simple H1/, @parser.doc.inner_html)
21
20
  end
22
21
  end
@@ -0,0 +1,96 @@
1
+ require 'test_helper'
2
+
3
+ class TestVariousTocHtml < Minitest::Test
4
+ TEST_HTML_1 = <<-HTML
5
+ <h1>h1</h1>
6
+ <h3>h3</h3>
7
+ <h6>h6</h6>
8
+ HTML
9
+
10
+ TEST_HTML_2 = <<-HTML
11
+ <h1>h1</h1>
12
+ <h3>h3</h3>
13
+ <h2>h2</h2>
14
+ <h6>h6</h6>
15
+ HTML
16
+
17
+ TEST_HTML_3 = <<-HTML
18
+ <h6>h6</h6>
19
+ <h5>h5</h5>
20
+ <h4>h4</h4>
21
+ <h3>h3</h3>
22
+ <h2>h2</h2>
23
+ <h1>h1</h1>
24
+ HTML
25
+
26
+ def test_nested_toc
27
+ @parser = Jekyll::TableOfContents::Parser.new(TEST_HTML_1)
28
+ doc = Nokogiri::HTML(@parser.toc)
29
+ assert_equal(doc.css('ul.section-nav').to_s, <<-HTML
30
+ <ul class="section-nav">
31
+ <li class="toc-entry toc-h1">
32
+ <a href="#h1">h1</a>
33
+ <ul>
34
+ <li class="toc-entry toc-h3">
35
+ <a href="#h3">h3</a>
36
+ <ul>
37
+ <li class="toc-entry toc-h6"><a href="#h6">h6</a></li>
38
+ </ul>
39
+ </li>
40
+ </ul>
41
+ </li>
42
+ </ul>
43
+ HTML
44
+ )
45
+ end
46
+
47
+ def test_complex_nested_toc
48
+ @parser = Jekyll::TableOfContents::Parser.new(TEST_HTML_2)
49
+ doc = Nokogiri::HTML(@parser.toc)
50
+ assert_equal(doc.css('ul.section-nav').to_s, <<-HTML
51
+ <ul class="section-nav">
52
+ <li class="toc-entry toc-h1">
53
+ <a href="#h1">h1</a>
54
+ <ul>
55
+ <ul>
56
+ <li class="toc-entry toc-h3"><a href="#h3">h3</a></li>
57
+ </ul>
58
+ <li class="toc-entry toc-h2">
59
+ <a href="#h2">h2</a>
60
+ <ul>
61
+ <li class="toc-entry toc-h6"><a href="#h6">h6</a></li>
62
+ </ul>
63
+ </li>
64
+ </ul>
65
+ </li>
66
+ </ul>
67
+ HTML
68
+ )
69
+ end
70
+
71
+ def test_decremental_headings
72
+ @parser = Jekyll::TableOfContents::Parser.new(TEST_HTML_3)
73
+ doc = Nokogiri::HTML(@parser.toc)
74
+ assert_equal(doc.css('ul.section-nav').to_s, <<-HTML
75
+ <ul class="section-nav">
76
+ <ul>
77
+ <ul>
78
+ <ul>
79
+ <ul>
80
+ <ul>
81
+ <li class="toc-entry toc-h6"><a href="#h6">h6</a></li>
82
+ </ul>
83
+ <li class="toc-entry toc-h5"><a href="#h5">h5</a></li>
84
+ </ul>
85
+ <li class="toc-entry toc-h4"><a href="#h4">h4</a></li>
86
+ </ul>
87
+ <li class="toc-entry toc-h3"><a href="#h3">h3</a></li>
88
+ </ul>
89
+ <li class="toc-entry toc-h2"><a href="#h2">h2</a></li>
90
+ </ul>
91
+ <li class="toc-entry toc-h1"><a href="#h1">h1</a></li>
92
+ </ul>
93
+ HTML
94
+ )
95
+ end
96
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-toc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0.rc
5
5
  platform: ruby
6
6
  authors:
7
7
  - toshimaru
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-07-20 00:00:00.000000000 Z
12
+ date: 2017-10-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
@@ -123,12 +123,12 @@ files:
123
123
  - LICENSE.md
124
124
  - README.md
125
125
  - Rakefile
126
- - gemfiles/jekyll_2.gemfile
127
126
  - gemfiles/jekyll_3.1.gemfile
128
127
  - gemfiles/jekyll_3.2.gemfile
129
128
  - gemfiles/jekyll_3.3.gemfile
130
129
  - gemfiles/jekyll_3.4.gemfile
131
130
  - gemfiles/jekyll_3.5.gemfile
131
+ - gemfiles/jekyll_3.6.gemfile
132
132
  - gemfiles/jekyll_3.gemfile
133
133
  - jekyll-toc.gemspec
134
134
  - lib/jekyll-toc.rb
@@ -138,6 +138,7 @@ files:
138
138
  - test/test_inject_anchors_filter.rb
139
139
  - test/test_toc_filter.rb
140
140
  - test/test_toc_only_filter.rb
141
+ - test/test_various_toc_html.rb
141
142
  homepage: https://github.com/toshimaru/jekyll-toc
142
143
  licenses:
143
144
  - MIT
@@ -156,12 +157,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
156
157
  version: 2.1.0
157
158
  required_rubygems_version: !ruby/object:Gem::Requirement
158
159
  requirements:
159
- - - ">="
160
+ - - ">"
160
161
  - !ruby/object:Gem::Version
161
- version: '0'
162
+ version: 1.3.1
162
163
  requirements: []
163
164
  rubyforge_project:
164
- rubygems_version: 2.5.2
165
+ rubygems_version: 2.6.13
165
166
  signing_key:
166
167
  specification_version: 4
167
168
  summary: Jekyll Table of Contents plugin
@@ -170,3 +171,4 @@ test_files:
170
171
  - test/test_inject_anchors_filter.rb
171
172
  - test/test_toc_filter.rb
172
173
  - test/test_toc_only_filter.rb
174
+ - test/test_various_toc_html.rb