uniword 1.2.5 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (209) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +313 -0
  3. data/CONTRIBUTING.md +1 -1
  4. data/config/ooxml/schemas/shared_types.yml +5 -4
  5. data/data/schemas/ecma/dc.xsd +118 -0
  6. data/data/schemas/ecma/dcmitype.xsd +50 -0
  7. data/data/schemas/ecma/dcterms.xsd +331 -0
  8. data/data/schemas/ecma/opc-coreProperties.xsd +2 -2
  9. data/data/schemas/ecma/xml.xsd +117 -0
  10. data/lib/uniword/builder/bibliography_builder.rb +1 -1
  11. data/lib/uniword/builder/chart_builder.rb +22 -14
  12. data/lib/uniword/builder/comment_anchorer.rb +221 -0
  13. data/lib/uniword/builder/document_builder.rb +97 -6
  14. data/lib/uniword/builder/image_builder.rb +11 -10
  15. data/lib/uniword/builder/run_utils.rb +2 -0
  16. data/lib/uniword/builder.rb +1 -0
  17. data/lib/uniword/cli/fonts_cli.rb +52 -0
  18. data/lib/uniword/cli/main.rb +110 -11
  19. data/lib/uniword/cli/page_cli.rb +85 -0
  20. data/lib/uniword/cli/styles_cli.rb +204 -0
  21. data/lib/uniword/cli/theme_cli.rb +88 -0
  22. data/lib/uniword/cli/toc_cli.rb +22 -2
  23. data/lib/uniword/comment.rb +20 -15
  24. data/lib/uniword/comment_range.rb +3 -3
  25. data/lib/uniword/comments_part.rb +51 -19
  26. data/lib/uniword/configuration/configuration_loader.rb +5 -3
  27. data/lib/uniword/configuration.rb +93 -1
  28. data/lib/uniword/content_types.rb +19 -39
  29. data/lib/uniword/document_factory.rb +12 -18
  30. data/lib/uniword/document_writer.rb +16 -5
  31. data/lib/uniword/docx/chart_part.rb +34 -0
  32. data/lib/uniword/docx/custom_xml_item.rb +91 -0
  33. data/lib/uniword/docx/header_footer_part.rb +140 -0
  34. data/lib/uniword/docx/header_footer_part_collection.rb +135 -0
  35. data/lib/uniword/docx/header_footer_view.rb +225 -0
  36. data/lib/uniword/docx/id_allocator.rb +130 -36
  37. data/lib/uniword/docx/image_part.rb +83 -0
  38. data/lib/uniword/docx/package.rb +141 -333
  39. data/lib/uniword/docx/package_defaults.rb +83 -99
  40. data/lib/uniword/docx/package_integrity_checker.rb +312 -0
  41. data/lib/uniword/docx/package_serialization.rb +144 -233
  42. data/lib/uniword/docx/part.rb +125 -0
  43. data/lib/uniword/docx/part_collection.rb +131 -0
  44. data/lib/uniword/docx/part_loader/chart_loader.rb +50 -0
  45. data/lib/uniword/docx/part_loader/custom_xml_loader.rb +53 -0
  46. data/lib/uniword/docx/part_loader/embedding_loader.rb +26 -0
  47. data/lib/uniword/docx/part_loader/header_footer_loader.rb +101 -0
  48. data/lib/uniword/docx/part_loader/image_loader.rb +105 -0
  49. data/lib/uniword/docx/part_loader/load_context.rb +85 -0
  50. data/lib/uniword/docx/part_loader/raw_part_loader.rb +183 -0
  51. data/lib/uniword/docx/part_loader/theme_media_loader.rb +39 -0
  52. data/lib/uniword/docx/part_loader/xml_model_loader.rb +56 -0
  53. data/lib/uniword/docx/part_loader.rb +92 -0
  54. data/lib/uniword/docx/raw_part.rb +75 -0
  55. data/lib/uniword/docx/reconciler/body.rb +59 -68
  56. data/lib/uniword/docx/reconciler/fix.rb +46 -0
  57. data/lib/uniword/docx/reconciler/fix_codes.rb +37 -30
  58. data/lib/uniword/docx/reconciler/helpers.rb +18 -31
  59. data/lib/uniword/docx/reconciler/notes.rb +17 -8
  60. data/lib/uniword/docx/reconciler/package_structure.rb +168 -221
  61. data/lib/uniword/docx/reconciler/parts.rb +36 -13
  62. data/lib/uniword/docx/reconciler/referential_integrity.rb +222 -123
  63. data/lib/uniword/docx/reconciler/tables.rb +78 -16
  64. data/lib/uniword/docx/reconciler/theme.rb +7 -4
  65. data/lib/uniword/docx/reconciler.rb +36 -14
  66. data/lib/uniword/docx.rb +13 -0
  67. data/lib/uniword/drawingml/blip.rb +0 -1
  68. data/lib/uniword/drawingml/color_scheme.rb +5 -6
  69. data/lib/uniword/drawingml/font_scheme.rb +4 -8
  70. data/lib/uniword/drawingml/theme.rb +12 -6
  71. data/lib/uniword/errors.rb +8 -1
  72. data/lib/uniword/footer.rb +4 -0
  73. data/lib/uniword/header.rb +4 -0
  74. data/lib/uniword/hyperlink.rb +1 -1
  75. data/lib/uniword/image.rb +6 -6
  76. data/lib/uniword/images/image_manager.rb +13 -13
  77. data/lib/uniword/lazy_loader.rb +1 -2
  78. data/lib/uniword/model_attribute_access.rb +20 -4
  79. data/lib/uniword/ooxml/dotx_package.rb +42 -2
  80. data/lib/uniword/ooxml/element_order.rb +94 -0
  81. data/lib/uniword/ooxml/part_definition.rb +329 -0
  82. data/lib/uniword/ooxml/part_registry.rb +504 -0
  83. data/lib/uniword/ooxml/relationships/image_relationship.rb +6 -3
  84. data/lib/uniword/ooxml/relationships/package_relationships.rb +20 -22
  85. data/lib/uniword/ooxml/schema/element_serializer.rb +8 -7
  86. data/lib/uniword/ooxml/types/hex_color_value.rb +38 -0
  87. data/lib/uniword/ooxml/types/ooxml_boolean.rb +27 -14
  88. data/lib/uniword/ooxml/types/ooxml_boolean_optional.rb +21 -16
  89. data/lib/uniword/ooxml/types/theme_color_value.rb +22 -0
  90. data/lib/uniword/ooxml/types/unsigned_decimal_number.rb +30 -0
  91. data/lib/uniword/ooxml/types.rb +10 -0
  92. data/lib/uniword/ooxml.rb +3 -0
  93. data/lib/uniword/properties/alignment.rb +8 -3
  94. data/lib/uniword/properties/border.rb +52 -6
  95. data/lib/uniword/properties/cell_vertical_align.rb +4 -1
  96. data/lib/uniword/properties/color_value.rb +8 -3
  97. data/lib/uniword/properties/highlight.rb +9 -5
  98. data/lib/uniword/properties/shading.rb +4 -3
  99. data/lib/uniword/properties/tab_stop.rb +6 -1
  100. data/lib/uniword/properties/table_justification.rb +4 -1
  101. data/lib/uniword/properties/underline.rb +4 -2
  102. data/lib/uniword/properties/vertical_align.rb +6 -3
  103. data/lib/uniword/review/review_manager.rb +23 -9
  104. data/lib/uniword/revision.rb +9 -9
  105. data/lib/uniword/schema/model_generator.rb +31 -15
  106. data/lib/uniword/shared_types/hex_color.rb +4 -1
  107. data/lib/uniword/shared_types/pixel_measure.rb +4 -1
  108. data/lib/uniword/shared_types/point_measure.rb +4 -1
  109. data/lib/uniword/shared_types/text_alignment.rb +6 -1
  110. data/lib/uniword/shared_types/twips_measure.rb +4 -1
  111. data/lib/uniword/spreadsheetml/phonetic_pr.rb +1 -1
  112. data/lib/uniword/spreadsheetml/shared_string_table.rb +2 -2
  113. data/lib/uniword/spreadsheetml/table_formula.rb +1 -1
  114. data/lib/uniword/template/variable_resolver.rb +40 -7
  115. data/lib/uniword/themes/theme_transformation.rb +55 -41
  116. data/lib/uniword/toc/toc_generator.rb +6 -5
  117. data/lib/uniword/transformation/mhtml_element_renderer.rb +1 -5
  118. data/lib/uniword/transformation/mhtml_metadata_builder.rb +6 -6
  119. data/lib/uniword/validation/engine.rb +35 -0
  120. data/lib/uniword/validation/link_checker.rb +1 -1
  121. data/lib/uniword/validation/opc_validator.rb +16 -2
  122. data/lib/uniword/validation/report/layer_result.rb +0 -1
  123. data/lib/uniword/validation/report/terminal_formatter.rb +0 -1
  124. data/lib/uniword/validation/report/verification_report.rb +0 -2
  125. data/lib/uniword/validation/report.rb +20 -0
  126. data/lib/uniword/validation/rules/base.rb +10 -2
  127. data/lib/uniword/validation/rules/bookmark_pairing_rule.rb +38 -0
  128. data/lib/uniword/validation/rules/bookmark_uniqueness_rule.rb +48 -0
  129. data/lib/uniword/validation/rules/bookmarks_rule.rb +0 -2
  130. data/lib/uniword/validation/rules/content_types_coverage_rule.rb +0 -2
  131. data/lib/uniword/validation/rules/core_properties_namespace_rule.rb +0 -2
  132. data/lib/uniword/validation/rules/document_body_rule.rb +25 -0
  133. data/lib/uniword/validation/rules/document_context.rb +7 -0
  134. data/lib/uniword/validation/rules/empty_paragraphs_rule.rb +27 -0
  135. data/lib/uniword/validation/rules/font_table_signature_rule.rb +0 -2
  136. data/lib/uniword/validation/rules/fonts_rule.rb +0 -2
  137. data/lib/uniword/validation/rules/footnotes_rule.rb +0 -2
  138. data/lib/uniword/validation/rules/headers_footers_rule.rb +0 -2
  139. data/lib/uniword/validation/rules/images_rule.rb +0 -2
  140. data/lib/uniword/validation/rules/mc_ignorable_namespace_rule.rb +0 -2
  141. data/lib/uniword/validation/rules/model_context.rb +32 -0
  142. data/lib/uniword/validation/rules/model_rule.rb +34 -0
  143. data/lib/uniword/validation/rules/numbering_preservation_rule.rb +0 -2
  144. data/lib/uniword/validation/rules/numbering_rule.rb +0 -2
  145. data/lib/uniword/validation/rules/relationship_integrity_rule.rb +0 -1
  146. data/lib/uniword/validation/rules/rsid_rule.rb +0 -2
  147. data/lib/uniword/validation/rules/section_properties_rule.rb +0 -2
  148. data/lib/uniword/validation/rules/settings_rule.rb +0 -2
  149. data/lib/uniword/validation/rules/settings_values_rule.rb +0 -2
  150. data/lib/uniword/validation/rules/style_references_rule.rb +0 -2
  151. data/lib/uniword/validation/rules/table_grid_rule.rb +27 -0
  152. data/lib/uniword/validation/rules/table_properties_rule.rb +27 -0
  153. data/lib/uniword/validation/rules/tables_rule.rb +0 -2
  154. data/lib/uniword/validation/rules/theme_completeness_rule.rb +0 -2
  155. data/lib/uniword/validation/rules/theme_rule.rb +0 -2
  156. data/lib/uniword/validation/rules.rb +61 -23
  157. data/lib/uniword/validation/schema_registry.rb +11 -0
  158. data/lib/uniword/validation/validators/document_semantics_validator.rb +1 -9
  159. data/lib/uniword/validation/validators/xml_schema_validator.rb +46 -2
  160. data/lib/uniword/validation/validators.rb +0 -9
  161. data/lib/uniword/validation/verify_orchestrator.rb +0 -7
  162. data/lib/uniword/validation.rb +7 -2
  163. data/lib/uniword/version.rb +1 -1
  164. data/lib/uniword/vml/imagedata.rb +0 -1
  165. data/lib/uniword/wordprocessingml/attached_template.rb +0 -1
  166. data/lib/uniword/wordprocessingml/deleted_text.rb +1 -1
  167. data/lib/uniword/wordprocessingml/document_root.rb +121 -122
  168. data/lib/uniword/wordprocessingml/document_styling.rb +246 -0
  169. data/lib/uniword/wordprocessingml/endnotes.rb +4 -1
  170. data/lib/uniword/wordprocessingml/font_replacer.rb +147 -0
  171. data/lib/uniword/wordprocessingml/footnotes.rb +4 -1
  172. data/lib/uniword/wordprocessingml/hdr_shape_defaults.rb +0 -1
  173. data/lib/uniword/wordprocessingml/hyperlink.rb +0 -1
  174. data/lib/uniword/wordprocessingml/level.rb +6 -20
  175. data/lib/uniword/wordprocessingml/math_pr.rb +0 -11
  176. data/lib/uniword/wordprocessingml/numbering_definition.rb +16 -8
  177. data/lib/uniword/wordprocessingml/numbering_elements.rb +0 -15
  178. data/lib/uniword/wordprocessingml/page_setup.rb +181 -0
  179. data/lib/uniword/wordprocessingml/paragraph_properties.rb +33 -28
  180. data/lib/uniword/wordprocessingml/rsids.rb +0 -2
  181. data/lib/uniword/wordprocessingml/run.rb +3 -0
  182. data/lib/uniword/wordprocessingml/section_properties.rb +2 -2
  183. data/lib/uniword/wordprocessingml/settings.rb +8 -34
  184. data/lib/uniword/wordprocessingml/shape_defaults.rb +0 -1
  185. data/lib/uniword/wordprocessingml/style.rb +2 -1
  186. data/lib/uniword/wordprocessingml/style_cleanup.rb +198 -0
  187. data/lib/uniword/wordprocessingml/table_cell_properties.rb +6 -6
  188. data/lib/uniword/wordprocessingml/update_fields.rb +27 -0
  189. data/lib/uniword/wordprocessingml/w14_attributes.rb +30 -2
  190. data/lib/uniword/wordprocessingml.rb +5 -2
  191. data/lib/uniword.rb +27 -2
  192. metadata +52 -19
  193. data/config/validation_rules.yml +0 -60
  194. data/config/warning_rules.yml +0 -57
  195. data/lib/uniword/validation/document_validator.rb +0 -272
  196. data/lib/uniword/validation/structural_validator.rb +0 -116
  197. data/lib/uniword/validation/validators/content_type_validator.rb +0 -165
  198. data/lib/uniword/validation/validators/file_structure_validator.rb +0 -104
  199. data/lib/uniword/validation/validators/ooxml_part_validator.rb +0 -128
  200. data/lib/uniword/validation/validators/relationship_validator.rb +0 -147
  201. data/lib/uniword/validation/validators/zip_integrity_validator.rb +0 -110
  202. data/lib/uniword/validators/element_validator.rb +0 -93
  203. data/lib/uniword/validators/paragraph_validator.rb +0 -116
  204. data/lib/uniword/validators/table_validator.rb +0 -134
  205. data/lib/uniword/validators.rb +0 -9
  206. data/lib/uniword/warnings/warning.rb +0 -130
  207. data/lib/uniword/warnings/warning_collector.rb +0 -234
  208. data/lib/uniword/warnings/warning_report.rb +0 -159
  209. data/lib/uniword/warnings.rb +0 -9
