lib/cosensee/parsed_line.rb in cosensee-0.6.0 vs lib/cosensee/parsed_line.rb in cosensee-0.8.0
- old
+ new
@@ -1,36 +1,54 @@
# frozen_string_literal: true
module Cosensee
# parse a line
class ParsedLine
- def initialize(indent: nil, line_content: nil, content: [], rest: nil, parsed: false)
+ 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
attr_accessor :indent, :line_content, :content, :rest, :parsed
+ attr_reader :raw
def codeblock?
- line_content.is_a?(Cosensee::Codeblock)
+ line_content.is_a?(Cosensee::Node::Codeblock)
end
def parsed?
@parsed
end
def rest?
!!rest
end
+ def song_tagged?
+ content.any? { |elem| elem.is_a?(Node::HashTag) && elem.anchor == '楽曲' }
+ end
+
+ 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
+
def line_content?
!!line_content
end
+ def append_text(text:, raw_line:)
+ self.line_content = line_content.append_text(text:, raw_line:)
+ end
+
def indent_level
indent.level
end
def match(pattern)
@@ -51,17 +69,17 @@
self
end
def internal_links
- content.select { |c| c.is_a?(Cosensee::InternalLinkBracket) || c.is_a?(Cosensee::HashTag) }.map(&:anchor)
+ content.select { |c| c.is_a?(Cosensee::Node::InternalLinkBracket) || c.is_a?(Cosensee::Node::HashTag) }.map(&:anchor)
end
- def raw
+ def to_s
if line_content?
- "#{indent.raw}#{line_content.raw}"
+ line_content.to_s
else
- "#{indent.raw}#{content.map(&:to_s).join}"
+ content.map(&:to_s).join
end
end
def ==(other)
other.is_a?(Cosensee::ParsedLine) &&