Module: Emfsvg::Svg::Parser
- Defined in:
- lib/emfsvg/svg/parser.rb
Overview
Parses an SVG document string into a Svg::Document value object. Nokogiri is the only XML/HTML parser used in this gem.
Width/height are coerced to floats (CSS units other than px are out of scope for v1; a numeric value is treated as user units). viewBox is parsed into a 4-tuple [min_x, min_y, width, height].
Class Method Summary collapse
Class Method Details
.call(string) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/emfsvg/svg/parser.rb', line 16 def call(string) doc = Nokogiri::XML(string, &:noblanks) root = doc.root raise ParseError, "not an SVG document: missing root element" unless root Document.new( width: parse_dimension(root["width"]), height: parse_dimension(root["height"]), view_box: parse_view_box(root["viewBox"]), root_element: Element.from_node(root) ) end |
.parse_dimension(value) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/emfsvg/svg/parser.rb', line 29 def parse_dimension(value) return 0.0 if value.nil? || value.empty? match = value.match(/\A(-?\d+(?:\.\d+)?)/) match ? match[1].to_f : 0.0 end |
.parse_view_box(value) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/emfsvg/svg/parser.rb', line 36 def parse_view_box(value) return nil if value.nil? || value.empty? parts = value.split(/[\s,]+/).map(&:to_f) parts.length == 4 ? parts : nil end |