Class: Coradoc::AsciiDoc::Model::Section

Inherits:
Base
  • Object
show all
Includes:
Anchorable
Defined in:
lib/coradoc/asciidoc/model/section.rb

Overview

Section element for organizing document content hierarchically.

Sections represent the hierarchical structure of an AsciiDoc document. They can contain nested subsections, paragraphs, and other content.

Examples:

Create a section

section = Coradoc::AsciiDoc::Model::Section.new(
  title: Coradoc::AsciiDoc::Model::Title.new("Chapter 1"),
  contents: [Coradoc::AsciiDoc::Model::Paragraph.new("Content here")]
)

Create nested sections

parent = Coradoc::AsciiDoc::Model::Section.new(
  title: Coradoc::AsciiDoc::Model::Title.new("Parent Section")
)
child = Coradoc::AsciiDoc::Model::Section.new(
  title: Coradoc::AsciiDoc::Model::Title.new("Child Section")
)
parent.sections << child

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Anchorable

#default_anchor, #gen_anchor, included

Methods inherited from Base

#inline?, #serialize_content, #simplify_block_content, #to_adoc, #to_h, visit, #visit

Constructor Details

#initialize(**attributes) ⇒ Section

Allow setting level directly during initialization



64
65
66
67
68
69
70
71
72
# File 'lib/coradoc/asciidoc/model/section.rb', line 64

def initialize(**attributes)
  level_value = attributes.delete(:level)
  super
  if level_value && title
    title.level_int = level_value
  elsif level_value
    self.title = Coradoc::AsciiDoc::Model::Title.new(content: '', level_int: level_value)
  end
end

Instance Attribute Details

#attrsArray<NamedAttribute> (readonly)

Returns Additional named attributes.

Returns:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/coradoc/asciidoc/model/section.rb', line 39

class Section < Base
  include Coradoc::AsciiDoc::Model::Anchorable

  def block_level?
    true
  end

  attribute :id, :string
  attribute :content, :string
  attribute :title, Coradoc::AsciiDoc::Model::Title
  attribute :attrs,
            Coradoc::AsciiDoc::Model::NamedAttribute,
            collection: true,
            initialize_empty: true
  attribute :contents,
            Coradoc::AsciiDoc::Model::Paragraph,
            collection: true,
            initialize_empty: true
  attribute :sections,
            Coradoc::AsciiDoc::Model::Section,
            collection: true,
            initialize_empty: true
  # attribute :anchor, Coradoc::AsciiDoc::Model::Inline::Anchor

  # Allow setting level directly during initialization
  def initialize(**attributes)
    level_value = attributes.delete(:level)
    super
    if level_value && title
      title.level_int = level_value
    elsif level_value
      self.title = Coradoc::AsciiDoc::Model::Title.new(content: '', level_int: level_value)
    end
  end

  def validate
    validate_title_type
    super
  end

  # Get the section level from the title
  # @return [Integer, nil] The section level (0-5 for standard sections)
  def level
    title&.level_int
  end

  # Set the section level on the title
  # @param value [Integer] The section level
  def level=(value)
    if title
      title.level_int = value
    else
      self.title = Coradoc::AsciiDoc::Model::Title.new(content: '', level_int: value)
    end
  end

  def safe_to_collapse?
    title.nil? && sections.empty?
  end

  private

  def validate_title_type
    return if title.nil? || title.is_a?(Coradoc::AsciiDoc::Model::Title)

    raise TypeError, "title must be a Coradoc::AsciiDoc::Model::Title, got #{title.class}"
  end
end

#contentString? (readonly)

Returns Optional string content (typically unused, see contents).

Returns:

  • (String, nil)

    Optional string content (typically unused, see contents)



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/coradoc/asciidoc/model/section.rb', line 39

class Section < Base
  include Coradoc::AsciiDoc::Model::Anchorable

  def block_level?
    true
  end

  attribute :id, :string
  attribute :content, :string
  attribute :title, Coradoc::AsciiDoc::Model::Title
  attribute :attrs,
            Coradoc::AsciiDoc::Model::NamedAttribute,
            collection: true,
            initialize_empty: true
  attribute :contents,
            Coradoc::AsciiDoc::Model::Paragraph,
            collection: true,
            initialize_empty: true
  attribute :sections,
            Coradoc::AsciiDoc::Model::Section,
            collection: true,
            initialize_empty: true
  # attribute :anchor, Coradoc::AsciiDoc::Model::Inline::Anchor

  # Allow setting level directly during initialization
  def initialize(**attributes)
    level_value = attributes.delete(:level)
    super
    if level_value && title
      title.level_int = level_value
    elsif level_value
      self.title = Coradoc::AsciiDoc::Model::Title.new(content: '', level_int: level_value)
    end
  end

  def validate
    validate_title_type
    super
  end

  # Get the section level from the title
  # @return [Integer, nil] The section level (0-5 for standard sections)
  def level
    title&.level_int
  end

  # Set the section level on the title
  # @param value [Integer] The section level
  def level=(value)
    if title
      title.level_int = value
    else
      self.title = Coradoc::AsciiDoc::Model::Title.new(content: '', level_int: value)
    end
  end

  def safe_to_collapse?
    title.nil? && sections.empty?
  end

  private

  def validate_title_type
    return if title.nil? || title.is_a?(Coradoc::AsciiDoc::Model::Title)

    raise TypeError, "title must be a Coradoc::AsciiDoc::Model::Title, got #{title.class}"
  end
