util: Add removeFormattingTags text function
This commit is contained in:
@@ -26,7 +26,6 @@ package net.runelite.client.plugins.emojis;
|
|||||||
|
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.regex.Matcher;
|
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
@@ -46,6 +45,7 @@ import net.runelite.client.eventbus.Subscribe;
|
|||||||
import net.runelite.client.plugins.Plugin;
|
import net.runelite.client.plugins.Plugin;
|
||||||
import net.runelite.client.plugins.PluginDescriptor;
|
import net.runelite.client.plugins.PluginDescriptor;
|
||||||
import net.runelite.client.util.ImageUtil;
|
import net.runelite.client.util.ImageUtil;
|
||||||
|
import net.runelite.client.util.Text;
|
||||||
|
|
||||||
@PluginDescriptor(
|
@PluginDescriptor(
|
||||||
name = "Emojis",
|
name = "Emojis",
|
||||||
@@ -55,7 +55,6 @@ import net.runelite.client.util.ImageUtil;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class EmojiPlugin extends Plugin
|
public class EmojiPlugin extends Plugin
|
||||||
{
|
{
|
||||||
private static final Pattern TAG_REGEXP = Pattern.compile("<[^>]*>");
|
|
||||||
private static final Pattern WHITESPACE_REGEXP = Pattern.compile("[\\s\\u00A0]");
|
private static final Pattern WHITESPACE_REGEXP = Pattern.compile("[\\s\\u00A0]");
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
@@ -179,7 +178,7 @@ public class EmojiPlugin extends Plugin
|
|||||||
for (int i = 0; i < messageWords.length; i++)
|
for (int i = 0; i < messageWords.length; i++)
|
||||||
{
|
{
|
||||||
// Remove tags except for <lt> and <gt>
|
// Remove tags except for <lt> and <gt>
|
||||||
final String trigger = removeTags(messageWords[i]);
|
final String trigger = Text.removeFormattingTags(messageWords[i]);
|
||||||
final Emoji emoji = Emoji.getEmoji(trigger);
|
final Emoji emoji = Emoji.getEmoji(trigger);
|
||||||
|
|
||||||
if (emoji == null)
|
if (emoji == null)
|
||||||
@@ -201,29 +200,4 @@ public class EmojiPlugin extends Plugin
|
|||||||
|
|
||||||
return Strings.join(messageWords, " ");
|
return Strings.join(messageWords, " ");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove tags, except for <lt> and <gt>
|
|
||||||
*
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
private static String removeTags(String str)
|
|
||||||
{
|
|
||||||
StringBuffer stringBuffer = new StringBuffer();
|
|
||||||
Matcher matcher = TAG_REGEXP.matcher(str);
|
|
||||||
while (matcher.find())
|
|
||||||
{
|
|
||||||
matcher.appendReplacement(stringBuffer, "");
|
|
||||||
String match = matcher.group(0);
|
|
||||||
switch (match)
|
|
||||||
{
|
|
||||||
case "<lt>":
|
|
||||||
case "<gt>":
|
|
||||||
stringBuffer.append(match);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
matcher.appendTail(stringBuffer);
|
|
||||||
return stringBuffer.toString();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ import com.google.common.base.Joiner;
|
|||||||
import com.google.common.base.Splitter;
|
import com.google.common.base.Splitter;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
import org.apache.commons.text.WordUtils;
|
import org.apache.commons.text.WordUtils;
|
||||||
import org.apache.commons.text.similarity.JaroWinklerDistance;
|
import org.apache.commons.text.similarity.JaroWinklerDistance;
|
||||||
@@ -83,6 +84,32 @@ public class Text
|
|||||||
return TAG_REGEXP.matcher(str).replaceAll("");
|
return TAG_REGEXP.matcher(str).replaceAll("");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove tags from the given string, except for <lt> and <gt>
|
||||||
|
*
|
||||||
|
* @param str The string to remove formatting tags from.
|
||||||
|
* @return The given string with all formatting tags removed from it.
|
||||||
|
*/
|
||||||
|
public static String removeFormattingTags(String str)
|
||||||
|
{
|
||||||
|
StringBuffer stringBuffer = new StringBuffer();
|
||||||
|
Matcher matcher = TAG_REGEXP.matcher(str);
|
||||||
|
while (matcher.find())
|
||||||
|
{
|
||||||
|
matcher.appendReplacement(stringBuffer, "");
|
||||||
|
String match = matcher.group(0);
|
||||||
|
switch (match)
|
||||||
|
{
|
||||||
|
case "<lt>":
|
||||||
|
case "<gt>":
|
||||||
|
stringBuffer.append(match);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
matcher.appendTail(stringBuffer);
|
||||||
|
return stringBuffer.toString();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* In addition to removing all tags, replaces nbsp with space, trims string and lowercases it
|
* In addition to removing all tags, replaces nbsp with space, trims string and lowercases it
|
||||||
* @param str The string to standardize
|
* @param str The string to standardize
|
||||||
|
|||||||
@@ -39,7 +39,21 @@ public class TextTest
|
|||||||
assertEquals("Not so much.", Text.removeTags("<col=FFFFFF This is a very special message.</col>Not so much."));
|
assertEquals("Not so much.", Text.removeTags("<col=FFFFFF This is a very special message.</col>Not so much."));
|
||||||
assertEquals("Use Item -> Man", Text.removeTags("Use Item -> Man"));
|
assertEquals("Use Item -> Man", Text.removeTags("Use Item -> Man"));
|
||||||
assertEquals("a < b", Text.removeTags("a < b"));
|
assertEquals("a < b", Text.removeTags("a < b"));
|
||||||
|
assertEquals("a b", Text.removeTags("a <lt> b"));
|
||||||
assertEquals("Remove no tags", Text.removeTags("Remove no tags"));
|
assertEquals("Remove no tags", Text.removeTags("Remove no tags"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void removeFormattingTags()
|
||||||
|
{
|
||||||
|
assertEquals("Test", Text.removeFormattingTags("<col=FFFFFF>Test</col>"));
|
||||||
|
assertEquals("Test", Text.removeFormattingTags("<img=1><s>Test</s>"));
|
||||||
|
assertEquals("Zezima (level-126)", Text.removeFormattingTags("<col=ffffff><img=2>Zezima<col=00ffff> (level-126)"));
|
||||||
|
assertEquals("", Text.removeFormattingTags("<colrandomtext test>"));
|
||||||
|
assertEquals("Not so much.", Text.removeFormattingTags("<col=FFFFFF This is a very special message.</col>Not so much."));
|
||||||
|
assertEquals("Use Item -> Man", Text.removeFormattingTags("Use Item -> Man"));
|
||||||
|
assertEquals("a < b", Text.removeFormattingTags("a < b"));
|
||||||
|
assertEquals("a <lt> b", Text.removeFormattingTags("a <lt> b"));
|
||||||
|
assertEquals("Remove no tags", Text.removeFormattingTags("Remove no tags"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user