Skip to content
This repository has been archived by the owner on Jul 23, 2019. It is now read-only.

C# Library: A new Hope #594

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/csharp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Emojione utility for C#


### How to use

This utility provides a method to convert from shortname to unicode characters.


### How to re-generate mapping

```
cd lib/csharp/generator
npm install
node generate.js
```
1 change: 1 addition & 0 deletions lib/csharp/generator/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
27 changes: 27 additions & 0 deletions lib/csharp/generator/Emojione.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Collections.Generic;

public class Emojione {

public static string ShortnameToUnicode(string shortname)
{
string unicode = null;
unicodeMap.TryGetValue(shortname, out unicode);
return unicode;
}

public static string UnicodeToShortname(string unicode)
{
string shortname = null;
reverseMap.TryGetValue(unicode, out shortname);
return shortname;
}

private static Dictionary<string, string> unicodeMap = new Dictionary<string, string>(){
<%= mapping %>
};

private static Dictionary<string, string> reverseMap = new Dictionary<string, string>(){
<%= shortname_uni %>
};

}
7 changes: 7 additions & 0 deletions lib/csharp/generator/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.PHONY:all
all:
node generate.js

.PHONY:clean
clean:
rm ../src/generate.js
74 changes: 74 additions & 0 deletions lib/csharp/generator/generate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
var util = require("util"),
fs = require("fs"),
_ = require("underscore");


// Load emojis
var emojis = require("../../../emoji_strategy.json");

// helper function for unicode parsing.
// c# only has 16 bit unicode literals so we need to have some magic.

function csharp(unicode) {
output = "\\u";
tmp = "";
map = "0123456789abcdef".split('');


for(i = 0; i < unicode.length; i++)
{
if(tmp.length != 0 && tmp.length % 4 == 0 ) {
output += tmp + "\\u";
tmp = "";
}
if(map.indexOf(unicode[i].toLowerCase()) == -1)
{
if(unicode[i] != "-")
console.log("don't know how to convert " + unicode);

if (tmp.length > 0) {
while(tmp.length < 4)
{
tmp = "0" + tmp;
}

output += tmp + "\\u";
tmp = "";
}
} else {
tmp += unicode[i];
}

}
while(tmp.length < 4)
tmp = "0"+tmp;

output += tmp;


if(output.substr(output.length - 2, output.length) == "\\u")
output = output.substr(0, output.length - 2);
return output;
}


// Generate C# mapping
var uni_shortname_mapping = _(emojis).map(function(data, unicode) {
// Get chars
return '{"' + data.shortname + '", "' + csharp(data.unicode_output) + '"},';
}).join("\n ");

var shortname_uni_mapping = _(emojis).map(function(data, unicode) {
// Get chars
return '{"' + csharp(data.unicode_output) + '", "' + data.shortname + '"},';
}).join("\n ");
// Generate C# class from template

var input = fs.readFileSync("./Emojione.cs");
var output = _(input.toString()).template()({ mapping: uni_shortname_mapping, shortname_uni : shortname_uni_mapping });

// Write C# class to file
var output_path = "../src/Emojione.cs";
fs.writeFileSync(output_path, output);

console.log("Generated " + output_path);
13 changes: 13 additions & 0 deletions lib/csharp/generator/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions lib/csharp/generator/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "emojione-csharp",
"version": "0.1.0",
"dependencies": {
"underscore": "^1.7.0"
}
}
Loading