Class: Postsvg::Svg::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/postsvg/svg/parser.rb

Overview

Parses an SVG document via Nokogiri and produces an Svg::Document. Pure: no I/O, no global state.

Class Method Summary collapse

Class Method Details

.call(svg_string) ⇒ Object

Raises:



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/postsvg/svg/parser.rb', line 10

def self.call(svg_string)
  doc = Nokogiri::XML(svg_string, &:noblanks)
  root = doc.root
  raise TranslationError, "no root element in SVG" unless root

  Elements.load_all! # populate Element.registry with all known tag handlers
  root_element = Element.from_node(root)
  viewbox, width, height = extract_root_dimensions(root)
  clip_paths = ClipPathRegistry.from_node(root)
  Document.new(root_element: root_element,
               viewbox: viewbox,
               width: width,
               height: height,
               clip_paths: clip_paths)
end

.extract_root_dimensions(root) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/postsvg/svg/parser.rb', line 26

def self.extract_root_dimensions(root)
  vb = root["viewBox"]
  width = root["width"]
  height = root["height"]
  parsed_vb =
    if vb
      nums = vb.split(/\s+|,/).map(&:to_f)
      nums if nums.length == 4
    end
  [parsed_vb, width&.to_f, height&.to_f]
end