Class: Flannel::BaseBlock

Inherits:
Object
  • Object
show all
Defined in:
lib/flannel/base_block.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(block) ⇒ BaseBlock

Returns a new instance of BaseBlock.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/flannel/base_block.rb', line 8

def initialize block
  form = block[0]

  if form == :block
    create_from_list block[1]
  else
    @text = block[1]
    @type = :paragraph
  end

  strip_and_encode_text
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



6
7
8
# File 'lib/flannel/base_block.rb', line 6

def attributes
  @attributes
end

#idObject (readonly)

Returns the value of attribute id.



6
7
8
# File 'lib/flannel/base_block.rb', line 6

def id
  @id
end

#parent_idObject (readonly)

Returns the value of attribute parent_id.



6
7
8
# File 'lib/flannel/base_block.rb', line 6

def parent_id
  @parent_id
end

#textObject (readonly)

Returns the value of attribute text.



6
7
8
# File 'lib/flannel/base_block.rb', line 6

def text
  @text
end

#typeObject (readonly)

Returns the value of attribute type.



6
7
8
# File 'lib/flannel/base_block.rb', line 6

def type
  @type
end

Instance Method Details

#create_from_list(list) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/flannel/base_block.rb', line 21

def create_from_list list
  header = list.shift
  @type = header.shift[1]
  @id = header.shift[1]
  @attributes = {}

  next_item = header.shift
  while next_item
    case next_item[0]
    when :parent_id then
      @parent_id = next_item[1]
    when :attribute_list then
      next_item.shift
      next_item.each do |attribute|
        @attributes[attribute[0]] = attribute[1]
      end
    end
    next_item = header.shift
  end

  @text = list.shift
end

#strip_and_encode_textObject



49
50
51
52
53
54
55
56
57
# File 'lib/flannel/base_block.rb', line 49

def strip_and_encode_text
  return unless @text
  @text.force_encoding("UTF-8")

  return if @type == :preformatted
  return unless @text

  @text = @text.strip
end

#to_hObject



44
45
46
47
# File 'lib/flannel/base_block.rb', line 44

def to_h
  html_formatter = Flannel::HtmlFormatter.new
  html_formatter.do(@text, @type, @id)
end