@@ -9,7 +9,8 @@ module Uniword
9
9
  VALID_NOTE_TYPES = %w[separator continuationSeparator
10
10
  footnoteSeparator continuationNotice].freeze
11
11
 
12
- # Sub-modules autoloaded — no require_relative for internal code.
12
+ # Sub-modules autoloaded — registered below, loaded on first use.
13
+ autoload :Fix, "#{__dir__}/reconciler/fix"
13
14
  autoload :FixCodes, "#{__dir__}/reconciler/fix_codes"
14
15
  autoload :Helpers, "#{__dir__}/reconciler/helpers"
15
16
  autoload :Notes, "#{__dir__}/reconciler/notes"
@@ -29,10 +30,16 @@ module Uniword
29
30
  include PackageStructure
30
31
  include Body
31
32
 
32
- def initialize(package, profile: nil, allocator: nil)
33
+ def initialize(package, profile: nil, allocator: nil,
34
+ builder_managed: nil)
33
35
  @package = package
34
36
  @profile = profile
35
37
  @allocator = allocator
38
+ @builder_managed = if builder_managed.nil?
39
+ !allocator.nil? || !package.allocator.nil?
40
+ else
41
+ builder_managed
42
+ end
36
43
  @applied_fixes = []
37
44
  end
38
45
 
