1
0
mirror of https://github.com/ikatyang/emoji-cheat-sheet.git synced 2024-11-22 02:13:48 +01:00

Update emojis

add github emojis
remove invalid emojis
update markdown format
This commit is contained in:
Ika 2017-03-18 15:27:33 +08:00
parent 570451caad
commit d7a88e7969
2 changed files with 58 additions and 33 deletions

View File

@ -6,36 +6,60 @@ const $ = require('cheerio');
const request = require('request');
const markdown = require('./markdown');
const url = 'http://www.emoji-cheat-sheet.com';
const title = 'emoji-cheat-sheet';
const apiUrl = 'https://api.github.com/emojis';
const sheetUrl = 'http://www.emoji-cheat-sheet.com';
const outDir = path.resolve(process.cwd(), './generated');
const outFile = path.join(outDir, 'README.md');
const columnDivisions = 2;
request.get(url, (error, response, body) => {
if (error || response.statusCode !== 200) {
throw error || `Unexpected response status code: ${response.statusCode}`;
} else {
const emojiTable = {};
const $html = $.load(body).root();
$html.find('h2').each((_, catalogElement) => {
const emojis = [];
const catalog = $(catalogElement).text();
$html.find(`#emoji-${catalog.toLowerCase()} li .name`).each((_, emojiElement) => {
const emoji = $(emojiElement).text();
emojis.push(`:${emoji}:`);
});
emojiTable[catalog] = emojis;
const getHTML = (url) => new Promise((resolve, reject) => {
const options = { url };
if (url === apiUrl) {
Object.assign(options, {
headers: { 'User-Agent': 'https://github.com/ikatyang/emoji-cheat-sheet' },
});
if (fs.existsSync(outDir)) {
if (!fs.statSync(outDir).isDirectory()) {
throw `OutDir '${outDir}' should be a directory.`;
}
} else {
fs.mkdirSync(outDir);
}
fs.writeFileSync(outFile, markdown.create(url, title, emojiTable, columnDivisions));
}
request.get(options, (error, response, html) => {
if (error || response.statusCode !== 200) {
reject(error || `Unexpected response status code: ${response.statusCode}`);
} else {
resolve(html);
}
});
});
Promise.all([getHTML(apiUrl), getHTML(sheetUrl)]).then(([apiHTML, sheetHTML]) => {
const apiEmojis = Object.keys(JSON.parse(apiHTML));
const emojiTable = {};
const $html = $.load(sheetHTML).root();
$html.find('h2').each((_, categoryElement) => {
const emojis = [];
const category = $(categoryElement).text();
$html.find(`#emoji-${category.toLowerCase()} li .name`).each((_, emojiElement) => {
const emoji = $(emojiElement).text();
const index = apiEmojis.indexOf(emoji);
if (index !== -1) {
apiEmojis.splice(index, 1);
emojis.push(emoji);
}
});
emojiTable[category] = emojis;
});
if (apiEmojis.length > 0) {
emojiTable['Uncategorized'] = apiEmojis;
}
if (fs.existsSync(outDir)) {
if (!fs.statSync(outDir).isDirectory()) {
throw `OutDir '${outDir}' should be a directory.`;
}
} else {
fs.mkdirSync(outDir);
}
fs.writeFileSync(outFile, markdown.create({
'GitHub Emoji API': apiUrl,
'Emoji Cheat Sheet': sheetUrl,
}, title, emojiTable, columnDivisions));
});

View File

@ -2,24 +2,25 @@ const format = str => str.trim().replace(/^ +/mg, '');
module.exports = class Markdown {
static create(url, title, emojiTable, columnDivisions) {
const emojiCatalogs = Object.keys(emojiTable);
static create(urls, title, emojiTable, columnDivisions) {
const categories = Object.keys(emojiTable);
const urlDescriptions = Object.keys(urls).map((site) => `[${site}](${urls[site]})`).join(' and ');
return format(`
# ${title}
This cheat sheet is auto-generated from <${url}> using [emoji-cheat-sheet-generator](https://github.com/ikatyang/emoji-cheat-sheet/tree/master).
This cheat sheet is auto-generated from ${urlDescriptions} using [emoji-cheat-sheet-generator](https://github.com/ikatyang/emoji-cheat-sheet/tree/master).
## Table of Contents
${emojiCatalogs.map(catalog => `- [${catalog}](#${catalog.toLowerCase()})`).join('\n')}
${categories.map(category => `- [${category}](#${category.toLowerCase()})`).join('\n')}
${
emojiCatalogs.map(catalog => {
const emojis = emojiTable[catalog];
categories.map(category => {
const emojis = emojiTable[category];
return format(`
### ${catalog}
### ${category}
${this.createTable(emojis, columnDivisions)}
@ -33,8 +34,8 @@ module.exports = class Markdown {
static createTableHead(columnDivisions) {
return format(`
| |${(' icon | emoji |').repeat(columnDivisions)}
| - |${(' ---- | ----- |').repeat(columnDivisions)}
| |${(' ico | emoji |').repeat(columnDivisions)}
| - |${(' --- | ----- |').repeat(columnDivisions)}
`);
}
@ -47,7 +48,7 @@ module.exports = class Markdown {
rowEmojis.push('');
table += format(`
| [top](#table-of-contents) |${rowEmojis.map((emoji) => emoji ? ` ${emoji} | \`${emoji}\` ` : ' | ').join(' | ')}|
| [top](#table-of-contents) |${rowEmojis.map((emoji) => emoji ? ` :${emoji}: | \`:${emoji}:\` ` : ' | ').join(' | ')}|
`) + '\n';
}