Class: Scripref::Processor
- Inherits:
-
Object
- Object
- Scripref::Processor
- Includes:
- Enumerable, BasicMethods
- Defined in:
- lib/scripref/processor.rb
Constant Summary
Constants included from BasicMethods
Instance Attribute Summary collapse
-
#text ⇒ Object
Returns the value of attribute text.
Instance Method Summary collapse
-
#each ⇒ Object
Iterate over each piece of
str
(text and parsed references) if block given, gets an iterator over the pieces otherwise. -
#each_ref ⇒ Object
Iterate over each parsed reference if block given, gets an iterator over each parsed reference otherwise.
- #fix_scanner_and_results(scanner) ⇒ Object
-
#initialize(text = nil, *mods) ⇒ Processor
constructor
A new instance of Processor.
- #inspect ⇒ Object
-
#reference_re ⇒ Object
Regular expression to heuristically identify a reference.
Methods included from BasicMethods
#book_re, #chapter_re, #verse_re
Constructor Details
Instance Attribute Details
#text ⇒ Object
Returns the value of attribute text.
14 15 16 |
# File 'lib/scripref/processor.rb', line 14 def text @text end |
Instance Method Details
#each ⇒ Object
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_ref ⇒ Object
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 |
#inspect ⇒ Object
57 58 59 |
# File 'lib/scripref/processor.rb', line 57 def inspect "#<#{self.class} #{@mods.inspect}>" end |
#reference_re ⇒ Object
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 |