@@ -71,23 +78,40 @@ module Uniword
71
78
  reconcile_referential_integrity
72
79
  end
73
80
 
74
- # Audit trail of fixes applied during reconciliation.
81
+ # Audit trail of repairs applied during reconciliation.
82
+ #
83
+ # @return [Array<Fix>] Fixes recorded by the most recent #reconcile
75
84
  attr_reader :applied_fixes
76
85
 
77
86
  private
78
87
 
79
88
  attr_reader :package, :profile
80
89
 
90
+ # The single rId/ID authority for this run. Falls back to an
91
+ # allocator populated from the package when the package carries
92
+ # none (direct Reconciler use outside Package#to_zip_content).
81
93
  def allocator
82
- @allocator || package.allocator
94
+ @allocator ||= package.allocator ||
95
+ IdAllocator.populate_from_package(package)
83
96
  end
84
97
 
85
- def record_fix(validity_rule, message)
86
- @applied_fixes << {
87
- validity_rule: validity_rule,
88
- message: message,
89
- timestamp: Time.now,
90
- }
98
+ # Whether the document's IDs were managed outside the reconciler
99
+ # (builder-created content, or a package loaded with an explicit
100
+ # allocator). Builder-managed documents get light-touch repairs;
101
+ # legacy documents get the full normalization repertoire (ID
102
+ # backfill, table normalization, note renumbering). rIds flow
103
+ # through the allocator either way. Decided at initialize time.
104
+ def builder_managed?
105
+ @builder_managed
106
+ end
107
+
108
+ # Record one applied repair as a Fix value object.
109
+ #
110
+ # @param code [String] Fix code (a FixCodes constant value)
111
+ # @param message [String] Human-readable description of the repair
112
+ # @param part [String, nil] Package part the repair applies to
113
+ def record_fix(code, message, part: nil)
114
+ @applied_fixes << Fix.new(code: code, message: message, part: part)
91
115
  end
