diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..164f481 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,7 @@ +AllCops: + TargetRubyVersion: 2.3 + Exclude: + - 'gemfiles/*' + +Metrics/LineLength: + Max: 100 diff --git a/.travis.yml b/.travis.yml index 0bfc15d..6e8b66a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,6 @@ language: ruby cache: bundler rvm: - - 2.2 - 2.3.7 - 2.4.4 - 2.5.1 diff --git a/Appraisals b/Appraisals index e8f09ef..af92959 100644 --- a/Appraisals +++ b/Appraisals @@ -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 diff --git a/jekyll-toc.gemspec b/jekyll-toc.gemspec index 6b2fc26..2e715d3 100644 --- a/jekyll-toc.gemspec +++ b/jekyll-toc.gemspec @@ -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' diff --git a/lib/jekyll-toc.rb b/lib/jekyll-toc.rb index 439b027..27077eb 100644 --- a/lib/jekyll-toc.rb +++ b/lib/jekyll-toc.rb @@ -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 diff --git a/lib/table_of_contents/parser.rb b/lib/table_of_contents/parser.rb index c0e357c..35d53c4 100644 --- a/lib/table_of_contents/parser.rb +++ b/lib/table_of_contents/parser.rb @@ -11,10 +11,10 @@ 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 = {}) @@ -22,10 +22,10 @@ def initialize(html, options = {}) 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 @@ -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 @@ -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 << %(
  • #{entry[:text]}) diff --git a/lib/version.rb b/lib/version.rb index d9f3f03..a9ee8cf 100644 --- a/lib/version.rb +++ b/lib/version.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module JekyllToc - VERSION = '0.9.0.beta1'.freeze + VERSION = '0.9.0.beta2' end diff --git a/test/test_helper.rb b/test/test_helper.rb index f1e5781..c4069d5 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -7,7 +7,7 @@ require 'jekyll' require 'jekyll-toc' -SIMPLE_HTML = <<-HTML.freeze +SIMPLE_HTML = <<~HTML

    Simple H1

    Simple H2

    Simple H3

    diff --git a/test/test_kramdown_list.rb b/test/test_kramdown_list.rb index 7196703..60019aa 100644 --- a/test/test_kramdown_list.rb +++ b/test/test_kramdown_list.rb @@ -5,15 +5,15 @@ class TestKramdownList < Minitest::Test # NOTE: kramdown automatically injects `id` attribute def test_kramdown_heading - text = <<-MARKDOWN -# h1 + text = <<~MARKDOWN + # h1 -## h2 + ## h2 MARKDOWN - expected = <<-HTML -

    h1

    + expected = <<~HTML +

    h1

    -

    h2

    +

    h2

    HTML actual = Kramdown::Document.new(text).to_html @@ -21,15 +21,15 @@ def test_kramdown_heading end def test_japanese_heading - text = <<-MARKDOWN -# 日本語見出し1 + text = <<~MARKDOWN + # 日本語見出し1 -## 日本語見出し2 + ## 日本語見出し2 MARKDOWN - expected = <<-HTML -

    日本語見出し1

    + expected = <<~HTML +

    日本語見出し1

    -

    日本語見出し2

    +

    日本語見出し2

    HTML actual = Kramdown::Document.new(text).to_html @@ -37,33 +37,33 @@ def test_japanese_heading end def test_kramdown_list_1 - text = <<-MARKDOWN -* level-1 - * level-2 - * level-3 - * level-4 - * level-5 + text = <<~MARKDOWN + * level-1 + * level-2 + * level-3 + * level-4 + * level-5 MARKDOWN - expected = <<-HTML - + expected = <<~HTML + HTML actual = Kramdown::Document.new(text).to_html @@ -71,30 +71,30 @@ def test_kramdown_list_1 end def test_kramdown_list_2 - text = <<-MARKDOWN -* level-1 - * level-3 - * level-2 - * level-4 - * level-5 + text = <<~MARKDOWN + * level-1 + * level-3 + * level-2 + * level-4 + * level-5 MARKDOWN - expected = <<-HTML - + expected = <<~HTML + HTML actual = Kramdown::Document.new(text).to_html @@ -102,16 +102,16 @@ def test_kramdown_list_2 end def test_kramdown_list_3 - text = <<-MARKDOWN - * level-4 - * level-3 - * level-2 -* level-1 + text = <<~MARKDOWN + * level-4 + * level-3 + * level-2 + * level-1 MARKDOWN - expected = <<-HTML -
      * level-4
    -* level-3   * level-2 * level-1
    -
    + expected = <<~HTML +
      * level-4
    +      * level-3   * level-2 * level-1
    +      
    HTML actual = Kramdown::Document.new(text).to_html @@ -119,24 +119,24 @@ def test_kramdown_list_3 end def test_kramdown_list_4 - text = <<-MARKDOWN -* level-1 - * level-4 - * level-3 - * level-2 -* level-1 + text = <<~MARKDOWN + * level-1 + * level-4 + * level-3 + * level-2 + * level-1 MARKDOWN - expected = <<-HTML - + expected = <<~HTML + HTML actual = Kramdown::Document.new(text).to_html @@ -144,22 +144,22 @@ def test_kramdown_list_4 end def test_kramdown_list_5 - text = <<-MARKDOWN -* level-1 - * level-3 - * level-2 -* level-1 + text = <<~MARKDOWN + * level-1 + * level-3 + * level-2 + * level-1 MARKDOWN - expected = <<-HTML - + expected = <<~HTML + HTML actual = Kramdown::Document.new(text).to_html diff --git a/test/test_option_error.rb b/test/test_option_error.rb index 8de4ee8..c469e29 100644 --- a/test/test_option_error.rb +++ b/test/test_option_error.rb @@ -4,10 +4,10 @@ class TestOptionError < Minitest::Test BASE_HTML = '

    h1

    ' - EXPECTED_HTML = <<-HTML - + EXPECTED_HTML = <<~HTML + HTML def test_option_is_nil diff --git a/test/test_various_toc_html.rb b/test/test_various_toc_html.rb index 522718e..0a698b4 100644 --- a/test/test_various_toc_html.rb +++ b/test/test_various_toc_html.rb @@ -4,81 +4,81 @@ class TestVariousTocHtml < Minitest::Test # ref. https://github.com/toshimaru/jekyll-toc/issues/45 - ANGLE_BRACKET_HTML = <<-HTML -

    h1

    -

    <base href>

    -

    & < >

    + ANGLE_BRACKET_HTML = <<~HTML +

    h1

    +

    <base href>

    +

    & < >

    HTML - NO_TOC_HTML = <<-HTML -

    h1

    -

    no_toc h1

    -

    h2

    -

    no_toc h2

    -

    h3

    -

    no_toc h3

    -

    h4

    -

    no_toc h4

    + NO_TOC_HTML = <<~HTML +

    h1

    +

    no_toc h1

    +

    h2

    +

    no_toc h2

    +

    h3

    +

    no_toc h3

    +

    h4

    +

    no_toc h4

    HTML - JAPANESE_HEADINGS_HTML = <<-HTML -

    -

    -

    + JAPANESE_HEADINGS_HTML = <<~HTML +

    +

    +

    HTML - TAGS_INSIDE_HEADINGS_HTML = <<-HTML -

    h2

    -

    h2

    + TAGS_INSIDE_HEADINGS_HTML = <<~HTML +

    h2

    +

    h2

    HTML - TEST_HTML_1 = <<-HTML -

    h1

    -

    h3

    -
    h6
    + TEST_HTML_1 = <<~HTML +

    h1

    +

    h3

    +
    h6
    HTML - TEST_HTML_2 = <<-HTML -

    h1

    -

    h3

    -

    h2

    -
    h6
    + TEST_HTML_2 = <<~HTML +

    h1

    +

    h3

    +

    h2

    +
    h6
    HTML - TEST_HTML_3 = <<-HTML -
    h6
    -
    h5
    -

    h4

    -

    h3

    -

    h2

    -

    h1

    + TEST_HTML_3 = <<~HTML +
    h6
    +
    h5
    +

    h4

    +

    h3

    +

    h2

    +

    h1

    HTML - TEST_HTML_4 = <<-HTML -

    h1

    -

    h3

    -

    h2

    -

    h4

    -
    h5
    + TEST_HTML_4 = <<~HTML +

    h1

    +

    h3

    +

    h2

    +

    h4

    +
    h5
    HTML def test_nested_toc parser = Jekyll::TableOfContents::Parser.new(TEST_HTML_1) doc = Nokogiri::HTML(parser.toc) - expected = <<-HTML - + expected = <<~HTML + HTML actual = doc.css('ul.section-nav').to_s @@ -88,10 +88,10 @@ def test_nested_toc def test_nested_toc_with_min_and_max parser = Jekyll::TableOfContents::Parser.new(TEST_HTML_1, { 'min_level' => 2, 'max_level' => 5 }) doc = Nokogiri::HTML(parser.toc) - expected = <<-HTML - + expected = <<~HTML + HTML actual = doc.css('ul.section-nav').to_s @@ -101,21 +101,21 @@ def test_nested_toc_with_min_and_max def test_complex_nested_toc parser = Jekyll::TableOfContents::Parser.new(TEST_HTML_2) doc = Nokogiri::HTML(parser.toc) - expected = <<-HTML - + expected = <<~HTML + HTML actual = doc.css('ul.section-nav').to_s @@ -125,15 +125,15 @@ def test_complex_nested_toc def test_decremental_headings1 parser = Jekyll::TableOfContents::Parser.new(TEST_HTML_3) doc = Nokogiri::HTML(parser.toc) - expected = <<-HTML - + expected = <<~HTML + HTML actual = doc.css('ul.section-nav').to_s @@ -143,26 +143,26 @@ def test_decremental_headings1 def test_decremental_headings2 parser = Jekyll::TableOfContents::Parser.new(TEST_HTML_4) doc = Nokogiri::HTML(parser.toc) - expected = <<-HTML - + expected = <<~HTML + HTML assert_equal(expected, doc.css('ul.section-nav').to_s) @@ -171,25 +171,25 @@ def test_decremental_headings2 def test_no_toc parser = Jekyll::TableOfContents::Parser.new(NO_TOC_HTML) doc = Nokogiri::HTML(parser.toc) - expected = <<-HTML - + expected = <<~HTML + HTML actual = doc.css('ul.section-nav').to_s @@ -199,20 +199,20 @@ def test_no_toc def test_japanese_toc parser = Jekyll::TableOfContents::Parser.new(JAPANESE_HEADINGS_HTML) doc = Nokogiri::HTML(parser.toc) - expected = <<-HTML - + expected = <<~HTML + HTML actual = doc.css('ul.section-nav').to_s @@ -222,12 +222,12 @@ def test_japanese_toc def test_angle_bracket parser = Jekyll::TableOfContents::Parser.new(ANGLE_BRACKET_HTML) doc = Nokogiri::HTML(parser.toc) - expected = <<-HTML - + expected = <<~HTML + HTML actual = doc.css('ul.section-nav').to_s @@ -237,43 +237,43 @@ def test_angle_bracket def test_tags_inside_heading parser = Jekyll::TableOfContents::Parser.new(TAGS_INSIDE_HEADINGS_HTML) doc = Nokogiri::HTML(parser.toc) - expected = <<-HTML - + expected = <<~HTML + HTML actual = doc.css('ul.section-nav').to_s assert_equal(expected, actual) end - TEST_HTML_IGNORE = <<-HTML -

    h1

    -
    -

    h2

    -
    -

    h3

    -
    h6
    + TEST_HTML_IGNORE = <<~HTML +

    h1

    +
    +

    h2

    +
    +

    h3

    +
    h6
    HTML def test_nested_toc_with_no_toc_section_class parser = Jekyll::TableOfContents::Parser.new(TEST_HTML_IGNORE) doc = Nokogiri::HTML(parser.toc) - expected = <<-HTML - + expected = <<~HTML + HTML actual = doc.css('ul.section-nav').to_s assert_equal(expected, actual) @@ -285,36 +285,36 @@ def test_nested_toc_with_no_toc_section_class assert_includes(html, '

    h2

    ') end - TEST_HTML_IGNORE_2 = <<-HTML -

    h1

    -
    -

    h2

    -
    -

    h3

    -
    -

    h4

    -
    h5
    -
    -
    h6
    + TEST_HTML_IGNORE_2 = <<~HTML +

    h1

    +
    +

    h2

    +
    +

    h3

    +
    +

    h4

    +
    h5
    +
    +
    h6
    HTML def test_nested_toc_with_no_toc_section_class_option parser = Jekyll::TableOfContents::Parser.new(TEST_HTML_IGNORE_2, { 'no_toc_section_class' => 'exclude' }) doc = Nokogiri::HTML(parser.toc) - expected = <<-HTML - + expected = <<~HTML + HTML actual = doc.css('ul.section-nav').to_s assert_equal(expected, actual) @@ -328,21 +328,21 @@ def test_nested_toc_with_no_toc_section_class_option assert_includes(html, '
    h5
    ') end - TEST_EXPLICIT_ID = <<-HTML -

    h1

    -

    h2

    -

    h3

    + TEST_EXPLICIT_ID = <<~HTML +

    h1

    +

    h2

    +

    h3

    HTML def test_toc_with_explicit_id parser = Jekyll::TableOfContents::Parser.new(TEST_EXPLICIT_ID) doc = Nokogiri::HTML(parser.toc) - expected = <<-HTML - + expected = <<~HTML + HTML actual = doc.css('ul.section-nav').to_s assert_equal(expected, actual) @@ -352,26 +352,29 @@ def test_toc_with_explicit_id assert_includes(html, %( - HTML + def test_custom_css_classes + parser = Jekyll::TableOfContents::Parser.new( + TEST_HTML_1, + { 'item_class' => 'custom-item', 'list_class' => 'custom-list', 'sublist_class' => 'custom-sublist', 'item_prefix' => 'custom-prefix-' } + ) + doc = Nokogiri::HTML(parser.toc) + expected = <<~HTML + + HTML - assert_equal(expected, doc.css('ul.custom-list').to_s) + assert_equal(expected, doc.css('ul.custom-list').to_s) + end end