Class: Uniword::Resource::ThemeMappingLoader

Inherits:
Object
  • Object
show all
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

Examples:

Look up Uniword theme from MS name

ThemeMappingLoader.ms_to_uniword_theme("Atlas")  #=> "meridian"

Look up MS name from Uniword theme

ThemeMappingLoader.uniword_to_ms_theme("meridian")  #=> "Atlas"

Get color fingerprint for detection

ThemeMappingLoader.theme_fingerprint("meridian")

Constant Summary collapse

MAPPING_PATH =
File.join(__dir__, "../../..", "config",
"theme_mapping.yml")

Class Method Summary collapse

Class Method Details

.all_styleset_mappingsHash

Get all styleset mappings

Returns:

  • (Hash)

    All styleset entries keyed by Uniword slug



93
94
95
# File 'lib/uniword/resource/theme_mapping_loader.rb', line 93

def self.all_styleset_mappings
  mapping["stylesets"] || {}
end

.all_theme_mappingsHash

Get all theme mappings

Returns:

  • (Hash)

    All theme entries keyed by Uniword slug



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

Parameters:

  • colors (Hash)

    Color hash with keys like “accent1”, “dk2”, etc.

Returns:

  • (String, nil)

    Uniword theme slug or nil



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)

Parameters:

  • actual (Hash)

    Actual colors from document

  • fingerprint (Hash)

    Fingerprint colors from mapping

Returns:

  • (Boolean)


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

.mappingHash

Load and cache the mapping YAML

Returns:

  • (Hash)

    Parsed mapping data



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

Parameters:

  • ms_name (String)

    MS styleset name

Returns:

  • (String, nil)

    Uniword styleset slug or nil



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

Parameters:

  • ms_name (String)

    MS theme name (e.g., “Atlas”)

Returns:

  • (String, nil)

    Uniword theme slug or nil



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

Parameters:

  • uniword_name (String)

    Uniword theme slug

Returns:

  • (Hash, nil)

    Color fingerprint hash with 10 keys



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

Parameters:

  • uniword_name (String)

    Uniword styleset slug

Returns:

  • (String, nil)

    MS styleset name or nil



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

Parameters:

  • uniword_name (String)

    Uniword theme slug (e.g., “meridian”)

Returns:

  • (String, nil)

    MS theme name or nil



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