Class: CiteProc::Number

Inherits:
Variable show all
Defined in:
lib/citeproc/number.rb

Overview

A CiteProc Variable used for numeric values.

Constant Summary collapse

MAX_ROMAN =
5000
FACTORS =
[
  ['m', 1000], ['cm', 900], ['d', 500], ['cd', 400],
  ['c',  100], ['xc',  90], ['l',  50], ['xl',  40],
  ['x',   10], ['ix',   9], ['v',   5], ['iv',   4],
  ['i',    1]
].freeze
RANGE_BOUND =

Numbers (with optional prefixes and suffixes) or roman numerals

/(?:[[:alnum:]]*\d[[:alnum:]]*|[ivxlcdm]+)/i
RANGE =

Ranges between numbers; escaped hyphens are not range delimiters

/(?<![[:alnum:]])#{RANGE_BOUND}\s*[–-]\s*#{RANGE_BOUND}(?![[:alnum:]])/
PAGE_RANGE =
/([[:alnum:]]+)\s*[–-]+\s*([[:alnum:]]+)/
PAGE =

A page number with optional prefix and suffix

/\A(\d*[[:alpha:]]+)?(\d+)([[:alpha:]]*)\z/
ROMAN =
/\A[ivxlcdm]+\z/i

Instance Attribute Summary

Attributes inherited from Variable

#value

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Variable

create, create!, #date?, #eql?, #hash, #initialize, #initialize_copy, #inspect, #numeric?, #plural?, #replace, #romanize, #strip_markup, #strip_markup!, #to_f, #to_i, #to_json, #to_s, #tokenize, #type

Constructor Details

This class inherits a constructor from CiteProc::Variable

Class Method Details

.format_page_range(pages, format = nil, delimiter = '–') ⇒ String?

Formats the page ranges in pages according to format:

  • "chicago-15" (or "chicago") and "chicago-16": page ranges are abbreviated according to the Chicago Manual of Style rules.
  • "expanded": Abbreviated page ranges are expanded to their non-abbreviated form: 42-45, 321-328, 2787-2816.
  • "minimal": All digits repeated in the second number are left out: 42-45, 321-8, 2787-816.
  • "minimal-two": As "minimal", but at least two digits are kept in the second number: 42-45, 321-28, 2787-816.

Without a format only the range delimiter is replaced. Ranges with different prefixes (e.g., "N110-5") keep a plain hyphen; escaped hyphens ("-") are not range delimiters.

Parameters:

  • pages (String) —

    the pages to format

  • format (String, nil) (defaults to: nil) —

    the page range format

  • delimiter (String) (defaults to: '–') —

    the range delimiter

Returns:

  • (String, nil) —

    the formatted pages



67
68
69
70
71
72
73
# File 'lib/citeproc/number.rb', line 67

def format_page_range(pages, format = nil, delimiter = '–')
  return if pages.nil?

  pages.to_s
    .gsub(PAGE_RANGE) { format_page_bounds($1, $2, format, delimiter) || "#{$1}-#{$2}" }
    .gsub('\\-', '-')
end

.pluralize?(string) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/citeproc/number.rb', line 30

def pluralize?(string)
  /\S[\s,&]\S|\df/.match?(string) || RANGE.match?(string)
end

.romanize(number) ⇒ String, Integer

Returns roman equivalent of the passed-in number; the number itself if it is out of range.

Parameters:

  • number (#to_i) —

    the number to convert

Returns:

  • (String, Integer) —

    roman equivalent of the passed-in number; the number itself if it is out of range



37
38
39
40
41
42
43
44
45
46
# File 'lib/citeproc/number.rb', line 37

def romanize(number)
  number = number.to_i

  return number unless number > 0 && number < MAX_ROMAN

  FACTORS.map { |code, factor|
    count, number = number.divmod(factor)
    code * count
  }.join
end

Instance Method Details

#<=>(other) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/citeproc/number.rb', line 151

def <=>(other)
  case
  when other.nil?
    1
  when numeric?
    if other.respond_to?(:to_i)
      to_i <=> other.to_i
    else
      nil
    end
  when other.is_a?(Variable) || other.is_a?(String)
    to_s <=> other.to_s
  else
    nil
  end
end