Module: Uniword::Docx::PackageSerialization Private

Included in:
Package
Defined in:
lib/uniword/docx/package_serialization.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Content type injection and XML serialization for DOCX package parts.

Handles the pre-serialization phase (injecting relationships and content types) and the serialization phase (converting model objects to XML).

Extracted from Package for separation of responsibilities. Included in Package for backward compatibility.

Instance Method Summary collapse

Instance Method Details

#inject_part_relationships(content, content_types, package_rels, document_rels) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Inject content types and relationships for all package parts



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/uniword/docx/package_serialization.rb', line 16

def inject_part_relationships(content, content_types, package_rels,
document_rels)
  inject_image_parts(content, content_types, document_rels)
  inject_chart_parts(content, content_types, document_rels)
  inject_bibliography(content_types, document_rels)
  inject_custom_properties(content_types, package_rels)
  inject_custom_xml(content_types)
  inject_headers(content_types, document_rels)
  inject_footers(content_types, document_rels)
  inject_header_footer_parts(content_types, document_rels)
  inject_notes(content_types, document_rels)
  inject_theme(content_types, document_rels)
  inject_numbering(content_types, document_rels)
end

#serialize_package_parts(content, content_types, package_rels, document_rels) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Serialize all package parts to XML and add to content hash



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
81
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/uniword/docx/package_serialization.rb', line 32

def serialize_package_parts(content, content_types, package_rels,
document_rels)
  # Package infrastructure
  content["[Content_Types].xml"] =
    content_types.to_xml(encoding: "UTF-8", declaration: true,
                         standalone: true)
  content["_rels/.rels"] =
    package_rels.to_xml(encoding: "UTF-8", declaration: true,
                        standalone: true)

  # Document properties
  if core_properties
    content["docProps/core.xml"] =
      core_properties.to_xml(encoding: "UTF-8",
                             prefix: true,
                             standalone: true)
  end
  if app_properties
    content["docProps/app.xml"] =
      app_properties.to_xml(encoding: "UTF-8",
                            prefix: false,
                            standalone: true)
  end
  if custom_properties
    content["docProps/custom.xml"] =
      custom_properties.to_xml(encoding: "UTF-8",
                               prefix: false,
                               standalone: true)
  end

  # Custom XML data items
  if custom_xml_items && !custom_xml_items.empty?
    custom_xml_items.each do |item|
      idx = item[:index]
      content["customXml/item#{idx}.xml"] = item[:xml_content]
      if item[:props_xml]
        content["customXml/itemProps#{idx}.xml"] =
          item[:props_xml]
      end
      if item[:rels_xml]
        content["customXml/_rels/item#{idx}.xml.rels"] =
          item[:rels_xml]
      end
    end
  end

  # Document parts
  if document
    content["word/document.xml"] =
      document.to_xml(encoding: "UTF-8", prefix: true,
                      fix_boolean_elements: true,
                      standalone: true)
  end
  if styles
    content["word/styles.xml"] =
      styles.to_xml(encoding: "UTF-8", prefix: true,
                    fix_boolean_elements: true,
                    standalone: true)
  end
  if numbering
    content["word/numbering.xml"] =
      numbering.to_xml(encoding: "UTF-8", prefix: true,
                       fix_boolean_elements: true,
                       standalone: true)
  end
  if settings
    content["word/settings.xml"] =
      settings.to_xml(encoding: "UTF-8", prefix: true,
                      standalone: true)
  end
  if font_table
    content["word/fontTable.xml"] =
      font_table.to_xml(encoding: "UTF-8", prefix: true,
                        standalone: true)
  end
  if web_settings
    content["word/webSettings.xml"] =
      web_settings.to_xml(encoding: "UTF-8", prefix: true,
                          standalone: true)
  end
  if document_rels
    content["word/_rels/document.xml.rels"] =
      document_rels.to_xml(encoding: "UTF-8", declaration: true,
                           standalone: true)
  end

  # Theme
  if theme
    content["word/theme/theme1.xml"] =
      theme.to_xml(encoding: "UTF-8", prefix: true,
                   standalone: true)
  end
  if theme_rels
    content["word/theme/_rels/theme1.xml.rels"] =
      theme_rels.to_xml(encoding: "UTF-8", declaration: true,
                        standalone: true)
  end

  # Notes
  if footnotes
    content["word/footnotes.xml"] =
      footnotes.to_xml(encoding: "UTF-8", prefix: true,
                       fix_boolean_elements: true,
                       standalone: true)
  end
  if endnotes
    content["word/endnotes.xml"] =
      endnotes.to_xml(encoding: "UTF-8", prefix: true,
                      fix_boolean_elements: true,
                      standalone: true)
  end

  # Bibliography sources
  if document&.bibliography_sources
    content["word/sources.xml"] =
      document.bibliography_sources.to_xml(encoding: "UTF-8",
                                           declaration: true,
                                           standalone: true)
  end

  # Headers and footers
  serialize_headers(content)
  serialize_footers(content)
  serialize_header_footer_parts(content)
end