Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require Ruby 2.3+ / Nokogiri 1.8+ #63

Merged
merged 11 commits into from
Oct 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
AllCops:
TargetRubyVersion: 2.3
Exclude:
- 'gemfiles/*'

Metrics/LineLength:
Max: 100
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: ruby
cache: bundler
rvm:
- 2.2
- 2.3.7
- 2.4.4
- 2.5.1
Expand Down
18 changes: 10 additions & 8 deletions Appraisals
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
appraise "jekyll-3.8" do
gem "jekyll", "3.8"
# frozen_string_literal: true

appraise 'jekyll-3.8' do
gem 'jekyll', '3.8'
end

appraise "jekyll-3.7" do
gem "jekyll", "3.7"
appraise 'jekyll-3.7' do
gem 'jekyll', '3.7'
end

appraise "jekyll-3.6" do
gem "jekyll", "3.6"
appraise 'jekyll-3.6' do
gem 'jekyll', '3.6'
end

appraise "jekyll-3.5" do
gem "jekyll", "3.5"
appraise 'jekyll-3.5' do
gem 'jekyll', '3.5'
end
4 changes: 2 additions & 2 deletions jekyll-toc.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ Gem::Specification.new do |spec|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ['lib']

spec.required_ruby_version = '>= 2.2.2'
spec.required_ruby_version = '>= 2.3'

spec.add_runtime_dependency 'nokogiri', '~> 1.7'
spec.add_runtime_dependency 'nokogiri', '~> 1.8'

spec.add_development_dependency 'appraisal'
spec.add_development_dependency 'codeclimate-test-reporter', '~> 1.0'
Expand Down
18 changes: 11 additions & 7 deletions lib/jekyll-toc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,31 @@
require 'table_of_contents/parser'

module Jekyll
class TocTag < Liquid::Tag
def render(context)
return unless context.registers[:page]['toc'] == true
content_html = context.registers[:page].content
::Jekyll::TableOfContents::Parser.new(content_html).build_toc
end
end
# class TocTag < Liquid::Tag
# def render(context)
# return unless context.registers[:page]['toc']
#
# content_html = context.registers[:page].content
# ::Jekyll::TableOfContents::Parser.new(content_html).build_toc
# end
# end

module TableOfContentsFilter
def toc_only(html)
return html unless toc_enabled?

::Jekyll::TableOfContents::Parser.new(html, toc_config).build_toc
end

def inject_anchors(html)
return html unless toc_enabled?

::Jekyll::TableOfContents::Parser.new(html, toc_config).inject_anchors_into_html
end

def toc(html)
return html unless toc_enabled?

::Jekyll::TableOfContents::Parser.new(html, toc_config).toc
end

Expand Down
22 changes: 11 additions & 11 deletions lib/table_of_contents/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ class Parser
'no_toc_section_class' => 'no_toc_section',
'min_level' => 1,
'max_level' => 6,
"list_class" => "section-nav",
"sublist_class" => "",
"item_class" => "toc-entry",
"item_prefix" => "toc-"
'list_class' => 'section-nav',
'sublist_class' => '',
'item_class' => 'toc-entry',
'item_prefix' => 'toc-'
}.freeze

def initialize(html, options = {})
@doc = Nokogiri::HTML::DocumentFragment.parse(html)
options = generate_option_hash(options)
@toc_levels = options['min_level']..options['max_level']
@no_toc_section_class = options['no_toc_section_class']
@list_class = options["list_class"]
@sublist_class = options["sublist_class"]
@item_class = options["item_class"]
@item_prefix = options["item_prefix"]
@list_class = options['list_class']
@sublist_class = options['sublist_class']
@item_class = options['item_class']
@item_prefix = options['item_prefix']
@entries = parse_content
end

Expand Down Expand Up @@ -61,7 +61,7 @@ def parse_content
.gsub(PUNCTUATION_REGEXP, '') # remove punctuation
.tr(' ', '-') # replace spaces with dash

uniq = headers[id] > 0 ? "-#{headers[id]}" : ''
uniq = headers[id].positive? ? "-#{headers[id]}" : ''
headers[id] += 1
header_content = node.children.first
next entries unless header_content
Expand All @@ -80,12 +80,12 @@ def parse_content
# Returns the list items for entries
def build_toc_list(entries)
i = 0
toc_list = ''.dup
toc_list = +''
min_h_num = entries.map { |e| e[:h_num] }.min

while i < entries.count
entry = entries[i]
ul_attributes = @sublist_class.empty? ? "" : %( class="#{@sublist_class}")
ul_attributes = @sublist_class.empty? ? '' : %( class="#{@sublist_class}")
if entry[:h_num] == min_h_num
# If the current entry should not be indented in the list, add the entry to the list
toc_list << %(<li class="#{@item_class} #{@item_prefix}#{entry[:node_name]}"><a href="##{entry[:id]}#{entry[:uniq]}">#{entry[:text]}</a>)
Expand Down
4 changes: 3 additions & 1 deletion lib/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module JekyllToc
VERSION = '0.9.0.beta1'.freeze
VERSION = '0.9.0.beta2'
end
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
require 'jekyll'
require 'jekyll-toc'

SIMPLE_HTML = <<-HTML.freeze
SIMPLE_HTML = <<~HTML
<h1>Simple H1</h1>
<h2>Simple H2</h2>
<h3>Simple H3</h3>
Expand Down
Loading