page-toc-filter 0.0.1 → 0.1.0

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: 86b76a41f01bb806ec13216e76bd748af27c77b2
4
- data.tar.gz: ce2f03b1fec1676acc1ca6728d3618e47906c2f0
3
+ metadata.gz: 45b7f9c69ba970ee849f37ba30d5662db658f14e
4
+ data.tar.gz: 2aad334c79bcf252995ff8812e883a80a179eb80
5
5
  SHA512:
6
- metadata.gz: 192a85e15ead4dfb0646a2626bf1a4cf947691d9a992877c3343c626a289c73bcd41c3c501e79310c17c72f7a93eb75aefcb609cf31e32f3a11d59944c89234e
7
- data.tar.gz: f748bdb2e314294e80ee052ff83860851e448529fbd63cf6c43e83f7e9cd33655c2bebdf461eafff3667721634108c054129c7965bed2933f1f067e615fdb742
6
+ metadata.gz: cb85a7bf92d8685f888665064538183ff073273894cc4e46110825e596209a0e98f8df14af89d5b6544fda0bdece7b7f3f775b9ffa0ef1ac204f17b8820e38e1
7
+ data.tar.gz: '092d37bfcdad2bd1de98db1dcdec337dce79fc05bf26068e549c754a6365f2c64b1fcbc67cdea015ee511db227b7d819a5c93bda72da5254d3c981d0d88e68a1'
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # PageTocFilter
2
2
 
3
+ Inserts a table of contents on a page via the [`HTML::Pipeline`](https://github.com/jch/html-pipeline).
4
+
3
5
  ## Installation
4
6
 
5
7
  Add this line to your application's Gemfile:
@@ -34,6 +36,8 @@ pipeline = HTML::Pipeline.new([
34
36
  ])
35
37
  ```
36
38
 
39
+ Then, on your page, enter the text `{:toc}` to have it replaced by a table of contents.
40
+
37
41
  By default, only `h2` headings will be converted into a table of contents. You can change this with the optional `toc_levels` setting, which should be a comma-separated string of heading levels. For example:
38
42
 
39
43
  ``` ruby
@@ -14,15 +14,41 @@ class PageTocFilter < HTML::Pipeline::Filter
14
14
  end
15
15
 
16
16
  def page_toc_filter(doc)
17
- toc = ''
18
- levels = context[:toc_levels] || 'h2'
17
+ levels = doc.search(context[:toc_levels] || 'h2')
18
+ return '' if levels.empty?
19
+
20
+ toc = %(<ul id="markdown-toc">\n)
21
+ last_level = nil
22
+ depth = 1
23
+
24
+ levels.each do |node|
25
+ current_level = node.name.match(/h(\d)/)[1]
19
26
 
20
- doc.search(levels).each do |node|
21
27
  text = node.text
22
28
  id = node.child['id']
23
- toc << %(<li><a href="##{id}" id="markdown-toc-#{id}">#{text}</a></li>\n)
29
+
30
+ link = %(<a href="##{id}" id="markdown-toc-#{id}">#{text}</a>)
31
+
32
+ if last_level.nil?
33
+ toc << %(<li>#{link})
34
+ elsif current_level == last_level
35
+ toc << %(</li>\n<li>#{link})
36
+ elsif current_level > last_level
37
+ depth += 1
38
+ toc << %(\n<ul><li>#{link})
39
+ elsif current_level < last_level
40
+ depth -= 1
41
+ toc << %(</li></ul>\n<li>#{link})
42
+ end
43
+
44
+ last_level = current_level
45
+ end
46
+
47
+ if depth < 0
48
+ raise ArgumentError, 'Your headings are not in sequential order. It seems that a lower heading level (like an h4) is being defined before a higher heading level (like an h1).'
24
49
  end
25
- toc = %(<ul id="markdown-toc">\n#{toc}</ul>) unless toc.empty?
50
+
51
+ toc << %(</li>\n</ul>) * depth
26
52
  toc
27
53
  end
28
54
  end
@@ -3,7 +3,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
3
 
4
4
  Gem::Specification.new do |spec|
5
5
  spec.name = 'page-toc-filter'
6
- spec.version = '0.0.1'
6
+ spec.version = '0.1.0'
7
7
  spec.authors = ['Garen Torikian']
8
8
  spec.email = ['gjtorikian@gmail.com']
9
9
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: page-toc-filter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Garen Torikian
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-11-20 00:00:00.000000000 Z
11
+ date: 2017-04-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: html-pipeline
@@ -115,7 +115,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
115
115
  version: '0'
116
116
  requirements: []
117
117
  rubyforge_project:
118
- rubygems_version: 2.4.5.1
118
+ rubygems_version: 2.6.8
119
119
  signing_key:
120
120
  specification_version: 4
121
121
  summary: Generates a table of contents for a single page, via HTML::Pipeline.