Class: Agio::Block

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

Overview

A Block is the fundamental collection for the Broker that is used to then generate the Markdown.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Block

Create the Block from a tag start.



31
32
33
34
35
# File 'lib/agio/block.rb', line 31

def initialize(name, options = {})
  @name, @options = name, options
  @description = Agio::HTMLElementDescription[name]
  @contents = []
end

Instance Attribute Details

#contentsObject (readonly)

The contents of the Block.



24
25
26
# File 'lib/agio/block.rb', line 24

def contents
  @contents
end

#descriptionObject (readonly)

The description of the HTML element the Block represents (this will always be a Nokogiri::HTML::ElementDescription or nil).



28
29
30
# File 'lib/agio/block.rb', line 28

def description
  @description
end

#nameObject (readonly)

The name of the element the Block is for.



18
19
20
# File 'lib/agio/block.rb', line 18

def name
  @name
end

#optionsObject (readonly)

The options, if provided, for the element.



21
22
23
# File 'lib/agio/block.rb', line 21

def options
  @options
end

Instance Method Details

#==(other) ⇒ Object

Used mostly for testing.



125
126
127
128
129
# File 'lib/agio/block.rb', line 125

def ==(other)
  name == other.name &&
    options == other.options &&
    contents == other.contents
end

#append(*contents) ⇒ Object

Append the contents provided to the Block.



38
39
40
# File 'lib/agio/block.rb', line 38

def append(*contents)
  @contents.push(*contents)
end

#block?Boolean

Returns true if this Block is an HTML block element.

Returns:

  • (Boolean)


56
57
58
# File 'lib/agio/block.rb', line 56

def block?
  description && description.block?
end

#can_contain?(other) ⇒ Boolean

Returns true if this Block can contain the other Block provided.

Returns:

  • (Boolean)


62
63
64
65
66
67
68
69
70
71
# File 'lib/agio/block.rb', line 62

def can_contain?(other)
  case other
  when Agio::Block
    description && description.sub_elements.include?(other.name)
  when String
    description && description.sub_elements.include?(other)
  else
    false
  end
end

#definition?Boolean

Returns true if the Block is a definition item (‘dt’ or ‘dd’).

Returns:

  • (Boolean)


93
94
95
# File 'lib/agio/block.rb', line 93

def definition?
  name == "dt" or name == "dd"
end

#dl?Boolean

Returns true if the block is a definition list (‘dl’) element.

Returns:

  • (Boolean)


99
100
101
# File 'lib/agio/block.rb', line 99

def dl?
  definition? or name == "dl"
end

#inline?Boolean

Returns true if this Block is an HTML inline element.

Returns:

  • (Boolean)


50
51
52
# File 'lib/agio/block.rb', line 50

def inline?
  description && description.inline?
end

#inspectObject



9
10
11
12
13
14
15
# File 'lib/agio/block.rb', line 9

def inspect
  if options.empty?
    %Q(#<#{self.class} #{name} #{contents.inspect}>)
  else
    %Q(#<#{self.class} #{name}(#{options.inspect}) #{contents.inspect}>)
  end
end

#li?Boolean

Returns true if the Block is a ‘li’ (list item) element.

Returns:

  • (Boolean)


75
76
77
# File 'lib/agio/block.rb', line 75

def li?
  name == "li"
end

#pre?Boolean

Returns true if the Block is a ‘pre’ element.

Returns:

  • (Boolean)


81
82
83
# File 'lib/agio/block.rb', line 81

def pre?
  name == "pre"
end

#sibling_of?(other) ⇒ Boolean

Determine whether the other Block is a sibling of this Block. Blocks are siblings if:

  1. This Block cannot contain the other Block.

  2. This Block is a definition (‘dt’ or ‘dd’) and so is the other Block.

  3. This Block’s name and the other Block’s name are the same.

Returns:

  • (Boolean)


111
112
113
114
115
116
117
118
119
120
121
# File 'lib/agio/block.rb', line 111

def sibling_of?(other)
  if can_contain? other
    false
  elsif definition? and other.definition?
    true
  elsif name == other.name
    true
  else
    false
  end
end

#standard?Boolean

Returns true if the Block is a standard HTML element (as understood by Nokogiri).

Returns:

  • (Boolean)


44
45
46
# File 'lib/agio/block.rb', line 44

def standard?
  !!description
end

#ul_ol?Boolean

Returns true if the Block is a ‘ul’ or ‘ol’ element.

Returns:

  • (Boolean)


87
88
89
# File 'lib/agio/block.rb', line 87

def ul_ol?
  name == "ul" or name == "ol"
end