Class: Uniword::Docx::HeaderFooterPart

Inherits:
Part
  • Object
show all
Defined in:
lib/uniword/docx/header_footer_part.rb

Overview

A header or footer part (word/headerN.xml / word/footerN.xml) held by the package — the single storage form for both round-tripped and builder-created headers/footers.

Parts loaded from an existing DOCX carry their original r_id, target and rel_type verbatim and are flagged loaded so the reconciler leaves their relationships and section references untouched. Parts added programmatically (via the Builder or the +headers+/+footers+ views) are flagged fresh: the reconciler assigns them a relationship and wires the sectPr reference.

The type ("default"/"first"/"even") is the sectPr reference type — derived from the section properties on load, set by the caller on insert. Content is usually a Wordprocessingml::Header or Wordprocessingml::Footer model; legacy Uniword::Header/Uniword::Footer convenience models are stored as given and converted at serialization time so callers can keep mutating the object they inserted.

Constant Summary collapse

TYPES =

Valid section reference types.

%w[default first even].freeze

Constants inherited from Part

Part::HASH_LOOKUP

Instance Attribute Summary collapse

Attributes inherited from Part

#content, #content_type, #r_id, #rel_type, #target

Instance Method Summary collapse

Methods inherited from Part

from_hash, #package_paths, #path

Constructor Details

#initialize(kind: nil, type: nil, loaded: false, **rest) ⇒ HeaderFooterPart

Returns a new instance of HeaderFooterPart.

Parameters:

  • kind (Symbol, nil) (defaults to: nil)

    :header or :footer (derived from rel_type, target or content when omitted)

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

    sectPr reference type

  • loaded (Boolean) (defaults to: false)

    true when extracted verbatim from an existing package



36
37
38
39
40
41
# File 'lib/uniword/docx/header_footer_part.rb', line 36

def initialize(kind: nil, type: nil, loaded: false, **rest)
  super(**rest)
  @kind = kind
  @type = type&.to_s
  @loaded = loaded
end

Instance Attribute Details

#loaded=(value) ⇒ Object (writeonly)

Mark whether the part originates from a loaded package.



65
66
67
# File 'lib/uniword/docx/header_footer_part.rb', line 65

def loaded=(value)
  @loaded = value
end

#typeString?

Returns sectPr reference type ("default"/"first"/"even").

Returns:

  • (String, nil)

    sectPr reference type ("default"/"first"/"even")



29
30
31
# File 'lib/uniword/docx/header_footer_part.rb', line 29

def type
  @type
end

Instance Method Details

#[](key) ⇒ Object

Hash-style read compatibility (+:type+ in addition to the Part keys).



95
96
97
# File 'lib/uniword/docx/header_footer_part.rb', line 95

def [](key)
  key.to_sym == :type ? type : super
end

#definitionOoxml::PartDefinition?

Registry definition for this part kind.

Returns:



70
71
72
# File 'lib/uniword/docx/header_footer_part.rb', line 70

def definition
  @definition ||= kind && Ooxml::PartRegistry.find_by_key(kind)
end

#empty?Boolean

Returns true when the content has no paragraphs/tables.

Returns:

  • (Boolean)

    true when the content has no paragraphs/tables



87
88
89
90
91
# File 'lib/uniword/docx/header_footer_part.rb', line 87

def empty?
  return true unless content

  paragraphs.empty? && tables.empty?
end

#footer?Boolean

Returns true for footer parts.

Returns:

  • (Boolean)

    true for footer parts



54
55
56
# File 'lib/uniword/docx/header_footer_part.rb', line 54

def footer?
  kind == :footer
end

#header?Boolean

Returns true for header parts.

Returns:

  • (Boolean)

    true for header parts



49
50
51
# File 'lib/uniword/docx/header_footer_part.rb', line 49

def header?
  kind == :header
end

#kindSymbol?

Returns :header or :footer.

Returns:

  • (Symbol, nil)

    :header or :footer



44
45
46
# File 'lib/uniword/docx/header_footer_part.rb', line 44

def kind
  @kind ||= derive_kind
end

#loaded?Boolean

Returns true when extracted from an existing package (relationships and section references are already in place).

Returns:

  • (Boolean)

    true when extracted from an existing package (relationships and section references are already in place)



60
61
62
# File 'lib/uniword/docx/header_footer_part.rb', line 60

def loaded?
  !!@loaded
end

#paragraphsArray<Wordprocessingml::Paragraph>

Returns content paragraphs.

Returns:



77
78
79
# File 'lib/uniword/docx/header_footer_part.rb', line 77

def paragraphs
  content&.paragraphs
end

#serializable_contentObject

The content model to serialize into the part file. Legacy Uniword::Header/Uniword::Footer models are converted to their Wordprocessingml counterparts (paragraph and table elements are shared, mc:Ignorable carried over); other content passes through unchanged.

Returns:

  • (Object)

    serializable content model



106
107
108
109
110
111
112
113
114
115
# File 'lib/uniword/docx/header_footer_part.rb', line 106

def serializable_content
  case content
  when Uniword::Header
    convert_legacy(Wordprocessingml::Header.new)
  when Uniword::Footer
    convert_legacy(Wordprocessingml::Footer.new)
  else
    content
  end
end

#tablesArray<Wordprocessingml::Table>

Returns content tables.

Returns:



82
83
84
# File 'lib/uniword/docx/header_footer_part.rb', line 82

def tables
  content&.tables
end