Class: Repubmark::Elems::CodeBlock

Inherits:
Base
  • Object
show all
Defined in:
lib/repubmark/elems/code_block.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#parent

Instance Method Summary collapse

Methods inherited from Base

#absolute_url, #config, #count_words, #html_class, #own_url, parent?, parents, #relative_url, #to_summary_plain, #word_count

Constructor Details

#initialize(parent, syntax, str, indent: 0) ⇒ CodeBlock

Returns a new instance of CodeBlock.



10
11
12
13
14
15
16
# File 'lib/repubmark/elems/code_block.rb', line 10

def initialize(parent, syntax, str, indent: 0)
  super parent

  self.syntax = syntax
  self.str = str
  self.indent = indent
end

Instance Attribute Details

#indentObject

Returns the value of attribute indent.



8
9
10
# File 'lib/repubmark/elems/code_block.rb', line 8

def indent
  @indent
end

#strObject

Returns the value of attribute str.



8
9
10
# File 'lib/repubmark/elems/code_block.rb', line 8

def str
  @str
end

#syntaxObject

Returns the value of attribute syntax.



8
9
10
# File 'lib/repubmark/elems/code_block.rb', line 8

def syntax
  @syntax
end

Instance Method Details

#highlighted_strObject (private)



56
57
58
# File 'lib/repubmark/elems/code_block.rb', line 56

def highlighted_str
  @highlighted_str ||= Highlight.call syntax, transformed_str
end

#indentationObject (private)



65
66
67
# File 'lib/repubmark/elems/code_block.rb', line 65

def indentation
  @indentation ||= (' ' * indent).freeze
end

#to_gemtextObject



24
25
26
27
28
29
30
# File 'lib/repubmark/elems/code_block.rb', line 24

def to_gemtext
  [
    "```\n",
    "#{str.strip}\n",
    "```\n",
  ].join.freeze
end

#to_htmlObject

Basic methods #



22
# File 'lib/repubmark/elems/code_block.rb', line 22

def to_html = highlighted_str

#transformed_strObject (private)



60
61
62
63
# File 'lib/repubmark/elems/code_block.rb', line 60

def transformed_str
  @transformed_str ||=
    str.lines.map { |line| "#{indentation}#{line}" }.join.freeze
end