Class: Uniword::Docx::Package

Inherits:
Lutaml::Model::Serializable
  • Object
show all
Includes:
PackageDefaults, PackageSerialization
Defined in:
lib/uniword/docx/package.rb

Overview

DOCX Package - Complete DOCX file format model

Represents the entire .docx file structure as a lutaml-model object. Each XML file within the ZIP is a separate lutaml-model class.

A DOCX package CONTAINS OOXML markup wrapped in an OPC ZIP container. This class lives in Uniword::Docx, not Uniword::Ooxml, because DOCX is a file format that uses OOXML, not the other way around.

Examples:

Load DOCX

package = Package.from_file('document.docx')
package.core_properties.title = 'New Title'
package.to_file('output.docx')

Access document content

package = Package.from_file('document.docx')
package.document.body.paragraphs.each { |p| puts p.text }

Constant Summary

Constants included from PackageSerialization

Uniword::Docx::PackageSerialization::DOCX_INFRA_OPTIONS, Uniword::Docx::PackageSerialization::DOCX_PART_OPTIONS, Uniword::Docx::PackageSerialization::DOCX_PROPS_OPTIONS, Uniword::Docx::PackageSerialization::DOCX_XML_OPTIONS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PackageSerialization

#add_standalone, #inject_part_relationships, #serialize_infrastructure, #serialize_package_parts, #serialize_part

Methods included from PackageDefaults

included

Instance Attribute Details

#allocatorObject

Central ID allocator — owns all rId, footnote, bookmark, etc. assignment. Seeded from template rels on load; used by builders during construction.



114
115
116
# File 'lib/uniword/docx/package.rb', line 114

def allocator
  @allocator
end

#bibliography_sourcesObject

Non-serialized attributes (DOCX packaging helpers)



97
98
99
# File 'lib/uniword/docx/package.rb', line 97

def bibliography_sources
  @bibliography_sources
end

#chart_partsObject

Non-serialized attributes (DOCX packaging helpers)



97
98
99
# File 'lib/uniword/docx/package.rb', line 97

def chart_parts
  @chart_parts
end

#custom_xml_itemsArray<CustomXmlItem>?

Custom XML data items (customXml/item*.xml)

Returns:



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

def custom_xml_items
  @custom_xml_items
end

#endnotes_relsObject

Returns the value of attribute endnotes_rels.



98
99
100
# File 'lib/uniword/docx/package.rb', line 98

def endnotes_rels
  @endnotes_rels
end

#footnotes_relsObject

Returns the value of attribute footnotes_rels.



98
99
100
# File 'lib/uniword/docx/package.rb', line 98

def footnotes_rels
  @footnotes_rels
end

#modified_part_pathsObject

Paths that have been modified and should not use raw XML passthrough.



122
123
124
# File 'lib/uniword/docx/package.rb', line 122

def modified_part_paths
  @modified_part_paths
end

#profileObject

Non-serialized attributes (DOCX packaging helpers)



97
98
99
# File 'lib/uniword/docx/package.rb', line 97

def profile
  @profile
end

#raw_xml_partsObject

Raw XML from template ZIP for unmodified parts. When present, these are used verbatim instead of re-serializing through lutaml-model (which drops unmapped elements).



119
120
121
# File 'lib/uniword/docx/package.rb', line 119

def raw_xml_parts
  @raw_xml_parts
end

#settings_relsObject

Returns the value of attribute settings_rels.



98
99
100
# File 'lib/uniword/docx/package.rb', line 98

def settings_rels
  @settings_rels
end

Class Method Details



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/uniword/docx/package.rb', line 358

