Refactor detectLinks (#297)

* Refactor detect-links
* Add autolink-java license
multisite^2
Andy Klimczak 8 years ago committed by Florens
parent bdd460e4db
commit 17d83bdb9e
  1. 1
      Clover/app/build.gradle
  2. 28
      Clover/app/src/main/assets/html/licenses.html
  3. 42
      Clover/app/src/main/java/org/floens/chan/chan/ChanParser.java

@ -130,4 +130,5 @@ dependencies {
compile 'com.davemorrissey.labs:subsampling-scale-image-view:3.5.0' compile 'com.davemorrissey.labs:subsampling-scale-image-view:3.5.0'
compile 'com.squareup.okhttp3:okhttp:3.4.1' compile 'com.squareup.okhttp3:okhttp:3.4.1'
compile 'de.greenrobot:eventbus:2.4.0' compile 'de.greenrobot:eventbus:2.4.0'
compile 'org.nibor.autolink:autolink:0.6.0'
} }

@ -194,5 +194,33 @@ The GIFLIB distribution is Copyright (c) 1997 Eric S. Raymond
</code> </code>
</pre> </pre>
<br> <br>
<h3>autolink-java</h3>
<a href="https://github.com/robinst/autolink-java">https://github.com/robinst/autolink-java</a>
<pre>
<code>
The MIT License (MIT)
Copyright (c) 2015 Robin Stocker
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
</code>
</pre>
<br>
</body> </body>
</html> </html>

@ -47,8 +47,12 @@ import org.jsoup.parser.Parser;
import org.jsoup.select.Elements; import org.jsoup.select.Elements;
import org.jsoup.select.NodeTraversor; import org.jsoup.select.NodeTraversor;
import org.jsoup.select.NodeVisitor; import org.jsoup.select.NodeVisitor;
import org.nibor.autolink.LinkExtractor;
import org.nibor.autolink.LinkSpan;
import org.nibor.autolink.LinkType;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -63,6 +67,8 @@ public class ChanParser {
private static ChanParser instance = new ChanParser(); private static ChanParser instance = new ChanParser();
private final DatabaseManager databaseManager; private final DatabaseManager databaseManager;
private final LinkExtractor linkExtractor = LinkExtractor.builder().linkTypes(EnumSet.of(LinkType.URL)).build();
public ChanParser() { public ChanParser() {
databaseManager = Chan.getDatabaseManager(); databaseManager = Chan.getDatabaseManager();
} }
@ -445,40 +451,16 @@ public class ChanParser {
} }
private void detectLinks(Theme theme, Post post, String text, SpannableString spannable) { private void detectLinks(Theme theme, Post post, String text, SpannableString spannable) {
int startPos = 0; // use autolink-java lib to detect links
int endPos; final Iterable<LinkSpan> links = linkExtractor.extractLinks(text);
while (true) { for(final LinkSpan link : links) {
startPos = text.indexOf("://", startPos); final String linkText = text.substring(link.getBeginIndex(), link.getEndIndex());
if (startPos < 0) break; final PostLinkable pl = new PostLinkable(theme, post, linkText, linkText, PostLinkable.Type.LINK);
spannable.setSpan(pl, link.getBeginIndex(), link.getEndIndex(), 0);
// go back to the first space
while (startPos > 0 && !isWhitespace(text.charAt(startPos - 1))) {
startPos--;
}
// find the last non whitespace character
endPos = startPos;
while (endPos < text.length() - 1 && !isWhitespace(text.charAt(endPos + 1))) {
endPos++;
}
// one past
endPos++;
String linkString = text.substring(startPos, endPos);
PostLinkable pl = new PostLinkable(theme, post, linkString, linkString, PostLinkable.Type.LINK);
spannable.setSpan(pl, startPos, endPos, 0);
post.linkables.add(pl); post.linkables.add(pl);
startPos = endPos;
} }
} }
private boolean isWhitespace(char c) {
return Character.isWhitespace(c) || c == '>'; // consider > as a link separator
}
// Below code taken from org.jsoup.nodes.Element.text(), but it preserves <br> // Below code taken from org.jsoup.nodes.Element.text(), but it preserves <br>
private String getNodeText(Element node) { private String getNodeText(Element node) {
final StringBuilder accum = new StringBuilder(); final StringBuilder accum = new StringBuilder();

Loading…
Cancel
Save