Skip to content

Instantly share code, notes, and snippets.

@cjheath
Created May 10, 2012 23:27
Show Gist options
  • Save cjheath/2656551 to your computer and use it in GitHub Desktop.
Save cjheath/2656551 to your computer and use it in GitHub Desktop.
THE WORLD'S FINEST AND MOST ACCURATE SVG TO RAPHAËL CONVERTER
#!/usr/bin/env ruby
#
# 1304046900 CONVERTS SVG TO RAPHAËL
require "nokogiri"
file = "world.svg"
tree = Nokogiri::XML(File.open(file))
str =
"function render_map(R, map, attr) {\n\n" +
tree.search('g').map do |gs|
"map.#{ gs[:id] } = R.path(\"" +
gs.search('path').map{ |paths|
paths[:d].tr(' ', '')
}.join +
gs.search('line').map { |lines|
x1 = lines[:x1]
y1 = lines[:y1]
"M%d, %dl%.3f, %.3fz" % [x1, y1, lines[:x2] - x1, lines[:y2] - y1]
}.join +
gs.search('polygon').map{ |polygons|
points = polygons[:points].tr(' ', '').split
prev_x = prev_y = 0
"M" + (points+[points[-1]]).map{ |point|
x, y = point.split(', ')
s, prev_x, prev_y = "%.3f,%.3f" % [x - prev_x, y - prev_y], x, y
s
}.join("l") +
"z"
}.join +
'").attr(attr);'
end.join("\n") +
"\n}\n"
puts str
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment