Class: Uniword::Themes::ThemeXmlParser

Inherits:
Object
  • Object
show all
Defined in:
lib/uniword/theme/theme_xml_parser.rb

Overview

Parses theme XML files into Theme models

Handles parsing of theme1.xml files from .thmx packages, extracting color schemes, font schemes, and format schemes.

Examples:

Parse a theme XML file

parser = ThemeXmlParser.new
theme = parser.parse(theme_xml_content)
puts theme.name
puts theme.color_scheme.colors[:accent1]

Constant Summary collapse

THEME_NS =

DrawingML namespace used in theme XML

{
  "a" => "http://schemas.openxmlformats.org/drawingml/2006/main",
}.freeze

Instance Method Summary collapse

Instance Method Details

#parse(xml) ⇒ Theme

Parse theme1.xml into Theme model

Parameters:

  • xml (String)

    Theme XML content

Returns:

  • (Theme)

    Parsed theme

Raises:

  • (ArgumentError)

    if XML is invalid or missing theme element



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/uniword/theme/theme_xml_parser.rb', line 28

def parse(xml)
  doc = Nokogiri::XML(xml)
  theme_node = doc.at_xpath("//a:theme", THEME_NS)

  unless theme_node
    raise ArgumentError,
          "Invalid theme XML: missing theme element"
  end

  theme = ::Uniword::Drawingml::Theme.new
  theme.name = theme_node["name"] || "Untitled Theme"

  # Create theme_elements if not exists
  theme.theme_elements ||= ::Uniword::Drawingml::ThemeElements.new

  # Parse color scheme
  color_scheme_node = doc.at_xpath("//a:themeElements/a:clrScheme",
                                   THEME_NS)
  if color_scheme_node
    scheme = parse_color_scheme(color_scheme_node)
    theme.theme_elements.clr_scheme = scheme
  end

  # Parse font scheme
  font_scheme_node = doc.at_xpath("//a:themeElements/a:fontScheme",
                                  THEME_NS)
  if font_scheme_node
    scheme = parse_font_scheme(font_scheme_node)
    theme.theme_elements.font_scheme = scheme
  end

  theme
end