Class: MdSpell::TextLine

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

Overview

Containing concatenated text from single document line.

Constant Summary collapse

ELEMENT_TYPES =

Accepted types of elements.

[:text, :smart_quote].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(element) ⇒ TextLine

Create a new TextLine from Kramdown::Element object.



17
18
19
20
21
22
23
# File 'lib/mdspell/text_line.rb', line 17

def initialize(element)
  TextLine.send :assert_element_type, element

  @content = ''
  @location = element.options[:location]
  self << element
end

Instance Attribute Details

#contentObject (readonly)

Textual content of the element.



13
14
15
# File 'lib/mdspell/text_line.rb', line 13

def content
  @content
end

#locationObject (readonly)

Line number of the element in document.



10
11
12
# File 'lib/mdspell/text_line.rb', line 10

def location
  @location
end

Class Method Details

.scan(document) ⇒ Object

Scan Kramdown::Document for TextLines.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/mdspell/text_line.rb', line 52

def self.scan(document)
  results = []

  get_all_textual_elements(document.root.children).each do |element|
    matching_element_found = results.any? do |text_element|
      if text_element.location == element.options[:location]
        text_element << element
        true
      else
        false
      end
    end

    results << TextLine.new(element) unless matching_element_found
  end

  results
end

Instance Method Details

#<<(element) ⇒ Object

Append Kramdown::Element’s content to this element.

Raises:

  • ArgumentError if element is in different location.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/mdspell/text_line.rb', line 33

def <<(element)
  TextLine.send :assert_element_type, element

  return self unless ELEMENT_TYPES.include? element.type
  return self unless element.options[:location] == @location

  value = element.value

  if element.type == :smart_quote
    appent_quote(value)
  else
    append_text(value)
  end

  self
end

#wordsObject

Return all words in this element.



26
27
28
# File 'lib/mdspell/text_line.rb', line 26

def words
  @content.scan(/[[[:alpha:]]']+/)
end