diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index d6ce471..819e7c2 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -7,6 +7,10 @@ For a detailed view of what has changed, refer to the {uri-repo}/commits/master[ == Unreleased +=== Added + +* Add :nested_list_marker_indent API option and --nested-list-marker-indent CLI option to control how many spaces to indent nested list markers per indent level + === Fixed * Convert language tag on source block to lowercase (#106) diff --git a/lib/kramdown-asciidoc/api.rb b/lib/kramdown-asciidoc/api.rb index 11d5960..5650831 100644 --- a/lib/kramdown-asciidoc/api.rb +++ b/lib/kramdown-asciidoc/api.rb @@ -27,6 +27,7 @@ module AsciiDoc # @option opts [Symbol] :wrap (:preserve) the line wrapping behavior to apply (:preserve, :ventilate, or :none). # @option opts [Integer] :heading_offset (0) the heading offset to apply to heading levels. # @option opts [Boolean] :auto_links (true) whether to allow raw URLs to be recognized as links. + # @option opts [Integer] :nested_list_marker_indent (1) how many spaces to indent nested list markers per indent level. # @option opts [Hash] :attributes ({}) additional AsciiDoc attributes to add to header of output document; reserved # attributes, like stem, may be overridden; some attributes may impact conversion, such as idprefix and idseparator # @option opts [String] :imagesdir (nil) the prefix to remove from image references found in the Markdown document; @@ -85,6 +86,7 @@ def self.convert markdown, opts = {} # @option opts [Symbol] :wrap (:preserve) the line wrapping behavior to apply (:preserve, :ventilate, or :none). # @option opts [Integer] :heading_offset (0) the heading offset to apply to heading levels. # @option opts [Boolean] :auto_links (true) whether to allow raw URLs to be recognized as links. + # @option opts [Integer] :nested_list_marker_indent (1) how many spaces to indent nested list markers per indent level. # @option opts [Hash] :attributes ({}) additional AsciiDoc attributes to add to header of output document; reserved # attributes, like stem, may be overridden; some attributes may impact conversion, such as idprefix and idseparator # @option opts [String] :imagesdir (nil) the prefix to remove from image references found in the Markdown document; diff --git a/lib/kramdown-asciidoc/cli.rb b/lib/kramdown-asciidoc/cli.rb index 3cfbc83..4fcf672 100644 --- a/lib/kramdown-asciidoc/cli.rb +++ b/lib/kramdown-asciidoc/cli.rb @@ -75,6 +75,10 @@ def parse args options[:auto_links] = auto_links end + opts.on '--nested-list-marker-indent=NUMBER', ::Integer, 'Set how many spaces to indent nested list markers per indent level (default: 1)' do |nested_list_marker_indent| + options[:nested_list_marker_indent] = nested_list_marker_indent + end + opts.on '-h', '--help', 'Display this help text and exit' do $stdout.write opts.help return 0 diff --git a/lib/kramdown-asciidoc/converter.rb b/lib/kramdown-asciidoc/converter.rb index 19635fa..f20013f 100644 --- a/lib/kramdown-asciidoc/converter.rb +++ b/lib/kramdown-asciidoc/converter.rb @@ -92,6 +92,7 @@ def initialize root, opts @ids_seen = {} @footnote_ids = ::Set.new @auto_links = opts.fetch :auto_links, true + @nested_list_marker_indent = [opts[:nested_list_marker_indent] || 1, 0].max @diagram_languages = opts[:diagram_languages] || %w(plantuml mermaid) @heading_offset = opts[:heading_offset] || 0 @imagesdir = opts[:imagesdir] || @attributes['imagesdir'] @@ -338,7 +339,7 @@ def convert_li el, opts remaining = children primary_lines = ['{blank}'] end - primary_lines.unshift %(#{indent > 0 ? ' ' * indent : ''}#{marker * level} #{primary_lines.shift}) + primary_lines.unshift %(#{indent > 0 ? ' ' * (indent * @nested_list_marker_indent) : ''}#{marker * level} #{primary_lines.shift}) writer.add_lines primary_lines return if remaining.empty? if remaining.find {|n| (type = n.type) == :blank ? nil : ((BLOCK_TYPES.include? type) ? true : break) } diff --git a/spec/cli_spec.rb b/spec/cli_spec.rb index 5a4adab..83a417f 100644 --- a/spec/cli_spec.rb +++ b/spec/cli_spec.rb @@ -201,6 +201,34 @@ (expect $stdout.string).to eql expected end + it 'does not indent ul list markers with --nested-list-marker-indent set to 0' do + the_source_file = scenario_file 'ul/nested-0-indent.md' + expected = File.read (scenario_file 'ul/nested-0-indent.adoc'), mode: 'rb' + (expect subject.run %W(-o - --nested-list-marker-indent=0 #{the_source_file})).to eql 0 + (expect $stdout.string).to eql expected + end + + it 'does not indent ol list markers with --nested-list-marker-indent set to 0' do + the_source_file = scenario_file 'ol/nested-0-indent.md' + expected = File.read (scenario_file 'ol/nested-0-indent.adoc'), mode: 'rb' + (expect subject.run %W(-o - --nested-list-marker-indent=0 #{the_source_file})).to eql 0 + (expect $stdout.string).to eql expected + end + + it 'does indent ul list markers according to --nested-list-marker-indent configuration' do + the_source_file = scenario_file 'ul/nested-3-indent.md' + expected = File.read (scenario_file 'ul/nested-3-indent.adoc'), mode: 'rb' + (expect subject.run %W(-o - --nested-list-marker-indent=3 #{the_source_file})).to eql 0 + (expect $stdout.string).to eql expected + end + + it 'does indent ol list markers according to --nested-list-marker-indent configuration' do + the_source_file = scenario_file 'ol/nested-3-indent.md' + expected = File.read (scenario_file 'ol/nested-3-indent.adoc'), mode: 'rb' + (expect subject.run %W(-o - --nested-list-marker-indent=3 #{the_source_file})).to eql 0 + (expect $stdout.string).to eql expected + end + it 'shifts headings by offset when --heading-offset is used' do the_source_file = scenario_file 'heading/offset.md' expected = File.read (scenario_file 'heading/offset.adoc'), mode: 'rb' diff --git a/spec/scenarios/ol/nested-0-indent.adoc b/spec/scenarios/ol/nested-0-indent.adoc new file mode 100644 index 0000000..47d21ec --- /dev/null +++ b/spec/scenarios/ol/nested-0-indent.adoc @@ -0,0 +1,11 @@ +. bread +.. french country +.. sourdough +.. multigrain +. milk +.. 2% +... whole +.. skim +. eggs +.. brown +.. white diff --git a/spec/scenarios/ol/nested-0-indent.md b/spec/scenarios/ol/nested-0-indent.md new file mode 100644 index 0000000..843001e --- /dev/null +++ b/spec/scenarios/ol/nested-0-indent.md @@ -0,0 +1,11 @@ +1. bread + 1. french country + 2. sourdough + 3. multigrain +2. milk + 1. 2% + 1. whole + 2. skim +3. eggs + 1. brown + 2. white diff --git a/spec/scenarios/ol/nested-0-indent.opts b/spec/scenarios/ol/nested-0-indent.opts new file mode 100644 index 0000000..a501dc2 --- /dev/null +++ b/spec/scenarios/ol/nested-0-indent.opts @@ -0,0 +1 @@ +:nested_list_marker_indent: 0 diff --git a/spec/scenarios/ol/nested-3-indent.adoc b/spec/scenarios/ol/nested-3-indent.adoc new file mode 100644 index 0000000..6e410ba --- /dev/null +++ b/spec/scenarios/ol/nested-3-indent.adoc @@ -0,0 +1,11 @@ +. bread + .. french country + .. sourdough + .. multigrain +. milk + .. 2% + ... whole + .. skim +. eggs + .. brown + .. white diff --git a/spec/scenarios/ol/nested-3-indent.md b/spec/scenarios/ol/nested-3-indent.md new file mode 100644 index 0000000..843001e --- /dev/null +++ b/spec/scenarios/ol/nested-3-indent.md @@ -0,0 +1,11 @@ +1. bread + 1. french country + 2. sourdough + 3. multigrain +2. milk + 1. 2% + 1. whole + 2. skim +3. eggs + 1. brown + 2. white diff --git a/spec/scenarios/ol/nested-3-indent.opts b/spec/scenarios/ol/nested-3-indent.opts new file mode 100644 index 0000000..f32e5c7 --- /dev/null +++ b/spec/scenarios/ol/nested-3-indent.opts @@ -0,0 +1 @@ +:nested_list_marker_indent: 3 diff --git a/spec/scenarios/ul/nested-0-indent.adoc b/spec/scenarios/ul/nested-0-indent.adoc new file mode 100644 index 0000000..8f387e8 --- /dev/null +++ b/spec/scenarios/ul/nested-0-indent.adoc @@ -0,0 +1,11 @@ +* bread +** french country +** sourdough +** multigrain +* milk +** 2% +*** whole +** skim +* eggs +** brown +** white diff --git a/spec/scenarios/ul/nested-0-indent.md b/spec/scenarios/ul/nested-0-indent.md new file mode 100644 index 0000000..ca69e8a --- /dev/null +++ b/spec/scenarios/ul/nested-0-indent.md @@ -0,0 +1,11 @@ +- bread + - french country + - sourdough + - multigrain +- milk + - 2% + - whole + - skim +- eggs + - brown + - white diff --git a/spec/scenarios/ul/nested-0-indent.opts b/spec/scenarios/ul/nested-0-indent.opts new file mode 100644 index 0000000..a501dc2 --- /dev/null +++ b/spec/scenarios/ul/nested-0-indent.opts @@ -0,0 +1 @@ +:nested_list_marker_indent: 0 diff --git a/spec/scenarios/ul/nested-3-indent.adoc b/spec/scenarios/ul/nested-3-indent.adoc new file mode 100644 index 0000000..ddac07c --- /dev/null +++ b/spec/scenarios/ul/nested-3-indent.adoc @@ -0,0 +1,11 @@ +* bread + ** french country + ** sourdough + ** multigrain +* milk + ** 2% + *** whole + ** skim +* eggs + ** brown + ** white diff --git a/spec/scenarios/ul/nested-3-indent.md b/spec/scenarios/ul/nested-3-indent.md new file mode 100644 index 0000000..ca69e8a --- /dev/null +++ b/spec/scenarios/ul/nested-3-indent.md @@ -0,0 +1,11 @@ +- bread + - french country + - sourdough + - multigrain +- milk + - 2% + - whole + - skim +- eggs + - brown + - white diff --git a/spec/scenarios/ul/nested-3-indent.opts b/spec/scenarios/ul/nested-3-indent.opts new file mode 100644 index 0000000..f32e5c7 --- /dev/null +++ b/spec/scenarios/ul/nested-3-indent.opts @@ -0,0 +1 @@ +:nested_list_marker_indent: 3