Module: AsciidocBib::ProcessorUtils
- Included in:
- Processor
- Defined in:
- lib/asciidoc-bib/processorutils.rb
Instance Method Summary collapse
-
#combine_consecutive_numbers(str) ⇒ Object
Used with numeric styles to combine consecutive numbers into ranges.
Instance Method Details
#combine_consecutive_numbers(str) ⇒ Object
Used with numeric styles to combine consecutive numbers into ranges
combine_consecutive_numbers("1,2,3") # -> "1-3"
combine_consecutive_numbers("1,2,3,6,7,8,9,12") # -> "1-3,6-9,12"
Leaves references with page numbers alone.
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 |
# File 'lib/asciidoc-bib/processorutils.rb', line 12 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 |