Class: Uniword::Ooxml::ThemePackage

Inherits:
Object
  • Object
show all
Defined in:
lib/uniword/ooxml/theme_package.rb

Overview

Specialized package handler for Theme (.thmx) files

Themes are stored in .thmx PowerPoint theme files with the main theme definition at theme/theme/theme1.xml. This class bridges the package infrastructure with Theme model serialization.

Examples:

Load a Theme

package = ThemePackage.new(path: 'Atlas.thmx')
theme = package.load_content
puts theme.name
package.cleanup

Save a Theme

package = ThemePackage.new(path: 'template.thmx')
package.extract
package.save_content(my_theme)
package.package('output.thmx')
package.cleanup

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:) ⇒ ThemePackage

Initialize with file path

Parameters:

  • path (String)

    Path to .thmx file



36
37
38
39
40
# File 'lib/uniword/ooxml/theme_package.rb', line 36

def initialize(path:)
  @path = path
  @extracted_content = nil
  @theme = nil
end

Instance Attribute Details

#extracted_contentObject

Extracted content (Hash of file paths => content)



28
29
30
# File 'lib/uniword/ooxml/theme_package.rb', line 28

def extracted_content
  @extracted_content
end

#pathObject

Source file path



25
26
27
# File 'lib/uniword/ooxml/theme_package.rb', line 25

def path
  @path
end

#themeObject (readonly)

Domain model loaded from package



31
32
33
# File 'lib/uniword/ooxml/theme_package.rb', line 31

def theme
  @theme
end

Class Method Details

.load_from_file(path) ⇒ Theme

Convenience: Load Theme from file

Parameters:

  • path (String)

    Path to .thmx file

Returns:

  • (Theme)

    Loaded Theme



132
133
134
135
136
137
# File 'lib/uniword/ooxml/theme_package.rb', line 132

def self.load_from_file(path)
  package = new(path: path)
  theme = package.load_content
  package.cleanup
  theme
end

.save_to_file(theme, output_path, template_path:) ⇒ String

Convenience: Save Theme to file

Creates new .thmx package with Theme, using template as base.

Parameters:

  • theme (Theme)

    Theme to save

  • output_path (String)

    Output .thmx file path

  • template_path (String)

    Template .thmx to base on

Returns:

  • (String)

    Path to created file



147
148
149
150
151
152
153
154
155
# File 'lib/uniword/ooxml/theme_package.rb', line 147

def self.save_to_file(theme, output_path, template_path:)
  package = new(path: template_path)
  package.extract
  package.save_content(theme)
  package.package(output_path)
  package.cleanup

  output_path
end

Instance Method Details

#cleanupvoid

This method returns an undefined value.

Cleanup extracted content (no-op since we use memory)



60
61
62
# File 'lib/uniword/ooxml/theme_package.rb', line 60

def cleanup
  @extracted_content = nil
end

#extractvoid

This method returns an undefined value.

Extract ZIP package



45
46
47
48
# File 'lib/uniword/ooxml/theme_package.rb', line 45

def extract
  extractor = Infrastructure::ZipExtractor.new
  @extracted_content = extractor.extract(@path)
end

#extracted_dirBoolean

Check if extracted

Returns:

  • (Boolean)


53
54
55
# File 'lib/uniword/ooxml/theme_package.rb', line 53

def extracted_dir
  @extracted_content
end

#load_contentTheme

Load Theme from package

Extracts package and parses theme/theme/theme1.xml into Theme model. Uses lutaml-model deserialization for XML parsing.

Returns:

  • (Theme)

    Loaded Theme model

Raises:

  • (ArgumentError)

    if package is invalid



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/uniword/ooxml/theme_package.rb', line 80

def load_content
  extract unless @extracted_content

  # Read theme XML
  theme_xml = read_theme

  # Parse into Theme using lutaml-model
  @theme = Drawingml::Theme.from_xml(theme_xml)

  # Store source file reference
  @theme.source_file = path if @theme

  @theme
end

#package(output_path) ⇒ void

This method returns an undefined value.

Package extracted content into ZIP file

Parameters:

  • output_path (String)

    Path to output .thmx file

Raises:

  • (RuntimeError)

    if package not extracted



121
122
123
124
125
126
# File 'lib/uniword/ooxml/theme_package.rb', line 121

def package(output_path)
  raise "Must extract before packaging" unless @extracted_content

  packager = Infrastructure::ZipPackager.new
  packager.package(@extracted_content, output_path)
end

#read_themeString

Read theme XML from extracted package

Returns:

  • (String)

    Theme XML content



67
68
69
70
71
# File 'lib/uniword/ooxml/theme_package.rb', line 67

def read_theme
  raise "Must extract before reading" unless @extracted_content

  @extracted_content["theme/theme/theme1.xml"]
end

#save_content(theme) ⇒ void

This method returns an undefined value.

Save Theme to package

Serializes Theme model to theme/theme/theme1.xml using lutaml-model. Preserves media files and variants unchanged.

Parameters:

  • theme (Theme)

    Theme model to save

Raises:

  • (RuntimeError)

    if package not extracted



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/uniword/ooxml/theme_package.rb', line 103

def save_content(theme)
  raise "Must extract before saving" unless @extracted_content

  @theme = theme

  # Serialize Theme to XML using lutaml-model with prefix: true
  # to preserve namespace prefixes for cross-namespace elements
  theme_xml = theme.to_xml(prefix: true)

  # Update content hash
  @extracted_content["theme/theme/theme1.xml"] = theme_xml
end