Class: Markbridge::AST::List

Inherits:
Element show all
Defined in:
lib/markbridge/ast/list.rb

Overview

Represents an ordered or unordered list element.

Examples:

Unordered list

list = AST::List.new
list << AST::ListItem.new

Ordered list

list = AST::List.new(ordered: true)
list << AST::ListItem.new

Instance Attribute Summary

Attributes inherited from Element

#children

Instance Method Summary collapse

Constructor Details

#initialize(ordered: false) ⇒ List

Create a new list element.

Parameters:

  • ordered (Boolean) (defaults to: false)

    whether this is an ordered (numbered) list



39
40
41
42
# File 'lib/markbridge/ast/list.rb', line 39

def initialize(ordered: false)
  super()
  @ordered = ordered
end

Instance Method Details

#<<(child) ⇒ List

Add content to this list.

  • ListItem children are added directly

  • Other nodes are wrapped in an implicit ListItem

  • Whitespace-only Text nodes are ignored

Parameters:

  • child (Node)

    the node to add

Returns:

  • (List)

    self for chaining

Raises:

  • (TypeError)

    if child is not a Node



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/markbridge/ast/list.rb', line 23

def <<(child)
  return self if child.is_a?(Text) && child.text.strip.empty?

  if child.is_a?(ListItem)
    super
  else
    @children << ListItem.new if @children.empty?
    @children.last << child
  end

  self
end

#ordered?Boolean

Check if this is an ordered list.

Returns:

  • (Boolean)

    true if this is an ordered (numbered) list



47
48
49
# File 'lib/markbridge/ast/list.rb', line 47

def ordered?
  @ordered
end