92
116
 
93
117
  def clear_stored_namespace_plans
@@ -102,15 +126,13 @@ module Uniword
102
126
  package.app_properties,
103
127
  package.footnotes,
104
128
  package.endnotes,
129
+ package.comments,
105
130
  ].compact
106
131
 
107
132
  (package.document&.header_footer_parts || []).each do |part|
108
- parts << part[:content] if part[:content]
133
+ parts << part.content if part.content
109
134
  end
110
135
 
111
- (package.document&.headers&.values || []).each { |h| parts << h }
112
- (package.document&.footers&.values || []).each { |f| parts << f }
113
-
114
136
  parts.each do |part|
115
137
  part.import_declaration_plan = nil
116
138
  part.pending_plan_root_element = nil
data/lib/uniword/docx.rb CHANGED
@@ -8,9 +8,22 @@ module Uniword
8
8
  # document-level reconciliation, and user-defined requirements.
9
9
  module Docx
10
10
  autoload :IdAllocator, "#{__dir__}/docx/id_allocator"
11
+ autoload :Part, "#{__dir__}/docx/part"
12
+ autoload :RawPart, "#{__dir__}/docx/raw_part"
13
+ autoload :ChartPart, "#{__dir__}/docx/chart_part"
14
+ autoload :ImagePart, "#{__dir__}/docx/image_part"
15
+ autoload :HeaderFooterPart, "#{__dir__}/docx/header_footer_part"
16
+ autoload :CustomXmlItem, "#{__dir__}/docx/custom_xml_item"
17
+ autoload :PartCollection, "#{__dir__}/docx/part_collection"
18
+ autoload :HeaderFooterPartCollection,
19
+ "#{__dir__}/docx/header_footer_part_collection"
20
+ autoload :HeaderFooterView, "#{__dir__}/docx/header_footer_view"
11
21
  autoload :Package, "#{__dir__}/docx/package"
