1 # SPDX-License-Identifier: MIT
2 require 'asciidoctor/extensions' unless RUBY_ENGINE == 'opal'
6 # An extension that converts a list of lines to an inline Oxford comma-separated list.
17 class CommaizeBlock < Extensions::BlockProcessor
22 on_contexts :paragraph, :open
23 # XXX What's the difference between text, raw, simple, verbatim, etc?
24 parse_content_as :simple
26 def process(parent, reader, attrs)
28 sort = attrs.fetch('sort', 'true') == 'true'
30 lines = lines.reject(&:empty?)
31 lines = lines.map(&:strip)
32 lines = lines.sort_by(&:downcase) if sort
35 create_paragraph parent, lines, attrs
36 elsif lines.length == 2
37 create_paragraph parent, lines.join(" and "), attrs
39 commaized = lines[0..-2].join(", ")
40 commaized << ", and " + lines[-1]
41 create_paragraph parent, commaized, attrs