def self.extract_header_footer_parts(zip_content, package)
  return unless package.document && package.document_rels

  header_files = zip_content.keys.grep(%r{^word/header\d+\.xml$})
  footer_files = zip_content.keys.grep(%r{^word/footer\d+\.xml$})

  return if header_files.empty? && footer_files.empty?

  store = package.document.header_footer_parts
  ref_types = header_footer_reference_types(package.document)

  header_files.sort.each do |path|
    target = path.sub("word/", "")
    rel = package.document_rels.relationships.find do |r|
      r.target == target &&
        r.type.to_s.include?("officeDocument/2006/relationships/header")
    end
    next unless rel

    store << HeaderFooterPart.new(
      kind: :header,
      r_id: rel.id,
      target: target,
      rel_type: rel.type,
      type: ref_types[rel.id],
      content: Uniword::Wordprocessingml::Header.from_xml(zip_content[path]),
      loaded: true,
    )
  end

  footer_files.sort.each do |path|
    target = path.sub("word/", "")
    rel = package.document_rels.relationships.find do |r|
      r.target == target &&
        r.type.to_s.include?("officeDocument/2006/relationships/footer")
    end
    next unless rel

    store << HeaderFooterPart.new(
      kind: :footer,
      r_id: rel.id,
      target: target,
      rel_type: rel.type,
      type: ref_types[rel.id],
      content: Uniword::Wordprocessingml::Footer.from_xml(zip_content[path]),
      loaded: true,
    )
  end
end

.extract_image_parts(zip_content, package, zip_path = nil) ⇒ Object

Extract image files from word/media/ directory in DOCX

Parameters:

  • zip_content (Hash)

    Extracted ZIP content (may have corrupted binary)

  • package (Package)

    Package to populate

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

    Original ZIP path for binary re-extraction



442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
# File 'lib/uniword/docx/package.rb', line 442

def self.extract_image_parts(zip_content, package, zip_path = nil)
  return unless package.document

  media_files = zip_content.keys.grep(%r{^word/media/.+$})
  return if media_files.empty?

  package.document.image_parts ||= {}

  media_files.each do |media_path|
    filename = File.basename(media_path)
    ext = File.extname(filename).delete(".").downcase
    content_type = case ext
                   when "jpg", "jpeg" then "image/jpeg"
                   when "png" then "image/png"
                   when "gif" then "image/gif"
                   when "bmp" then "image/bmp"
                   when "tiff", "tif" then "image/tiff"
                   when "svg" then "image/svg+xml"
                   else "image/#{ext}"
                   end

    # Key the part by the rId the loaded document rels assign to
    # this target, so downstream consumers (MHTML rendering, image
    # manager) resolve r:embed references correctly. Media with no
    # document-level rel (e.g. theme-only images) gets a synthetic
    # key; it never becomes a relationship.
    r_id = loaded_image_rid(package, filename) ||
      next_synthetic_image_key(package)

    binary_data = if zip_path
                    read_binary_from_zip(zip_path, media_path)
                  else
                    zip_content[media_path]
                  end

    package.document.image_parts[r_id] = {
      data: binary_data,
      target: "media/#{filename}",
      content_type: content_type
    }
  end
end

.extract_theme_media(zip_content) ⇒ Object

Extract media files from word/theme/media/ directory



558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
# File 'lib/uniword/docx/package.rb', line 558

def self.extract_theme_media(zip_content)
  media = {}

  zip_content.each_key do |file_path|
    next unless file_path =~ %r{^word/theme/media/(.+)$}

    filename = Regexp.last_match(1)
    media[filename] = Uniword::Themes::MediaFile.new(
      filename: filename,
      content: zip_content[file_path],
      source_path: file_path
    )
  end

  media
end

.find_document_rels_path(doc_path) ⇒ Object

Find the document relationships path from the main document path



691
692
693
694
695
696
697
# File 'lib/uniword/docx/package.rb', line 691

def self.find_document_rels_path(doc_path)
  return nil unless doc_path

  dir = File.dirname(doc_path)
  basename = File.basename(doc_path)
  File.join(dir, "_rels", "#{basename}.rels")
end

.find_main_document_path(package_rels) ⇒ Object

Find the main document path from package relationships



677
678
679
680
681
682
683
684
685
686
687
688
# File 'lib/uniword/docx/package.rb', line 677

def self.find_main_document_path(package_rels)
  return nil unless package_rels&.relationships

  rel = package_rels.relationships.find do |r|
    r.type.to_s.include?("officeDocument/2006/relationships/officeDocument")
  end
  return nil unless rel&.target

  path = rel.target.dup
  path.sub!(%r{^/}, "")
  path
end

.from_file(path) ⇒ Package

Load DOCX package from file

Parameters:

  • path (String)

    Path to .docx file

Returns:

  • (Package)

    Package with all parts loaded



