Skip to content

Commit

Permalink
Add generator
Browse files Browse the repository at this point in the history
  • Loading branch information
ikatyang committed Mar 17, 2017
1 parent 189ed95 commit 8558569
Show file tree
Hide file tree
Showing 9 changed files with 633 additions and 246 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text eol=lf
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
generated
node_modules
35 changes: 35 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
language: node_js

node_js:
- '6'

before_install:
- git config --global user.name "$GIT_USER_NAME"
- git config --global user.email "$GIT_USER_EMAIL"
- git config --global push.default simple

before_script:
- git clone --depth=1 --branch=generated "https://github.com/$TRAVIS_REPO_SLUG.git" ./generated
- rm -rf ./generated/*

script:
- yarn run generate

after_success:
- cd ./generated
- git add --all
- git commit -m "Update emoji-cheat-sheet"
- git push -qf "https://$GITHUB_ACCESS_TOKEN@github.com/$TRAVIS_REPO_SLUG.git"

branches:
only:
- master

cache:
yarn: true
directories:
- node_modules

env:
global:
secure: DGAmpkJk6+RZIuNpobOd+6gHY9lTkNa/BQXDjbeX1UVO28kCT7vUuq6kmrFYcXLuHc6VCPzqve73byoy2uIbgpLjcp1P3k97x950f4atlqGXyEGCuzPN/wnJi2DHgAYAWC0bYqvgYjIXi/fdS3etPTBp1S/gUDGrvxkQuG4KTLmT163hnfLAQVHX/eE+0pPzR5ZbhUO0v3hyhqGhreYfvH22OaurbrBfxkE+d8cs9v9llRw3Xsj4KZZiMrHu9xAaoo9uxW0QM6PEElPitGfB5fCogQvmBFpk02Gsl3UoN++/7tjb6wkGgayhbQzopS+NyYXw5NCIKuzDkt5X68B0J+Dp8gcNCp4Fp08jmK4D7xqmqJopHDulyuQqgZ+/nue6+6j7J6I9tBnIftjPoDyYIme2YeMkrt2+P2ofv2E+Wv3aTLqZsg1CbkAhUHURGhG40UoaKSk3ft75+i5/VWdBe0QV6aLLmMl/2Hu5dJCtt9uGldAuC0huu6gIjXR0pPVMlGinxtPvjU0k/2ADxHzqwhLeaYpPX0HPWbYd25Can1IlwtmLELlVnTTzMwYugJzgpswm1pmH9jaq7oGjgv9VTRBz1Ab8uAaOivQi/W5Mb1LsWigHOiXmwTkMTs5NHGkolEX307Xv9XASr9F5T8hGNkUFBYQ6DJkjIm9uR14TcdQ=
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Ika

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
246 changes: 0 additions & 246 deletions README.md

This file was deleted.

15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "emoji-cheat-sheet-generator",
"private": true,
"author": "ikatyang",
"license": "MIT",
"homepage": "https://github.com/ikatyang/emoji-cheat-sheet",
"repository": "https://github.com/ikatyang/emoji-cheat-sheet/tree/master",
"scripts": {
"generate": "node ./scripts/generate.js"
},
"dependencies": {
"cheerio": "0.22.0",
"request": "2.81.0"
}
}
41 changes: 41 additions & 0 deletions scripts/generate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/env node

const fs = require('fs');
const path = require('path');
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 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;
});
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(title, emojiTable, columnDivisions));
}
});
50 changes: 50 additions & 0 deletions scripts/markdown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const format = str => str.trim().replace(/^ +/mg, '');

module.exports = class Markdown {

static create(title, emojiTable, columnDivisions) {
return format(`
# ${title}
${
Object.keys(emojiTable).map(catalog => {
const emojis = emojiTable[catalog];
return format(`
#### ${catalog}
${this.createTable(emojis, columnDivisions)}
`);
}).join(('\n').repeat(2))
}
`);
}

static createTableHead(columnDivisions) {
return format(`
|${(' icon | emoji |').repeat(columnDivisions)}
|${(' ---- | ----- |').repeat(columnDivisions)}
`);
}

static createTable(emojis, columnDivisions) {
let table = this.createTableHead(columnDivisions) + '\n';
for (let i = 0; i < emojis.length; i += columnDivisions) {
const rowEmojis = emojis.slice(i, i + columnDivisions);
while (rowEmojis.length < columnDivisions)
rowEmojis.push('');
table += format(`
|${rowEmojis.map((emoji) => ` :${emoji}: | \`:${emoji}:\` `).join(' | ')}|
`) + '\n';
}
return table;
}

};
Loading

0 comments on commit 8558569

Please sign in to comment.