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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
# File 'lib/obf/external.rb', line 2
def self.to_obf(hash, dest_path, path_hash=nil)
if hash['boards']
old_hash = hash
hash = old_hash['boards'][0]
hash['images'] = old_hash['images'] || []
hash['sounds'] = old_hash['sounds'] || []
path_hash = nil
end
res = OBF::Utils.obf_shell
res['id'] = hash['id']
res['locale'] = hash['locale'] || 'en'
res['format'] = OBF::OBF::FORMAT
res['name'] = hash['name']
res['default_layout'] = hash['default_layout'] || 'landscape'
res['url'] = hash['url']
res['data_url'] = hash['data_url']
res['default_locale'] = hash['default_locale'] if hash['default_locale']
res['label_locale'] = hash['label_locale'] if hash['label_locale']
res['vocalization_locale'] = hash['vocalization_locale'] if hash['vocalization_locale']
res['description_html'] = hash['description_html']
res['protected_content_user_identifier'] = hash['protected_content_user_identifier'] if hash['protected_content_user_identifier']
res['license'] = OBF::Utils.parse_license(hash['license'])
hash.each do |key, val|
if key && key.match(/^ext_/)
res[key] = val
end
end
grid = []
images = []
sounds = []
res['buttons'] = []
buttons = hash['buttons'] button_count = buttons.length
buttons.each_with_index do |original_button, idx|
button = {
'id' => original_button['id'],
'label' => original_button['label'],
'vocalization' => original_button['vocalization'],
'action' => original_button['action'],
'actions' => original_button['actions'],
'left' => original_button['left'],
'top' => original_button['top'],
'width' => original_button['width'],
'height' => original_button['height'],
'border_color' => OBF::Utils.fix_color(original_button['border_color'] || "#aaa", 'rgb'),
'background_color' => OBF::Utils.fix_color(original_button['background_color'] || "#fff", 'rgb')
}
if original_button['load_board']
button['load_board'] = {
'id' => original_button['load_board']['id'],
'url' => original_button['load_board']['url'],
'data_url' => original_button['load_board']['data_url']
}
if path_hash && path_hash['included_boards'] && path_hash['included_boards'][original_button['load_board']['id']]
button['load_board']['path'] = "board_#{original_button['load_board']['id']}.obf"
end
end
if original_button['translations']
original_button['translations'].each do |loc, hash|
next unless hash.is_a?(Hash)
button['translations'] ||= {}
button['translations'][loc] ||= {}
button['translations'][loc]['label'] = hash['label'].to_s if hash['label']
button['translations'][loc]['vocalization'] = hash['vocalization'].to_s if hash['vocalization']
(hash['inflections'] || {}).each do |key, val|
if key.match(/^ext_/)
button['translations'][loc]['inflections'] ||= {}
button['translations'][loc]['inflections'][key] = val
else
button['translations'][loc]['inflections'] ||= {}
button['translations'][loc]['inflections'][key] = val.to_s
end
end
hash.keys.each do |key|
button['translations'][loc][key] = hash[key] if key.to_s.match(/^ext_/)
end
end
end
if original_button['hidden']
button['hidden'] = original_button['hidden']
end
if original_button['url']
button['url'] = original_button['url']
end
original_button.each do|key, val|
if key.match(/^ext_/)
button[key] = val
end
end
if original_button['image_id'] && hash['images']
image = hash['images'].detect{|i| i['id'] == original_button['image_id']}
if image
images << image
button['image_id'] = image['id']
end
end
if original_button['sound_id']
sound = hash['sounds'].detect{|s| s['id'] == original_button['sound_id']}
if sound
sounds << sound
button['sound_id'] = sound['id']
end
end
res['buttons'] << trim_empties(button)
OBF::Utils.update_current_progress(idx.to_f / button_count.to_f)
end
images.each do |original_image|
image = {
'id' => original_image['id'],
'width' => original_image['width'],
'height' => original_image['height'],
'license' => OBF::Utils.parse_license(original_image['license']),
'url' => original_image['url'],
'data' => original_image['data'],
'data_url' => original_image['data_url'],
'content_type' => original_image['content_type']
}
if !path_hash
image['data'] ||= OBF::Utils.image_base64(image['url']) if image['url']
if image['data'] && (!image['content_type'] || !image['width'] || !image['height'])
attrs = OBF::Utils.image_attrs(image['data'])
image['content_type'] ||= attrs['content_type']
image['width'] ||= attrs['width']
image['height'] ||= attrs['height']
end
else
if path_hash['images'] && path_hash['images'][image['id']]
image['path'] = path_hash['images'][image['id']]['path']
image['content_type'] ||= path_hash['images'][image['id']]['content_type']
image['width'] ||= path_hash['images'][image['id']]['width']
image['height'] ||= path_hash['images'][image['id']]['height']
else
image_fetch = OBF::Utils.image_raw(image['data'] || image['url'])
if image_fetch
if !image['content_type'] || !image['width'] || !image['height']
attrs = OBF::Utils.image_attrs(image_fetch['data'])
image['content_type'] ||= image_fetch['content_type'] || attrs['content_type']
image['width'] ||= attrs['width']
image['height'] ||= attrs['height']
end
zip_path = "images/image_#{image['id']}#{image_fetch['extension']}"
path_hash['images'] ||= {}
path_hash['images'][image['id']] = {
'path' => zip_path,
'content_type' => image['content_type'],
'width' => image['width'],
'height' => image['height']
}
path_hash['zip'].add(zip_path, image_fetch['data'])
image['path'] = zip_path
end
end
end
res['images'] << trim_empties(image)
end
sounds.each do |original_sound|
sound = {
'id' => original_sound['id'],
'duration' => original_sound['duration'],
'license' => OBF::Utils.parse_license(original_sound['license']),
'url' => original_sound['url'],
'data' => original_sound['data'],
'data_url' => original_sound['data_url'],
'content_type' => original_sound['content_type']
}
if !path_hash
sound['data'] = OBF::Utils.sound_base64(sound['url']) if sound['url']
else
if path_hash['sounds'] && path_hash['sounds'][sound['id']]
sound['path'] = path_hash['sounds'][sound['id']]['path']
else
sound_fetch = OBF::Utils.sound_raw(sound['url'] || sound['data'])
if sound_fetch
zip_path = "sounds/sound_#{sound['id']}#{sound_fetch['extension']}"
path_hash['sounds'] ||= {}
path_hash['sounds'][sound['id']] = {
'path' => zip_path
}
path_hash['zip'].add(zip_path, sound_fetch['data'])
sound['path'] = zip_path
end
end
sound['path'] = zip_path
end
res['sounds'] << trim_empties(sound)
end
res['grid'] = OBF::Utils.parse_grid(hash['grid']) if path_hash
zip_path = "board_#{res['id']}.obf"
path_hash['boards'] ||= {}
path_hash['boards'][res['id']] = {
'path' => zip_path
}
path_hash['zip'].add(zip_path, JSON.pretty_generate(res))
else
File.open(dest_path, 'w') {|f| f.write(JSON.pretty_generate(res)) }
end
return dest_path
end
|