| 30 | public class Tokenizer { |
| 31 | |
| 32 | public static Pair<List<MessageToken>, Boolean> tokenize(List<MessageToken> orig) { |
| 33 | Context ctx = Data.ctx; |
| 34 | int channel = Data.currentBroadcasterId; |
| 35 | |
| 36 | // Don't add Emotes, when we don't have the channel's emotes (yet) |
| 37 | // and we don't need to Highlight any messages |
| 38 | if (!Emotes.channelHasEmotes(ctx, channel) && Highlight.getInstance().isEmpty()) { |
| 39 | return new Pair<>(orig, false); |
| 40 | } |
| 41 | |
| 42 | ArrayList<MessageToken> newTokens = new ArrayList<>(orig.size() + 5); |
| 43 | boolean shouldHighlight = false; |
| 44 | |
| 45 | for (MessageToken token : orig) { |
| 46 | // possible issue: emotes won't work in e.g. MentionToken or BitsToken |
| 47 | if (!(token instanceof TextToken)) { |
| 48 | if (token instanceof EmoticonToken && !newTokens.isEmpty()) { |
| 49 | if (newTokens.get(newTokens.size() - 1) instanceof EmoticonToken) { |
| 50 | newTokens.add(new TextToken(" ", new AutoModMessageFlags())); |
| 51 | } |
| 52 | } |
| 53 | newTokens.add(token); |
| 54 | continue; |
| 55 | } |
| 56 | |
| 57 | TextToken text = (TextToken) token; |
| 58 | |
| 59 | if (text.getText().equals(" ")) { |
| 60 | // " ".split(" ") will produce an empty array |
| 61 | // this is why we need to handle this edge-case |
| 62 | newTokens.add(token); |
| 63 | continue; |
| 64 | } |
| 65 | String[] tokens = text.getText().split(" "); |
| 66 | |
| 67 | StringBuilder currentText = new StringBuilder(); |
| 68 | for (String word : tokens) { |
| 69 | Emote emote = Emotes.getEmote(ctx, word, channel); |
| 70 | if (Highlight.shouldHighlight(word)) { |
| 71 | shouldHighlight = true; |
| 72 | } |
| 73 | if (emote == null) { |
| 74 | currentText.append(word).append(" "); |
| 75 | continue; |
| 76 | } |
| 77 | // emote found |
| 78 | String before = currentText.toString(); |
| 79 | if (!before.isEmpty()) { |
| 80 | newTokens.add(new TextToken(currentText.toString(), text.getFlags())); // add everything before Emote as TextToken |
| 81 | } |
| 82 | newTokens.add(new EmoticonToken(word, "BTTV-" + emote.id)); // add Emote |
| 83 | |
| 84 | // prepare next TextToken |
| 85 | currentText.setLength(0); |
| 86 | currentText.append(' '); |
| 87 | } |
| 88 | String before = currentText.toString(); |
| 89 | if (!before.trim().isEmpty()) { |