X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ftext%2FFreenetLinkParser.java;h=f36b6df81f43cc71465e6b678bf66b2ba6c6efe3;hb=c1b01520dde989b59b94bcd7a894be025ac70404;hp=7749b0091e8c567f08e445f8d0853d19d81bc6af;hpb=d41b3a9bd03c83d82fdbd19ac56686e41e25d9f5;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/text/FreenetLinkParser.java b/src/main/java/net/pterodactylus/sone/text/FreenetLinkParser.java index 7749b00..f36b6df 100644 --- a/src/main/java/net/pterodactylus/sone/text/FreenetLinkParser.java +++ b/src/main/java/net/pterodactylus/sone/text/FreenetLinkParser.java @@ -40,7 +40,28 @@ public class FreenetLinkParser implements Parser { private static final Logger logger = Logging.getLogger(FreenetLinkParser.class); /** Pattern to detect whitespace. */ - private static final Pattern whitespacePattern = Pattern.compile("[\\p{javaWhitespace}]"); + private static final Pattern whitespacePattern = Pattern.compile("[\u0020\u00a0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u200c\u200d\u202f\u205f\u2060\u2800\u3000]"); + + /** + * Enumeration for all recognized link types. + * + * @author David ‘Bombe’ Roden + */ + private enum LinkType { + + /** Link is a KSK. */ + KSK, + + /** Link is a CHK. */ + CHK, + + /** Link is an SSK. */ + SSK, + + /** Link is a USK. */ + USK + + } /** The template factory. */ private final TemplateFactory templateFactory; @@ -79,17 +100,26 @@ public class FreenetLinkParser implements Parser { break; } int next = Integer.MAX_VALUE; + LinkType linkType = null; if ((nextKsk > -1) && (nextKsk < next)) { next = nextKsk; + linkType = LinkType.KSK; } if ((nextChk > -1) && (nextChk < next)) { next = nextChk; + linkType = LinkType.CHK; } if ((nextSsk > -1) && (nextSsk < next)) { next = nextSsk; + linkType = LinkType.SSK; } if ((nextUsk > -1) && (nextUsk < next)) { next = nextUsk; + linkType = LinkType.USK; + } + if ((next >= 8) && (line.substring(next - 8, next).equals("freenet:"))) { + next -= 8; + line = line.substring(0, next) + line.substring(next + 8); } Matcher matcher = whitespacePattern.matcher(line); int nextSpace = matcher.find(next) ? matcher.start() : line.length(); @@ -99,7 +129,7 @@ public class FreenetLinkParser implements Parser { String name = link; logger.log(Level.FINER, "Found link: " + link); logger.log(Level.FINEST, "Next: %d, CHK: %d, SSK: %d, USK: %d", new Object[] { next, nextChk, nextSsk, nextUsk }); - if (((next == nextChk) || (next == nextSsk) || (next == nextUsk)) && (link.length() > 98) && (link.charAt(47) == ',') && (link.charAt(91) == ',') && (link.charAt(99) == '/')) { + if (((linkType == LinkType.CHK) || (linkType == LinkType.SSK) || (linkType == LinkType.USK)) && (link.length() > 98) && (link.charAt(47) == ',') && (link.charAt(91) == ',') && (link.charAt(99) == '/')) { name = link.substring(0, 47) + "…" + link.substring(99); } parts.add(createLinkPart(link, name));