uniword 1.7.9 → 1.7.10

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 06711e3879a7ef26b9e433fd0385b0c3d25bc23061ea8527ef0954f3a5b03f6c
4
- data.tar.gz: c473820f6894ac19ee21ef8ce36e229f0486cdfeebff18a3eaec76297437f1fe
3
+ metadata.gz: 014c542e235636124a1582857acd60b35b69e60ac3e8e532798c3657e0e57938
4
+ data.tar.gz: 34bb4d1174fba71f7b6fab4b8957d90478b9cc885012ff6df5e7244d267203a4
5
5
  SHA512:
6
- metadata.gz: 3bd1973b1dc8d91639ab93baa310e11d02dc8a59d2c187b4fe335d9ed2ac81df4b346284172cadecdd101fade943135cc473c625a1fe7157172159e7fd11231b
7
- data.tar.gz: 6041e14afc2bd9369d5d31c2bc2f6849cf51a76862dd4832a80ba5549d4c20bd934f9737d5c014b7d58b022a07b2813d9279fd97f0afa85d90f7e86271ff0f1b
6
+ metadata.gz: 2de50e546cc071412b5f1430e677476d2be42676a3911f0e5633ca33e7bac7aed6c2c51956ca2bb7e34bd022de94799ae3c8a108775f785c72fa63ff5741842f
7
+ data.tar.gz: 39a90db4bdb21facfeb096e0e7535f121ee2d1d8019c1144967c11c0e47006d734312eb6f27c85fc73337af8e6f35e3da0b878f412f2dd73f31dfed7276f80a2
@@ -44,6 +44,8 @@ module Uniword
44
44
  # @example Load with explicit format
45
45
  # document = DocumentFactory.from_file("doc.mht", format: :mhtml)
46
46
  def from_file(path, format: :auto)
47
+ Uniword.ensure_adapter!
48
+
47
49
  # Handle binary strings (for docx gem compatibility)
48
50
  # Convert to StringIO if it's a binary string (contains null bytes or has binary encoding)
49
51
  path = StringIO.new(path) if path.is_a?(String) && (path.encoding == Encoding::ASCII_8BIT || path.include?("\x00"))
@@ -105,6 +107,8 @@ module Uniword
105
107
  # data = File.binread("doc.docx")
106
108
  # document = DocumentFactory.from_file_data(data)
107
109
  def from_file_data(stream, format: :auto)
110
+ Uniword.ensure_adapter!
111
+
108
112
  # Convert binary string to StringIO if needed
109
113
  stream = StringIO.new(stream) if stream.is_a?(String)
110
114
 
@@ -295,7 +295,7 @@ module Uniword
295
295
  # Accepts Omml::Models::CTOMath/OMath/OMathPara (the typed attribute
296
296
  # values on Paragraph) — anything responding to `to_xml` works.
297
297
  def omath_to_html(o_math)
298
- xml = o_math.to_xml
298
+ xml = o_math_to_xml_string(o_math)
299
299
  # Strip XML declaration and namespace prefixes for clean inline HTML
300
300
  xml = xml.gsub(/<\?[^>]+>\s*/, "")
301
301
  # Remove namespace declarations that Word HTML doesn't use
