Class: Uniword::Ooxml::ThmxPackage

Inherits:
Lutaml::Model::Serializable
  • Object
show all
Defined in:
lib/uniword/ooxml/thmx_package.rb

Overview

THMX Package - Word Theme format

Represents .thmx (theme) files, which are standalone theme packages. Unlike DOCX/DOTX, THMX contains ONLY a theme1.xml file.

This is the CORRECT OOP approach:

  • ONE model class for the container

  • Theme as the sole content attribute

  • No serializer/deserializer anti-pattern

Examples:

Load theme

theme = ThmxPackage.from_file('celestial.thmx')
theme.name # => "Celestial"

Save theme

ThmxPackage.to_file(theme, 'output.thmx')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#themeObject

Access theme



57
58
59
# File 'lib/uniword/ooxml/thmx_package.rb', line 57

def theme
  @theme
end

Class Method Details

.from_file(path) ⇒ Theme

Load THMX package from file

Parameters:

  • path (String)

    Path to .thmx file

Returns:

  • (Theme)

    Loaded theme



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/uniword/ooxml/thmx_package.rb', line 31

def self.from_file(path)
  # Extract ZIP content
  extractor = Infrastructure::ZipExtractor.new
  zip_content = extractor.extract(path)

  # Parse package
  package = from_zip_content(zip_content)

  # Return theme directly (NOT a Document!)
  package.theme || Theme.new
end

.from_zip_content(zip_content) ⇒ ThmxPackage

Create package from extracted ZIP content

Parameters:

  • zip_content (Hash)

    Extracted ZIP files

Returns:



47
48
49
50
51
52
53
54
# File 'lib/uniword/ooxml/thmx_package.rb', line 47

def self.from_zip_content(zip_content)
  package = new

  # Parse theme (the only content)
  package.theme = Theme.from_xml(zip_content["theme/theme/theme1.xml"]) if zip_content["theme/theme/theme1.xml"]

  package
end

.supported_extensionsArray<String>

Get supported file extensions

Returns:

  • (Array<String>)

    Array of supported extensions



62
63
64
# File 'lib/uniword/ooxml/thmx_package.rb', line 62

def self.supported_extensions
  [".thmx"]
end

.to_file(theme, path) ⇒ Object

Save theme to file

Parameters:

  • theme (Theme)

    The theme to save

  • path (String)

    Output path



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/uniword/ooxml/thmx_package.rb', line 70

def self.to_file(theme, path)
  # Create package
  package = new
  package.theme = theme

  # Generate ZIP content
  zip_content = package.to_zip_content

  # Add required OOXML infrastructure files
  add_required_files(zip_content)

  # Package and save
  packager = Infrastructure::ZipPackager.new
  packager.package(zip_content, path)
end

Instance Method Details

#to_zip_contentHash

Generate ZIP content hash

Returns:

  • (Hash)

    File paths => content



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/uniword/ooxml/thmx_package.rb', line 89

def to_zip_content
  content = {}

  # Serialize theme (the only content)
  if theme
    content["theme/theme/theme1.xml"] =
      theme.to_xml(encoding: "UTF-8")
  end

  content
end