aboutsummaryrefslogtreecommitdiffstats
path: root/docbook/asciidoctor-macros/commaize-block/extension.rb
blob: ae8fd305689e181b7f06061f46e39bf16d5cdc2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# SPDX-License-Identifier: MIT
require 'asciidoctor/extensions' unless RUBY_ENGINE == 'opal'

include Asciidoctor

# An extension that converts a list of lines to an inline Oxford comma-separated list.
#
# Usage
#
#   [commaize]
#   --
#   item1
#   item2
#   item3
#   --
#
class CommaizeBlock < Extensions::BlockProcessor
  use_dsl

  named :commaize
  on_contexts :paragraph, :open
  # XXX What's the difference between text, raw, simple, verbatim, etc?
  parse_content_as :simple

  def process(parent, reader, attrs)
    lines = reader.lines
    sort = attrs.fetch('sort', 'true') == 'true'

    lines = lines.reject(&:empty?)
    lines = lines.map(&:strip)
    lines = lines.sort_by(&:downcase) if sort

    if lines.length < 2
      create_paragraph parent, lines, attrs
    elsif lines.length == 2
      create_paragraph parent, lines.join(" and "), attrs
    else
      commaized = lines[0..-2].join(", ")
      commaized << ", and " + lines[-1]
      create_paragraph parent, commaized, attrs
    end
  end
end