Class: ExternalReferenceResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/external_reference_resolver.rb

Direct Known Subclasses

ActResolver

Constant Summary collapse

OPTIONAL_COMMA =

optional comma, but not ', the' or ', and the'

'(?:,(?!(?:\sAND|\sand)?\s(?:THE|the)))?'
TITLE_CASE_WORD =
/(?:[A-Z][a-z\-]+ # titlecase letter followed by at least one lower case letter, or hyphen
#{OPTIONAL_COMMA}
'?[s]?                            # optional apostrophe and s
)
/x
CAPS_WORD =
/(?:[A-Z]+              # at least one capital letter
#{OPTIONAL_COMMA}                 # not followed by the word UNDER
'?S?                              # optional apostrophe and s
(?!\sUNDER\s))
/x
CONJUNCTION_IN_MATCH =
/and(?!\s(of\s|in\s)?the)|for|of|by(?!\sthe)|to|in|the|into(?!\sthe)|other|etc\.|&C\./i

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ ExternalReferenceResolver

Returns a new instance of ExternalReferenceResolver


23
24
25
26
# File 'lib/external_reference_resolver.rb', line 23

def initialize(text)
  self.text = text
  self.scanner = StringScanner.new(text)
end

Instance Attribute Details

#scannerObject

Returns the value of attribute scanner


5
6
7
# File 'lib/external_reference_resolver.rb', line 5

def scanner
  @scanner
end

#textObject

Returns the value of attribute text


5
6
7
# File 'lib/external_reference_resolver.rb', line 5

def text
  @text
end

Instance Method Details

#any_positive_patternObject


44
45
46
# File 'lib/external_reference_resolver.rb', line 44

def any_positive_pattern
  Regexp.union( *positive_patterns )
end

#any_references?Boolean

Returns:

  • (Boolean)

52
53
54
55
56
57
58
59
60
61
# File 'lib/external_reference_resolver.rb', line 52

def any_references?
  if screening_pattern
    return false unless self.scanner.exist?(screening_pattern)
  end

  positive_patterns.each do |pattern|
    return true if self.scanner.exist?(pattern)
  end
  false
end

#each_referenceObject


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/external_reference_resolver.rb', line 63

def each_reference
  return unless any_references?
  loop do
    found = self.scanner.scan_until(any_positive_pattern)
    break if not found
    reference = self.scanner.matched

    unless match_patterns(negative_patterns, reference)
      if (reference_data = get_match reference)
        yield reference_data
      end
    end
  end
  self.scanner.reset
end

#markup_referencesObject


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/external_reference_resolver.rb', line 87

def markup_references
  marked_up = ''
  last_position = 0
  each_reference do |reference, start_position, end_position|
    marked_up += self.text[last_position...start_position]
    last_position = end_position
    if block_given?
      marked_up += yield(reference)
    else
      marked_up += reference_replacement(reference)
    end

  end
  marked_up += self.text[last_position...self.text.size]
end

#negative_patternsObject


40
41
42
# File 'lib/external_reference_resolver.rb', line 40

def negative_patterns
  []
end

#positive_pattern_groupsObject


32
33
34
# File 'lib/external_reference_resolver.rb', line 32

def positive_pattern_groups
  []
end

#positive_patternsObject


36
37
38
# File 'lib/external_reference_resolver.rb', line 36

def positive_patterns
  positive_pattern_groups.map{ |pattern, group| pattern }
end

#reference_replacement(reference) ⇒ Object


48
49
50
# File 'lib/external_reference_resolver.rb', line 48

def reference_replacement(reference)
  reference
end

#referencesObject


79
80
81
82
83
84
85
# File 'lib/external_reference_resolver.rb', line 79

def references
  refs = []
  each_reference do |reference, start_position, end_position|
    refs << reference
  end
  refs
end

#screening_patternObject


28
29
30
# File 'lib/external_reference_resolver.rb', line 28

def screening_pattern
  nil
end