forked from sarabander/sicp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
svg_cleanup.rb
executable file
·36 lines (31 loc) · 986 Bytes
/
svg_cleanup.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#! /usr/bin/ruby -w
# -*- coding: utf-8 -*-
# Deletes those elements and attributes from SVG files
# that are not in SVG 1.1 specification and will cause
# epubcheck to complain. Give files to process as arguments.
require 'nokogiri'
deletables = {
'g' => ['groupmode', 'label'],
'path' => ['connector-curvature', 'nodetypes'],
'svg' => ['docname', 'export-filename', 'export-xdpi',
'export-ydpi', 'version'],
'text' => ['linespacing'],
'tspan' => ['role']
}
def delete_attributes(svg, deletables)
deletables.each do |element, attributes|
svg.css(element).each do |tag|
attributes.each do |attribute|
tag.delete(attribute)
end
end
end
end
ARGV.each do |file|
svg = Nokogiri::XML(open(file))
# gives error if 'sodipodi' namespace is not declared:
svg.css("sodipodi|namedview").each { |e| e.remove }
delete_attributes(svg, deletables)
svg.css("svg").each { |e| e['version'] = "1.1" }
IO.write(file, svg)
end