Class: Minidown::CodeBlockElement

Inherits:
Element
  • Object
show all
Defined in:
lib/minidown/elements/code_block_element.rb

Instance Attribute Summary

Attributes inherited from Element

#children, #content, #doc, #nodes

Instance Method Summary collapse

Methods inherited from Element

#blank?, #raw_content, #raw_content=, #unparsed_lines

Methods included from HtmlHelper

#br_tag, #build_tag

Constructor Details

#initialize(*_) ⇒ CodeBlockElement

Returns a new instance of CodeBlockElement.



4
5
6
7
# File 'lib/minidown/elements/code_block_element.rb', line 4

def initialize *_
  super
  @code_block_handler = doc.options[:code_block_handler]
end

Instance Method Details

#children_htmlObject



29
30
31
# File 'lib/minidown/elements/code_block_element.rb', line 29

def children_html
  children.map(&:to_html).join("\n".freeze)
end

#langObject



25
26
27
# File 'lib/minidown/elements/code_block_element.rb', line 25

def lang
  @lang ||= content.empty? ? nil : content
end

#parseObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/minidown/elements/code_block_element.rb', line 9

def parse
  nodes << self
  while(line = unparsed_lines.shift) do
    case line
    when Utils::Regexp[:code_block]
      break
    else
      child = TextElement.new(doc, line)
      child.escape = false
      child.sanitize = @code_block_handler.nil?
      child.convert = false
      children << child
    end
  end
end

#to_htmlObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/minidown/elements/code_block_element.rb', line 33

def to_html
  if @code_block_handler
    @code_block_handler.call(lang, children_html).to_s
  else
    attr = lang ? {class: lang} : nil
    build_tag 'pre'.freeze do |pre|
      pre << build_tag('code'.freeze, attr){ |code| code << children_html }
    end
  end
end