Robot Has No Heart

Xavier Shay blogs here

A robot that does not have a heart

Practical Hpricot: SVG

Inkscape does a pretty good job of creating plain SVG files, but they could be nicer. A particular file I was working on had many path elements, all with the same style attribute that I wanted to move into a parent tag (or external style or whatever). What better way to strip them out than Hpricot?

1
2
3
4
5
6
7
8
9
10
11
require 'hpricot'

doc = open(ARGV[0]) { |f| Hpricot.XML(f) }

(doc/:path).each do |path|
  [:id, :style].each do |attr| 
    path.remove_attribute(attr)
  end
end

puts doc

And you get the benefit of prettier formatting!

A pretty flower Another pretty flower