Class: Scripref::Processor

Inherits:
Object
  • Object
show all
Includes:
Enumerable, BasicMethods
Defined in:
lib/scripref/processor.rb

Constant Summary

Constants included from BasicMethods

BasicMethods::NUMBER_RE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BasicMethods

#book_re, #chapter_re, #verse_re

Constructor Details

#initialize(text = nil, *mods) ⇒ Processor

Returns a new instance of Processor.

Parameters:

  • text (defaults to: nil)

    text to parse

  • mods

    one or more modules to include in processor and parser



18
19
20
21
22
23
# File 'lib/scripref/processor.rb', line 18

def initialize text=nil, *mods
  @text = text
  @mods = mods
  mods.each {|m| extend m}
  @parser = Parser.new(*mods)
end

Instance Attribute Details

#textObject

Returns the value of attribute text.



14
15
16
# File 'lib/scripref/processor.rb', line 14

def text
  @text
end

Instance Method Details

#eachObject

Iterate over each piece of str (text and parsed references) if block given, gets an iterator over the pieces otherwise.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/scripref/processor.rb', line 42

def each
  if block_given?
    scanner = StringScanner.new(text)
    while scanner.scan(/(.*?)(#{reference_re.source})/m)
      text, ref = fix_scanner_and_results(scanner)
      yield text unless text.empty?
      yield @parser.parse(ref)
    end
    yield scanner.rest if scanner.rest?
    self
  else
    enum_for :each
  end
end

#each_refObject

Iterate over each parsed reference if block given, gets an iterator over each parsed reference otherwise.



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/scripref/processor.rb', line 27

def each_ref
  if block_given?
    scanner = StringScanner.new(text)
    while scanner.scan(/(.*?)(#{reference_re.source})/m)
      _, ref = fix_scanner_and_results(scanner)
      yield @parser.parse(ref)
    end
    self
  else
    enum_for :each_ref
  end
end

#fix_scanner_and_results(scanner) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/scripref/processor.rb', line 74

def fix_scanner_and_results scanner
  text = scanner[1]
  ref = scanner[2]
  re = /#{punctuation_marks_re.source}$/
  if ref =~ re
    scanner.pos -= $&.size
    ref.sub! re, ''
  end
  [text, ref]
end

#inspectObject



57
58
59
# File 'lib/scripref/processor.rb', line 57

def inspect
  "#<#{self.class} #{@mods.inspect}>"
end

#reference_reObject

Regular expression to heuristically identify a reference



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/scripref/processor.rb', line 62

def reference_re
  return @reference_re if @reference_re
  verse_with_optional_addon_or_postfix =
    [verse_re, '(', postfix_one_following_verse_re, '|', postfix_more_following_verses_re, '|', verse_addon_re, ')?']
  re_parts = [
     '(', book_re, ')', '(', verse_with_optional_addon_or_postfix, '|', chapter_re, ')',
    # more than one passage
    '(', verse_with_optional_addon_or_postfix, '|', Regexp.union(cv_sep_re, verse_sep_re, hyphen_re, pass_sep_re, book_re, chapter_re), ')*'
  ].map {|e| Regexp === e ? e.source : e}
  @reference_re = Regexp.compile(re_parts.join, nil)
end