Sha256: efba6768d974f48614cf34ceb6816099f233c33ce786adb9daabf4aa8b44e3e0

Contents?: true

Size: 1.02 KB

Versions: 2

Compression:

Stored size: 1.02 KB

Contents

module AsciidoctorBibliography
  module ProcessorUtils
    # Used with numeric styles to combine consecutive numbers into ranges
    # e.g. 1,2,3 -> 1-3, or 1,2,3,6,7,8,9,12 -> 1-3,6-9,12
    # leave references with page numbers alone
    def combine_consecutive_numbers str
      nums = str.split(",").collect(&:strip)
      res = ""
      # Loop through ranges
      start_range = 0
      while start_range < nums.length do
        end_range = start_range
        while (end_range < nums.length-1 and
               nums[end_range].is_i? and
               nums[end_range+1].is_i? and
               nums[end_range+1].to_i == nums[end_range].to_i + 1) do
          end_range += 1
        end
        if end_range - start_range >= 2
          res += "#{nums[start_range]}-#{nums[end_range]}, "
        else
          start_range.upto(end_range) do |i|
            res += "#{nums[i]}, "
          end
        end
        start_range = end_range + 1
      end
      # finish by removing last comma
      res.gsub(/, $/, '')
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
asciidoctor-bibliography-0.1 deprecated/asciidoctor-bibliography/processorutils.rb
asciidoctor-bibliography-0.0.1.dev deprecated/asciidoctor-bibliography/processorutils.rb