-
Notifications
You must be signed in to change notification settings - Fork 1
/
images_to_icons.rb
62 lines (52 loc) · 2.05 KB
/
images_to_icons.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
require 'csv'
require 'spacewalk_html_clean/command'
module SpacewalkHtmlClean
module Commands
class ImagesToIcons < Command
def applicable?(tag)
tag.name == 'table'
['rhn:toolbar', 'rhn-toolbar', 'img'].include?(tag.name)
end
def process_tag(source, out, tag, path)
case tag.name
when 'rhn:toolbar' then process_toolbar(source, out, tag, path)
when 'rhn-toolbar' then process_toolbar(source, out, tag, path)
when 'img' then process_image(source, out, tag, path)
end
end
def load_image_map!
return if @image_map
# read csv into hash like [{'/img/rhn-icon-user.gif' => {:newicon => 'icon-user', :color => 'default'}, ...]
imglist = File.join(File.dirname(__FILE__), '../../../data/spacewalk_image_map.csv')
csv_options = {:headers => true, :header_converters => :symbol, :converters => :all}
@image_map = {}
CSV.foreach(imglist, csv_options) do |line|
@image_map[line.fields[0]] = Hash[line.headers[1..-1].zip(line.fields[1..-1])]
end
end
def process_image(source, out, tag, path)
load_image_map!
src = tag.getAttributes.get('src')
if @image_map.has_key?(src.getValue)
fields = @image_map[src.getValue]
if fields[:color] != 'default'
out.replace(tag, %Q{<i class="#{fields[:newicon]} #{fields[:color]}"></i>}) unless fields[:newicon].nil?
else
out.replace(tag, %Q{<i class="#{fields[:newicon]}"></i>}) unless fields[:newicon].nil?
end
end
end
def process_toolbar(source, out, tag, path)
load_image_map!
src = tag.getAttributes.get('img')
return if not src
if @image_map.has_key?(src.getValue)
fields = @image_map[src.getValue]
out.replace(src, %Q{icon="#{fields[:newicon]}"}) unless fields[:newicon].nil?
alt = tag.getAttributes.get('imgAlt')
out.replace(alt, %Q{iconAlt="#{alt.getValue}"}) if alt
end
end
end
end
end