Class: Uniword::Toc::TocEntry

Inherits:
Object
  • Object
show all
Defined in:
lib/uniword/toc/toc_entry.rb

Overview

Value object representing a single entry in a Table of Contents.

Each entry corresponds to a heading paragraph found in the document. Holds the heading level, text, page number (if known), style name, and the paragraph index within the document body.

Examples:

Create a TOC entry

entry = TocEntry.new(
  level: 1,
  text: "Introduction",
  style_name: "Heading1",
  paragraph_index: 3
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(level:, text:, page: nil, style_name: nil, paragraph_index: nil) ⇒ TocEntry

Initialize a TOC entry.

Parameters:

  • level (Integer)

    Heading level (1-6)

  • text (String)

    Heading text content

  • page (Integer, nil) (defaults to: nil)

    Page number, if known

  • style_name (String, nil) (defaults to: nil)

    Paragraph style name

  • paragraph_index (Integer, nil) (defaults to: nil)

    Index of the source paragraph



41
42
43
44
45
46
47
48
# File 'lib/uniword/toc/toc_entry.rb', line 41

def initialize(level:, text:, page: nil, style_name: nil,
               paragraph_index: nil)
  @level = level
  @text = text
  @page = page
  @style_name = style_name
  @paragraph_index = paragraph_index
end

Instance Attribute Details

#levelInteger (readonly)

Returns Heading level (1-6).

Returns:

  • (Integer)

    Heading level (1-6)



20
21
22
# File 'lib/uniword/toc/toc_entry.rb', line 20

def level
  @level
end

#pageInteger? (readonly)

Returns Page number (populated by Word after update).

Returns:

  • (Integer, nil)

    Page number (populated by Word after update)



26
27
28
# File 'lib/uniword/toc/toc_entry.rb', line 26

def page
  @page
end

#paragraph_indexInteger? (readonly)

Returns Index of the paragraph in the document body.

Returns:

  • (Integer, nil)

    Index of the paragraph in the document body



32
33
34
# File 'lib/uniword/toc/toc_entry.rb', line 32

def paragraph_index
  @paragraph_index
end

#style_nameString? (readonly)

Returns Style name that produced this entry.

Returns:

  • (String, nil)

    Style name that produced this entry



29
30
31
# File 'lib/uniword/toc/toc_entry.rb', line 29

def style_name
  @style_name
end

#textString (readonly)

Returns Heading text content.

Returns:

  • (String)

    Heading text content



23
24
25
# File 'lib/uniword/toc/toc_entry.rb', line 23

def text
  @text
end

Instance Method Details

#to_hHash

Convert to a hash suitable for JSON output.

Returns:

  • (Hash)


62
63
64
65
66
67
68
69
70
# File 'lib/uniword/toc/toc_entry.rb', line 62

def to_h
  {
    level: level,
    text: text,
    page: page,
    style_name: style_name,
    paragraph_index: paragraph_index,
  }
end

#to_sString

Human-readable representation.

Returns:

  • (String)


53
54
55
56
57
# File 'lib/uniword/toc/toc_entry.rb', line 53

def to_s
  indent = "  " * (level - 1)
  page_str = page ? " (p.#{page})" : ""
  "#{indent}#{text}#{page_str}"
end