Class: Coradoc::CoreModel::ListBlock

Inherits:
Base
  • Object
show all
Defined in:
lib/coradoc/core_model/list_block.rb

Overview

Represents a list block with proper nesting support

Handles all list types:

  • Unordered lists

  • Ordered lists

  • Description lists

Lists can contain nested lists at multiple levels, with each level tracked through marker_level.

Examples:

Creating an unordered list

list = CoreModel::ListBlock.new(
  marker_type: "unordered",
  marker_level: 1,
  items: [
    ListItem.new(marker: "*", content: "First item"),
    ListItem.new(marker: "*", content: "Second item")
  ]
)

Creating a nested list

nested = CoreModel::ListBlock.new(
  marker_type: "unordered",
  marker_level: 2,
  items: [ListItem.new(marker: "**", content: "Nested item")]
)
list = CoreModel::ListBlock.new(
  marker_type: "unordered",
  marker_level: 1,
  items: [
    ListItem.new(
      marker: "*",
      content: "Parent item",
      nested_list: nested
    )
  ]
)

Instance Attribute Summary collapse

Attributes inherited from Base

#element_attributes, #id, #metadata_entries, #title

Instance Method Summary collapse

Methods inherited from Base

#accept, #attr, #body_content?, build, #flat_text, #metadata, #semantically_equivalent?, #set_attr, #set_metadata, #whitespace_only?

Instance Attribute Details

#itemsArray<ListItem>

Returns collection of list items.

Returns:

  • (Array<ListItem>)

    collection of list items



109
# File 'lib/coradoc/core_model/list_block.rb', line 109

attribute :items, ListItem, collection: true

#marker_levelInteger

Returns nesting level of the list (default: 1).

Returns:

  • (Integer)

    nesting level of the list (default: 1)



101
# File 'lib/coradoc/core_model/list_block.rb', line 101

attribute :marker_level, :integer, default: -> { 1 }

#marker_typeString?

Returns type of list marker (e.g., ‘unordered’, ‘ordered’, ‘definition’).

Returns:

  • (String, nil)

    type of list marker (e.g., ‘unordered’, ‘ordered’, ‘definition’)



97
# File 'lib/coradoc/core_model/list_block.rb', line 97

attribute :marker_type, :string

#startInteger?

Returns starting number for ordered lists.

Returns:

  • (Integer, nil)

    starting number for ordered lists



105
# File 'lib/coradoc/core_model/list_block.rb', line 105

attribute :start, :integer

Instance Method Details

#add_item(marker: self.marker_type == 'ordered' ? '.' : '*') ⇒ Object

Append a new ListItem, built via ListItem.build. The block (if given) is yielded the new item so callers can chain add_text / add_link on it inline:

ListBlock.build do |ul|
  children.each { |c| ul.add_item { |li| li.add_link(c[:slug], text: c[:title]) } }
end

Returns self for chaining at the list level.



122
123
124
125
126
# File 'lib/coradoc/core_model/list_block.rb', line 122

def add_item(marker: self.marker_type == 'ordered' ? '.' : '*')
  item = ListItem.build(marker: marker) { |li| yield li if block_given? }
  self.items = Array(items) + [item]
  self
end