Class: Coradoc::CoreModel::Block

Inherits:
Base
  • Object
show all
Includes:
ChildrenContent
Defined in:
lib/coradoc/core_model/block.rb

Overview

Generic block model

Represents all block-level elements in a format-neutral way. Each block has a ‘block_semantic_type` symbol (e.g., :source_code, :quote, :example) that captures its semantic meaning without tying it to any specific format’s syntax.

Examples:

Creating a source code block

block = CoreModel::Block.new(
  block_semantic_type: :source_code,
  content: "puts 'Hello, World!'",
  language: "ruby"
)

Creating a paragraph with inline formatting

block = CoreModel::Block.new(
  block_semantic_type: :paragraph,
  children: [
    "Text with ",
    CoreModel::InlineElement.new(format_type: "bold", content: "bold"),
    " text"
  ]
)

Instance Attribute Summary collapse

Attributes inherited from Base

#element_attributes, #id, #metadata_entries, #title

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ChildrenContent

#children=, #flat_text, included, #initialize, #renderable_content, #to_hash

Methods inherited from Base

#accept, #attr, #metadata, #semantically_equivalent?, #set_attr, #set_metadata

Instance Attribute Details

#block_semantic_typeString?

Returns the semantic type of the block (e.g., ‘source_code’, ‘quote’, ‘example’, ‘paragraph’).

Returns:

  • (String, nil)

    the semantic type of the block (e.g., ‘source_code’, ‘quote’, ‘example’, ‘paragraph’)



83
# File 'lib/coradoc/core_model/block.rb', line 83

attribute :block_semantic_type, :string

#contentString?

Returns the block’s text content (simple string) For mixed content with inline elements, use children instead.

Returns:

  • (String, nil)

    the block’s text content (simple string) For mixed content with inline elements, use children instead.



102
# File 'lib/coradoc/core_model/block.rb', line 102

attribute :content, :string

#delimiter_lengthInteger

Returns number of delimiter characters (default: 4).

Returns:

  • (Integer)

    number of delimiter characters (default: 4)



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

attribute :delimiter_length, :integer, default: -> { 4 }

#delimiter_typeString?

Returns DEPRECATED — use block_semantic_type. Retained for backward compatibility during migration.

Returns:

  • (String, nil)

    DEPRECATED — use block_semantic_type. Retained for backward compatibility during migration.



93
# File 'lib/coradoc/core_model/block.rb', line 93

attribute :delimiter_type, :string

#element_typeString?

Returns the structural role of the block (e.g., ‘paragraph’, ‘block’).

Returns:

  • (String, nil)

    the structural role of the block (e.g., ‘paragraph’, ‘block’)



88
# File 'lib/coradoc/core_model/block.rb', line 88

attribute :element_type, :string

#languageString?

Returns language identifier for source code blocks.

Returns:

  • (String, nil)

    language identifier for source code blocks



110
# File 'lib/coradoc/core_model/block.rb', line 110

attribute :language, :string

#linesArray<String>?

Returns individual lines of content.

Returns:

  • (Array<String>, nil)

    individual lines of content



106
# File 'lib/coradoc/core_model/block.rb', line 106

attribute :lines, :string, collection: true

Class Method Details

.semantic_typeObject

Class-level semantic type — overridden by each typed subclass. Returns nil for the generic Block base class.



64
65
66
# File 'lib/coradoc/core_model/block.rb', line 64

def semantic_type
  nil
end

Instance Method Details

#resolve_semantic_typeObject

Resolve the semantic type from this block instance. Checks class-level semantic_type first (typed subclasses), then the block_semantic_type attribute, then element_type, then delimiter_type fallback.



73
74
75
76
77
78
# File 'lib/coradoc/core_model/block.rb', line 73

def resolve_semantic_type
  self.class.semantic_type ||
    (block_semantic_type&.to_sym) ||
    resolve_semantic_from_element_type ||
    resolve_semantic_from_delimiter
end