Class: Uniword::StyleSet

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

Overview

Represents a StyleSet containing a collection of style definitions

StyleSets are named collections of paragraph, character, table, and numbering styles that provide consistent formatting for Word documents. They work in conjunction with themes to create professionally styled documents.

Examples:

Load StyleSet from .dotx file

styleset = Uniword::StyleSet.from_dotx('Distinctive.dotx')
puts styleset.name
puts "Styles: #{styleset.styles.count}"

Apply StyleSet to document

doc = Uniword::Wordprocessingml::DocumentRoot.new
styleset.apply_to(doc)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ StyleSet

Initialize StyleSet

Parameters:

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

    StyleSet attributes



39
40
41
42
43
# File 'lib/uniword/styleset.rb', line 39

def initialize(attributes = {})
  super
  @styles ||= []
  @source_file ||= nil
end

Class Method Details

.available_stylesetsArray<String>

List all available bundled StyleSets

Examples:

List StyleSets

stylesets = StyleSet.available_stylesets
# => ["distinctive", "basic", ...]

Returns:

  • (Array<String>)

    StyleSet names



127
128
129
# File 'lib/uniword/styleset.rb', line 127

def self.available_stylesets
  Stylesets::YamlStyleSetLoader.available_stylesets
end

.from_dotx(path) ⇒ StyleSet

Load StyleSet from .dotx file

Examples:

Load StyleSet

styleset = StyleSet.from_dotx('Distinctive.dotx')

Parameters:

  • path (String)

    Path to .dotx file

Returns:



105
106
107
# File 'lib/uniword/styleset.rb', line 105

def self.from_dotx(path)
  Stylesets::Package.from_file(path).styleset
end

.load(name) ⇒ StyleSet

Load bundled StyleSet by name

Examples:

Load bundled StyleSet

styleset = StyleSet.load('distinctive')

Parameters:

  • name (String)

    StyleSet name (e.g., ‘distinctive’, ‘basic’)

Returns:



116
117
118
# File 'lib/uniword/styleset.rb', line 116

def self.load(name)
  Stylesets::YamlStyleSetLoader.load_bundled(name)
end

Instance Method Details

#apply_to(document, strategy: :keep_existing) ⇒ void

This method returns an undefined value.

Apply this StyleSet to a document

Merges all styles from this StyleSet into the document’s styles configuration.

Parameters:

  • document (Document)

    Target document

  • strategy (Symbol) (defaults to: :keep_existing)

    Conflict resolution strategy

    • :keep_existing (default) - Keep existing styles

    • :replace - Replace existing with StyleSet styles

    • :rename - Keep both, rename imported styles



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/uniword/styleset.rb', line 142

def apply_to(document, strategy: :keep_existing)
  return unless document.respond_to?(:styles_configuration)

  styles.each do |style|
    case strategy
    when :replace
      document.styles_configuration.add_style(style.dup,
                                              allow_overwrite: true)
    when :keep_existing
      document.styles_configuration.add_style(style.dup) unless document.styles_configuration.style_exists?(style.id)
    when :rename
      if document.styles_configuration.style_exists?(style.id)
        new_style = style.dup
        new_style.id = "#{style.id}_styleset"
        document.styles_configuration.add_style(new_style)
      else
        document.styles_configuration.add_style(style.dup)
      end
    else
      raise ArgumentError, "Invalid strategy: #{strategy}"
    end
  end
end

#character_stylesArray<Style>

Get character styles

Returns:

  • (Array<Style>)

    Character styles



78
79
80
# File 'lib/uniword/styleset.rb', line 78

def character_styles
  styles_by_type("character")
end

#countInteger

Get the number of styles

Returns:

  • (Integer)

    Number of styles



48
49
50
# File 'lib/uniword/styleset.rb', line 48

def count
  styles.size
end

#inspectString

Provide detailed inspection for debugging

Returns:

  • (String)

    A readable representation of the StyleSet



92
93
94
95
96
# File 'lib/uniword/styleset.rb', line 92

def inspect
  "#<Uniword::StyleSet name=#{name.inspect} " \
    "styles=#{styles.count} " \
    "source=#{source_file.inspect}>"
end

#paragraph_stylesArray<Style>

Get paragraph styles

Returns:

  • (Array<Style>)

    Paragraph styles



71
72
73
# File 'lib/uniword/styleset.rb', line 71

def paragraph_styles
  styles_by_type("paragraph")
end

#styles_by_type(type) ⇒ Array<Style>

Get styles by type

Parameters:

  • type (String, Symbol)

    Style type (:paragraph, :character, :table, :numbering)

Returns:

  • (Array<Style>)

    Styles of the specified type



63
64
65
66
# File 'lib/uniword/styleset.rb', line 63

def styles_by_type(type)
  type_str = type.to_s
  styles.select { |s| s.type == type_str }
end

#table_stylesArray<Style>

Get table styles

Returns:

  • (Array<Style>)

    Table styles



85
86
87
# File 'lib/uniword/styleset.rb', line 85

def table_styles
  styles_by_type("table")
end

#valid?Boolean

Check if StyleSet is valid

Returns:

  • (Boolean)

    true if valid



55
56
57
# File 'lib/uniword/styleset.rb', line 55

def valid?
  !name.nil? && !name.empty? && styles.is_a?(Array)
end