Skip to content

Commit

Permalink
fixed #5 and added markdown stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
fennifith committed Mar 18, 2017
1 parent 3d46d55 commit 072ed31
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 15 deletions.
10 changes: 3 additions & 7 deletions app/src/main/java/doubledotlabs/butteryslack/data/EmojiData.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ public String getUnicode() {
}
}

return getName().replace("_", " ");
return getSlackName();
}

public String getUnicode(int i) {
try {
return String.valueOf(Character.toChars(Integer.parseInt(unicodes[i], 16)));
} catch (Exception e) {
e.printStackTrace();
return getName().replace("_", " ");
return getSlackName();
}
}

Expand All @@ -56,12 +56,8 @@ public static EmojiData from(JSONObject object) {
emoji.name = (String) object.get("short_name");
emoji.file = (String) object.get("image");
emoji.unicode = (String) object.get("unified");
if (emoji.unicode != null) {
if (emoji.unicode != null)
emoji.unicodes = emoji.unicode.split("-");
for (int i = 0; i < emoji.unicodes.length; i++) {
emoji.unicodes[i] = emoji.unicodes[i].replace("-", "");
}
}

return emoji;
}
Expand Down
52 changes: 44 additions & 8 deletions app/src/main/java/doubledotlabs/butteryslack/utils/SlackUtils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package doubledotlabs.butteryslack.utils;

import android.content.Context;
import android.util.Log;

import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.LazyHeaders;
Expand All @@ -12,6 +13,7 @@
import org.json.simple.JSONObject;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import doubledotlabs.butteryslack.ButterySlack;
Expand Down Expand Up @@ -47,16 +49,26 @@ public static String getChannelTopic(SlackChannel channel) {
}

public static String getEmojiMessage(ButterySlack butterySlack, String content) {
if (butterySlack.getEmojis() != null) {
if (content.contains(":") && butterySlack.getEmojis() != null) {
for (EmojiData emoji : butterySlack.getEmojis()) {
if (emoji.getUnicodeSize() > 0)
content = content.replaceAll(emoji.getSlackName(), emoji.getUnicode());
if (emoji.getUnicodeSize() > 0 && content.contains(emoji.getSlackName()))
content = content.replace(emoji.getSlackName(), emoji.getUnicode());
}
}

return content;
}

public static String getMarkdownMessage(ButterySlack butterySlack, String content) {
content = replaceInside(content, "_", "<i>%1$s</i>");
content = replaceInside(content, "*", "<b>%1$s</b>");

if (content.startsWith("&gt;"))
content = "<blockquote>&nbsp;" + content.replace("&gt;", "") + "</blockquote>";

return getEmojiMessage(butterySlack, content);
}

public static String getHtmlMessage(ButterySlack butterySlack, String content) {
while (true) {
try {
Expand All @@ -78,11 +90,20 @@ public static String getHtmlMessage(ButterySlack butterySlack, String content) {
String name = id;

if (id.startsWith("@")) {
SlackUser user = butterySlack.session.findUserById(id.substring(1, id.length()));
if (user != null) name = "@" + user.getUserName();
if (endIndex != realEndIndex) {
name = "@" + content.substring(endIndex + 1, realEndIndex);
Log.d("HtmlMessage", name);
} else {
SlackUser user = butterySlack.session.findUserById(id.substring(1, id.length()));
if (user != null) name = "@" + user.getUserName();
}
} else if (id.startsWith("#")) {
SlackChannel channel = butterySlack.session.findChannelById(id.substring(1, id.length()));
if (channel != null) name = "#" + channel.getName();
if (endIndex != realEndIndex) {
name = "#" + content.substring(endIndex + 1, realEndIndex);
} else {
SlackChannel channel = butterySlack.session.findChannelById(id.substring(1, id.length()));
if (channel != null) name = "#" + channel.getName();
}
} else if (id.startsWith("http") && endIndex != realEndIndex) {
name = content.substring(endIndex + 1, realEndIndex);
}
Expand All @@ -94,7 +115,22 @@ public static String getHtmlMessage(ButterySlack butterySlack, String content) {
}
}

return getEmojiMessage(butterySlack, content);
return getMarkdownMessage(butterySlack, content);
}

public static String replaceInside(String content, String character, String format) {
try {
int index;
while ((index = content.indexOf(character)) >= 0) {
int endIndex = content.indexOf(character, index + 1);
String inside = String.format(Locale.getDefault(), format, content.substring(index + 1, endIndex));
content = content.replace(content.substring(index, endIndex + 1), inside);
}
} catch (StringIndexOutOfBoundsException e) {
e.printStackTrace();
}

return content;
}

public static String getHtmlLink(String href, String name) {
Expand Down

0 comments on commit 072ed31

Please sign in to comment.