137
138
139
140
141
142
143
# File 'lib/uniword/docx/package.rb', line 137

def self.from_file(path)
  extractor = Infrastructure::ZipExtractor.new
  zip_content = extractor.extract(path)
  package = from_zip_content(zip_content, path)
  package.populate_allocator
  package
end

.from_zip_content(zip_content, zip_path = nil) ⇒ Package

Create package from extracted ZIP content

Parameters:

  • zip_content (Hash)

    Extracted ZIP files

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

    Original ZIP path for binary re-extraction

Returns:



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/uniword/docx/package.rb', line 150

def self.from_zip_content(zip_content, zip_path = nil)
  package = new

  # Parse Content Types
  if zip_content["[Content_Types].xml"]
    package.content_types = Uniword::ContentTypes::Types.from_xml(
      zip_content["[Content_Types].xml"]
    )
  end

  # Parse Package Relationships
  if zip_content["_rels/.rels"]
    package.package_rels = Ooxml::Relationships::PackageRelationships.from_xml(
      zip_content["_rels/.rels"]
    )
  end

  # Find the main document path from officeDocument relationship
  main_doc_path = find_main_document_path(package.package_rels)
  main_doc_rels_path = find_document_rels_path(main_doc_path)

  # Parse Document Properties
  if zip_content["docProps/core.xml"]
    package.core_properties = Ooxml::CoreProperties.from_xml(
      zip_content["docProps/core.xml"]
    )
  end

  if zip_content["docProps/app.xml"]
    package.app_properties = Ooxml::AppProperties.from_xml(
      zip_content["docProps/app.xml"]
    )
  end

  # Parse Custom Properties
  if zip_content["docProps/custom.xml"]
    package.custom_properties = Ooxml::CustomProperties.from_xml(
      zip_content["docProps/custom.xml"]
    )
  end

  # Parse Custom XML Data items (customXml/item*.xml)
  custom_xml_files = zip_content.keys.grep(%r{^customXml/item(\d+)\.xml$})
  if custom_xml_files.any?
    package.custom_xml_items = []
    custom_xml_files.sort_by { |f| f[/item(\d+)/, 1].to_i }.each do |item_path|
      index = item_path[/item(\d+)/, 1].to_i
      props_path = "customXml/itemProps#{index}.xml"
      rels_path = "customXml/_rels/item#{index}.xml.rels"

      package.custom_xml_items << CustomXmlItem.new(
        index: index,
        xml_content: zip_content[item_path],
        props_xml: zip_content[props_path],
        rels_xml: zip_content[rels_path],
      )
    end
  end

  # Parse Document Parts - use dynamic path from package relationships
  if main_doc_path && zip_content[main_doc_path]
    package.document = Uniword::Wordprocessingml::DocumentRoot.from_xml(
      zip_content[main_doc_path]
    )
  elsif zip_content["word/document.xml"]
    package.document = Uniword::Wordprocessingml::DocumentRoot.from_xml(
      zip_content["word/document.xml"]
    )
  end

  if zip_content["word/styles.xml"]
    package.styles = Uniword::Wordprocessingml::StylesConfiguration.from_xml(
      zip_content["word/styles.xml"]
    )
  end

  if zip_content["word/numbering.xml"]
    package.numbering = Uniword::Wordprocessingml::NumberingConfiguration.from_xml(
      zip_content["word/numbering.xml"]
    )
  end

  if zip_content["word/settings.xml"]
    package.settings = Uniword::Wordprocessingml::Settings.from_xml(
      zip_content["word/settings.xml"]
    )
  end

  if zip_content["word/_rels/settings.xml.rels"]
    package.settings_rels =
      Ooxml::Relationships::PackageRelationships.from_xml(
        zip_content["word/_rels/settings.xml.rels"]
      )
  end

  if zip_content["word/fontTable.xml"]
    package.font_table = Uniword::Wordprocessingml::FontTable.from_xml(
      zip_content["word/fontTable.xml"]
    )
  end

  if zip_content["word/webSettings.xml"]
    package.web_settings = Uniword::Wordprocessingml::WebSettings.from_xml(
      zip_content["word/webSettings.xml"]
    )
  end

  # Parse document relationships - use dynamic path based on main document
  if main_doc_rels_path && zip_content[main_doc_rels_path]
    package.document_rels = Ooxml::Relationships::PackageRelationships.from_xml(
      zip_content[main_doc_rels_path]
    )
  elsif zip_content["word/_rels/document.xml.rels"]
    package.document_rels = Ooxml::Relationships::PackageRelationships.from_xml(
      zip_content["word/_rels/document.xml.rels"]
    )
  end

  # Parse Theme
  if zip_content["word/theme/theme1.xml"]
    package.theme = Drawingml::Theme.from_xml(
      zip_content["word/theme/theme1.xml"]
    )

    theme_media = extract_theme_media(zip_content)
    package.theme.media_files = theme_media if theme_media.any?
  end

  if zip_content["word/theme/_rels/theme1.xml.rels"]
    package.theme_rels = Ooxml::Relationships::PackageRelationships.from_xml(
      zip_content["word/theme/_rels/theme1.xml.rels"]
    )
  end

  # Parse Footnotes
  if zip_content["word/footnotes.xml"]
    package.footnotes = Uniword::Wordprocessingml::Footnotes.from_xml(
      zip_content["word/footnotes.xml"]
    )
  end

  # Parse Endnotes
  if zip_content["word/endnotes.xml"]
    package.endnotes = Uniword::Wordprocessingml::Endnotes.from_xml(
      zip_content["word/endnotes.xml"]
    )
  end

  # Parse Comments
  if zip_content["word/comments.xml"]
    package.comments = Uniword::CommentsPart.from_xml(
      zip_content["word/comments.xml"]
    )
  end

  # Parse note part relationships (r:id refs inside notes, e.g.
  # hyperlinks in footnotes) so they survive the round-trip.
  if zip_content["word/_rels/footnotes.xml.rels"]
    package.footnotes_rels =
      Ooxml::Relationships::PackageRelationships.from_xml(
        zip_content["word/_rels/footnotes.xml.rels"]
      )
  end

  if zip_content["word/_rels/endnotes.xml.rels"]
    package.endnotes_rels =
      Ooxml::Relationships::PackageRelationships.from_xml(
        zip_content["word/_rels/endnotes.xml.rels"]
      )
  end

  # Parse Header and Footer parts
  extract_header_footer_parts(zip_content, package)

  # Parse Chart parts
  chart_files = zip_content.keys.grep(%r{^word/charts/chart\d+\.xml$})
  if chart_files.any? && package.document_rels
    chart_files.each do |chart_path|
      chart_target = chart_path.sub("word/", "")
      rel = package.document_rels.relationships.find do |r|
        r.target == chart_target &&
          r.type.to_s.include?("officeDocument/2006/relationships/chart")
      end
      next unless rel

      package.document.chart_parts[rel.id] = {
        xml: zip_content[chart_path],
        target: chart_target
      }
    end
  end

  # Extract image parts from word/media/ directory
  extract_image_parts(zip_content, package, zip_path)

  # Extract OLE/embedded object binaries from word/embeddings/
  embedding_files = zip_content.keys.grep(%r{^word/embeddings/.+$})
  if embedding_files.any?
    package.embeddings = {}
    embedding_files.each do |emb_path|
      target = emb_path.sub("word/", "")
      package.embeddings[target] = zip_content[emb_path]
    end
  end

  package
