jekyll-toc-helpers 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 61ac4d47460a4674accaee642a67c84f6a838ea8
4
+ data.tar.gz: c6f7900b4a03f102c09fc46a74b3645d3d9b94d4
5
+ SHA512:
6
+ metadata.gz: 286867923fca5b53ee62a95f6a044a6b4229c33d5c57760346a2c5264bc93d5993a322c981a56af9f15d3a5331ec3544591561202e474082bd224c380fecebf8
7
+ data.tar.gz: ec9a78b4fc65a414e33e6d4491fb66aed8e950b67811ad22a4e18b3013f5b3cecef69a70caff16e0eb104deb6c5c59499da5a8faab607ed17bc1f48a3a366204
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ spec/fixtures/_site
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1
4
+ - 2.0
5
+ - 1.9.3
6
+ script: "script/cibuild"
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in jekyll-alt-urls.gemspec
4
+ gemspec
data/README.md ADDED
File without changes
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,25 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = 'jekyll-toc-helpers'
6
+ spec.version = '0.1.0'
7
+ spec.authors = ['Garen Torikian']
8
+ spec.email = ['gjtorikian@gmail.com']
9
+ spec.summary = 'Some helper tags for generating TOCs.'
10
+ spec.description = ''
11
+ spec.homepage = 'https://github.com/gjtorikian/jekyll-toc-helpers'
12
+ spec.license = 'MIT'
13
+
14
+ spec.files = `git ls-files`.split($/)
15
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
+ spec.test_files = spec.files.grep(%r{^(test)/})
17
+ spec.require_paths = ['lib']
18
+
19
+ spec.add_runtime_dependency 'jekyll', '~> 2.0'
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.3'
22
+ spec.add_development_dependency 'nokogiri', '~> 1.6'
23
+ spec.add_development_dependency 'rake'
24
+ spec.add_development_dependency 'rspec'
25
+ end
@@ -0,0 +1,92 @@
1
+ module Jekyll
2
+ module TOCHelper
3
+ def check_existing_content(context, prefix, id)
4
+ context.registers[:site].data["#{prefix}_#{id}"]
5
+ end
6
+
7
+ def construct_datafile(context, keys)
8
+ datafile = context.registers[:site].data
9
+
10
+ while path = keys.shift
11
+ datafile = datafile[path]
12
+ end
13
+
14
+ datafile
15
+ end
16
+ end
17
+
18
+ class TOCTag < Liquid::Tag
19
+ include TOCHelper
20
+
21
+ def initialize(tag_name, text, tokens)
22
+ super
23
+ keys = text.strip.split('.')
24
+ keys.shift(2) # this is just site.data
25
+ @keys = keys
26
+ @id = keys.join('.')
27
+ end
28
+
29
+ def render(context)
30
+ existing_content = check_existing_content(context, 'toc', @id)
31
+ return existing_content unless existing_content.nil?
32
+
33
+ list = []
34
+ datafile = construct_datafile(context, @keys)
35
+
36
+ datafile.each do |line|
37
+ if line.is_a? Hash
38
+ line.each_pair do |parent, children|
39
+ list << "- [#{parent}](#{Utils.slugify parent})"
40
+ children.each do |subsection|
41
+ list << " - [#{subsection}](#{Utils.slugify subsection})"
42
+ end
43
+ end
44
+ else
45
+ list << "- [#{line}](#{Utils.slugify line})"
46
+ end
47
+ end
48
+
49
+ context.registers[:site].data["toc_#{@id}"] = list.join("\n")
50
+ context.registers[:site].data["toc_#{@id}"]
51
+ end
52
+ end
53
+
54
+ class NextPrevTag < Liquid::Tag
55
+ include TOCHelper
56
+
57
+ def initialize(tag_name, text, tokens)
58
+ super
59
+ keys = text.strip.split('.')
60
+ keys.shift(2) # this is just site.data
61
+ @keys = keys
62
+ @id = keys.join('.')
63
+ end
64
+
65
+ def render(context)
66
+ existing_content = check_existing_content(context, 'next_prev', @id)
67
+ return existing_content unless existing_content.nil?
68
+
69
+ list = []
70
+ datafile = construct_datafile(context, @keys)
71
+
72
+ datafile.each do |line|
73
+ if line.is_a? Hash
74
+ line.each_pair do |parent, children|
75
+ list << parent
76
+ children.each do |subsection|
77
+ list << subsection
78
+ end
79
+ end
80
+ else
81
+ list << line
82
+ end
83
+ end
84
+
85
+ context.registers[:site].data["next_prev_#{@id}"] = list.join('|')
86
+ context.registers[:site].data["next_prev_#{@id}"]
87
+ end
88
+ end
89
+ end
90
+
91
+ Liquid::Template.register_tag('toc', Jekyll::TOCTag)
92
+ Liquid::Template.register_tag('next_prev', Jekyll::NextPrevTag)
data/script/bootstrap ADDED
@@ -0,0 +1,3 @@
1
+ #! /bin/bash
2
+
3
+ bundle install
data/script/cibuild ADDED
@@ -0,0 +1,4 @@
1
+ #! /bin/bash
2
+
3
+ script/bootstrap > /dev/null 2>&1
4
+ bundle exec rake spec
@@ -0,0 +1,11 @@
1
+ collections:
2
+ source:
3
+ output: true
4
+
5
+ defaults:
6
+ -
7
+ scope:
8
+ path: ""
9
+ type: "source"
10
+ values:
11
+ layout: "layout"
@@ -0,0 +1,5 @@
1
+ - One File
2
+ - Two File:
3
+ - One Subfile
4
+ - Two Subfile
5
+ - Three File
@@ -0,0 +1,23 @@
1
+ {% capture flat_links %}{% next_prev site.data.toc %}{% endcapture %}
2
+ {% assign flat_links = flat_links | split: '|' %}
3
+
4
+ <div class="pager">
5
+ <ul>
6
+ {% for title in flat_links %}
7
+ {% if title == page.title %}
8
+ {% unless forloop.first %}
9
+ <li class="left"><a href="source/{{ prev | slugify }}">&lt; Previous</a></li>
10
+ {% endunless %}
11
+
12
+ {% unless forloop.last %}
13
+ <li class="right"><a href="source/{{ flat_links[forloop.index] | slugify }}">Next &gt;</a></li>
14
+ {% endunless %}
15
+
16
+ {% break %}
17
+ {% endif %}
18
+ {% assign prev = title %}
19
+ {% endfor %}
20
+ </ul>
21
+ </div>
22
+
23
+ {{ content }}
@@ -0,0 +1,3 @@
1
+ ---
2
+ title: One File
3
+ ---
@@ -0,0 +1,3 @@
1
+ ---
2
+ title: Two File
3
+ ---
@@ -0,0 +1,3 @@
1
+ ---
2
+ title: Three File
3
+ ---
@@ -0,0 +1,5 @@
1
+ ---
2
+ title: The Index
3
+ ---
4
+
5
+ {% toc site.data.toc %}
@@ -0,0 +1,3 @@
1
+ ---
2
+ title: One Subfile
3
+ ---
@@ -0,0 +1,3 @@
1
+ ---
2
+ title: Two Subfile
3
+ ---
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+ require 'nokogiri'
3
+
4
+ describe('PrevNext') do
5
+ let(:file1) { destination_file('source/file1.html') }
6
+ let(:file2) { destination_file('source/file2.html') }
7
+ let(:subfile1) { destination_file('source/subfile1.html') }
8
+ let(:subfile2) { destination_file('source/subfile2.html') }
9
+ let(:file3) { destination_file('source/file3.html') }
10
+
11
+ it 'writes the default frontmatter using a variable' do
12
+ file1_doc = Nokogiri::HTML(file1)
13
+ expect(0).to equal(file1_doc.css('.left').size)
14
+ expect(1).to equal(file1_doc.css('.right').size)
15
+ expect('source/two-file').to eq(file1_doc.css('.right a').attr('href').text)
16
+
17
+ file2_doc = Nokogiri::HTML(file2)
18
+ expect(1).to equal(file2_doc.css('.left').size)
19
+ expect(1).to equal(file2_doc.css('.right').size)
20
+ expect('source/one-file').to eq(file2_doc.css('.left a').attr('href').text)
21
+ expect('source/one-subfile').to eq(file2_doc.css('.right a').attr('href').text)
22
+
23
+ file3_doc = Nokogiri::HTML(file3)
24
+ expect(1).to equal(file3_doc.css('.left').size)
25
+ expect(0).to equal(file3_doc.css('.right').size)
26
+ expect('source/two-subfile').to eq(file3_doc.css('.left a').attr('href').text)
27
+ end
28
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe('TOC') do
4
+ let(:index) { destination_file('source/index.html') }
5
+
6
+ it 'writes the TOC out when asked' do
7
+ toc = <<-eos
8
+ <ul>
9
+ <li><a href="one-file">One File</a></li>
10
+ <li><a href="two-file">Two File</a>
11
+ <ul>
12
+ <li><a href="one-subfile">One Subfile</a></li>
13
+ <li><a href="two-subfile">Two Subfile</a></li>
14
+ </ul>
15
+ </li>
16
+ <li><a href="three-file">Three File</a></li>
17
+ </ul>
18
+ eos
19
+
20
+ expect(index).to match(toc)
21
+ end
22
+ end
@@ -0,0 +1,53 @@
1
+ require 'jekyll'
2
+ require 'yaml'
3
+ require File.expand_path('lib/jekyll-toc-helpers.rb')
4
+
5
+ RSpec.configure do |config|
6
+
7
+ config.run_all_when_everything_filtered = true
8
+ config.filter_run :focus
9
+ config.order = 'random'
10
+
11
+ config.expect_with :rspec do |c|
12
+ c.syntax = :expect
13
+ end
14
+
15
+ config.before(:each) do
16
+ Jekyll.logger.log_level = :error
17
+
18
+ @fixtures_path = Pathname.new(__FILE__).parent.join('fixtures')
19
+ @dest = @fixtures_path.join('_site')
20
+ @config_src = @fixtures_path.join('_config.yml')
21
+ @posts_src = @fixtures_path.join('_posts')
22
+ @layouts_src = @fixtures_path.join('_layouts')
23
+ @plugins_src = @fixtures_path.join('_plugins')
24
+
25
+ @site = Jekyll::Site.new(Jekyll.configuration(
26
+ 'source' => @fixtures_path.to_s,
27
+ 'destination' => @dest.to_s,
28
+ 'plugins' => @plugins_src.to_s,
29
+ 'collections' => {
30
+ 'source' => { 'output' => true }
31
+ }
32
+ ))
33
+
34
+ @dest.rmtree if @dest.exist?
35
+ @site.process
36
+ end
37
+
38
+ # config.after(:each) do
39
+ # @dest.rmtree if @dest.exist?
40
+ # end
41
+
42
+ def setup_doc(doc_filename)
43
+ @site.collections['source'].docs.find { |d| d.relative_path.match(doc_filename) }
44
+ end
45
+
46
+ def destination_file_exists?(file)
47
+ File.exist?(File.join(@dest.to_s, file))
48
+ end
49
+
50
+ def destination_file(file)
51
+ File.read(File.join(@dest.to_s, file))
52
+ end
53
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-toc-helpers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Garen Torikian
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jekyll
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: nokogiri
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: ''
84
+ email:
85
+ - gjtorikian@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".travis.yml"
92
+ - Gemfile
93
+ - README.md
94
+ - Rakefile
95
+ - jekyll-toc-helpers.gemspec
96
+ - lib/jekyll-toc-helpers.rb
97
+ - script/bootstrap
98
+ - script/cibuild
99
+ - spec/fixtures/_config.yml
100
+ - spec/fixtures/_data/toc.yml
101
+ - spec/fixtures/_layouts/layout.html
102
+ - spec/fixtures/_source/file1.md
103
+ - spec/fixtures/_source/file2.md
104
+ - spec/fixtures/_source/file3.md
105
+ - spec/fixtures/_source/index.md
106
+ - spec/fixtures/_source/subfile1.md
107
+ - spec/fixtures/_source/subfile2.md
108
+ - spec/jekyll-toc-helpers/prev_next_spec.rb
109
+ - spec/jekyll-toc-helpers/toc_spec.rb
110
+ - spec/spec_helper.rb
111
+ homepage: https://github.com/gjtorikian/jekyll-toc-helpers
112
+ licenses:
113
+ - MIT
114
+ metadata: {}
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 2.2.2
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: Some helper tags for generating TOCs.
135
+ test_files: []