Class: Uniword::Toc::TocGenerator
- Inherits:
-
Object
- Object
- Uniword::Toc::TocGenerator
- Defined in:
- lib/uniword/toc/toc_generator.rb
Overview
Orchestrator for Table of Contents operations.
Scans a document for heading paragraphs, builds TocEntry objects, inserts a TOC field into the document, and refreshes existing TOC fields.
Heading detection looks at paragraph style references matching “Heading1” through “Heading6” (case-insensitive, with or without space between “Heading” and the digit).
Constant Summary collapse
- HEADING_PATTERN =
Pattern matching heading style names like “Heading1” or “Heading 1”
/^Heading\s*(\d+)$/i- MAX_LEVEL =
Maximum heading level recognized
6
Instance Attribute Summary collapse
-
#document ⇒ Wordprocessingml::DocumentRoot
readonly
The source document.
Instance Method Summary collapse
-
#generate(max_level: MAX_LEVEL) ⇒ Array<TocEntry>
Generate TOC entries by scanning paragraphs for heading styles.
-
#initialize(document) ⇒ TocGenerator
constructor
Initialize the generator with a document.
-
#insert(toc_entries, position: 0, max_level: MAX_LEVEL) ⇒ void
Insert a TOC field into the document at the given position.
-
#update(max_level: MAX_LEVEL) ⇒ Array<TocEntry>
Update an existing TOC in the document.
Constructor Details
#initialize(document) ⇒ TocGenerator
Initialize the generator with a document.
42 43 44 |
# File 'lib/uniword/toc/toc_generator.rb', line 42 def initialize(document) @document = document end |
Instance Attribute Details
#document ⇒ Wordprocessingml::DocumentRoot (readonly)
Returns The source document.
37 38 39 |
# File 'lib/uniword/toc/toc_generator.rb', line 37 def document @document end |
Instance Method Details
#generate(max_level: MAX_LEVEL) ⇒ Array<TocEntry>
Generate TOC entries by scanning paragraphs for heading styles.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/uniword/toc/toc_generator.rb', line 50 def generate(max_level: MAX_LEVEL) entries = [] document.paragraphs.each_with_index do |paragraph, index| level = heading_level(paragraph) next unless level next if level > max_level text = extract_text(paragraph) next if text.nil? || text.strip.empty? style_name = resolve_style_name(paragraph) entries << TocEntry.new( level: level, text: text.strip, style_name: style_name, paragraph_index: index, ) end entries end |
#insert(toc_entries, position: 0, max_level: MAX_LEVEL) ⇒ void
This method returns an undefined value.
Insert a TOC field into the document at the given position.
Creates a Structured Document Tag (SDT) containing:
-
A title paragraph (“Table of Contents”)
-
A TOC field instruction paragraph
-
Placeholder paragraphs for each entry
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/uniword/toc/toc_generator.rb', line 85 def insert(toc_entries, position: 0, max_level: MAX_LEVEL) sdt = build_toc_sdt(toc_entries, max_level: max_level) body = document.body body. << sdt # Add the SDT to element_order so it serializes correctly return unless body.element_order insert_element = Lutaml::Xml::Element.new("Element", "sdt") if position.positive? && body.element_order.size >= position body.element_order.insert(position, insert_element) else body.element_order.insert(0, insert_element) end end |
#update(max_level: MAX_LEVEL) ⇒ Array<TocEntry>
Update an existing TOC in the document.
Re-scans headings and rebuilds the first TOC SDT found. If no existing TOC is found, does nothing and returns an empty array.
109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/uniword/toc/toc_generator.rb', line 109 def update(max_level: MAX_LEVEL) entries = generate(max_level: max_level) return [] if entries.empty? # Find the first TOC SDT in the body toc_sdt = find_toc_sdt return [] unless toc_sdt # Rebuild the SDT content rebuild_sdt_content(toc_sdt, entries, max_level: max_level) entries end |