end

Map relationship id => sectPr reference type ("default"/"first"/"even") from every section properties element (body-level and paragraph-level).



411
412
413
414
415
416
417
418
419
420
421
422
# File 'lib/uniword/docx/package.rb', line 411

def self.header_footer_reference_types(document)
  types = {}
  section_properties_of(document).each do |sect_pr|
    (sect_pr.header_references || []).each do |ref|
      types[ref.r_id] = ref.type if ref.r_id
    end
    (sect_pr.footer_references || []).each do |ref|
      types[ref.r_id] = ref.type if ref.r_id
    end
  end
  types
end

.loaded_image_rid(package, filename) ⇒ Object

The rId a loaded document relationship assigns to "media/", or nil when no image rel targets it.



487
488
489
490
491
492
493
# File 'lib/uniword/docx/package.rb', line 487

def self.loaded_image_rid(package, filename)
  image_type = Ooxml::PartRegistry.find_by_key(:image).rel_type
  rel = package.document_rels&.relationships&.find do |r|
    r.target == "media/#{filename}" && r.type.to_s == image_type
  end
  rel&.id
end

.next_synthetic_image_key(package) ⇒ Object

Synthetic image-part key that cannot collide with a loaded relationship id or an already-keyed image part.



497
498
499
500
501
502
# File 'lib/uniword/docx/package.rb', line 497

