jekyll-toc 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +3 -0
- data/CHANGELOG.md +1 -0
- data/README.md +38 -30
- data/jekyll-toc.gemspec +2 -1
- data/lib/jekyll-toc.rb +10 -70
- data/lib/table_of_contents/parser.rb +66 -0
- data/lib/version.rb +1 -1
- data/test/test_helper.rb +3 -0
- metadata +24 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5f0601165123bc065a76d355ae7fff00c7143c59
|
4
|
+
data.tar.gz: a6debb0a1f9999f1714a0db81993dade1f514cc0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aadfda21dacca72d16ef4ac3577f18bd00753d6c21dd2e74f1cf63f70afb559760bb43d23770a00aece77f671320090dc1b33ba27da8d8764b9bc877e54e1bed
|
7
|
+
data.tar.gz: 2eaf6f22f02f2b9444540f4cd3484224c1e37319233fc7a44b3d4d0e7004a3e5fdf80434322654b98986da64c492ab1045d6f69335a4f772b27ff256b453cbe8
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Changelog is maintained under [Github Releases](https://github.com/toshimaru/jekyll-toc/releases).
|
data/README.md
CHANGED
@@ -4,8 +4,9 @@
|
|
4
4
|
[![Gem Version](https://badge.fury.io/rb/jekyll-toc.svg)](http://badge.fury.io/rb/jekyll-toc)
|
5
5
|
[![Dependency Status](https://gemnasium.com/toshimaru/jekyll-toc.svg)](https://gemnasium.com/toshimaru/jekyll-toc)
|
6
6
|
[![Code Climate](https://codeclimate.com/github/toshimaru/jekyll-toc/badges/gpa.svg)](https://codeclimate.com/github/toshimaru/jekyll-toc)
|
7
|
+
[![Test Coverage](https://codeclimate.com/github/toshimaru/jekyll-toc/badges/coverage.svg)](https://codeclimate.com/github/toshimaru/jekyll-toc/coverage)
|
7
8
|
|
8
|
-
#
|
9
|
+
# Installation
|
9
10
|
|
10
11
|
Add jekyll-toc plugin in your site's `Gemfile`.
|
11
12
|
|
@@ -30,39 +31,47 @@ toc: true
|
|
30
31
|
---
|
31
32
|
```
|
32
33
|
|
34
|
+
# Usage
|
35
|
+
|
33
36
|
There are three Liquid filters available now, which all should be applied
|
34
37
|
to some HTML content, e.g. the Liquid variable `content` available in
|
35
38
|
Jekyll's templates.
|
36
39
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
</a>
|
51
|
-
```
|
52
|
-
|
53
|
-
This is only useful when the TOC itself should be placed at some other
|
54
|
-
location with the `toc_only` filter.
|
55
|
-
|
56
|
-
3. `toc`
|
57
|
-
This is the concatenation of the two above, where the TOC is placed
|
58
|
-
directly above the content.
|
59
|
-
|
60
|
-
Add `toc` filter to your site's `{{ content }}` (e.g. `_layouts/post.html`).
|
61
|
-
|
62
|
-
```
|
63
|
-
{{ content | toc }}
|
64
|
-
```
|
40
|
+
## Basic Usage
|
41
|
+
|
42
|
+
### `toc` filter
|
43
|
+
|
44
|
+
Add `toc` filter to your site's `{{ content }}` (e.g. `_layouts/post.html`).
|
45
|
+
|
46
|
+
```
|
47
|
+
{{ content | toc }}
|
48
|
+
```
|
49
|
+
|
50
|
+
This filter places the TOC directly above the content.
|
51
|
+
|
52
|
+
## Advanced Usage
|
65
53
|
|
54
|
+
If you'd like separated TOC and content, you can use `toc_only` and `inject_anchors` filters.
|
55
|
+
|
56
|
+
### `toc_only` filter
|
57
|
+
|
58
|
+
Generates the TOC itself as described [below](#generated-table-of-contents-html).
|
59
|
+
Mostly useful in cases where the TOC should _not_ be placed immediately
|
60
|
+
above the content but at some other place of the page, i.e. an aside.
|
61
|
+
|
62
|
+
### `inject_anchors` filter
|
63
|
+
|
64
|
+
Injects HTML anchors into the content without actually outputing the
|
65
|
+
TOC itself. They are of the form:
|
66
|
+
|
67
|
+
```html
|
68
|
+
<a id="heading11" class="anchor" href="#heading1-1" aria-hidden="true">
|
69
|
+
<span class="octicon octicon-link"></span>
|
70
|
+
</a>
|
71
|
+
```
|
72
|
+
|
73
|
+
This is only useful when the TOC itself should be placed at some other
|
74
|
+
location with the `toc_only` filter.
|
66
75
|
|
67
76
|
## Generated Table of Contents HTML
|
68
77
|
|
@@ -122,4 +131,3 @@ all other links:
|
|
122
131
|
display: none;
|
123
132
|
}
|
124
133
|
```
|
125
|
-
|
data/jekyll-toc.gemspec
CHANGED
@@ -17,8 +17,9 @@ Gem::Specification.new do |spec|
|
|
17
17
|
|
18
18
|
spec.add_runtime_dependency "nokogiri", "~> 1.6"
|
19
19
|
|
20
|
-
spec.add_development_dependency "appraisal"
|
21
20
|
spec.add_development_dependency 'minitest', "~> 5.0"
|
21
|
+
spec.add_development_dependency "appraisal"
|
22
|
+
spec.add_development_dependency "codeclimate-test-reporter"
|
22
23
|
spec.add_development_dependency "jekyll", "~> 2.0"
|
23
24
|
spec.add_development_dependency "rake"
|
24
25
|
end
|
data/lib/jekyll-toc.rb
CHANGED
@@ -1,90 +1,30 @@
|
|
1
1
|
require 'nokogiri'
|
2
|
+
require 'table_of_contents/parser'
|
2
3
|
|
3
4
|
module Jekyll
|
4
|
-
module TableOfContents
|
5
|
-
PUNCTUATION_REGEXP = RUBY_VERSION > '1.9' ? /[^\p{Word}\- ]/u : /[^\w\- ]/
|
6
|
-
|
7
|
-
class Parser
|
8
|
-
attr_reader :doc
|
9
|
-
|
10
|
-
def initialize(html)
|
11
|
-
@doc = Nokogiri::HTML::DocumentFragment.parse(html)
|
12
|
-
@entries = parse_content
|
13
|
-
end
|
14
|
-
|
15
|
-
def build_toc
|
16
|
-
toc = %Q{<ul class="section-nav">\n}
|
17
|
-
|
18
|
-
@entries.each do |entry|
|
19
|
-
toc << %Q{<li class="toc-entry toc-#{entry[:node_name]}"><a href="##{entry[:id]}#{entry[:uniq]}">#{entry[:text]}</a></li>\n}
|
20
|
-
end
|
21
|
-
|
22
|
-
toc << '</ul>'
|
23
|
-
end
|
24
|
-
|
25
|
-
def inject_anchors_into_html
|
26
|
-
@entries.each do |entry|
|
27
|
-
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>})
|
28
|
-
end
|
29
|
-
|
30
|
-
@doc.inner_html
|
31
|
-
end
|
32
|
-
|
33
|
-
def toc
|
34
|
-
build_toc + inject_anchors_into_html
|
35
|
-
end
|
36
|
-
|
37
|
-
# parse logic is from html-pipeline toc_filter
|
38
|
-
# https://github.com/jch/html-pipeline/blob/v1.1.0/lib/html/pipeline/toc_filter.rb
|
39
|
-
private
|
40
|
-
def parse_content
|
41
|
-
entries = []
|
42
|
-
headers = Hash.new(0)
|
43
|
-
|
44
|
-
@doc.css('h1, h2, h3, h4, h5, h6').each do |node|
|
45
|
-
text = node.text
|
46
|
-
id = text.downcase
|
47
|
-
id.gsub!(PUNCTUATION_REGEXP, '') # remove punctuation
|
48
|
-
id.gsub!(' ', '-') # replace spaces with dash
|
49
|
-
|
50
|
-
uniq = (headers[id] > 0) ? "-#{headers[id]}" : ''
|
51
|
-
headers[id] += 1
|
52
|
-
if header_content = node.children.first
|
53
|
-
entries << {
|
54
|
-
id: id,
|
55
|
-
uniq: uniq,
|
56
|
-
text: text,
|
57
|
-
node_name: node.name,
|
58
|
-
content_node: header_content
|
59
|
-
}
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
entries
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
5
|
module TableOfContentsFilter
|
69
6
|
def toc_only(html)
|
70
|
-
page = @context.registers[:page]
|
71
7
|
return html unless page['toc']
|
72
8
|
|
73
|
-
Jekyll::TableOfContents::Parser.new(html).build_toc
|
9
|
+
::Jekyll::TableOfContents::Parser.new(html).build_toc
|
74
10
|
end
|
75
11
|
|
76
12
|
def inject_anchors(html)
|
77
|
-
page = @context.registers[:page]
|
78
13
|
return html unless page['toc']
|
79
14
|
|
80
|
-
Jekyll::TableOfContents::Parser.new(html).inject_anchors_into_html
|
15
|
+
::Jekyll::TableOfContents::Parser.new(html).inject_anchors_into_html
|
81
16
|
end
|
82
17
|
|
83
18
|
def toc(html)
|
84
|
-
page = @context.registers[:page]
|
85
19
|
return html unless page['toc']
|
86
20
|
|
87
|
-
Jekyll::TableOfContents::Parser.new(html).toc
|
21
|
+
::Jekyll::TableOfContents::Parser.new(html).toc
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def page
|
27
|
+
@context.registers[:page]
|
88
28
|
end
|
89
29
|
end
|
90
30
|
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
module Jekyll
|
2
|
+
module TableOfContents
|
3
|
+
class Parser
|
4
|
+
PUNCTUATION_REGEXP = RUBY_VERSION > '1.9' ? /[^\p{Word}\- ]/u : /[^\w\- ]/
|
5
|
+
|
6
|
+
attr_reader :doc
|
7
|
+
|
8
|
+
def initialize(html)
|
9
|
+
@doc = Nokogiri::HTML::DocumentFragment.parse(html)
|
10
|
+
@entries = parse_content
|
11
|
+
end
|
12
|
+
|
13
|
+
def build_toc
|
14
|
+
toc = %Q{<ul class="section-nav">\n}
|
15
|
+
|
16
|
+
@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}
|
18
|
+
end
|
19
|
+
|
20
|
+
toc << '</ul>'
|
21
|
+
end
|
22
|
+
|
23
|
+
def inject_anchors_into_html
|
24
|
+
@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>})
|
26
|
+
end
|
27
|
+
|
28
|
+
@doc.inner_html
|
29
|
+
end
|
30
|
+
|
31
|
+
def toc
|
32
|
+
build_toc + inject_anchors_into_html
|
33
|
+
end
|
34
|
+
|
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
|
+
private
|
38
|
+
|
39
|
+
def parse_content
|
40
|
+
entries = []
|
41
|
+
headers = Hash.new(0)
|
42
|
+
|
43
|
+
@doc.css('h1, h2, h3, h4, h5, h6').each do |node|
|
44
|
+
text = node.text
|
45
|
+
id = text.downcase
|
46
|
+
id.gsub!(PUNCTUATION_REGEXP, '') # remove punctuation
|
47
|
+
id.gsub!(' ', '-') # replace spaces with dash
|
48
|
+
|
49
|
+
uniq = (headers[id] > 0) ? "-#{headers[id]}" : ''
|
50
|
+
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
|
60
|
+
end
|
61
|
+
|
62
|
+
entries
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
data/lib/version.rb
CHANGED
data/test/test_helper.rb
CHANGED
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.1.
|
4
|
+
version: 0.1.1
|
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: 2016-
|
12
|
+
date: 2016-04-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: nokogiri
|
@@ -25,6 +25,20 @@ dependencies:
|
|
25
25
|
- - "~>"
|
26
26
|
- !ruby/object:Gem::Version
|
27
27
|
version: '1.6'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: minitest
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '5.0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '5.0'
|
28
42
|
- !ruby/object:Gem::Dependency
|
29
43
|
name: appraisal
|
30
44
|
requirement: !ruby/object:Gem::Requirement
|
@@ -40,19 +54,19 @@ dependencies:
|
|
40
54
|
- !ruby/object:Gem::Version
|
41
55
|
version: '0'
|
42
56
|
- !ruby/object:Gem::Dependency
|
43
|
-
name:
|
57
|
+
name: codeclimate-test-reporter
|
44
58
|
requirement: !ruby/object:Gem::Requirement
|
45
59
|
requirements:
|
46
|
-
- - "
|
60
|
+
- - ">="
|
47
61
|
- !ruby/object:Gem::Version
|
48
|
-
version: '
|
62
|
+
version: '0'
|
49
63
|
type: :development
|
50
64
|
prerelease: false
|
51
65
|
version_requirements: !ruby/object:Gem::Requirement
|
52
66
|
requirements:
|
53
|
-
- - "
|
67
|
+
- - ">="
|
54
68
|
- !ruby/object:Gem::Version
|
55
|
-
version: '
|
69
|
+
version: '0'
|
56
70
|
- !ruby/object:Gem::Dependency
|
57
71
|
name: jekyll
|
58
72
|
requirement: !ruby/object:Gem::Requirement
|
@@ -90,6 +104,7 @@ files:
|
|
90
104
|
- ".gitignore"
|
91
105
|
- ".travis.yml"
|
92
106
|
- Appraisals
|
107
|
+
- CHANGELOG.md
|
93
108
|
- Gemfile
|
94
109
|
- LICENSE.md
|
95
110
|
- README.md
|
@@ -99,6 +114,7 @@ files:
|
|
99
114
|
- gemfiles/jekyll_3.gemfile
|
100
115
|
- jekyll-toc.gemspec
|
101
116
|
- lib/jekyll-toc.rb
|
117
|
+
- lib/table_of_contents/parser.rb
|
102
118
|
- lib/version.rb
|
103
119
|
- test/test_helper.rb
|
104
120
|
- test/test_inject_anchors_filter.rb
|
@@ -124,7 +140,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
124
140
|
version: '0'
|
125
141
|
requirements: []
|
126
142
|
rubyforge_project:
|
127
|
-
rubygems_version: 2.4.5
|
143
|
+
rubygems_version: 2.4.5.1
|
128
144
|
signing_key:
|
129
145
|
specification_version: 4
|
130
146
|
summary: Jekyll Table of Contents plugin
|