Class: Uniword::Stylesets::Package

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

Overview

Represents a .dotx StyleSet package (ZIP with word/styles.xml)

Follows MODEL-DRIVEN architecture using lutaml-model for XML deserialization. No manual parsing.

Examples:

Load StyleSet from .dotx

package = Package.from_file('Distinctive.dotx')
styleset = package.styleset
puts styleset.name

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Package

Initialize Package

Parameters:

  • attributes (Hash) (defaults to: {})

    Package attributes



26
27
28
29
# File 'lib/uniword/stylesets/package.rb', line 26

def initialize(attributes = {})
  super
  @styles_configuration ||= Uniword::Wordprocessingml::StylesConfiguration.new
end

Class Method Details

.extract_zip(path) ⇒ Hash

Extract ZIP contents

Parameters:

  • path (String)

    ZIP file path

Returns:

  • (Hash)

    Extracted files => content



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

def self.extract_zip(path)
  require "zip"

  contents = {}
  Zip::File.open(path) do |zip|
    zip.each do |entry|
      next if entry.directory?

      contents[entry.name] = entry.get_input_stream.read
    end
  end
  contents
rescue Zip::Error => e
  raise Uniword::CorruptedFileError.new(path,
                                        "Failed to extract: #{e.message}")
end

.from_file(path) ⇒ Package

Load Package from .dotx file

Examples:

pkg = Package.from_file('Distinctive.dotx')

Parameters:

  • path (String)

    Path to .dotx file

Returns:

Raises:



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/uniword/stylesets/package.rb', line 40

def self.from_file(path)
  # Validate file exists
  raise Uniword::FileNotFoundError, path unless File.exist?(path)

  # Extract ZIP
  extracted = extract_zip(path)

  # Deserialize styles.xml using lutaml-model (MODEL-DRIVEN!)
  styles_xml = extracted["word/styles.xml"]
  unless styles_xml
    raise Uniword::CorruptedFileError.new(path,
                                          "styles.xml missing")
  end

  styles_config = Uniword::Wordprocessingml::StylesConfiguration.from_xml(styles_xml)

  # Create Package instance
  new(
    styles_configuration: styles_config,
    source_path: path,
  )
end

Instance Method Details

#stylesetStyleSet

Convert to StyleSet model

Returns:

  • (StyleSet)

    StyleSet with styles from package



66
67
68
69
70
71
72
# File 'lib/uniword/stylesets/package.rb', line 66

def styleset
  StyleSet.new(
    name: extract_name,
    styles: styles_configuration.styles,
    source_file: source_path,
  )
end