Class: Uniword::Wordprocessingml::StylesConfiguration
- Inherits:
-
Lutaml::Model::Serializable
- Object
- Lutaml::Model::Serializable
- Uniword::Wordprocessingml::StylesConfiguration
- Defined in:
- lib/uniword/wordprocessingml/styles_configuration.rb
Overview
Manages document-level style configuration Responsibility: Registry and factory for document styles
This class maintains the collection of all styles in a document, provides access to styles by ID or name, and generates default styles.
Instance Method Summary collapse
-
#add_character_style(style, allow_overwrite: true) ⇒ Object
Add a character style (alias for add_style with type check) TEMPORARY: Type checking disabled for v2.0.
-
#add_default_styles ⇒ void
Add default styles required by Word.
-
#add_paragraph_style(style, allow_overwrite: true) ⇒ Object
Add a paragraph style (alias for add_style with type check) TEMPORARY: Type checking disabled for v2.0.
-
#add_style(style, allow_overwrite: false) ⇒ Style
Add a style to the configuration.
-
#character_styles ⇒ Array<CharacterStyle>
Get all character styles.
-
#count ⇒ Integer
Get the number of styles.
-
#create_character_style(id, name, **attributes) ⇒ Style
Create a custom character style.
-
#create_paragraph_style(id, name, **attributes) ⇒ Style
Create a custom paragraph style.
-
#custom_styles ⇒ Array<Style>
Get all custom styles.
-
#default_styles ⇒ Array<Style>
Get all default styles.
-
#empty? ⇒ Boolean
Check if configuration has any styles.
-
#export_styles ⇒ Array<Style>
Export all styles (creates deep copies).
-
#find_by_id(style_id) ⇒ Style?
Find a style by ID (alias for clarity).
-
#import_from_document(source_document) ⇒ void
Import styles from another document.
-
#initialize(attributes = {}) ⇒ StylesConfiguration
constructor
Initialize with optional default styles.
-
#merge(other_config, conflict_resolution: :keep_existing) ⇒ void
Merge styles from another configuration.
-
#paragraph_styles ⇒ Array<ParagraphStyle>
Get all paragraph styles.
-
#remove_style(id) ⇒ Style?
Remove a style by ID.
-
#resolve_inheritance(style_id) ⇒ Hash
Resolve style inheritance chain Returns the effective style properties by following basedOn chain.
-
#style(id_or_name) ⇒ Style?
Find a style by ID or name.
-
#style_by_id(id) ⇒ Style?
Find a style by ID.
-
#style_by_name(name) ⇒ Style?
Find a style by name.
-
#style_exists?(style_id) ⇒ Boolean
Check if a style with the given ID exists.
-
#valid? ⇒ Boolean
Validate all styles.
Constructor Details
#initialize(attributes = {}) ⇒ StylesConfiguration
Initialize with optional default styles
61 62 63 64 65 |
# File 'lib/uniword/wordprocessingml/styles_configuration.rb', line 61 def initialize(attributes = {}) include_defaults = attributes.delete(:include_defaults) != false super add_default_styles if include_defaults && styles.empty? end |
Instance Method Details
#add_character_style(style, allow_overwrite: true) ⇒ Object
Add a character style (alias for add_style with type check) TEMPORARY: Type checking disabled for v2.0
308 309 310 |
# File 'lib/uniword/wordprocessingml/styles_configuration.rb', line 308 def add_character_style(style, allow_overwrite: true) add_style(style, allow_overwrite: allow_overwrite) end |
#add_default_styles ⇒ void
This method returns an undefined value.
Add default styles required by Word
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/uniword/wordprocessingml/styles_configuration.rb', line 196 def add_default_styles # Add Normal paragraph style (required) add_style(ParagraphStyle.normal) unless style_by_id("Normal") # Add default character style add_style(CharacterStyle.default_char) unless style_by_id("DefaultParagraphFont") # Add heading styles (1-9) (1..9).each do |level| id = "Heading#{level}" add_style(ParagraphStyle.heading(level)) unless style_by_id(id) end # Add common character styles add_style(CharacterStyle.emphasis) unless style_by_id("Emphasis") add_style(CharacterStyle.strong) unless style_by_id("Strong") end |
#add_paragraph_style(style, allow_overwrite: true) ⇒ Object
Add a paragraph style (alias for add_style with type check) TEMPORARY: Type checking disabled for v2.0
302 303 304 |
# File 'lib/uniword/wordprocessingml/styles_configuration.rb', line 302 def add_paragraph_style(style, allow_overwrite: true) add_style(style, allow_overwrite: allow_overwrite) end |
#add_style(style, allow_overwrite: false) ⇒ Style
Add a style to the configuration
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/uniword/wordprocessingml/styles_configuration.rb', line 72 def add_style(style, allow_overwrite: false) unless style.is_a?(Style) raise ArgumentError, "Style must be a Style instance" end # Skip if ID is empty if style.id.to_s.strip.empty? raise ArgumentError, "Style must have a non-empty ID" end existing = style_by_id(style.id) if existing unless allow_overwrite raise ArgumentError, "Style with ID '#{style.id}' already exists" end # Remove existing and add new remove_style(style.id) end styles << style style end |
#character_styles ⇒ Array<CharacterStyle>
Get all character styles
145 146 147 |
# File 'lib/uniword/wordprocessingml/styles_configuration.rb', line 145 def character_styles styles.select(&:character_style?) end |
#count ⇒ Integer
Get the number of styles
166 167 168 |
# File 'lib/uniword/wordprocessingml/styles_configuration.rb', line 166 def count styles.size end |
#create_character_style(id, name, **attributes) ⇒ Style
Create a custom character style
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/uniword/wordprocessingml/styles_configuration.rb', line 265 def create_character_style(id, name, **attributes) # Convert underscore naming to camelCase for lutaml-model attributes # and wrap string values in proper type objects if attributes.key?(:based_on) val = attributes.delete(:based_on) attributes[:basedOn] = BasedOn.new(val: val) if val end if attributes.key?(:next_style) val = attributes.delete(:next_style) attributes[:nextStyle] = Next.new(val: val) if val end if attributes.key?(:link) val = attributes.delete(:link) attributes[:link] = Link.new(val: val) if val end if attributes.key?(:ui_priority) val = attributes.delete(:ui_priority) attributes[:uiPriority] = UiPriority.new(val: val) if val end if attributes.key?(:quick_format) val = attributes.delete(:quick_format) attributes[:qFormat] = Properties::QuickFormat.new(val: val) if val end style = Style.new( styleId: id, name: StyleName.new(val: name), customStyle: true, type: "character", **attributes, ) add_style(style) style end |
#create_paragraph_style(id, name, **attributes) ⇒ Style
Create a custom paragraph style
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/uniword/wordprocessingml/styles_configuration.rb', line 220 def create_paragraph_style(id, name, **attributes) # Convert underscore naming to camelCase for lutaml-model attributes # and wrap string values in proper type objects if attributes.key?(:based_on) val = attributes.delete(:based_on) attributes[:basedOn] = BasedOn.new(val: val) if val end if attributes.key?(:next_style) val = attributes.delete(:next_style) attributes[:nextStyle] = Next.new(val: val) if val end if attributes.key?(:link) val = attributes.delete(:link) attributes[:link] = Link.new(val: val) if val end if attributes.key?(:ui_priority) val = attributes.delete(:ui_priority) attributes[:uiPriority] = UiPriority.new(val: val) if val end if attributes.key?(:quick_format) val = attributes.delete(:quick_format) attributes[:qFormat] = Properties::QuickFormat.new(val: val) if val end if attributes.key?(:run_properties) val = attributes.delete(:run_properties) attributes[:rPr] = val if val end style = Style.new( styleId: id, name: StyleName.new(val: name), customStyle: true, type: "paragraph", **attributes, ) add_style(style) style end |
#custom_styles ⇒ Array<Style>
Get all custom styles
159 160 161 |
# File 'lib/uniword/wordprocessingml/styles_configuration.rb', line 159 def custom_styles styles.select(&:custom) end |
#default_styles ⇒ Array<Style>
Get all default styles
152 153 154 |
# File 'lib/uniword/wordprocessingml/styles_configuration.rb', line 152 def default_styles styles.select(&:default) end |
#empty? ⇒ Boolean
Check if configuration has any styles
173 174 175 |
# File 'lib/uniword/wordprocessingml/styles_configuration.rb', line 173 def empty? styles.empty? end |
#export_styles ⇒ Array<Style>
Export all styles (creates deep copies)
344 345 346 |
# File 'lib/uniword/wordprocessingml/styles_configuration.rb', line 344 def export_styles styles.map(&:dup) end |
#find_by_id(style_id) ⇒ Style?
Find a style by ID (alias for clarity)
189 190 191 |
# File 'lib/uniword/wordprocessingml/styles_configuration.rb', line 189 def find_by_id(style_id) style_by_id(style_id) end |
#import_from_document(source_document) ⇒ void
This method returns an undefined value.
Import styles from another document
335 336 337 338 339 |
# File 'lib/uniword/wordprocessingml/styles_configuration.rb', line 335 def import_from_document(source_document) source_document.styles_configuration.styles.each do |style| add_style(style.dup) unless style_exists?(style.id) end end |
#merge(other_config, conflict_resolution: :keep_existing) ⇒ void
This method returns an undefined value.
Merge styles from another configuration
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 |
# File 'lib/uniword/wordprocessingml/styles_configuration.rb', line 356 def merge(other_config, conflict_resolution: :keep_existing) other_config.styles.each do |style| if style_exists?(style.id) case conflict_resolution when :keep_existing next when :replace remove_style(style.id) add_style(style.dup) when :rename new_style = style.dup new_style.styleId = "#{style.id}_imported" add_style(new_style) else raise ArgumentError, "Invalid conflict_resolution: #{conflict_resolution}" end else add_style(style.dup) end end end |
#paragraph_styles ⇒ Array<ParagraphStyle>
Get all paragraph styles
138 139 140 |
# File 'lib/uniword/wordprocessingml/styles_configuration.rb', line 138 def paragraph_styles styles.select(&:paragraph_style?) end |
#remove_style(id) ⇒ Style?
Remove a style by ID
103 104 105 106 107 108 109 |
# File 'lib/uniword/wordprocessingml/styles_configuration.rb', line 103 def remove_style(id) style = style_by_id(id) return nil unless style styles.delete(style) style end |
#resolve_inheritance(style_id) ⇒ Hash
Resolve style inheritance chain Returns the effective style properties by following basedOn chain
324 325 326 327 328 329 |
# File 'lib/uniword/wordprocessingml/styles_configuration.rb', line 324 def resolve_inheritance(style_id) style = style_by_id(style_id) return {} unless style style.effective_properties(self) end |
#style(id_or_name) ⇒ Style?
Find a style by ID or name
131 132 133 |
# File 'lib/uniword/wordprocessingml/styles_configuration.rb', line 131 def style(id_or_name) style_by_id(id_or_name) || style_by_name(id_or_name) end |
#style_by_id(id) ⇒ Style?
Find a style by ID
115 116 117 |
# File 'lib/uniword/wordprocessingml/styles_configuration.rb', line 115 def style_by_id(id) styles.find { |s| s.id == id } end |
#style_by_name(name) ⇒ Style?
Find a style by name
123 124 125 |
# File 'lib/uniword/wordprocessingml/styles_configuration.rb', line 123 def style_by_name(name) styles.find { |s| s.style_name == name || s.name&.val == name } end |
#style_exists?(style_id) ⇒ Boolean
Check if a style with the given ID exists
181 182 183 |
# File 'lib/uniword/wordprocessingml/styles_configuration.rb', line 181 def style_exists?(style_id) !style_by_id(style_id).nil? end |
#valid? ⇒ Boolean
Validate all styles
315 316 317 |
# File 'lib/uniword/wordprocessingml/styles_configuration.rb', line 315 def valid? styles.all?(&:valid?) end |