Class: Syobocal::Comment::Element::TextNode

Inherits:
Object
  • Object
show all
Defined in:
lib/syobocal/comment/element/text_node.rb

Constant Summary collapse

SUBJECT_SEPARATOR =
""
ENCODED_SEPARATOR =
"\t"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text_elements) ⇒ TextNode

Returns a new instance of TextNode.



10
11
12
# File 'lib/syobocal/comment/element/text_node.rb', line 10

def initialize(text_elements)
  @text_elements = text_elements
end

Instance Attribute Details

#text_elementsObject (readonly)

Returns the value of attribute text_elements.



5
6
7
# File 'lib/syobocal/comment/element/text_node.rb', line 5

def text_elements
  @text_elements
end

Class Method Details

.match?(line) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/syobocal/comment/element/text_node.rb', line 37

def self.match?(line)
  true
end

.parse(text) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/syobocal/comment/element/text_node.rb', line 41

def self.parse(text)
  elements = text.split(/(\[\[[^\[\]]*?\]\])/).select { |s| !s.empty? }.map do |s|
    if s.match(/\A\[\[[^\[\]]*?\]\]\Z/)
      Link.create(s)
    else
      Text.create(s)
    end
  end

  Element::TextNode.new(elements)
end

Instance Method Details

#==(other) ⇒ Object



18
19
20
# File 'lib/syobocal/comment/element/text_node.rb', line 18

def ==(other)
  other.instance_of?(self.class) && other.text_elements == text_elements
end

#inner_textObject



14
15
16
# File 'lib/syobocal/comment/element/text_node.rb', line 14

def inner_text
  text_elements.map(&:str).join("")
end

#splitObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/syobocal/comment/element/text_node.rb', line 22

def split
  buffer = ""

  text_elements.each do |node|
    case node
    when Element::Text
      buffer << node.str.gsub(SUBJECT_SEPARATOR, ENCODED_SEPARATOR)
    when Element::Link
      buffer << node.str
    end
  end

  buffer.scan(/[^#{ENCODED_SEPARATOR}\(]+(?:\(.*?\))?/).map { |str| str.gsub(ENCODED_SEPARATOR, SUBJECT_SEPARATOR).strip }
end