Class: MdBlockConverter::Block

Inherits:
Object
  • Object
show all
Defined in:
lib/md_block_converter/block.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lines, indent, lang) ⇒ Block

Returns a new instance of Block.



7
8
9
10
11
# File 'lib/md_block_converter/block.rb', line 7

def initialize(lines, indent, lang)
  @lines = lines.map(&:lstrip)
  @indent = indent
  @lang = lang
end

Instance Attribute Details

#indentObject

Returns the value of attribute indent.



5
6
7
# File 'lib/md_block_converter/block.rb', line 5

def indent
  @indent
end

#langObject

Returns the value of attribute lang.



5
6
7
# File 'lib/md_block_converter/block.rb', line 5

def lang
  @lang
end

#linesObject

Returns the value of attribute lines.



5
6
7
# File 'lib/md_block_converter/block.rb', line 5

def lines
  @lines
end

Instance Method Details

#convert!Object



13
14
15
16
17
18
# File 'lib/md_block_converter/block.rb', line 13

def convert!
  html = Nokogiri::HTML.fragment(@lines.join("\n"))
  text = html.text
  @lines = text.lines.map(&:chomp)
  self
end

#to_sObject



20
21
22
# File 'lib/md_block_converter/block.rb', line 20

def to_s
  ["```#{@lang}", *@lines, "```"].map { |line| "#{' ' * @indent}#{line}" }.join("\n")
end