@@ -304,6 +304,19 @@ module Uniword
304
304
  %(<span class="stem">#{xml}</span>)
305
305
  end
306
306
 
307
+ # Serialize an OMML model for the inline MHTML payload. A bare
308
+ # Omml::Models::CTOMath serializes with its schema TYPE name as the
309
+ # root element (`m:CT_OMath`); Word only accepts the m:oMath ELEMENT
310
+ # there — inside a paragraph the DOCX writer emits oMath via the
311
+ # paragraph mapping, so this root-level path is MHTML-only. Rename
312
+ # the root tag so the equation survives the .doc round-trip.
313
+ def o_math_to_xml_string(o_math)
314
+ xml = o_math.to_xml
315
+ xml.sub(/\A<\s*m?:CT_OMath/, "<m:oMath")
316
+ .sub(/<\s*m?:CT_OMath([^>]*)\/>/, "<m:oMath\\1>")
317
+ .sub(/<\/\s*m?:CT_OMath\s*>\z/, "</m:oMath>")
318
+ end
319
+
307
320
  # Convert OOXML Table to HTML
308
321
  def table_to_html(table)
309
322
  rows = table.rows || []
@@ -379,12 +392,16 @@ module Uniword
379
392
  # Convert OOXML TableCell to HTML
380
393
  # For simple cells (single plain paragraph), render text directly.
381
394
  # For complex cells (multiple paragraphs, styled text), wrap in <p>.
395
+ # Nested tables (<w:tbl> inside <w:tc>) are rendered as <table>
396
+ # inside the cell — dropping them silently loses the whole nested
397
+ # table from the MHTML output while the DOCX keeps it.
382
398
  # Header cells (<th>) use <th> tag instead of <td>.
383
399
  #
384
400
  # @param cell [TableCell] The cell to render
385
401
  # @param rowspan [Integer, nil] Rowspan from vMerge computation
386
402
  def table_cell_to_html(cell, rowspan: nil)
387
403
  paragraphs = cell.paragraphs || []
404
+ nested_tables = cell.tables || []
388
405
  tag = cell.header ? "th" : "td"
389
406
 
390
407
  # Build merge attributes
@@ -395,14 +412,13 @@ module Uniword
395
412
  attrs << "rowspan=#{rowspan}" if rowspan
396
413
  attr_str = attrs.empty? ? "" : " #{attrs.join(' ')}"
397
414
 
398
- if paragraphs.size == 1 && simple_cell_paragraph?(paragraphs.first)
415
+ if nested_tables.empty? && paragraphs.size == 1 &&
416
+ simple_cell_paragraph?(paragraphs.first)
399
417
  # Simple cell: render text content directly (no <p> wrapping)
400
418
  content = paragraph_content_to_html(paragraphs.first)
401
419
  %(<#{tag}#{attr_str}>#{content}</#{tag}>)
402
420
  else
403
- content = paragraphs.map do |para|
404
- paragraph_to_html(para)
405
- end.join("\n")
421
+ content = table_cell_content_to_html(cell, paragraphs, nested_tables)
406
422
 
407
423
  <<~HTML
408
424
  <#{tag}#{attr_str}>
@@ -412,6 +428,44 @@ module Uniword
412
428
  end
413
429
  end
414
430
 
431
+ # Cell body HTML: paragraphs and nested tables in document order.
432
+ # element_order (tracked by the builders and by lutaml's
433
+ # mixed-content parse) drives the interleaving; models without it
434
+ # fall back to paragraphs-then-tables. Content not represented in
435
+ # element_order (appended programmatically after a parse) is
436
+ # appended, so no cell content is ever dropped.
437
+ def table_cell_content_to_html(cell, paragraphs, nested_tables)
438
+ order = Array(cell.element_order)
439
+ if order.empty?
440
+ paragraph_html = paragraphs.map { |para| paragraph_to_html(para) }
441
+ table_html = nested_tables.map { |tbl| table_to_html(tbl) }
442
+ return (paragraph_html + table_html).join("\n")
443
+ end
444
+
445
+ para_idx = 0
446
+ table_idx = 0
447
+ ordered_html = order.filter_map do |entry|
448
+ next unless entry.element?
449
+
450
+ case entry.name
451
+ when "p"
452
+ para = paragraphs[para_idx]
453
+ para_idx += 1
454
+ paragraph_to_html(para) if para
455
+ when "tbl"
456
+ tbl = nested_tables[table_idx]
457
+ table_idx += 1
458
+ table_to_html(tbl) if tbl
459
+ end
460
+ end
461
+
462
+ leftover_html =
463
+ paragraphs[para_idx..].to_a.map { |para| paragraph_to_html(para) } +
464
+ nested_tables[table_idx..].to_a.map { |tbl| table_to_html(tbl) }
465
+
466
+ (ordered_html + leftover_html).join("\n")
467
+ end
468
+
415
469
  # Check if a paragraph is simple enough to render without <p> wrapping
416
470
  # in a table cell. Single paragraphs with inline content (runs, hyperlinks)
417
471
  # are considered simple. Only block-level structures (SDTs, oMathPara)
@@ -98,13 +98,21 @@ module Uniword
98
98
 
99
99
  # Convert OOXML TableCell to HTML
100
100
  #
101
+ # Nested tables (<w:tbl> inside <w:tc>) are rendered after the
102
+ # cell's paragraphs; dropping them silently loses the whole nested
103
+ # table from the HTML output while the DOCX keeps it.
104
+ #
101
105
  # @param cell [Uniword::Wordprocessingml::TableCell]
102
106
  # @return [String] HTML <td> element
103
107
  def self.table_cell_to_html(cell)
104
108
  paragraphs_html = cell.paragraphs.map do |p|
105
109
  paragraph_to_html(p)
106
110
  end.join("\n")
107
- "<td>\n#{paragraphs_html}\n</td>"
111
+ tables_html = Array(cell.tables).map do |tbl|
112
+ table_to_html(tbl)
113
+ end.join("\n")
114
+ content = [paragraphs_html, tables_html].reject(&:empty?).join("\n")
115
+ "<td>\n#{content}\n</td>"
108
116
  end
109
117
 
110
118
  # Wrap HTML content in full HTML document
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Uniword
4
- VERSION = "1.7.9"
4
+ VERSION = "1.7.10"
5
5
  end
@@ -254,6 +254,7 @@ module Uniword
254
254
  # writing; nil falls back to Uniword.configuration.validate_on_save
255
255
  # @return [void]
256
256
  def save(path, format: :auto, profile: nil, validate: nil)
257
+ Uniword.ensure_adapter!
257
258
  writer = DocumentWriter.new(self)
258
259
  writer.save(path, format: format, profile: profile, validate: validate)
259
260
  end
data/lib/uniword.rb CHANGED
@@ -52,6 +52,27 @@ require "zip"
52
52
  #
53
53
  # @see DocumentFactory Factory for reading documents
54
54
  module Uniword
55
+ # The adapter boot resolved (leptris when usable, else moxml's
56
+ # nokogiri fallback). Gems loaded after uniword can overwrite lutaml's
57
+ # process-global adapter at their own require or first-parse time -
58
+ # plurimath's mml chain writes
59
+ # Lutaml::Model::Config.xml_adapter_type = :ox (plurimath#588) - so
60
+ # every public entry point re-asserts this value via ensure_adapter!.
61
+ UNIWORD_XML_ADAPTER = Moxml::Config.default_adapter
62
+
63
+ # Re-assert the adapter boot resolved when another gem's global write
64
+ # has replaced it. nil means the unset delegate form (which resolves to
65
+ # the same Moxml default) and is left alone; a concrete foreign value
66
+ # is replaced. Every DocumentFactory/DocumentRoot entry point calls
67
+ # this, so a mid-process adapter flip cannot change serialization or
68
+ # validation semantics of uniword round trips.
69
+ def self.ensure_adapter!
70
+ current = Lutaml::Model::Config.xml_adapter_type
71
+ return if current.nil? || current == UNIWORD_XML_ADAPTER
72
+
73
+ Lutaml::Model::Config.xml_adapter_type = UNIWORD_XML_ADAPTER
74
+ end
75
+
55
76
  # Wire Uniword into Omml's context system so Omml::Models types resolve
56
77
  # correctly when used as attribute types on Uniword's serializable classes
57
78
  # (Paragraph#o_maths typed as Omml::Models::CTOMath, etc.).
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uniword
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.9
4
+ version: 1.7.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.