12
22
  autoload :PackageDefaults, "#{__dir__}/docx/package_defaults"
23
+ autoload :PackageIntegrityChecker,
24
+ "#{__dir__}/docx/package_integrity_checker"
13
25
  autoload :PackageSerialization, "#{__dir__}/docx/package_serialization"
26
+ autoload :PartLoader, "#{__dir__}/docx/part_loader"
14
27
  autoload :Profile, "#{__dir__}/docx/profile"
15
28
  autoload :DocumentStatistics, "#{__dir__}/docx/document_statistics"
16
29
  autoload :Reconciler, "#{__dir__}/docx/reconciler"
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "lutaml/model"
4
- require "uniword/ooxml/types/relationship_id"
5
4
 
6
5
  module Uniword
7
6
  module Drawingml
@@ -399,14 +399,13 @@ module Uniword
399
399
 
400
400
  # Duplicate the color scheme
401
401
  #
402
+ # Deep copy via XML round-trip — preserves system-color entries
403
+ # (dk1/lt1 as windowText/window) and every color slot, not just
404
+ # the standard hex values.
405
+ #
402
406
  # @return [ColorScheme] A deep copy of this color scheme
403
407
  def dup
404
- new_scheme = ColorScheme.new(name: name)
405
- THEME_COLORS.each do |color_name|
406
- value = self[color_name]
407
- new_scheme[color_name] = value if value
408
- end
409
- new_scheme
408
+ self.class.from_xml(to_xml)
410
409
  end
411
410
  end
412
411
  end
@@ -225,16 +225,12 @@ module Uniword
225
225
 
226
226
  # Duplicate the font scheme
227
227
  #
228
+ # Deep copy via XML round-trip — preserves per-script font entries
229
+ # (Jpan/Hang/Arab/...) that the previous hand-rolled copy dropped.
230
+ #
228
231
  # @return [FontScheme] A deep copy of this font scheme
229
232
  def dup
230
- new_scheme = FontScheme.new(name: name)
231
- new_scheme.major_font = major_font
232
- new_scheme.minor_font = minor_font
233
- new_scheme.major_east_asian = major_east_asian if major_east_asian
234
- new_scheme.major_complex_script = major_complex_script if major_complex_script
235
- new_scheme.minor_east_asian = minor_east_asian if minor_east_asian
236
- new_scheme.minor_complex_script = minor_complex_script if minor_complex_script
237
- new_scheme
233
+ self.class.from_xml(to_xml)
238
234
  end
239
235
  end
240
236
  end
@@ -127,14 +127,20 @@ module Uniword
127
127
 
128
128
  # Duplicate the theme
129
129
  #
130
+ # Deep copy via XML round-trip: copies color scheme, font scheme,
131
+ # format scheme (fill/line/effect lists), and every other mapped
132
+ # part. The previous hand-rolled copy dropped fmt_scheme (and
133
+ # object_defaults/ext_lst/media_files), producing themes with
134
+ # empty fillStyleLst that Word rejects.
135
+ #
130
136
  # @return [Theme] A deep copy of this theme
131
137
  def dup
132
- new_theme = Theme.new(name: name)
133
- new_theme.color_scheme = color_scheme.dup if color_scheme
134
- new_theme.font_scheme = font_scheme.dup if font_scheme
135
- new_theme.variants = @variants.dup if @variants
136
- new_theme.source_file = @source_file
137
- new_theme
138
+ self.class.from_xml(to_xml).tap do |copy|
139
+ copy.variants = @variants&.dup
140
+ copy.source_file = @source_file
141
+ copy.media_files = @media_files
142
+ copy.raw_xml = @raw_xml
143
+ end
138
144
  end
139
145
 
140
146
  # Check if theme is valid
@@ -54,11 +54,14 @@ module Uniword
54
54
  class ValidationError < Error
55
55
  # @param element [Element] The element that failed validation
56
56
  # @param errors [Array<String>] Array of validation error messages
