forked from Rob--W/open-in-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen-mime-info.js
executable file
·285 lines (261 loc) · 10.2 KB
/
gen-mime-info.js
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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/usr/bin/env node
/**
* (c) 2013 Rob Wu <rob@robwu.nl> (https://robwu.nl)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* Generates i18n strings and MIME-mappings from the shared mime-info database.
*
* 1. Add extra MIME-types by copying files from /usr/share/mime/packages to shared-mime-info.
* and put add the file to the inputFiles array (see below).
* 2. Copy custom icons to icons/
* 3. Run this script. Done!
*/
/* eslint-env node */
'use strict';
var fs = require('fs');
var path = require('path');
var xml2js = require('xml2js');
var mkdirSync = require('mkdirp').sync;
//
// Begin config
//
var inputFileDirectory = path.resolve(__dirname, 'shared-mime-info');
var inputFiles = [
'freedesktop.org.xml',
'gcr-crypto-types.xml',
'virtualbox.xml',
'calibre.xml',
'webp.xml',
'mime.xml'
];
var extensionRoot = path.resolve(__dirname, 'extension');
var outputFile = path.join(extensionRoot, 'mime-metadata.js');
var iconPathPrefix = 'icons/'; // Relative to extensionRoot
var localePathPrefix = '_locales/mimetypes/'; // Relative to extensionRoot
var localePathSuffix = '.json';
// Used to exclude localized strings that are not supported by the CWS.
// Based on https://developers.google.com/chrome/web-store/docs/i18n?csw=1#localeTable
var i18n_supported_langs = [
'ar', 'am', 'bg', 'bn', 'ca', 'cs', 'da', 'de', 'el', 'en', 'en_GB', 'en_US', 'es', 'es_419',
'et', 'fa', 'fi', 'fil', 'fr', 'gu', 'he', 'hi', 'hr', 'hu', 'id', 'it', 'ja', 'kn', 'ko',
'lt', 'lv', 'ml', 'mr', 'ms', 'nl', 'no', 'pl', 'pt_BR', 'pt_PT', 'ro', 'ru', 'sk', 'sl', 'sr',
'sv', 'sw', 'ta', 'te', 'th', 'tr', 'uk', 'vi', 'zh_CN', 'zh_TW'
];
//
// End of config
//
var inputFileIndex = -1;
process.nextTick(next);
function next() {
var filename = inputFiles[++inputFileIndex];
if (filename) {
console.log('Processing ' + filename);
filename = path.join(inputFileDirectory, filename);
xml2mime(filename, next);
} else {
afterProcessingAllInputFiles();
}
}
function afterProcessingAllInputFiles() {
// Generate files for chrome.i18n
Object.keys(i18n).forEach(function(locale) {
var filename = path.join(extensionRoot, localePathPrefix + locale + localePathSuffix);
// Pretty-print for better git diff
i18n[locale] = sortObjectValues(i18n[locale]);
var messages = JSON.stringify(i18n[locale], null, '\t');
mkdirSync(path.dirname(filename));
fs.writeFileSync(filename, messages);
});
// Generate metadata definitions
// Assume that all icons are PNG files
var iconNames = fs.readdirSync(path.join(extensionRoot, iconPathPrefix))
.filter(function(name) { return (/\.png$/).test(name); })
.map(function(name) { return name.replace('.png', ''); });
var mimeMetadata = [
'/* AUTOGENERATED - DO NOT EDIT */',
'\'use strict\';',
'/* exported mime_fromFilename, mime_getFriendlyName, mime_getIcon */',
'function mime_fromFilename(filename) {',
'\tvar mime;',
'\tvar parts = filename.split(".");',
'\tvar length = parts.length;',
'\tif (length > 2) { // example.tar.gz',
'\t\tmime = mimeMetadata.extensionToMime[parts[length - 2] + "." + parts[length - 1]];',
'\t}',
'\tif (!mime) { // example.txt',
'\t\tmime = mimeMetadata.extensionToMime[parts[length - 1]];',
'\t}',
'\treturn mime ? mime_getMime(mime) : "";',
'}',
'var _cachedMimeMessages = {};',
'function mime_getFriendlyName(mime, /*optional*/ locale) {',
'\tmime = mime_getMime(mime);',
'\tif (locale) {',
'\t\tif (mimeMetadata.supportedLocales.indexOf(locale) === -1) locale = "en";',
'\t\tvar messages = _cachedMimeMessages[locale];',
'\t\tif (!messages) {',
'\t\t\tvar x = new XMLHttpRequest();',
'\t\t\tx.open("GET", "' + localePathPrefix + '" + locale + "' + localePathSuffix + '", false);',
'\t\t\tx.overrideMimeType("application/json");',
'\t\t\tx.send();',
'\t\t\tmessages = _cachedMimeMessages[locale] = JSON.parse(x.responseText);',
'\t\t}',
'\t\treturn messages[mime];',
'\t}',
'\tlocale = navigator.language.replace("-", "_") || "en";',
'\tif (mimeMetadata.supportedLocales.indexOf(locale) === -1) locale = locale.split("_")[0];',
'\treturn mime_getFriendlyName(mime, locale) || mime_getFriendlyName(mime, "en");',
'}',
'function mime_getIcon(mime) {',
'\tmime = mime_getMime(mime);',
'\tvar icon = mime.replace(/\\//g, "-");',
'\tif (mimeMetadata.nonGenericIcons.indexOf(icon) === -1) {',
'\t\ticon = mimeMetadata.mimeToIcon[mime];',
'\t\tif (!icon) icon = mime.split("/", 1)[0] + "-x-generic";',
'\t}',
'\treturn "' + iconPathPrefix + '" + icon + ".png";',
'}',
'function mime_getMime(mime) {',
'\treturn mimeMetadata.aliases[mime] || mime;',
'}',
'',
'/* eslint-disable max-len */',
];
mimeMetadata = mimeMetadata.join('\n') + '\n';
metadata.nonGenericIcons = iconNames;
metadata.supportedLocales = Object.keys(i18n);
for (var [key, value] of Object.entries(metadata)) {
metadata[key] = sortObjectValues(value);
}
mimeMetadata += 'var mimeMetadata = ' + JSON.stringify(metadata, null, '\t') + ';\n';
fs.writeFileSync(outputFile, mimeMetadata);
}
function sortObjectValues(object) {
// Stably sort the values in the dictionary, lexicographically.
if (Array.isArray(object)) {
return object.sort();
}
var obj = {};
for (var k of Object.keys(object).sort()) {
obj[k] = object[k];
}
return obj;
}
var metadata = {
mimeToIcon: {},
extensionToMime: {},
aliases: {},
allMimeTypes: [],
nonGenericIcons: [],
supportedLocales: [],
};
var i18n = {};
var extensionToMimeWeight = {};
// Parse XML files from the Shared MIME-info database
// Duplicate entries are merged as follows:
// - The last icon takes the highest priority.
// - The last glob takes the highest priority.
// - The last alias takes the highest priority.
// - The first friendly name (i18n) takes the higest priority, all later definitions are ignored.
//
// See also:
// - /usr/share/mime/
// - http://freedesktop.org/wiki/Software/shared-mime-info/
// - http://standards.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-0.18.html
//
// Puts result in global variables metadata and i18n
function xml2mime(xmlFilename, /*function*/ done) {
var xml = fs.readFileSync(xmlFilename, 'utf8');
var parser = new xml2js.Parser();
parser.parseString(xml, function(err, result) {
if (err) {
throw err;
}
// Get the identifier of the icon corresponding to the item
var getIcon = function(/*object*/item, /*number*/i) {
if (item.icon)
return item.icon[0].$.name;
if (item['generic-icon'])
return item['generic-icon'][0].$.name;
if (item['sub-class-of']) {
var type = item['sub-class-of'][0].$.type;
if (metadata.mimeToIcon[type])
return metadata.mimeToIcon[type];
else
while (++i < items.length)
if (items[i].$.type === type)
return getIcon(items[i], i);
}
};
var items = result['mime-info']['mime-type'];
for (var i = 0; i < items.length; ++i) {
var item = items[i];
var mimeType = item.$.type;
if (!mimeType) {
console.warn('MIME type not found at index ' + i + '!');
continue;
}
if (metadata.allMimeTypes.indexOf(mimeType) === -1) {
metadata.allMimeTypes.push(mimeType);
}
var icon = getIcon(item, i);
if (icon) {
metadata.mimeToIcon[mimeType] = icon;
}
var globs = item.glob;
if (!Array.isArray(globs)) {
globs = globs ? [globs] : [];
}
globs.forEach(function(glob) {
var extension = glob.$.pattern;
if (!/^\*(?:\.[a-z0-9+]+){1,2}$/i.test(extension)) {
return;
}
extension = extension.slice(2).toLowerCase();
var weight = parseInt(glob.$.weight, 10);
weight = isNaN(weight) ? 50 : weight;
var previousWeight = extensionToMimeWeight[extension];
if (weight < previousWeight) { // also false if previousWeight is undefined.
return;
}
extensionToMimeWeight[extension] = weight;
metadata.extensionToMime[extension] = mimeType;
});
var aliases = item.alias;
if (aliases) {
for (var j = 0; j < aliases.length; ++j) {
// Assume that no previous aliases were defined
metadata.aliases[ aliases[j].$.type ] = mimeType;
}
}
var comments = item.comment;
if (comments) {
for (var k = 0; k < comments.length; ++k) {
var comment = comments[k];
var lang = 'en'; // Default language
if (typeof comment !== 'string') {
lang = comment.$['xml:lang'];
comment = comment._;
}
if (i18n_supported_langs.indexOf(lang) === -1) {
// Skip unsupported language
continue;
}
if (!i18n[lang]) i18n[lang] = {};
if (i18n[lang][mimeType]) {
if (i18n[lang][mimeType] !== comment) {
console.warn('i18n.' + lang + '.' + mimeType + ' already exists. \n' +
'Existing: ' + i18n[lang][mimeType] + '\n' +
'Ignored : ' + comment);
}
} else {
i18n[lang][mimeType] = comment;
}
}
}
}
done();
});
}