Step 1. Add the JitPack repository to your build file
Add it in your root settings.gradle at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
Add it in your settings.gradle.kts at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url = uri("https://jitpack.io") }
}
}
Add to pom.xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add it in your build.sbt at the end of resolvers:
resolvers += "jitpack" at "https://jitpack.io"
Add it in your project.clj at the end of repositories:
:repositories [["jitpack" "https://jitpack.io"]]
Step 2. Add the dependency
dependencies {
implementation 'com.github.MTDdk:autolink-java:autolink-0.5.0'
}
dependencies {
implementation("com.github.MTDdk:autolink-java:autolink-0.5.0")
}
<dependency>
<groupId>com.github.MTDdk</groupId>
<artifactId>autolink-java</artifactId>
<version>autolink-0.5.0</version>
</dependency>
libraryDependencies += "com.github.MTDdk" % "autolink-java" % "autolink-0.5.0"
:dependencies [[com.github.MTDdk/autolink-java "autolink-0.5.0"]]
Java library to extract links such as URLs and email addresses from plain text. Fast, small and tries to be smart with matching (text is hard).
Inspired by Rinku. Similar to it, regular expressions are not used. Instead, the input text is parsed in one pass with limited backtracking.
This library requires Java 7. It works on Android (minimum API level 15). It has no external dependencies.
Maven coordinates (see here for other build systems):
<dependency>
<groupId>org.nibor.autolink</groupId>
<artifactId>autolink</artifactId>
<version>0.5.0</version>
</dependency>
Extract links:
import org.nibor.autolink.*;
String input = "wow, so example: http://test.com";
LinkExtractor linkExtractor = LinkExtractor.builder().build();
Iterable<LinkSpan> links = linkExtractor.extractLinks(input);
LinkSpan link = links.iterator().next();
link.getType(); // LinkType.URL
link.getBeginIndex(); // 17
link.getEndIndex(); // 32
input.substring(link.getBeginIndex(), link.getEndIndex()); // "http://test.com"
Wrapping URLs in an <a> tag (doesn't handle escaping, uses Java 8):
import org.nibor.autolink.*;
String input = "wow http://test.com such linked";
LinkExtractor linkExtractor = LinkExtractor.builder()
.linkTypes(EnumSet.of(LinkType.URL)) // limit to URLs
.build();
Iterable<LinkSpan> links = linkExtractor.extractLinks(input);
String result = Autolink.renderLinks(input, links, (link, text, sb) -> {
sb.append("<a href=\"");
sb.append(text, link.getBeginIndex(), link.getEndIndex());
sb.append("\">");
sb.append(text, link.getBeginIndex(), link.getEndIndex());
sb.append("</a>");
});
result; // "wow <a href=\"http://test.com\">http://test.com</a> such linked"
Extracts URLs of the form scheme://example with any scheme. URIs such
as example:test are not matched (may be added as an option in the future).
If only certain schemes should be allowed, the result can be filtered.
Includes heuristics for not including trailing delimiters such as punctuation and unbalanced parentheses, see examples below.
Supports internationalized domain names (IDN). Note that they are not validated and as a result, invalid URLs may be matched.
Example input and linked result:
http://example.com. → http://example.com.http://example.com, → http://example.com,(http://example.com) → (http://example.com)(... (see http://example.com)) → (... (see http://example.com))https://en.wikipedia.org/wiki/Link_(The_Legend_of_Zelda) →
https://en.wikipedia.org/wiki/Link_(The_Legend_of_Zelda)http://üñîçøðé.com/ → http://üñîçøðé.com/Also see test cases.
Extract links not starting with scheme:// but just starts with www. such as www.example.com.
The same heuristics apply as for the URL extraction.
Examples:
www.example.com. → www.example.com.(www.example.com) → (www.example.com)[..] link:www.example.com [..] → [..] link:www.example.com [..]Not supported:
www's, e.g. WWW.example.com and wWw.example.comw's, e.g. wwww.example.comThe domain must have at least 3 parts, so www.com is not valid, but www.something.co.uk is.
Also see test cases.
Extracts emails such as foo@example.com. Matches international email
addresses, but doesn't verify the domain name (may match too much).
Examples:
foo@example.com → foo@example.comfoo@example.com. → foo@example.com.foo@example.com, → foo@example.com,üñîçøðé@üñîçøðé.com → üñîçøðé@üñîçøðé.comNot supported:
"this is sparta"@example.comfoo@[127.0.0.1]Note that the domain must have at least one dot (e.g. foo@com isn't
matched), unless the emailDomainMustHaveDot option is disabled.
Also see test cases.
Pull requests, issues and comments welcome ☺. For pull requests:
Copyright (c) 2015-2016 Robin Stocker
MIT licensed, see LICENSE file.