Module: Uniword::Docx::Reconciler::PackageStructure

Included in:
Uniword::Docx::Reconciler
Defined in:
lib/uniword/docx/reconciler/package_structure.rb

Overview

Package-level consistency enforcement.

Rebuilds content types and relationships for standard parts, preserving non-standard entries from the source document.

Constant Summary collapse

UNSUPPORTED_REL_TYPES =

Relationship types for parts we don't model or serialize.

Set[
  "http://schemas.microsoft.com/office/2007/relationships/stylesWithEffects",
].freeze
PACKAGE_LEVEL_REL_TYPES =

Rel types that belong in package-level _rels/.rels, not document.xml.rels.

Set[
  "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument",
  "http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties",
  "http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties",
  "http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties",
].freeze

Instance Method Summary collapse

Instance Method Details

#reconcile_content_typesObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/uniword/docx/reconciler/package_structure.rb', line 24

def reconcile_content_types
  ct = package.content_types
  return unless ct

  ct.defaults = [
    Uniword::ContentTypes::Default.new(
      extension: "rels",
      content_type: "application/vnd.openxmlformats-package.relationships+xml",
    ),
    Uniword::ContentTypes::Default.new(
      extension: "xml",
      content_type: "application/xml",
    ),
  ]

  standard = content_type_overrides_for_present_parts
  standard_parts = standard.to_set(&:part_name)
  non_standard = ct.overrides.reject do |o|
    standard_parts.include?(o.part_name)
  end

  ct.overrides = standard + non_standard
  record_fix(FixCodes::CONTENT_TYPES_ASSEMBLED, "Rebuilt content types for standard parts")
end

#reconcile_document_relsObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/uniword/docx/reconciler/package_structure.rb', line 82

def reconcile_document_rels
  rels = package.document_rels
  return unless rels

  base = "http://schemas.openxmlformats.org/officeDocument/2006/relationships"
  defs = [
    ["styles", "styles.xml", package.styles],
    ["settings", "settings.xml", package.settings],
    ["webSettings", "webSettings.xml", package.web_settings],
    ["fontTable", "fontTable.xml", package.font_table],
    ["theme", "theme/theme1.xml", package.theme],
    ["numbering", "numbering.xml", package.numbering],
    ["footnotes", "footnotes.xml", package.footnotes],
    ["endnotes", "endnotes.xml", package.endnotes],
  ]

  standard_targets = defs.filter_map { |_, target, obj| target if obj }.to_set

  # If allocator is present, use it to build rels — preserves existing rIds
  alloc = allocator
  if alloc
    reconcile_document_rels_from_allocator(rels, base, defs, standard_targets, alloc)
  else
    reconcile_document_rels_legacy(rels, base, defs, standard_targets)
  end
end

#reconcile_package_relsObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/uniword/docx/reconciler/package_structure.rb', line 49

def reconcile_package_rels
  rels = package.package_rels
  return unless rels

  base = "http://schemas.openxmlformats.org"
  standard_defs = [
    ["rId1",
     "#{base}/officeDocument/2006/relationships/officeDocument",
     "word/document.xml"],
    ["rId2",
     "#{base}/package/2006/relationships/metadata/core-properties",
     "docProps/core.xml"],
    ["rId3",
     "#{base}/officeDocument/2006/relationships/extended-properties",
     "docProps/app.xml"],
  ]

  standard_targets = standard_defs.to_set { |_, _, t| t }
  standard_rids = standard_defs.to_set { |rid, _, _| rid }
  non_standard = rels.relationships.reject do |r|
    standard_targets.include?(r.target) || standard_rids.include?(r.id)
  end

  existing_by_target = rels.relationships.to_h { |r| [r.target, r] }
  standard = standard_defs.map do |rid, type, target|
    existing = existing_by_target[target]
    build_rel(existing ? existing.id : rid, type, target)
  end

  rels.relationships = standard + non_standard
  record_fix(FixCodes::RELATIONSHIPS_ASSEMBLED, "Rebuilt package relationships for standard parts")
end