class Xlsxrb::Ooxml::StylesParser::Listener
SAX listener for parsing styles.xml content.
Attributes
result
[R]
untyped
Public Class Methods
() → untyped
Source
# File lib/xlsxrb/ooxml/styles_parser.rb, line 30 def initialize @result = { num_fmts: {}, fonts: [], fills: [], borders: [], cell_xfs: [], cell_style_xfs: [], cell_styles: [], dxfs: [] } @context = [] @current_font = nil @current_fill = nil @current_fill_pattern = nil @current_gradient_stop = nil @current_border = nil @current_border_side = nil @current_xf = nil @in_cell_xfs = false @in_cell_style_xfs = false @current_dxf = nil end
Public Instance Methods
(untyped _text) → untyped
Source
# File lib/xlsxrb/ooxml/styles_parser.rb, line 194 def characters(_text) # No text content needed for styles end
(untyped _uri, untyped localname, untyped _qname) → untyped
Source
# File lib/xlsxrb/ooxml/styles_parser.rb, line 138 def end_element(_uri, localname, _qname) case localname when "font" if @current_font if @current_dxf @current_dxf[:font] = @current_font else @result[:fonts] << @current_font end @current_font = nil end when "stop" if @current_gradient_stop @current_fill[:gradient][:stops] << @current_gradient_stop if @current_fill&.dig(:gradient) @current_gradient_stop = nil end when "fill" if @current_fill if @current_fill_pattern @current_fill[:pattern_type] = @current_fill_pattern @current_fill[:pattern] = @current_fill_pattern end if @current_dxf @current_dxf[:fill] = @current_fill else @result[:fills] << @current_fill end @current_fill = nil @current_fill_pattern = nil end when "dxf" if @current_dxf (@result[:dxfs] ||= []) << @current_dxf @current_dxf = nil end when "border" if @current_border if @current_dxf @current_dxf[:border] = @current_border else @result[:borders] << @current_border end @current_border = nil end when "left", "right", "top", "bottom", "diagonal" @current_border_side = nil when "cellXfs" @in_cell_xfs = false when "cellStyleXfs" @in_cell_style_xfs = false when "xf" finalize_xf end @context.pop end
(untyped _uri, untyped localname, untyped _qname, untyped attrs) → untyped
Source
# File lib/xlsxrb/ooxml/styles_parser.rb, line 54 def start_element(_uri, localname, _qname, attrs) @context.push(localname) case localname when "numFmt" id = attrs["numFmtId"]&.to_i code = attrs["formatCode"] if @current_dxf @current_dxf[:number_format] = { num_fmt_id: id, format_code: code } if id && code elsif parent_context?("numFmts") && id && code @result[:num_fmts][id] = code end when "dxf" @current_dxf = {} if parent_context?("dxfs") when "font" @current_font = {} if parent_context?("fonts") || (@current_dxf && parent_context?("dxf")) when "b" @current_font[:bold] = truthy_val?(attrs) if @current_font when "i" @current_font[:italic] = truthy_val?(attrs) if @current_font when "strike" @current_font[:strike] = truthy_val?(attrs) if @current_font when "shadow" @current_font[:shadow] = truthy_val?(attrs) if @current_font when "outline" @current_font[:outline] = truthy_val?(attrs) if @current_font when "condense" @current_font[:condense] = truthy_val?(attrs) if @current_font when "extend" @current_font[:extend] = truthy_val?(attrs) if @current_font when "u" @current_font[:underline] = attrs["val"] || "single" if @current_font when "vertAlign" @current_font[:vert_align] = attrs["val"] if @current_font && attrs["val"] when "sz" if @current_font sz_val = attrs["val"]&.to_f @current_font[:sz] = sz_val @current_font[:size] = sz_val end when "color" handle_color(attrs) when "name" @current_font[:name] = attrs["val"] if @current_font when "family" @current_font[:family] = attrs["val"].to_i if @current_font && attrs["val"] when "scheme" @current_font[:scheme] = attrs["val"] if @current_font && attrs["val"] when "fill" @current_fill = {} if parent_context?("fills") || (@current_dxf && parent_context?("dxf")) when "patternFill" @current_fill_pattern = attrs["patternType"] if @current_fill when "gradientFill" if @current_fill gradient = { type: attrs["type"], degree: attrs["degree"]&.to_f } %w[left right top bottom].each { |side| gradient[side.to_sym] = attrs[side].to_f if attrs[side] } @current_fill[:gradient] = gradient.compact.merge(stops: []) end when "stop" @current_gradient_stop = { position: attrs["position"].to_f } if @current_fill&.dig(:gradient) when "fgColor", "bgColor" handle_fill_color(localname, attrs) if @current_fill when "border" if parent_context?("borders") || (@current_dxf && parent_context?("dxf")) @current_border = {} @current_border[:diagonal_up] = true if boolean_val(attrs["diagonalUp"]) @current_border[:diagonal_down] = true if boolean_val(attrs["diagonalDown"]) end when "left", "right", "top", "bottom", "diagonal" handle_border_side(localname, attrs) when "cellXfs" @in_cell_xfs = true when "cellStyleXfs" @in_cell_style_xfs = true when "cellStyle" @result[:cell_styles] << { name: attrs["name"], xf_id: attrs["xfId"]&.to_i, builtin_id: attrs["builtinId"]&.to_i }.compact when "xf" handle_xf(attrs) when "alignment" handle_alignment(attrs) when "protection" handle_protection(attrs) end end