def self.next_synthetic_image_key(package)
  taken = package.document_rels&.relationships&.map(&:id) || []
  n = package.document.image_parts.size + 1
  n += 1 while taken.include?("rId#{n}")
  "rId#{n}"
end

.read_binary_from_zip(zip_path, entry_path) ⇒ Object

Read binary data directly from ZIP file without UTF-8 encoding



505
506
507
508
509
510
511
512
513
# File 'lib/uniword/docx/package.rb', line 505

def self.read_binary_from_zip(zip_path, entry_path)
  require "zip"
  Zip::File.open(zip_path) do |zip_file|
    entry = zip_file.find_entry(entry_path)
    return nil unless entry

    entry.get_input_stream.read
  end
end

.section_properties_of(document) ⇒ Object



424
425
426
427
428
429
430
431
432
433
434
435
# File 'lib/uniword/docx/package.rb', line 424

def self.section_properties_of(document)
  body = document.body
  return [] unless body

  sect_prs = []
  sect_prs << body.section_properties if body.section_properties
  (body.paragraphs || []).each do |para|
    sect_pr = para.properties&.section_properties
    sect_prs << sect_pr if sect_pr
  end
  sect_prs
end

.to_file(document, path, profile: nil, validate: nil) ⇒ void

This method returns an undefined value.

Save document to file (class method for DocumentWriter compatibility)

Parameters:

  • document (Wordprocessingml::DocumentRoot)

    Document to save

  • path (String)

    Output file path

  • profile (Profile, nil) (defaults to: nil)

    Reconciliation profile

  • validate (Boolean, nil) (defaults to: nil)

    Run the package integrity gate before writing; nil falls back to Uniword.configuration.validate_on_save



523
524
525
526
527
528
529
530
531
532
533
534
535
# File 'lib/uniword/docx/package.rb', line 523

def self.to_file(document, path, profile: nil, validate: nil)
  package = new
  package.document = document
  package.profile = profile || Profile.defaults
  copy_document_parts_to_package(document, package)
  package.content_types ||= minimal_content_types
  package.package_rels ||= minimal_package_rels
  package.document_rels ||= minimal_document_rels
  package.settings ||= Uniword::Wordprocessingml::Settings.new
  package.font_table ||= Uniword::Wordprocessingml::FontTable.new
  package.web_settings ||= Uniword::Wordprocessingml::WebSettings.new
  package.to_file(path, validate: validate)
end

Instance Method Details

#applied_fixesArray<Reconciler::Fix>

Audit trail of repairs applied by the Reconciler during the most recent save (to_zip_content). Empty before the first save and when the package needed no repairs.

Returns:



129
130
131
# File 'lib/uniword/docx/package.rb', line 129

def applied_fixes
  @applied_fixes ||= []
end

#bodyObject



654
655
656
# File 'lib/uniword/docx/package.rb', line 654

def body
  document&.body
end

#chartsObject



668
669
670
# File 'lib/uniword/docx/package.rb', line 668

def charts
  document&.charts || []
end

#each_paragraphObject



662
663
664
# File 'lib/uniword/docx/package.rb', line 662

def each_paragraph(&)
  paragraphs.each(&)
end

#embeddingsPartCollection

