Module: AsciidoctorBibtex::StringUtils

Defined in:
lib/asciidoctor-bibtex/string_utils.rb

Class Method Summary collapse

Class Method Details

.combine_consecutive_numbers(str) ⇒ Object

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



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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) ⇒ Object

Converts html output produced by citeproc to asciidoc markup



17
18
19
20
21
22
23
# 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) ⇒ Boolean

Provides a check that a string is in integer

Returns:

  • (Boolean)


26
27
28
# File 'lib/asciidoctor-bibtex/string_utils.rb', line 26

def self.is_i?(s)
  !!(s =~ /^[-+]?[0-9]+$/)
end