forked from noties/Markwon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
text-added-listener for core-plugin and linkify module
- Loading branch information
Showing
10 changed files
with
185 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
apply plugin: 'com.android.library' | ||
|
||
android { | ||
|
||
compileSdkVersion config['compile-sdk'] | ||
buildToolsVersion config['build-tools'] | ||
|
||
defaultConfig { | ||
minSdkVersion config['min-sdk'] | ||
targetSdkVersion config['target-sdk'] | ||
versionCode 1 | ||
versionName version | ||
} | ||
} | ||
|
||
dependencies { | ||
api project(':markwon-core') | ||
} | ||
|
||
registerArtifact(this) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
POM_NAME=Linkify | ||
POM_ARTIFACT_ID=linkify | ||
POM_DESCRIPTION=Markwon plugin to linkify text (based on Android Linkify) | ||
POM_PACKAGING=aar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<manifest package="ru.noties.markwon.linkify" /> |
91 changes: 91 additions & 0 deletions
91
markwon-linkify/src/main/java/ru/noties/markwon/linkify/LinkifyPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package ru.noties.markwon.linkify; | ||
|
||
import android.support.annotation.IntDef; | ||
import android.support.annotation.NonNull; | ||
import android.text.SpannableStringBuilder; | ||
import android.text.util.Linkify; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
|
||
import ru.noties.markwon.AbstractMarkwonPlugin; | ||
import ru.noties.markwon.MarkwonVisitor; | ||
import ru.noties.markwon.SpannableBuilder; | ||
import ru.noties.markwon.core.CorePlugin; | ||
|
||
public class LinkifyPlugin extends AbstractMarkwonPlugin { | ||
|
||
@IntDef(flag = true, value = { | ||
Linkify.EMAIL_ADDRESSES, | ||
Linkify.PHONE_NUMBERS, | ||
Linkify.WEB_URLS, | ||
Linkify.ALL | ||
}) | ||
@Retention(RetentionPolicy.SOURCE) | ||
@interface LinkifyMask { | ||
} | ||
|
||
@NonNull | ||
public static LinkifyPlugin create() { | ||
return create(Linkify.EMAIL_ADDRESSES | Linkify.PHONE_NUMBERS | Linkify.WEB_URLS); | ||
} | ||
|
||
@NonNull | ||
public static LinkifyPlugin create(@LinkifyMask int mask) { | ||
return new LinkifyPlugin(mask); | ||
} | ||
|
||
private final int mask; | ||
|
||
@SuppressWarnings("WeakerAccess") | ||
LinkifyPlugin(@LinkifyMask int mask) { | ||
this.mask = mask; | ||
} | ||
|
||
@Override | ||
public void configure(@NonNull Registry registry) { | ||
registry.require(CorePlugin.class, new Action<CorePlugin>() { | ||
@Override | ||
public void apply(@NonNull CorePlugin corePlugin) { | ||
corePlugin.addOnTextAddedListener(new LinkifyTextAddedListener(mask)); | ||
} | ||
}); | ||
} | ||
|
||
private static class LinkifyTextAddedListener implements CorePlugin.OnTextAddedListener { | ||
|
||
private final int mask; | ||
private final SpannableStringBuilder builder; | ||
|
||
LinkifyTextAddedListener(int mask) { | ||
this.mask = mask; | ||
this.builder = new SpannableStringBuilder(); | ||
} | ||
|
||
@Override | ||
public void onTextAdded(@NonNull MarkwonVisitor visitor, @NonNull String text, int start) { | ||
|
||
// clear previous state | ||
builder.clear(); | ||
builder.clearSpans(); | ||
|
||
// append text to process | ||
builder.append(text); | ||
|
||
if (Linkify.addLinks(builder, mask)) { | ||
final Object[] spans = builder.getSpans(0, builder.length(), Object.class); | ||
if (spans != null | ||
&& spans.length > 0) { | ||
final SpannableBuilder spannableBuilder = visitor.builder(); | ||
for (Object span : spans) { | ||
spannableBuilder.setSpan( | ||
span, | ||
start + builder.getSpanStart(span), | ||
start + builder.getSpanEnd(span), | ||
builder.getSpanFlags(span)); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters