jekyll-toc 0.2.1 → 0.3.0.pre1

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: 491e3fec762e5cd84506a21a98e0a19c4e2018b5
4
- data.tar.gz: ff448ad90aada2e391cbcae847f65d7c28306e9d
3
+ metadata.gz: bd7a5a0373a967ef42103de10288c8da0db20f0b
4
+ data.tar.gz: 84427b7348a6db378b8bc27b1b938797f2b6f84f
5
5
  SHA512:
6
- metadata.gz: 92b25158017d448d0dc8db8ee76b030946f3fc19e4d5e807951fca33daef99d8a693a11d056355755e3ef5edca326c5274275a98e193974efbaad62205c221a4
7
- data.tar.gz: 972a128dd4054041d1b6bcd6ffe2f6990a5eced0b6f3176861d99b06e8df030a4089cb7e4567b1a9b2478a98182e8433f25f743f2740a8a66df4a46dc29d0fc3
6
+ metadata.gz: d4d5644477eebaca659eed0cfcced2d07e4771a26f02d4ed2056c90dc2ff3d51314464671b9504ac35346f67bb238794fd6eb87f30599a1cdf7912b8d12114e8
7
+ data.tar.gz: f68deeb9b77fb14381924694ca12da5003f85b95bd9e4f3debe43d03b9925e95506d75f58107fbb09f559f07142758203a7c8b5b5d26145bcd954235abdd19f6
data/.travis.yml CHANGED
@@ -5,8 +5,9 @@ rvm:
5
5
  - 2.2
6
6
  - 2.3.1
7
7
  - 2.4.0
8
- # Put this in your .travis.yml
8
+ # gemfile is generated by appraisal
9
9
  gemfile:
10
+ - gemfiles/jekyll_3.4.gemfile
10
11
  - gemfiles/jekyll_3.3.gemfile
11
12
  - gemfiles/jekyll_3.2.gemfile
12
13
  - gemfiles/jekyll_3.1.gemfile
data/README.md CHANGED
@@ -37,19 +37,19 @@ There are three Liquid filters available now, which all should be applied
37
37
  to some HTML content, e.g. the Liquid variable `content` available in
38
38
  Jekyll's templates.
39
39
 
40
- ## Basic Usage
40
+ ## 1. Basic Usage
41
41
 
42
42
  ### `toc` filter
43
43
 
44
44
  Add `toc` filter to your site's `{{ content }}` (e.g. `_layouts/post.html`).
45
45
 
46
- ```
46
+ ```liquid
47
47
  {{ content | toc }}
48
48
  ```
49
49
 
50
50
  This filter places the TOC directly above the content.
51
51
 
52
- ## Advanced Usage
52
+ ## 2. Advanced Usage
53
53
 
54
54
  If you'd like separated TOC and content, you can use `toc_only` and `inject_anchors` filters.
55
55
 
@@ -1,7 +1,8 @@
1
1
  module Jekyll
2
2
  module TableOfContents
3
+ # Parse html contents and generate table of contents
3
4
  class Parser
4
- PUNCTUATION_REGEXP = RUBY_VERSION > '1.9' ? /[^\p{Word}\- ]/u : /[^\w\- ]/
5
+ PUNCTUATION_REGEXP = /[^\p{Word}\- ]/u
5
6
 
6
7
  attr_reader :doc
7
8
 
@@ -11,18 +12,21 @@ module Jekyll
11
12
  end
12
13
 
13
14
  def build_toc
14
- toc = %Q{<ul class="section-nav">\n}
15
+ toc = %(<ul class="section-nav">\n)
15
16
 
17
+ min_h_num = 6
16
18
  @entries.each do |entry|
17
- toc << %Q{<li class="toc-entry toc-#{entry[:node_name]}"><a href="##{entry[:id]}#{entry[:uniq]}">#{entry[:text]}</a></li>\n}
19
+ h_num = entry[:node_name].delete('h').to_i
20
+ min_h_num = [min_h_num, h_num].min
18
21
  end
22
+ toc << build_lis(@entries, min_h_num)
19
23
 
20
24
  toc << '</ul>'
21
25
  end
22
26
 
23
27
  def inject_anchors_into_html
24
28
  @entries.each do |entry|
25
- entry[:content_node].add_previous_sibling(%Q{<a id="#{entry[:id]}#{entry[:uniq]}" class="anchor" href="##{entry[:id]}#{entry[:uniq]}" aria-hidden="true"><span class="octicon octicon-link"></span></a>})
29
+ entry[:content_node].add_previous_sibling(%(<a id="#{entry[:id]}#{entry[:uniq]}" class="anchor" href="##{entry[:id]}#{entry[:uniq]}" aria-hidden="true"><span class="octicon octicon-link"></span></a>))
26
30
  end
27
31
 
28
32
  @doc.inner_html
@@ -32,10 +36,10 @@ module Jekyll
32
36
  build_toc + inject_anchors_into_html
33
37
  end
34
38
 
35
- # parse logic is from html-pipeline toc_filter
36
- # https://github.com/jch/html-pipeline/blob/v1.1.0/lib/html/pipeline/toc_filter.rb
37
39
  private
38
40
 
41
+ # parse logic is from html-pipeline toc_filter
42
+ # https://github.com/jch/html-pipeline/blob/v1.1.0/lib/html/pipeline/toc_filter.rb
39
43
  def parse_content
40
44
  entries = []
41
45
  headers = Hash.new(0)
@@ -46,21 +50,74 @@ module Jekyll
46
50
  id.gsub!(PUNCTUATION_REGEXP, '') # remove punctuation
47
51
  id.gsub!(' ', '-') # replace spaces with dash
48
52
 
49
- uniq = (headers[id] > 0) ? "-#{headers[id]}" : ''
53
+ uniq = headers[id] > 0 ? "-#{headers[id]}" : ''
50
54
  headers[id] += 1
51
- if header_content = node.children.first
52
- entries << {
53
- id: id,
54
- uniq: uniq,
55
- text: text,
56
- node_name: node.name,
57
- content_node: header_content
58
- }
59
- end
55
+ header_content = node.children.first
56
+ next unless header_content
57
+
58
+ entries << {
59
+ id: id,
60
+ uniq: uniq,
61
+ text: text,
62
+ node_name: node.name,
63
+ content_node: header_content
64
+ }
60
65
  end
61
66
 
62
67
  entries
63
68
  end
69
+
70
+ # Returns the list items for entries
71
+ def build_lis(entries, min_h_num)
72
+ lis = ''
73
+ i = 0
74
+ while i < entries.length
75
+ entry = entries[i]
76
+ curr_h_num = entry[:node_name].delete('h').to_i
77
+ if curr_h_num == min_h_num
78
+ # 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>)
80
+ # If the next entry should be indented in the list, generate a sublist
81
+ if i + 1 < entries.length
82
+ 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
91
+ end
92
+ end
93
+ # Add the closing tag for the current entry in the list
94
+ lis << %(</li>\n)
95
+ elsif curr_h_num > min_h_num
96
+ # 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
102
+ end
103
+ i += 1
104
+ end
105
+ lis
106
+ end
107
+
108
+ # Returns the entries in a nested list
109
+ # The nested list starts at the first entry in entries (inclusive)
110
+ # The nested list ends at the first entry in entries with depth min_h_num or greater (exclusive)
111
+ 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)
118
+ end
119
+ nest_entries
120
+ end
64
121
  end
65
122
  end
66
123
  end
data/lib/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module JekyllToc
2
- VERSION = '0.2.1'
2
+ VERSION = '0.3.0.pre1'
3
3
  end
data/test/test_helper.rb CHANGED
@@ -4,14 +4,14 @@ SimpleCov.start
4
4
  require 'jekyll'
5
5
  require 'minitest/autorun'
6
6
 
7
- SIMPLE_HTML = <<EOL
7
+ SIMPLE_HTML = <<HTML
8
8
  <h1>Simple H1</h1>
9
9
  <h2>Simple H2</h2>
10
10
  <h3>Simple H3</h3>
11
11
  <h4>Simple H4</h4>
12
12
  <h5>Simple H5</h5>
13
13
  <h6>Simple H6</h6>
14
- EOL
14
+ HTML
15
15
 
16
16
  module TestHelpers
17
17
  def read_html_and_create_parser
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.2.1
4
+ version: 0.3.0.pre1
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-03-28 00:00:00.000000000 Z
12
+ date: 2017-04-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
@@ -138,9 +138,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
138
138
  version: 2.1.0
139
139
  required_rubygems_version: !ruby/object:Gem::Requirement
140
140
  requirements:
141
- - - ">="
141
+ - - ">"
142
142
  - !ruby/object:Gem::Version
143
- version: '0'
143
+ version: 1.3.1
144
144
  requirements: []
145
145
  rubyforge_project:
146
146
  rubygems_version: 2.5.2