Skip to content

Commit

Permalink
Ordered list support (#122)
Browse files Browse the repository at this point in the history
* Use LF instead CRLF

* Simplify list_tag method and memorize the result

* Use ordered_list instead of use_ordered_list

* ordered_list instead of use_ordered_list

* Place ordered_list after toc_levels

* Simplify list_tag
  • Loading branch information
toshimaru authored Oct 13, 2020
1 parent d6c4d04 commit a74a637
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 99 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# jekyll-toc

![CI](https://github.com/toshimaru/jekyll-toc/workflows/CI/badge.svg)
[![Gem Version](https://badge.fury.io/rb/jekyll-toc.svg)](http://badge.fury.io/rb/jekyll-toc)
[![Gem Version](https://badge.fury.io/rb/jekyll-toc.svg)](https://badge.fury.io/rb/jekyll-toc)
[![Code Climate](https://codeclimate.com/github/toshimaru/jekyll-toc/badges/gpa.svg)](https://codeclimate.com/github/toshimaru/jekyll-toc)
[![Test Coverage](https://api.codeclimate.com/v1/badges/cd56b207f327603662a1/test_coverage)](https://codeclimate.com/github/toshimaru/jekyll-toc/test_coverage)

Expand Down Expand Up @@ -147,12 +147,12 @@ jekyll-toc generates an unordered list. The HTML output is as follows.
toc:
min_level: 1
max_level: 6
ordered_list: false
no_toc_section_class: no_toc_section
list_class: section-nav
sublist_class: ''
item_class: toc-entry
item_prefix: toc-
use_ordered_list: false
```

## Customization
Expand Down Expand Up @@ -255,7 +255,7 @@ This can be configured in `_config.yml`:

```yml
toc:
use_ordered_list: true # default is false
ordered_list: true # default is false
```

In order to change the list type, use the [list-style-type](https://developer.mozilla.org/en-US/docs/Web/CSS/list-style-type) css property.
Expand All @@ -265,7 +265,7 @@ Example

```yml
toc:
use_ordered_list: true
ordered_list: true
list_class: my-list-class
sublist_class: my-sublist-class
```
Expand All @@ -282,4 +282,4 @@ toc:

This will produce:

![screenshot](https://user-images.githubusercontent.com/7675276/85813980-a0ea5a80-b719-11ea-9458-ccf9b86a778b.png)
![screenshot](https://user-images.githubusercontent.com/7675276/85813980-a0ea5a80-b719-11ea-9458-ccf9b86a778b.png)
11 changes: 5 additions & 6 deletions lib/table_of_contents/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,31 @@ module Jekyll
module TableOfContents
# jekyll-toc configuration class
class Configuration
attr_reader :toc_levels, :no_toc_class, :no_toc_section_class,
:list_class, :sublist_class, :item_class, :item_prefix,
:use_ordered_list
attr_reader :toc_levels, :no_toc_class, :ordered_list, :no_toc_section_class,
:list_class, :sublist_class, :item_class, :item_prefix

DEFAULT_CONFIG = {
'min_level' => 1,
'max_level' => 6,
'ordered_list' => false,
'no_toc_section_class' => 'no_toc_section',
'list_class' => 'section-nav',
'sublist_class' => '',
'item_class' => 'toc-entry',
'item_prefix' => 'toc-',
'use_ordered_list' => false
'item_prefix' => 'toc-'
}.freeze

def initialize(options)
options = generate_option_hash(options)

@toc_levels = options['min_level']..options['max_level']
@ordered_list = options['ordered_list']
@no_toc_class = 'no_toc'
@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']
@use_ordered_list = options['use_ordered_list']
end

private
Expand Down
11 changes: 2 additions & 9 deletions lib/table_of_contents/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ module TableOfContents
class Parser
include ::Jekyll::TableOfContents::Helper

ORDERED_LIST_HTML_TAG = 'ol'
UNORDERED_LIST_HTML_TAG = 'ul'

def initialize(html, options = {})
@doc = Nokogiri::HTML::DocumentFragment.parse(html)
@configuration = Configuration.new(options)
Expand Down Expand Up @@ -110,7 +107,7 @@ def toc_headings
end

def toc_headings_in_no_toc_section
if @configuration.no_toc_section_class.is_a? Array
if @configuration.no_toc_section_class.is_a?(Array)
@configuration.no_toc_section_class.map { |cls| toc_headings_within(cls) }.join(',')
else
toc_headings_within(@configuration.no_toc_section_class)
Expand All @@ -125,12 +122,8 @@ def ul_attributes
@ul_attributes ||= @configuration.sublist_class.empty? ? '' : %( class="#{@configuration.sublist_class}")
end

def use_ordered_list?
@configuration.use_ordered_list
end

def list_tag
use_ordered_list? ? ORDERED_LIST_HTML_TAG : UNORDERED_LIST_HTML_TAG
@list_tag ||= @configuration.ordered_list ? 'ol' : 'ul'
end
end
end
Expand Down
152 changes: 76 additions & 76 deletions test/parser/test_ordered_list.rb
Original file line number Diff line number Diff line change
@@ -1,76 +1,76 @@
# frozen_string_literal: true

require 'test_helper'

class TestOrderedList < Minitest::Test
include TestHelpers

def test_default_configuration
configuration = Jekyll::TableOfContents::Configuration.new({})

assert_equal configuration.use_ordered_list, false
end

def test_disabled_ordered_list
configuration = Jekyll::TableOfContents::Configuration.new('use_ordered_list' => false)

assert_equal configuration.use_ordered_list, false
end

def test_enabled_ordered_list
configuration = Jekyll::TableOfContents::Configuration.new('use_ordered_list' => true)

assert_equal configuration.use_ordered_list, true
end

def test_basic_ordered_list_top_heading
parse_with_ordered_list
html = @parser.toc

assert_match(/^<ol class="section-nav">/, html)
end

def test_ordered_list_sub_headings
parse_with_ordered_list
html = @parser.toc

assert_match(/<ol>\n<li class="toc-entry/, html)
end

def test_ordered_list_top_heading_with_classes
parse_with_ordered_list_and_classes
html = @parser.toc

assert_match(/^<ol class="top-list-class">/, html)
end

def test_ordered_list_sub_headings_with_classes
parse_with_ordered_list_and_classes
html = @parser.toc

assert_match(/<ol class="sublist-class">/, html)
end

def test_ordered_list_subheadings_with_classes_nested_structure
parse_with_ordered_list_and_classes
html = @parser.toc

occurrences = html.scan(/<ol class="sublist-class">/).count

assert_equal occurrences, 5
end
private

def parse_with_ordered_list
read_html_and_create_parser('use_ordered_list' => true)
end

def parse_with_ordered_list_and_classes
read_html_and_create_parser(
'use_ordered_list' => true,
'list_class' => 'top-list-class',
'sublist_class' => 'sublist-class'
)
end
end
# frozen_string_literal: true

require 'test_helper'

class TestOrderedList < Minitest::Test
include TestHelpers

def test_default_configuration
configuration = Jekyll::TableOfContents::Configuration.new({})

assert_equal configuration.ordered_list, false
end

def test_disabled_ordered_list
configuration = Jekyll::TableOfContents::Configuration.new('ordered_list' => false)

assert_equal configuration.ordered_list, false
end

def test_enabled_ordered_list
configuration = Jekyll::TableOfContents::Configuration.new('ordered_list' => true)

assert_equal configuration.ordered_list, true
end

def test_basic_ordered_list_top_heading
parse_with_ordered_list
html = @parser.toc

assert_match(/^<ol class="section-nav">/, html)
end

def test_ordered_list_sub_headings
parse_with_ordered_list
html = @parser.toc

assert_match(/<ol>\n<li class="toc-entry/, html)
end

def test_ordered_list_top_heading_with_classes
parse_with_ordered_list_and_classes
html = @parser.toc

assert_match(/^<ol class="top-list-class">/, html)
end

def test_ordered_list_sub_headings_with_classes
parse_with_ordered_list_and_classes
html = @parser.toc

assert_match(/<ol class="sublist-class">/, html)
end

def test_ordered_list_subheadings_with_classes_nested_structure
parse_with_ordered_list_and_classes
html = @parser.toc

occurrences = html.scan(/<ol class="sublist-class">/).count

assert_equal occurrences, 5
end

private

def parse_with_ordered_list
read_html_and_create_parser('ordered_list' => true)
end

def parse_with_ordered_list_and_classes
read_html_and_create_parser(
'ordered_list' => true,
'list_class' => 'top-list-class',
'sublist_class' => 'sublist-class'
)
end
end
6 changes: 3 additions & 3 deletions test/test_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@
require 'test_helper'

class TestConfiguration < Minitest::Test
def test_default_conf1guration
def test_default_configuration
configuration = Jekyll::TableOfContents::Configuration.new({})

assert_equal configuration.toc_levels, 1..6
assert_equal configuration.ordered_list, false
assert_equal configuration.no_toc_section_class, 'no_toc_section'
assert_equal configuration.list_class, 'section-nav'
assert_equal configuration.sublist_class, ''
assert_equal configuration.item_class, 'toc-entry'
assert_equal configuration.item_prefix, 'toc-'
assert_equal configuration.use_ordered_list, false
end

def test_type_error
configuration = Jekyll::TableOfContents::Configuration.new('TypeError!')

assert_equal configuration.toc_levels, 1..6
assert_equal configuration.ordered_list, false
assert_equal configuration.no_toc_section_class, 'no_toc_section'
assert_equal configuration.list_class, 'section-nav'
assert_equal configuration.sublist_class, ''
assert_equal configuration.item_class, 'toc-entry'
assert_equal configuration.item_prefix, 'toc-'
assert_equal configuration.use_ordered_list, false
end
end

0 comments on commit a74a637

Please sign in to comment.