Class: Vita::NoteScanner
- Inherits:
-
Object
- Object
- Vita::NoteScanner
- Defined in:
- lib/vita/note_scanner.rb
Defined Under Namespace
Classes: Match
Constant Summary collapse
- CONTEXT_BOUNDARY =
/(?<=\A|[.!?])|\n\n|\z/
- NOT_WHITESPACE =
/\S/
Instance Attribute Summary collapse
-
#string ⇒ Object
readonly
Returns the value of attribute string.
Instance Method Summary collapse
- #flatten_newlines(string) ⇒ Object
-
#initialize(string) ⇒ NoteScanner
constructor
A new instance of NoteScanner.
- #scan(regexp) ⇒ Object
Constructor Details
#initialize(string) ⇒ NoteScanner
Returns a new instance of NoteScanner.
10 11 12 |
# File 'lib/vita/note_scanner.rb', line 10 def initialize(string) @string = string end |
Instance Attribute Details
#string ⇒ Object (readonly)
Returns the value of attribute string.
8 9 10 |
# File 'lib/vita/note_scanner.rb', line 8 def string @string end |
Instance Method Details
#flatten_newlines(string) ⇒ Object
38 39 40 |
# File 'lib/vita/note_scanner.rb', line 38 def flatten_newlines(string) string.tr("\n", " ") end |
#scan(regexp) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/vita/note_scanner.rb', line 14 def scan(regexp) matches = string.to_enum(:scan, regexp).map { Regexp.last_match } matches.map do |match| link_text = match[0] match_start, match_end = match.offset(0) prev_context_boundary = string.rindex(CONTEXT_BOUNDARY, match_start) next_context_boundary = string.index(CONTEXT_BOUNDARY, match_end) context_start = string.index(NOT_WHITESPACE, prev_context_boundary) context_end = string.rindex(NOT_WHITESPACE, next_context_boundary) context_around_match = string[context_start..context_end] Match.new( position: match_start, text: link_text, context: flatten_newlines(context_around_match), context_position: match_start - context_start ) end end |