end

#contentsArray<Paragraph> (readonly)

Returns Paragraph content within this section.

Returns:

  • (Array<Paragraph>)

    Paragraph content within this section



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/coradoc/asciidoc/model/section.rb', line 39

class Section < Base
  include Coradoc::AsciiDoc::Model::Anchorable

  def block_level?
    true
  end

  attribute :id, :string
  attribute :content, :string
  attribute :title, Coradoc::AsciiDoc::Model::Title
  attribute :attrs,
            Coradoc::AsciiDoc::Model::NamedAttribute,
            collection: true,
            initialize_empty: true
  attribute :contents,
            Coradoc::AsciiDoc::Model::Paragraph,
            collection: true,
            initialize_empty: true
  attribute :sections,
            Coradoc::AsciiDoc::Model::Section,
            collection: true,
            initialize_empty: true
  # attribute :anchor, Coradoc::AsciiDoc::Model::Inline::Anchor

  # Allow setting level directly during initialization
  def initialize(**attributes)
    level_value = attributes.delete(:level)
    super
    if level_value && title
      title.level_int = level_value
    elsif level_value
      self.title = Coradoc::AsciiDoc::Model::Title.new(content: '', level_int: level_value)
    end
  end

  def validate
    validate_title_type
    super
  end

  # Get the section level from the title
  # @return [Integer, nil] The section level (0-5 for standard sections)
  def level
    title&.level_int
  end

  # Set the section level on the title
  # @param value [Integer] The section level
  def level=(value)
    if title
      title.level_int = value
    else
      self.title = Coradoc::AsciiDoc::Model::Title.new(content: '', level_int: value)
    end
  end

  def safe_to_collapse?
    title.nil? && sections.empty?
  end

  private

  def validate_title_type
    return if title.nil? || title.is_a?(Coradoc::AsciiDoc::Model::Title)

    raise TypeError, "title must be a Coradoc::AsciiDoc::Model::Title, got #{title.class}"
  end
end

#idString? (readonly)

Returns Optional identifier for the section.

Returns:

  • (String, nil)

    Optional identifier for the section



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/coradoc/asciidoc/model/section.rb', line 39

class Section < Base
  include Coradoc::AsciiDoc::Model::Anchorable

  def block_level?
    true
  end

  attribute :id, :string
  attribute :content, :string
  attribute :title, Coradoc::AsciiDoc::Model::Title
  attribute :attrs,
            Coradoc::AsciiDoc::Model::NamedAttribute,
            collection: true,
            initialize_empty: true
  attribute :contents,
            Coradoc::AsciiDoc::Model::Paragraph,
            collection: true,
            initialize_empty: true
  attribute :sections,
            Coradoc::AsciiDoc::Model::Section,
            collection: true,
            initialize_empty: true
  # attribute :anchor, Coradoc::AsciiDoc::Model::Inline::Anchor

  # Allow setting level directly during initialization
  def initialize(**attributes)
    level_value = attributes.delete(:level)
    super
    if level_value && title
      title.level_int = level_value
    elsif level_value
      self.title = Coradoc::AsciiDoc::Model::Title.new(content: '', level_int: level_value)
    end
  end

  def validate
    validate_title_type
    super
  end

  # Get the section level from the title
  # @return [Integer, nil] The section level (0-5 for standard sections)
  def level
    title&.level_int
  end

  # Set the section level on the title
  # @param value [Integer] The section level
  def level=(value)
    if title
      title.level_int = value
    else
      self.title = Coradoc::AsciiDoc::Model::Title.new(content: '', level_int: value)
    end
  end

  def safe_to_collapse?
    title.nil? && sections.empty?
  end

  private

  def validate_title_type
    return if title.nil? || title.is_a?(Coradoc::AsciiDoc::Model::Title)

    raise TypeError, "title must be a Coradoc::AsciiDoc::Model::Title, got #{title.class}"
  end
end

#sectionsArray<Section> (readonly)

Returns Nested subsections.

