From d7a88e7969c84ca7bf286079e05a6e35118af51c Mon Sep 17 00:00:00 2001 From: Ika Date: Sat, 18 Mar 2017 15:27:33 +0800 Subject: [PATCH] Update emojis add github emojis remove invalid emojis update markdown format --- scripts/generate.js | 70 ++++++++++++++++++++++++++++++--------------- scripts/markdown.js | 21 +++++++------- 2 files changed, 58 insertions(+), 33 deletions(-) diff --git a/scripts/generate.js b/scripts/generate.js index b63af16..c9d954f 100644 --- a/scripts/generate.js +++ b/scripts/generate.js @@ -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)); }); diff --git a/scripts/markdown.js b/scripts/markdown.js index 60af25e..2a43543 100644 --- a/scripts/markdown.js +++ b/scripts/markdown.js @@ -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'; }