57
- def initialize(element, errors)
57
+ # @param issues [Array] Structured issues (e.g.
58
+ # Validation::Report::ValidationIssue with code, part, message)
59
+ def initialize(element, errors, issues: [])
58
60
  element_name = element.class.name.split("::").last
59
61
  super("Validation failed for #{element_name}: #{errors.join(', ')}")
60
62
  @element = element
61
63
  @errors = errors
64
+ @issues = issues
62
65
  end
63
66
 
64
67
  # @return [Element] The element that failed
@@ -66,6 +69,10 @@ module Uniword
66
69
 
67
70
  # @return [Array<String>] The validation errors
68
71
  attr_reader :errors
72
+
73
+ # @return [Array] Structured issues (code, part, message) — populated
74
+ # by the write-time package integrity gate
75
+ attr_reader :issues
69
76
  end
70
77
 
71
78
  # Raised when trying to write to a read-only document
@@ -12,6 +12,10 @@ module Uniword
12
12
  attribute :tables, Wordprocessingml::Table, collection: true,
13
13
  initialize_empty: true
14
14
 
15
+ # mc:Ignorable prefix list carried to the serialized
16
+ # Wordprocessingml footer part (set by the reconciler).
17
+ attr_accessor :mc_ignorable
18
+
15
19
  # Valid footer types
16
20
  TYPES = %w[default first even].freeze
17
21
 
@@ -12,6 +12,10 @@ module Uniword
12
12
  attribute :tables, Wordprocessingml::Table, collection: true,
13
13
  initialize_empty: true
14
14
 
15
+ # mc:Ignorable prefix list carried to the serialized
16
+ # Wordprocessingml header part (set by the reconciler).
17
+ attr_accessor :mc_ignorable
18
+
15
19
  # Valid header types
16
20
  TYPES = %w[default first even].freeze
17
21
 
@@ -15,7 +15,7 @@ module Uniword
15
15
  # link = Hyperlink.new(anchor: 'section1', text: 'Go to section')
16
16
  # link.internal? # => true
17
17
  class Hyperlink
18
- REL_TYPE = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink"
18
+ REL_TYPE = Ooxml::PartRegistry.find_by_key(:hyperlink).rel_type
19
19
 
20
20
  attr_reader :url, :anchor, :text, :tooltip
21
21
 
data/lib/uniword/image.rb CHANGED
@@ -15,12 +15,6 @@ module Uniword
15
15
  class Image < Element
16
16
  extend LazyLoader
17
17
 
18
- # OOXML namespace configuration for images (drawing elements)
19
- xml do
20
- element "drawing"
21
- namespace Ooxml::Namespaces::WordProcessingML
22
- end
23
-
24
18
  # Image relationship ID (reference to the image file in the package)
25
19
  attribute :relationship_id, :string
26
20
 
@@ -52,6 +46,12 @@ module Uniword
52
46
  # Text wrapping style (:square, :tight, :through, :none, etc.)
53
47
  attribute :text_wrapping, :string
54
48
 
49
+ # OOXML namespace configuration for images (drawing elements)
50
+ xml do
51
+ element "drawing"
52
+ namespace Ooxml::Namespaces::WordProcessingML
53
+ end
54
+
55
55
  # Image data loader (proc that loads the actual binary data)
56
56
  # This is not serialized - it's used for lazy loading
57
57
  attr_accessor :data_loader
@@ -7,9 +7,9 @@ module Uniword
7
7
  module Images
8
8
  # Orchestrator for image operations on a DocumentRoot.
9
9
  #
10
- # Works with the document's +image_parts+ hash (populated during DOCX
11
- # loading) and the package ZIP to enumerate, extract, insert, and
12
- # remove images.
10
+ # Works with the document's +image_parts+ collection (populated
11
+ # during DOCX loading) and the package ZIP to enumerate, extract,
12
+ # insert, and remove images.
13
13
  #
14
14
  # @example List images
15
15
  # doc = Uniword::DocumentFactory.from_file("report.docx")
@@ -50,15 +50,15 @@ module Uniword
50
50
  parts = @document.image_parts
51
51
  return [] unless parts && !parts.empty?
52
52
 
53
- parts.map do |_r_id, entry|
54
- data = entry[:data]
53
+ parts.map do |_r_id, part|
54
+ data = part.data
55
55
  px_w, px_h = detect_pixel_dimensions(data) if data
56
- name = File.basename(entry[:target].to_s)
56
+ name = File.basename(part.target.to_s)
57
57
 