Returns:

  • (Array<Section>)

    Nested subsections



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/coradoc/asciidoc/model/section.rb', line 39

class Section < Base
  include Coradoc::AsciiDoc::Model::Anchorable

  def block_level?
    true
  end

  attribute :id, :string
  attribute :content, :string
  attribute :title, Coradoc::AsciiDoc::Model::Title
  attribute :attrs,
            Coradoc::AsciiDoc::Model::NamedAttribute,
            collection: true,
            initialize_empty: true
  attribute :contents,
            Coradoc::AsciiDoc::Model::Paragraph,
            collection: true,
            initialize_empty: true
  attribute :sections,
            Coradoc::AsciiDoc::Model::Section,
            collection: true,
            initialize_empty: true
  # attribute :anchor, Coradoc::AsciiDoc::Model::Inline::Anchor

  # Allow setting level directly during initialization
  def initialize(**attributes)
    level_value = attributes.delete(:level)
    super
    if level_value && title
      title.level_int = level_value
    elsif level_value
      self.title = Coradoc::AsciiDoc::Model::Title.new(content: '', level_int: level_value)
    end
  end

  def validate
    validate_title_type
    super
  end

  # Get the section level from the title
  # @return [Integer, nil] The section level (0-5 for standard sections)
  def level
    title&.level_int
  end

  # Set the section level on the title
  # @param value [Integer] The section level
  def level=(value)
    if title
      title.level_int = value
    else
      self.title = Coradoc::AsciiDoc::Model::Title.new(content: '', level_int: value)
    end
  end

  def safe_to_collapse?
    title.nil? && sections.empty?
  end

  private

  def validate_title_type
    return if title.nil? || title.is_a?(Coradoc::AsciiDoc::Model::Title)

    raise TypeError, "title must be a Coradoc::AsciiDoc::Model::Title, got #{title.class}"
  end
end

#titleTitle (readonly)

Returns Section title.

Returns:

  • (Title)

    Section title



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/coradoc/asciidoc/model/section.rb', line 39

class Section < Base
  include Coradoc::AsciiDoc::Model::Anchorable

  def block_level?
    true
  end

  attribute :id, :string
  attribute :content, :string
  attribute :title, Coradoc::AsciiDoc::Model::Title
  attribute :attrs,
            Coradoc::AsciiDoc::Model::NamedAttribute,
            collection: true,
            initialize_empty: true
  attribute :contents,
            Coradoc::AsciiDoc::Model::Paragraph,
            collection: true,
            initialize_empty: true
  attribute :sections,
            Coradoc::AsciiDoc::Model::Section,
            collection: true,
            initialize_empty: true
  # attribute :anchor, Coradoc::AsciiDoc::Model::Inline::Anchor

  # Allow setting level directly during initialization
  def initialize(**attributes)
    level_value = attributes.delete(:level)
    super
    if level_value && title
      title.level_int = level_value
    elsif level_value
      self.title = Coradoc::AsciiDoc::Model::Title.new(content: '', level_int: level_value)
    end
  end

  def validate
    validate_title_type
    super
  end

  # Get the section level from the title
  # @return [Integer, nil] The section level (0-5 for standard sections)
  def level
    title&.level_int
  end

  # Set the section level on the title
  # @param value [Integer] The section level
  def level=(value)
    if title
      title.level_int = value
    else
      self.title = Coradoc::AsciiDoc::Model::Title.new(content: '', level_int: value)
    end
  end

  def safe_to_collapse?
    title.nil? && sections.empty?
  end

  private

  def validate_title_type
    return if title.nil? || title.is_a?(Coradoc::AsciiDoc::Model::Title)

    raise TypeError, "title must be a Coradoc::AsciiDoc::Model::Title, got #{title.class}"
  end
end

Instance Method Details

#block_level?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/coradoc/asciidoc/model/section.rb', line 42

def block_level?
  true
end

#levelInteger?

Get the section level from the title

Returns:

  • (Integer, nil)

    The section level (0-5 for standard sections)



81
82
83
# File 'lib/coradoc/asciidoc/model/section.rb', line 81

def level
  title&.level_int
end

#level=(value) ⇒ Object

Set the section level on the title

Parameters:

  • value (Integer)

    The section level



87
88
89
90
91
92
93
# File 'lib/coradoc/asciidoc/model/section.rb', line 87

def level=(value)
  if title
    title.level_int = value
  else
    self.title = Coradoc::AsciiDoc::Model::Title.new(content: '', level_int: value)
  end
end

#safe_to_collapse?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/coradoc/asciidoc/model/section.rb', line 95

def safe_to_collapse?
  title.nil? && sections.empty?
end

#validateObject



74
75
76
77
# File 'lib/coradoc/asciidoc/model/section.rb', line 74

def validate
  validate_title_type
  super
end