Class: Uniword::Builder::TocBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/uniword/builder/toc_builder.rb

Overview

Builds Table of Contents for documents.

Examples:

Add a TOC to a document

doc.paragraph do |p|
  p << Builder.toc('Table of Contents')
end

Class Method Summary collapse

Class Method Details

.build(title: "Table of Contents", styles: nil) ⇒ Array<Wordprocessingml::Paragraph>

Create a TOC as structured content (paragraphs with field codes)

Parameters:

  • title (String) (defaults to: "Table of Contents")

    TOC title

  • styles (Array<String>) (defaults to: nil)

    Heading styles to include

Returns:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/uniword/builder/toc_builder.rb', line 17

def self.build(title: "Table of Contents", styles: nil)
  paragraphs = []

  # Title paragraph
  title_para = Wordprocessingml::Paragraph.new
  title_run = Wordprocessingml::Run.new(text: title)
  title_para.runs << title_run
  paragraphs << title_para

  # TOC field paragraph
  toc_para = Wordprocessingml::Paragraph.new

  # Field begin
  begin_char = Wordprocessingml::FieldChar.new
  begin_char.fldCharType = "begin"
  toc_para.field_chars << begin_char

  # Instruction text
  instr = Wordprocessingml::InstrText.new
  instr.text = ' TOC \\o "1-3" \\h \\z \\u '
  toc_para.instr_text << instr

  # Field separate
  sep_char = Wordprocessingml::FieldChar.new
  sep_char.fldCharType = "separate"
  toc_para.field_chars << sep_char

  # Field end
  end_char = Wordprocessingml::FieldChar.new
  end_char.fldCharType = "end"
  toc_para.field_chars << end_char

  paragraphs << toc_para
  paragraphs
end