Class: Nyaplot::Plot

Inherits:
Object
  • Object
show all
Defined in:
lib/nyaplot/export_svg.rb

Constant Summary collapse

SVG_HEADER =
'<svg xmlns="http://www.w3.org/2000/svg">'
SVG_CLOSE =
'</svg>'
TEMP_NAME =
'temp.html'

Instance Method Summary collapse

Instance Method Details

#export_contents(name = SecureRandom.uuid.to_s + ".svg") ⇒ Object

Export context of plot as SVG

Parameters:

  • name (String) (defaults to: SecureRandom.uuid.to_s + ".svg")

    the name of svg file to export



35
36
37
38
39
40
41
# File 'lib/nyaplot/export_svg.rb', line 35

def export_contents(name=SecureRandom.uuid.to_s + ".svg")
  html = get_nodes
  contents = Nokogiri::HTML.parse(html).at('.context_child').to_s
  str = SVG_HEADER + contents + SVG_CLOSE
  path = File.expand_path("./" + name, Dir.pwd)
  File.write(path, str)
end

#export_svg(name = SecureRandom.uuid.to_s + ".svg") ⇒ Object

Export plot as SVG

Parameters:

  • name (String) (defaults to: SecureRandom.uuid.to_s + ".svg")

    the name of svg file to export



26
27
28
29
30
31
# File 'lib/nyaplot/export_svg.rb', line 26

def export_svg(name=SecureRandom.uuid.to_s + ".svg")
  html = get_nodes
  str = Nokogiri::HTML.parse(html).xpath('//svg').to_s
  path = File.expand_path("./" + name, Dir.pwd)
  File.write(path, str)
end

#get_nodesString

Get html nodes created by JavaScript with browsers

Returns:

  • (String)

    built html



13
14
15
16
17
18
19
20
21
22
# File 'lib/nyaplot/export_svg.rb', line 13

def get_nodes
  require 'watir'
  self.export_html(TEMP_NAME)
  path = File.expand_path("./" + TEMP_NAME, Dir.pwd)
  browser = Watir::Browser.start("file://" + path)
  html = browser.html
  browser.close
  FileUtils.rm_r(path)
  return html
end