diff options
Diffstat (limited to 'doc/asciidoctor-macros/commaize-block')
-rw-r--r-- | doc/asciidoctor-macros/commaize-block/extension.rb | 44 | ||||
-rw-r--r-- | doc/asciidoctor-macros/commaize-block/sample.adoc | 31 |
2 files changed, 75 insertions, 0 deletions
diff --git a/doc/asciidoctor-macros/commaize-block/extension.rb b/doc/asciidoctor-macros/commaize-block/extension.rb new file mode 100644 index 00000000..710f1a7e --- /dev/null +++ b/doc/asciidoctor-macros/commaize-block/extension.rb @@ -0,0 +1,44 @@ +# 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 + include WsUtils + 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 diff --git a/doc/asciidoctor-macros/commaize-block/sample.adoc b/doc/asciidoctor-macros/commaize-block/sample.adoc new file mode 100644 index 00000000..9cb2e1ec --- /dev/null +++ b/doc/asciidoctor-macros/commaize-block/sample.adoc @@ -0,0 +1,31 @@ += Sorted, delimited, empty lines + +[commaize] +-- +One +two + +red + +blue +Fish +-- + += Unsorted + +[commaize, sort=false] +One +two +red +blue +Fish + += Two elements + +[commaize] +One +Two + += One element +[commaize] +Just the one |