class MdSpell::TextLine

Containing concatenated text from single document line.

Constants

ELEMENT_TYPES

Accepted types of elements.

Attributes

content[R]

Textual content of the element.

location[R]

Line number of the element in document.

Public Class Methods

new(element) click to toggle source

Create a new TextLine from Kramdown::Element object. @param element [Kramdown::Element]

# 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
scan(document) click to toggle source

Scan Kramdown::Document for TextLines. @param document [Kramdown::Document]

# 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

Private Class Methods

assert_element_type(elem) click to toggle source

Private class methods

# File lib/mdspell/text_line.rb, line 95
def self.assert_element_type(elem)
  raise ArgumentError, 'expected Kramdown::Element' unless elem.instance_of? Kramdown::Element
end
get_all_textual_elements(elements) click to toggle source
# File lib/mdspell/text_line.rb, line 100
def self.get_all_textual_elements(elements)
  result = []

  elements.each do |element|
    if ELEMENT_TYPES.include? element.type
      result << element
    else
      result |= get_all_textual_elements(element.children)
    end
  end

  result
end

Public Instance Methods

<<(element) click to toggle source

Append Kramdown::Element's content to this element. @param element [Kramdown::Element] @raise ArgumentError if element is in different location.

# 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
words() click to toggle source

Return all words in this element.

# File lib/mdspell/text_line.rb, line 26
def words
  @content.scan(/[[[:alpha:]]']+/)
end

Private Instance Methods

append_text(value) click to toggle source
# File lib/mdspell/text_line.rb, line 73
def append_text(value)
  return if value.nil? || value.empty?

  value = value.strip
  @content += if @content.empty? || @content[-1] == "'"
                value
              else
                ' ' + value
              end
end
appent_quote(type) click to toggle source
# File lib/mdspell/text_line.rb, line 84
def appent_quote(type)
  case type
  when :lsquo, :rsquo
    @content += "'"
  when :ldquo, :rdquo
    @content += '"'
  end
end