Created
March 6, 2015 10:02
-
-
Save lethean/bdd5a657f103b6cb0c23 to your computer and use it in GitHub Desktop.
Generate Font Awesome icon name + code definition for C language
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
// | |
// Generate FontAwesome icon definitions | |
// | |
var http = require('http'); | |
http.get('http://fontawesome.io/cheatsheet/', function (result) { | |
var body = ''; | |
result.on('data', function (chunk) { body += chunk; }); | |
result.on('end', function () { parse(body); }); | |
}); | |
function parse(body) { | |
var strs = body.match(/fa[-a-z]+\n.*\n.*span>$/mg); | |
if (!strs) | |
return; | |
for (var i = 0; i < strs.length; i++) { | |
var str = strs[i]; | |
str = str.replace(/\s+/g, ' ') | |
.replace('[&', '&') | |
.replace(';]', ';') | |
.replace('(alias)', '') | |
.replace('<span class="muted">', '') | |
.replace('<span class="text-muted">', '') | |
.replace(/<\/span>/g, '') | |
.replace(/-/g, '_') | |
.replace(/(fa[_a-z]+)/g, function (v) { return v.toUpperCase(); }) | |
.replace('FA_', '#define FA_ICON_') | |
.replace(/&#x([a-z0-9]+);/g, function(v, v1) { | |
var utf16Code = parseInt(v1, 16); | |
var utf8CStr = toCStringUTF8(String.fromCharCode(utf16Code)); | |
return '"' + utf8CStr + '" /* ' + v + ' */'; | |
}) | |
.replace(/\s+/g, ' '); | |
console.log(str); | |
} | |
} | |
function toCStringUTF8(str) { | |
var utf8 = []; | |
var i; | |
for (i = 0; i < str.length; i++) { | |
var charcode = str.charCodeAt(i); | |
if (charcode < 0x80) utf8.push(charcode); | |
else if (charcode < 0x800) { | |
utf8.push(0xc0 | (charcode >> 6), | |
0x80 | (charcode & 0x3f)); | |
} | |
else if (charcode < 0xd800 || charcode >= 0xe000) { | |
utf8.push(0xe0 | (charcode >> 12), | |
0x80 | ((charcode>>6) & 0x3f), | |
0x80 | (charcode & 0x3f)); | |
} | |
// surrogate pair | |
else { | |
i++; | |
// UTF-16 encodes 0x10000-0x10FFFF by | |
// subtracting 0x10000 and splitting the | |
// 20 bits of 0x0-0xFFFFF into two halves | |
charcode = 0x10000 + (((charcode & 0x3ff)<<10) | |
| (str.charCodeAt(i) & 0x3ff)); | |
utf8.push(0xf0 | (charcode >>18), | |
0x80 | ((charcode>>12) & 0x3f), | |
0x80 | ((charcode>>6) & 0x3f), | |
0x80 | (charcode & 0x3f)); | |
} | |
} | |
for (i = 0; i < utf8.length; i++) | |
utf8[i] = '\\x' + decimalToHex(utf8[i], 2); | |
return utf8.join(''); | |
} | |
function decimalToHex(d, padding) { | |
var hex = Number(d).toString(16); | |
padding = typeof (padding) === "undefined" || padding === null ? padding = 2 : padding; | |
while (hex.length < padding) { | |
hex = "0" + hex; | |
} | |
return hex; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment