1
0
mirror of https://github.com/twitter/twemoji.git synced 2024-11-16 20:25:58 +00:00
twemoji/2/scripts/preview
2018-06-27 10:57:35 -07:00

75 lines
1.8 KiB
JavaScript
Executable File

#!/usr/bin/env node
/*! Copyright Twitter Inc. and other contributors. Licensed under MIT *//*
https://github.com/twitter/twemoji/blob/gh-pages/LICENSE
*/
// dependencies
var fs = require('fs');
var path = require('path');
var regex = new RegExp(fs.readFileSync(file('utils/regex')).toString(), 'g');
function fromCodePoint(codepoint) {
var code = typeof codepoint === 'string' ?
parseInt(codepoint, 16) : codepoint;
if (code < 0x10000) {
return String.fromCharCode(code);
}
code -= 0x10000;
return String.fromCharCode(
0xD800 + (code >> 10),
0xDC00 + (code & 0x3FF)
);
}
function countEmoji(emoji) {
var count = 0;
regex.lastIndex = 0;
while (regex.exec(emoji)) {
count++;
}
return count;
}
function file(which) {
return path.join(__dirname, '..', which);
}
fs.readdir(file('assets'), function (err, files) {
var page = fs.readFileSync(file('templates/preview.html')).toString().replace(
'{{emoji-list}}',
'<li>' + files.map(function (filename) {
var codepoints = filename.replace('.ai', '').split('-');
var emoji = codepoints.map(function(codepoint) {
return fromCodePoint(codepoint);
}).join('');
if (countEmoji(emoji + '\ufe0f') === 1) {
codepoints.push('fe0f');
}
return codepoints.map(function (codepoint) {
return '&#x' + codepoint.toUpperCase() + ';';
}).join('');
}).join('</li>\n <li>')+ '</li>'
);
fs.writeFileSync(
file('test/preview.html'),
page.replace(
'{{emoji-options}}',
JSON.stringify({
size: 72
})
)
);
fs.writeFileSync(
file('test/preview-svg.html'),
page.replace(
'{{emoji-options}}',
JSON.stringify({
folder: '../svg',
ext: '.svg',
base: ''
})
)
);
});