Class: Cosensee::ParsedLine

Inherits:
Object
  • Object
show all
Defined in:
lib/cosensee/parsed_line.rb

Overview

parse a line

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(indent: nil, line_content: nil, content: [], rest: nil, raw: '', parsed: false) ⇒ ParsedLine

Returns a new instance of ParsedLine.



6
7
8
9
10
11
12
13
# File 'lib/cosensee/parsed_line.rb', line 6

def initialize(indent: nil, line_content: nil, content: [], rest: nil, raw: '', parsed: false)
  @indent = indent
  @line_content = line_content
  @content = content
  @rest = rest
  @raw = raw
  @parsed = parsed
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



15
16
17
# File 'lib/cosensee/parsed_line.rb', line 15

def content
  @content
end

#indentObject

Returns the value of attribute indent.



15
16
17
# File 'lib/cosensee/parsed_line.rb', line 15

def indent
  @indent
end

#line_contentObject

Returns the value of attribute line_content.



15
16
17
# File 'lib/cosensee/parsed_line.rb', line 15

def line_content
  @line_content
end

#parsedObject

Returns the value of attribute parsed.



15
16
17
# File 'lib/cosensee/parsed_line.rb', line 15

def parsed
  @parsed
end

#rawObject (readonly)

Returns the value of attribute raw.



16
17
18
# File 'lib/cosensee/parsed_line.rb', line 16

def raw
  @raw
end

#restObject

Returns the value of attribute rest.



15
16
17
# File 'lib/cosensee/parsed_line.rb', line 15

def rest
  @rest
end

Instance Method Details

#==(other) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/cosensee/parsed_line.rb', line 85

def ==(other)
  other.is_a?(Cosensee::ParsedLine) &&
    other.indent == indent &&
    other.line_content == line_content &&
    other.content == content &&
    other.rest == rest &&
    other.parsed? == parsed?
end

#append_text(text:, raw_line:) ⇒ Object



46
47
48
# File 'lib/cosensee/parsed_line.rb', line 46

def append_text(text:, raw_line:)
  self.line_content = line_content.append_text(text:, raw_line:)
end

#codeblock?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/cosensee/parsed_line.rb', line 18

def codeblock?
  line_content.is_a?(Cosensee::Node::Codeblock)
end

#first_imageObject



34
35
36
37
38
39
40
# File 'lib/cosensee/parsed_line.rb', line 34

def first_image
  if line_content?
    line_content.is_a?(Node::Quote) && line_content.content.find { |elem| elem.is_a?(Node::ImageBracket) || elem.is_a?(Node::GyazoImageBracket) }
  else
    content.find { |elem| elem.is_a?(Node::ImageBracket) || elem.is_a?(Node::GyazoImageBracket) }
  end
end

#indent_levelObject



50
51
52
# File 'lib/cosensee/parsed_line.rb', line 50

def indent_level
  indent.level
end


73
74
75
# File 'lib/cosensee/parsed_line.rb', line 73

def internal_links
  content.select { |c| c.is_a?(Cosensee::Node::InternalLinkBracket) || c.is_a?(Cosensee::Node::HashTag) }.map(&:anchor)
end

#line_content?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/cosensee/parsed_line.rb', line 42

def line_content?
  !!line_content
end

#match(pattern) ⇒ Object



54
55
56
# File 'lib/cosensee/parsed_line.rb', line 54

def match(pattern)
  rest.match(pattern)
end

#parsed?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/cosensee/parsed_line.rb', line 22

def parsed?
  @parsed
end

#rest?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/cosensee/parsed_line.rb', line 26

def rest?
  !!rest
end

#song_tagged?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/cosensee/parsed_line.rb', line 30

def song_tagged?
  content.any? { |elem| elem.is_a?(Node::HashTag) && elem.anchor == '楽曲' }
end

#split_rest_by(str) ⇒ Object



58
59
60
# File 'lib/cosensee/parsed_line.rb', line 58

def split_rest_by(str)
  rest.split(str, -1)
end

#to_sObject



77
78
79
80
81
82
83
# File 'lib/cosensee/parsed_line.rb', line 77

def to_s
  if line_content?
    line_content.to_s
  else
    content.map(&:to_s).join
  end
end

#update(**attributes) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/cosensee/parsed_line.rb', line 62

def update(**attributes)
  attributes.each do |key, value|
    # Check if the key is a valid accessor
    raise ArgumentError, "Attribute #{key} is not allowed to be updated." unless self.class.method_defined?("#{key}=")

    public_send("#{key}=", value)
  end

  self
end