module AsciidoctorBibtex::StringUtils

Public Class Methods

combine_consecutive_numbers(str) click to toggle source

Merge consecutive number so that “1,2,3,5” becomes “1-3,5”

# File lib/asciidoctor-bibtex/string_utils.rb, line 31
def self.combine_consecutive_numbers(str)
  nums = str.split(',').collect(&:strip)
  res = ''
  # Loop through ranges
  start_range = 0
  while start_range < nums.length
    end_range = start_range
    while (end_range < nums.length - 1) &&
          is_i?(nums[end_range]) &&
          is_i?(nums[end_range + 1]) &&
          (nums[end_range + 1].to_i == nums[end_range].to_i + 1)
      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
html_to_asciidoc(s) click to toggle source

Converts html output produced by citeproc to asciidoc markup

# File lib/asciidoctor-bibtex/string_utils.rb, line 17
def self.html_to_asciidoc(s)
  s = s.gsub(%r{</?i>}, '_')
  s = s.gsub(%r{</?b>}, '*')
  s = s.gsub(%r{</?span.*?>}, '')
  s = s.gsub(/\{|\}/, '')
  s
end
is_i?(s) click to toggle source

Provides a check that a string is in integer

# File lib/asciidoctor-bibtex/string_utils.rb, line 26
def self.is_i?(s)
  !!(s =~ /^[-+]?[0-9]+$/)
end