Class: Uniword::Assembly::TocInstruction

Inherits:
Lutaml::Model::Serializable
  • Object
show all
Defined in:
lib/uniword/assembly/toc_instruction.rb

Overview

Table of Contents field instruction.

Represents the TOC field code that Word uses to generate and update TOC. Example field code: { TOC o “1-3” h z u }

Examples:

Create TOC instruction

instr = TocInstruction.new(
  outline_levels: "1-3",
  hyperlinks: true,
  hidden_formats: true,
  use_paragraph_outline: true
)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse(str) ⇒ TocInstruction

Parse a TOC field instruction string.

Examples:

TocInstruction.parse('TOC \\o "1-3" \\h \\z')

Parameters:

  • str (String)

    The field instruction string

Returns:



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/uniword/assembly/toc_instruction.rb', line 68

def self.parse(str)
  return nil if str.nil? || str.strip.empty?

  instr = new

  # Parse outline levels: \o "1-3"
  instr.outline_levels = ::Regexp.last_match(1) if str =~ /\\o\s+"([^"]+)"/

  # Parse switches
  instr.hyperlinks = str.include?('\\h')
  instr.hidden_formats = str.include?('\\z')
  instr.use_paragraph_outline = str.include?('\\u')
  instr.preserve_tabs = str.include?('\\w')

  # Parse separator: \s "separator"
  instr.separator = ::Regexp.last_match(1) if str =~ /\\s\s+"([^"]+)"/

  # Parse leader: \l "leader"
  instr.leader = ::Regexp.last_match(1) if str =~ /\\l\s+"([^"]+)"/

  instr
end

Instance Method Details

#hidden_formatsBoolean

Returns Use hidden formats.

Returns:

  • (Boolean)

    Use hidden formats



27
# File 'lib/uniword/assembly/toc_instruction.rb', line 27

attribute :hidden_formats, :boolean, default: -> { true }

Returns Include hyperlinks in TOC.

Returns:

  • (Boolean)

    Include hyperlinks in TOC



24
# File 'lib/uniword/assembly/toc_instruction.rb', line 24

attribute :hyperlinks, :boolean, default: -> { true }

#leaderString?

Returns Custom leader.

Returns:

  • (String, nil)

    Custom leader



39
# File 'lib/uniword/assembly/toc_instruction.rb', line 39

attribute :leader, :string

#outline_levelsString?

Returns Outline levels to include (e.g., “1-3”).

Returns:

  • (String, nil)

    Outline levels to include (e.g., “1-3”)



21
# File 'lib/uniword/assembly/toc_instruction.rb', line 21

attribute :outline_levels, :string

#preserve_tabsBoolean

Returns Preserve tab stops.

Returns:

  • (Boolean)

    Preserve tab stops



33
# File 'lib/uniword/assembly/toc_instruction.rb', line 33

attribute :preserve_tabs, :boolean, default: -> { false }

#separatorString?

Returns Custom separator.

Returns:

  • (String, nil)

    Custom separator



36
# File 'lib/uniword/assembly/toc_instruction.rb', line 36

attribute :separator, :string

#to_sString

Generate the field instruction string.

Examples:

instr.to_s # => "TOC \\o \"1-3\" \\h \\z \\u"

Returns:

  • (String)

    The TOC field instruction



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/uniword/assembly/toc_instruction.rb', line 47

def to_s
  parts = ["TOC"]

  parts << "\\o \"#{outline_levels}\"" if outline_levels
  parts << '\\h' if hyperlinks
  parts << '\\z' if hidden_formats
  parts << '\\u' if use_paragraph_outline
  parts << '\\w' if preserve_tabs
  parts << "\\s \"#{separator}\"" if separator
  parts << "\\l \"#{leader}\"" if leader

  parts.join(" ")
end

#use_paragraph_outlineBoolean

Returns Use paragraph outline level.

Returns:

  • (Boolean)

    Use paragraph outline level



30
# File 'lib/uniword/assembly/toc_instruction.rb', line 30

attribute :use_paragraph_outline, :boolean, default: -> { true }