Class: Uniword::Resource::ThemeMappingLoader
- Inherits:
-
Object
- Object
- Uniword::Resource::ThemeMappingLoader
- Defined in:
- lib/uniword/resource/theme_mapping_loader.rb
Overview
Loads and queries the Uniword <-> MS Office theme/styleset mapping
The mapping is stored in config/theme_mapping.yml and provides:
-
Bidirectional name lookup (Uniword <-> MS)
-
Color fingerprint data for MS theme detection
-
Customizable mapping entries
Constant Summary collapse
- MAPPING_PATH =
File.join(__dir__, "../../..", "config", "theme_mapping.yml")
Class Method Summary collapse
-
.all_styleset_mappings ⇒ Hash
Get all styleset mappings.
-
.all_theme_mappings ⇒ Hash
Get all theme mappings.
-
.find_by_colors(colors) ⇒ String?
Find Uniword theme by matching a color hash against fingerprints.
-
.fingerprint_match?(actual, fingerprint) ⇒ Boolean
Match a color hash against a fingerprint (primary keys: accent1, accent2, dk2).
-
.mapping ⇒ Hash
Load and cache the mapping YAML.
-
.ms_to_uniword_styleset(ms_name) ⇒ String?
Look up Uniword styleset slug from MS styleset name.
-
.ms_to_uniword_theme(ms_name) ⇒ String?
Look up Uniword theme slug from MS theme name.
- .normalize_colors(colors) ⇒ Object
- .normalize_hex(hex) ⇒ Object
-
.reload! ⇒ Object
Clear cached mapping (useful after file changes).
-
.theme_fingerprint(uniword_name) ⇒ Hash?
Get color fingerprint for a Uniword theme.
-
.uniword_to_ms_styleset(uniword_name) ⇒ String?
Look up MS styleset name from Uniword styleset slug.
-
.uniword_to_ms_theme(uniword_name) ⇒ String?
Look up MS theme name from Uniword theme slug.
Class Method Details
.all_styleset_mappings ⇒ Hash
Get all styleset mappings
93 94 95 |
# File 'lib/uniword/resource/theme_mapping_loader.rb', line 93 def self.all_styleset_mappings mapping["stylesets"] || {} end |
.all_theme_mappings ⇒ Hash
Get all theme mappings
86 87 88 |
# File 'lib/uniword/resource/theme_mapping_loader.rb', line 86 def self.all_theme_mappings mapping["themes"] || {} end |
.find_by_colors(colors) ⇒ String?
Find Uniword theme by matching a color hash against fingerprints
101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/uniword/resource/theme_mapping_loader.rb', line 101 def self.find_by_colors(colors) normalized = normalize_colors(colors) all_theme_mappings.each do |uniword_slug, entry| fp = entry["color_fingerprint"] next unless fp return uniword_slug if fingerprint_match?(normalized, fp) end nil end |
.fingerprint_match?(actual, fingerprint) ⇒ Boolean
Match a color hash against a fingerprint (primary keys: accent1, accent2, dk2)
119 120 121 122 123 |
# File 'lib/uniword/resource/theme_mapping_loader.rb', line 119 def self.fingerprint_match?(actual, fingerprint) %w[accent1 accent2 dk2].all? do |key| normalize_hex(actual[key]) == normalize_hex(fingerprint[key]) end end |
.mapping ⇒ Hash
Load and cache the mapping YAML
27 28 29 |
# File 'lib/uniword/resource/theme_mapping_loader.rb', line 27 def self.mapping @mapping ||= YAML.load_file(MAPPING_PATH) end |
.ms_to_uniword_styleset(ms_name) ⇒ String?
Look up Uniword styleset slug from MS styleset name
68 69 70 71 72 73 |
# File 'lib/uniword/resource/theme_mapping_loader.rb', line 68 def self.ms_to_uniword_styleset(ms_name) mapping["stylesets"]&.each do |uniword_slug, entry| return uniword_slug if entry["ms_name"] == ms_name end nil end |
.ms_to_uniword_theme(ms_name) ⇒ String?
Look up Uniword theme slug from MS theme name
49 50 51 52 53 54 |
# File 'lib/uniword/resource/theme_mapping_loader.rb', line 49 def self.ms_to_uniword_theme(ms_name) mapping["themes"]&.each do |uniword_slug, entry| return uniword_slug if entry["ms_name"] == ms_name end nil end |
.normalize_colors(colors) ⇒ Object
125 126 127 128 129 |
# File 'lib/uniword/resource/theme_mapping_loader.rb', line 125 def self.normalize_colors(colors) result = {} colors.each { |k, v| result[k.to_s] = v } result end |
.normalize_hex(hex) ⇒ Object
131 132 133 134 135 |
# File 'lib/uniword/resource/theme_mapping_loader.rb', line 131 def self.normalize_hex(hex) return nil unless hex hex.to_s.upcase.gsub(/^#/, "").rjust(6, "0") end |
.reload! ⇒ Object
Clear cached mapping (useful after file changes)
32 33 34 35 |
# File 'lib/uniword/resource/theme_mapping_loader.rb', line 32 def self.reload! @mapping = nil mapping end |
.theme_fingerprint(uniword_name) ⇒ Hash?
Get color fingerprint for a Uniword theme
79 80 81 |
# File 'lib/uniword/resource/theme_mapping_loader.rb', line 79 def self.theme_fingerprint(uniword_name) mapping["themes"]&.dig(uniword_name, "color_fingerprint") end |
.uniword_to_ms_styleset(uniword_name) ⇒ String?
Look up MS styleset name from Uniword styleset slug
60 61 62 |
# File 'lib/uniword/resource/theme_mapping_loader.rb', line 60 def self.uniword_to_ms_styleset(uniword_name) mapping["stylesets"]&.dig(uniword_name, "ms_name") end |
.uniword_to_ms_theme(uniword_name) ⇒ String?
Look up MS theme name from Uniword theme slug
41 42 43 |
# File 'lib/uniword/resource/theme_mapping_loader.rb', line 41 def self.uniword_to_ms_theme(uniword_name) mapping["themes"]&.dig(uniword_name, "ms_name") end |