58
58
  ImageInfo.new(
59
59
  name: name,
60
- path: "word/#{entry[:target]}",
61
- content_type: entry[:content_type].to_s,
60
+ path: "word/#{part.target}",
61
+ content_type: part.content_type.to_s,
62
62
  size: data ? data.bytesize : 0,
63
63
  width: px_w,
64
64
  height: px_h,
@@ -79,11 +79,11 @@ module Uniword
79
79
  parts = @document.image_parts
80
80
  return 0 unless parts && !parts.empty?
81
81
 
82
- parts.each_value do |entry|
83
- data = entry[:data]
82
+ parts.each_value do |part|
83
+ data = part.data
84
84
  next unless data
85
85
 
86
- filename = File.basename(entry[:target].to_s)
86
+ filename = File.basename(part.target.to_s)
87
87
  File.binwrite(File.join(output_dir, filename), data)
88
88
  end
89
89
 
@@ -160,8 +160,8 @@ module Uniword
160
160
  parts = @document.image_parts
161
161
  return false unless parts && !parts.empty?
162
162
 
163
- key = parts.find do |_k, entry|
164
- File.basename(entry[:target].to_s) == image_name
163
+ key = parts.find do |_k, part|
164
+ File.basename(part.target.to_s) == image_name
165
165
  end&.first
166
166
 
167
167
  return false unless key
@@ -7,8 +7,7 @@ module Uniword
7
7
  # which helps reduce memory usage for large documents.
8
8
  #
9
9
  # All cached values are stored in a single Hash (`@_lazy_cache`)
10
- # accessed via direct ivar reference — no instance_variable_get/set,
11
- # no public_send, no remove_instance_variable.
10
+ # accessed via direct ivar reference inside the generated methods.
12
11
  #
13
12
  # @example Using lazy attributes
14
13
  # class Document
@@ -7,21 +7,37 @@ module Uniword
7
7
  # getters/setters generated by lutaml-model for declared attributes.
8
8
  #
9
9
  # Validates that the attribute name exists in the model's schema before
10
- # dispatching — unlike raw send() which bypasses visibility checks, this
11
- # only accesses attributes that are part of the model's declared public API.
10
+ # dispatching through the model's public method table — only attributes
11
+ # that are part of the model's declared public API can be accessed.
12
12
  module ModelAttributeAccess
13
13
  # Read a declared attribute's value by name.
14
14
  # @param name [Symbol] Declared attribute name
15
15
  # @return [Object] The attribute value
16
+ # @raise [ArgumentError] if the attribute is not declared on the model
16
17
  def read_attribute(name)
17
- __send__(name)
18
+ validate_declared_attribute!(name)
19
+ method(name).call
18
20
  end
19
21
 
20
22
  # Write a declared attribute's value by name.
21
23
  # @param name [Symbol] Declared attribute name
22
24
  # @param value [Object] The value to assign
25
+ # @raise [ArgumentError] if the attribute is not declared on the model
23
26
  def write_attribute(name, value)
24
- __send__(:"#{name}=", value)
27
+ validate_declared_attribute!(name)
28
+ method(:"#{name}=").call(value)
29
+ end
30
+
31
+ private
32
+
33
+ # Validate that the attribute is declared in the model's schema.
34
+ # @param name [Symbol, String] Attribute name
35
+ # @raise [ArgumentError] if the attribute is not declared
36
+ def validate_declared_attribute!(name)
37
+ return if self.class.attributes.key?(name.to_sym)
38
+
39
+ raise ArgumentError,
40
+ "undeclared attribute #{name.inspect} for #{self.class}"
25
41
  end
26
42
  end
27
43
  end
@@ -195,7 +195,13 @@ module Uniword
195
195
  #
196
196
  # @param document [Document] The document to save (Generated::Wordprocessingml::DocumentRoot)
197
197
  # @param path [String] Output path
198
- def self.to_file(document, path, profile: nil)
198
+ # @param profile [Object, nil] Unused (DOCX reconciliation profile)
199
+ # @param validate [Boolean, nil] Run the package integrity gate before
200
+ # writing; nil falls back to Uniword.configuration.validate_on_save
201
+ # @return [void]
202
+ # @raise [Uniword::ValidationError] when the gate is enabled and the
203
+ # generated package content is invalid
204
+ def self.to_file(document, path, profile: nil, validate: nil)
199
205
  # Create package
200
206
  package = new
201
207
 
@@ -217,6 +223,9 @@ module Uniword
217
223
  # Add required OOXML infrastructure files
218
224
  add_required_files(zip_content)
219
225
 
226
+ # Refuse invalid output before packaging
227
+ package.enforce_package_integrity(zip_content, validate)
228
+
220
229
  # Package and save
221
230
  packager = Infrastructure::ZipPackager.new
222
231
  packager.package(zip_content, path)
@@ -225,13 +234,44 @@ module Uniword
225
234
  # Save package to file
226
235
  #
227
236
  # @param path [String] Output path
228
- def to_file(path)
237
+ # @param validate [Boolean, nil] Run the package integrity gate before
238
+ # writing; nil falls back to Uniword.configuration.validate_on_save
239
+ # @return [void]
240
+ # @raise [Uniword::ValidationError] when the gate is enabled and the
241
+ # generated package content is invalid
242
+ def to_file(path, validate: nil)
229
243
  zip_content = to_zip_content
230
244
 
245
+ enforce_package_integrity(zip_content, validate)
246
+
231
247
  packager = Infrastructure::ZipPackager.new
232
248
  packager.package(zip_content, path)
233
249
  end
234
250
 
251
+ # Write-time integrity gate: refuse invalid package content.
252
+ #
253
+ # DOTX is an OPC package like DOCX, so the same
254
+ # Docx::PackageIntegrityChecker invariants apply.
255
+ #
256
+ # @api private
257
+ # @param zip_content [Hash] File paths => content
258
+ # @param validate [Boolean, nil] explicit override; nil reads policy
259
+ # @return [void]
260
+ # @raise [Uniword::ValidationError] listing all integrity issues
261
+ def enforce_package_integrity(zip_content, validate)
262
+ validate = Uniword.configuration.validate_on_save if validate.nil?
263
+ return unless validate
264
+
265
+ issues = Docx::PackageIntegrityChecker.new.check(zip_content)
266
+ return if issues.empty?
267
+
268
+ raise Uniword::ValidationError.new(
269
+ self,
270
+ issues.map { |issue| "#{issue.code} (#{issue.part}): #{issue.message}" },
271
+ issues: issues,
272
+ )
273
+ end
274
+
235
275
  # Generate ZIP content hash
236
276
  #
237
277
  # @return [Hash] File paths => content
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Uniword
4
+ module Ooxml
5
+ # Safe mutation of lutaml-model `element_order` arrays.
6
+ #
7
+ # lutaml-model freezes `element_order` on parsed models; mutating
8
+ # it raises FrozenError. These helpers thaw on demand (dup +
9
+ # assign back) and insert entries in schema position — the single
10
+ # mechanism for the whole library, not a monkey-patch.
11
+ #
12
+ # @example Thaw and append
13
+ # order = Ooxml::ElementOrder.mutable_order(footnotes)
14
+ # order << Lutaml::Xml::Element.new("Element", "footnote")
15
+ #
16
+ # @example Idempotent singleton insert
17
+ # Ooxml::ElementOrder.insert_once(settings, "updateFields",
18
+ # after: "characterSpacingControl")
19
+ module ElementOrder
20
+ # Return the model's element_order as a mutable array.
21
+ #
22
+ # The frozen parsed array is replaced by a dup assigned back to
23
+ # the model; already-mutable arrays are returned as-is (so
24
+ # repeated calls keep mutating the same registered array).
25
+ #
26
+ # @param model [Lutaml::Model::Serializable] model to thaw
27
+ # @return [Array, nil] mutable element_order, or nil when the
28
+ # model carries none
29
+ def mutable_order(model)
30
+ order = model.element_order
31
+ return unless order
32
+ return order unless order.frozen?
33
+
34
+ model.element_order = order.dup
35
+ end
36
+ module_function :mutable_order
37
+
38
+ # Append an entry (repeatable elements allowed).
39
+ #
40
+ # @param model [Lutaml::Model::Serializable] model to mutate
41
+ # @param entry [Lutaml::Xml::Element] entry to append
42
+ # @return [Array, nil] the mutated order
43
+ def append(model, entry)
44
+ mutable_order(model)&.<<(entry)
45
+ end
46
+ module_function :append
47
+
48
+ # Insert an entry at an index (repeatable elements allowed).
49
+ #
50
+ # @param model [Lutaml::Model::Serializable] model to mutate
51
+ # @param position [Integer] insertion index
52
+ # @param entry [Lutaml::Xml::Element] entry to insert
53
+ # @return [Array, nil] the mutated order
54
+ def insert_at(model, position, entry)
55
+ mutable_order(model)&.insert(position, entry)
56
+ end
57
+ module_function :insert_at
58
+
59
+ # Insert a singleton element in schema position, by name.
60
+ # Idempotent: no-op when an entry of that name already exists.
61
+ # Missing anchors fall back to end (after:) or start (before:).
62
+ #
63
+ # @param model [Lutaml::Model::Serializable] model to mutate
64
+ # @param name [String] element name to insert
65
+ # @param after [String, nil] insert after this element name
66
+ # @param before [String, nil] insert before this element name
67
+ # @param position [Integer, nil] explicit index (when neither
68
+ # after nor before is given; defaults to end)
69
+ # @return [Array, nil] the mutated order
70
+ def insert_once(model, name, after: nil, before: nil, position: nil)
71
+ order = mutable_order(model)
72
+ return unless order
73
+ return if order.any? { |e| e.name == name }
74
+
75
+ idx = insertion_index(order, after, before, position)
76
+ order.insert(idx, Lutaml::Xml::Element.new("Element", name))
77
+ end
78
+ module_function :insert_once
79
+
80
+ # @return [Integer] index for the insert
81
+ def insertion_index(order, after, before, position)
82
+ if after
83
+ anchor = order.index { |e| e.name == after }
84
+ anchor ? anchor + 1 : order.size
85
+ elsif before
86
+ order.index { |e| e.name == before } || 0
87
+ else
88
+ position || order.size
89
+ end
90
+ end
91
+ module_function :insertion_index
92
+ end
93
+ end
94
+ end