OLE/embedded object binaries (word/embeddings/*), keyed by target.

Returns:



103
104
105
# File 'lib/uniword/docx/package.rb', line 103

def embeddings
  @embeddings ||= PartCollection.new(:target, Part)
end

#embeddings=(value) ⇒ Object

Bulk-assign embeddings (Hash of target => Part/binary; nil clears).



108
109
110
# File 'lib/uniword/docx/package.rb', line 108

def embeddings=(value)
  embeddings.replace_all(value)
end

#paragraphsObject

Delegate common DocumentRoot methods for API compatibility



646
647
648
# File 'lib/uniword/docx/package.rb', line 646

def paragraphs
  document&.paragraphs || []
end

#populate_allocatorObject

Populate the allocator from all existing template data. Must be called BEFORE any builder runs (populate-first principle).



539
540
541
# File 'lib/uniword/docx/package.rb', line 539

def populate_allocator
  @allocator = IdAllocator.populate_from_package(self)
end

#prepare_allocatorObject

Guarantee the single rId authority before reconciliation: reuse the package's allocator (loaded/builder-seeded) or start one, then seed it from the package's current relationships. Seeding preserves loaded rIds verbatim; earlier builder allocations keep their ids — a seeded rel whose id collides is reallocated deterministically (IdAllocator#seed_from_rels).



549
550
551
552
553
554
555
# File 'lib/uniword/docx/package.rb', line 549

def prepare_allocator
  self.allocator ||= IdAllocator.new
  allocator.seed_from_rels(package_rels&.relationships,
                           scope: :package)
  allocator.seed_from_rels(document_rels&.relationships)
  allocator
end

#reorder_content_hash(content) ⇒ Object

Ensure [Content_Types].xml is first, _rels/.rels is second.



637
638
639
640
641
642
# File 'lib/uniword/docx/package.rb', line 637

def reorder_content_hash(content)
  priority = {}
  priority["[Content_Types].xml"] = content.delete("[Content_Types].xml") if content.key?("[Content_Types].xml")
  priority["_rels/.rels"] = content.delete("_rels/.rels") if content.key?("_rels/.rels")
  content.replace(priority.merge(content))
end

#styles_configurationObject



672
673
674
# File 'lib/uniword/docx/package.rb', line 672

def styles_configuration
  document&.styles_configuration
end

#tablesObject



650
651
652
# File 'lib/uniword/docx/package.rb', line 650

def tables
  document&.tables || []
end

#textObject



658
659
660
# File 'lib/uniword/docx/package.rb', line 658

def text
  document&.text || ""
end

#to_file(path, validate: nil) ⇒ void Also known as: save

This method returns an undefined value.

Save package to file

Parameters:

  • path (String)

    Output file path

  • validate (Boolean, nil) (defaults to: nil)

    Run the package integrity gate before writing; nil falls back to Uniword.configuration.validate_on_save

Raises:



583
584
585
586
587
# File 'lib/uniword/docx/package.rb', line 583

def to_file(path, validate: nil)
  zip_content = to_zip_content(validate: validate)
  packager = Infrastructure::ZipPackager.new
  packager.package(zip_content, path)
end

#to_zip_content(validate: nil) ⇒ Hash

Generate ZIP content hash

Runs the Reconciler (the only mutating pass), records its repair report on #applied_fixes, and — unless validation is disabled — refuses invalid output via the PackageIntegrityChecker gate.

Parameters:

  • validate (Boolean, nil) (defaults to: nil)

    Run the package integrity gate; nil falls back to Uniword.configuration.validate_on_save

Returns:

  • (Hash)

    File paths => content

Raises:



600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
# File 'lib/uniword/docx/package.rb', line 600

def to_zip_content(validate: nil)
  content = {}

  self.content_types ||= self.class.minimal_content_types
  self.package_rels ||= self.class.minimal_package_rels
  self.document_rels ||= self.class.minimal_document_rels

  self.settings ||= Uniword::Wordprocessingml::Settings.new
  self.font_table ||= Uniword::Wordprocessingml::FontTable.new
  self.web_settings ||= Uniword::Wordprocessingml::WebSettings.new

  # An allocator carried into the save (builder-managed document
  # or loaded package) selects light-touch repairs; documents
  # without one get the full normalization repertoire. rIds flow
  # through the allocator either way.
  builder_managed = !allocator.nil?
  prepare_allocator

  reconciler = Reconciler.new(self,
                              profile: profile || Profile.defaults,
                              allocator: allocator,
                              builder_managed: builder_managed)
  reconciler.reconcile
  @applied_fixes = reconciler.applied_fixes
  log_applied_fixes

  inject_part_relationships(content, content_types, package_rels, document_rels)
  serialize_package_parts(content, content_types, package_rels, document_rels)

  # OOXML requires [Content_Types].xml as the first ZIP entry.
  reorder_content_hash(content)

  enforce_package_integrity(content, validate)
  content
end