Module: Utilities::Bibliography

Defined in:
lib/utilities/bibliography.rb

Overview

Pieces of bibliographic citation text, as written: years, volumes, pages. For authors see Utilities::AuthorNames; for DOIs Identifier::Global::Doi.

Usage:

'1936a'[Utilities::Bibliography::YEAR_WITH_SUFFIX] # => '1936a'
Utilities::Bibliography.volume_number('Vol. 10(2):149-158') # => '10'
Utilities::Bibliography.first_page('149-158') # => '149'

Constant Summary collapse

YEAR =

A publication year, 1500 to 2099.

/1[5-9]\d{2}|20\d{2}/
YEAR_SUFFIX =

A year suffix as TaxonWorks stores it (Source::Bibtex#year=): letters, either case, more than one when an author's works in a year outnumber the alphabet, 1936a, 1936B, 1936ab.

/[a-zA-Z]+/
YEAR_SUFFIX_IN_TEXT =

A year suffix in running text, e.g. a verbatim citation: YEAR_SUFFIX written against the year, 1936a, 1936B, 1936ab; or one lowercase letter set off by a space, 1936 b. After a space a capital or a longer word is the next word of the text, 1990 A new species, 1990 Annales, so it isn't read as a suffix. (A lowercase one-letter word right after a year still is, 1990 a revision; citations rarely have one there.)

/#{YEAR_SUFFIX}|\s+[a-z]\b/
YEAR_WITH_SUFFIX =

A year with an optional suffix in running text: captures the year and the suffix (as written, so it may start with spaces).

/\b(#{YEAR})(#{YEAR_SUFFIX_IN_TEXT})?\b/
VOLUME_LABEL =

A volume's label, Vol. 10, Volume 10.

/\Avol(?:ume)?\.?\s*/i
PAGE_COUNT_AFTER_COLON =

A page count after an article number, : 7 pages, : 9 pp.

/\s*:\s*\d+\s*(?:pages?\b|pp\b)/
VOLUME_PAGES_PATTERNS =

A volume and its first page in a citation, captured in that order: 96(2):285-298, 1027: 55-63, , 16, 81–97, , 58(3), 529–536, a list of pages ending in a range, written without spaces, , 11(4), 5,6–8 (with spaces it's the volume and pages after a year or a series, , 2005, 2, 546–554), and a single page, , 44(3), 115., , 12, p. 5: one ending the citation or followed by ./;, so , 2, 248 pp. and , 12, 3 figs. aren't read as pages, nor is a list of pages alone, , 25, 167,179. An article number with a page count is the first page, 88, 125871:7 pages, 13(11), 1044: 18 pages, not a volume and a page.

[
  /\b(\d+)\s*(?:\([^)]*\))?\s*:\s*(\d+)(?!\d|\s*(?:pages?\b|pp\b))/,
  /\b(\d+)\s*(?:\([^)]*\))?\s*,\s*(\d+)#{PAGE_COUNT_AFTER_COLON}/,
  /,\s*(\d+)\s*(?:\([^)]*\))?\s*,\s*(?:pp?\.\s*)?(\d+)(?:,\d+)*\s*[-–—]\s*\d+/,
  /,\s*(\d+)\s*(?:\([^)]*\))?\s*,\s+(?:p\.\s*)?(\d+)\s*(?:[.;]|\z)/
].freeze
LABELLED_PAGES_PATTERN =

Pages labelled p./pp. with no volume before them, as a book chapter's: pp. 20–34, p. 5; captures the first page. The label comes before the number, so a page count (248 pp.) isn't read as a page, nor is what follows one (248 pp. 12 figs.): no label right after a number.

/(?<!\d)(?<!\d\s)\bpp?\.\s*(\d+)(?:\s*[-–—]\s*\d+)?/
NOT_PAGES_PATTERN =

Numbered things in pages that aren't pages: plates, figures, maps, tables, labelled before the number (pl. 4, pls.XV-XVI, figs. 3-5) or after it (23 pls., 1217 text figs.); and article numbers, e14793.

/
  \b(?:pls?|plates?|figs?|figures?|maps?|tabs?|tables?)\b\.?\s*
    [\dIVXLCDM]+(?:\s*[-–—]+\s*[\dIVXLCDM]+)?
  |\d+\s*(?:text[-\s]*)?(?:pls?|plates?|figs?|figures?|maps?|tabs?|tables?)\b
  |\be\d+(?:\s*[-–—]+\s*\d+)?
/xi
PAGES_PATTERN =

What a page number in pages is, captured: a page count (248 pp., 17 pages), a range (149-158, BibTeX's 149--158), or a page (19). Not the decimal part of a page, 3166.1.

/
  (?<!\d\.)(?<!\d)
  (?:
    (\d+)\s*(?:pp\b\.?|pages\b)
    |(\d+)\s*[-–—]+\s*(\d+)
    |(\d+)
  )
/xi

Class Method Summary collapse

Class Method Details

.first_page(value) ⇒ String?

The first number in pages: 149 of 149-158, 5 of p. 5. A page count reads as a page, 248 of xii + 248 pp..

Parameters:

  • value (String, nil)

Returns:

  • (String, nil)


70
71
72
# File 'lib/utilities/bibliography.rb', line 70

def self.first_page(value)
  value.to_s[/\d+/]
end

.page_range(first, last) ⇒ Range? (private)

Parameters:

  • first (String) —

    232

  • last (String) —

    257, or abbreviated, 57

Returns:

  • (Range, nil)


122
123
124
125
126
127
# File 'lib/utilities/bibliography.rb', line 122

def self.page_range(first, last)
  if last.length < first.length && last.to_i < first.to_i
    last = first[0, first.length - last.length] + last
  end
  first.to_i..last.to_i if last.to_i >= first.to_i
end

.page_ranges(value) ⇒ Array<Range>

The pages and page ranges in free-text pages, as written in a bibliography: i, v-vii, 19, 77-100, plate 4 is 19 and 77-100. Roman numerals (front matter), plates, figures, maps, tables and article numbers (see NOT_PAGES_PATTERN) aren't read. A page count is the pages it counts, xii + 248 pp. is 1-248. A range abbreviated to its last digits is read in full, 232-57 is 232-257; one that otherwise ends before it starts, 274-175, isn't read.

Parameters:

  • value (String, nil)

Returns:

  • (Array<Range>) —

    Integer ranges, a page alone as 19..19



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/utilities/bibliography.rb', line 106

def self.page_ranges(value)
  value.to_s.gsub(NOT_PAGES_PATTERN, ' ').scan(PAGES_PATTERN)
    .filter_map { |count, first, last, page|
      if count
        1..count.to_i
      elsif page
        page.to_i..page.to_i
      else # a range: first and last are both captured
        page_range(first, last)
      end
    }.uniq
end

.volume_number(value) ⇒ String?

Returns the volume alone, e.g. 10.

Parameters:

  • value (String, nil) —

    a volume, optionally labelled and followed by issue and pages, e.g. 10(2):149-158, 65(1), Vol. 10 (2)

Returns:

  • (String, nil) —

    the volume alone, e.g. 10



62
63
64
# File 'lib/utilities/bibliography.rb', line 62

def self.volume_number(value)
  value.to_s.strip.sub(VOLUME_LABEL, '')[/\A([^\s(:,;]+)/, 1]
end