Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
AloqaIM-Android
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
AloqaIM-Android
Commits
2f9014bb
Commit
2f9014bb
authored
Dec 20, 2016
by
Yusuke Iwaki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
render MarkDown link.
parent
b617a4b8
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
61 additions
and
33 deletions
+61
-33
Linkify.java
.../main/java/chat/rocket/android/widget/helper/Linkify.java
+2
-33
MarkDown.java
...main/java/chat/rocket/android/widget/helper/MarkDown.java
+59
-0
No files found.
rocket-chat-android-widgets/src/main/java/chat/rocket/android/widget/helper/Linkify.java
View file @
2f9014bb
package
chat
.
rocket
.
android
.
widget
.
helper
;
import
android.content.ClipData
;
import
android.content.ClipboardManager
;
import
android.content.Context
;
import
android.content.Intent
;
import
android.net.Uri
;
import
android.text.SpannableString
;
import
android.text.Spanned
;
import
android.text.style.ClickableSpan
;
import
android.view.View
;
import
android.widget.TextView
;
import
android.widget.Toast
;
import
org.nibor.autolink.LinkExtractor
;
import
org.nibor.autolink.LinkSpan
;
import
org.nibor.autolink.LinkType
;
...
...
@@ -41,33 +33,10 @@ public class Linkify {
final
int
idx2
=
link
.
getEndIndex
();
final
String
url
=
text
.
subSequence
(
idx1
,
idx2
).
toString
();
spannableString
.
setSpan
(
createLinkSpan
(
url
),
idx1
,
idx2
,
Spanned
.
SPAN_EXCLUSIVE_EXCLUSIVE
);
spannableString
.
setSpan
(
MarkDown
.
createLinkSpan
(
url
),
idx1
,
idx2
,
Spanned
.
SPAN_EXCLUSIVE_EXCLUSIVE
);
}
return
spannableString
;
}
private
static
ClickableSpan
createLinkSpan
(
final
String
url
)
{
return
new
ClickableSpan
()
{
@Override
public
void
onClick
(
View
view
)
{
final
Context
context
=
view
.
getContext
();
try
{
Intent
intent
=
new
Intent
(
Intent
.
ACTION_VIEW
,
Uri
.
parse
(
url
));
intent
.
setFlags
(
Intent
.
FLAG_ACTIVITY_NEW_TASK
);
context
.
startActivity
(
intent
);
return
;
}
catch
(
Exception
exception
)
{
}
try
{
ClipboardManager
clipboardManager
=
(
ClipboardManager
)
context
.
getSystemService
(
Context
.
CLIPBOARD_SERVICE
);
clipboardManager
.
setPrimaryClip
(
ClipData
.
newPlainText
(
"linkURL"
,
url
));
Toast
.
makeText
(
context
,
"Copied to clipboard"
,
Toast
.
LENGTH_SHORT
).
show
();
}
catch
(
Exception
exception
)
{
}
}
};
}
}
\ No newline at end of file
rocket-chat-android-widgets/src/main/java/chat/rocket/android/widget/helper/MarkDown.java
View file @
2f9014bb
package
chat
.
rocket
.
android
.
widget
.
helper
;
import
android.content.ClipData
;
import
android.content.ClipboardManager
;
import
android.content.Context
;
import
android.content.Intent
;
import
android.graphics.Typeface
;
import
android.net.Uri
;
import
android.text.SpannableString
;
import
android.text.Spanned
;
import
android.text.style.AbsoluteSizeSpan
;
import
android.text.style.ClickableSpan
;
import
android.text.style.StrikethroughSpan
;
import
android.text.style.StyleSpan
;
import
android.view.View
;
import
android.widget.TextView
;
import
android.widget.Toast
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
...
...
@@ -22,6 +30,8 @@ public class MarkDown {
public
static
void
apply
(
TextView
textView
)
{
SpannableString
text
=
new
SpannableString
(
textView
.
getText
());
removeImage
(
text
);
highlightLink1
(
text
);
highlightLink2
(
text
);
bold
(
text
);
italic
(
text
);
strike
(
text
);
...
...
@@ -39,6 +49,31 @@ public class MarkDown {
}
}
private
static
void
highlightLink1
(
SpannableString
inputText
)
{
Pattern
linkPattern
=
Pattern
.
compile
(
"\\[([^\\]]+)\\]\\(((?:http|https):\\/\\/[^\\)]+)\\)"
,
Pattern
.
MULTILINE
);
final
Matcher
matcher
=
linkPattern
.
matcher
(
inputText
);
while
(
matcher
.
find
())
{
ClickableSpan
span
=
createLinkSpan
(
matcher
.
group
(
2
));
setSpan
(
span
,
inputText
,
matcher
.
start
(),
matcher
.
end
(),
1
,
matcher
.
group
(
2
).
length
()
+
3
);
}
}
private
static
void
highlightLink2
(
SpannableString
inputText
)
{
Pattern
linkPattern
=
Pattern
.
compile
(
"(?:<|<)((?:http|https):\\/\\/[^\\|]+)\\|(.+?)(?=>|>)(?:>|>)"
,
Pattern
.
MULTILINE
);
Matcher
matcher
=
linkPattern
.
matcher
(
inputText
);
while
(
matcher
.
find
())
{
ClickableSpan
span
=
createLinkSpan
(
matcher
.
group
(
2
));
setSpan
(
span
,
inputText
,
matcher
.
start
(),
matcher
.
end
(),
matcher
.
group
(
1
).
length
()
+
matcher
.
group
(
2
).
length
()
+
1
,
matcher
.
group
(
5
).
length
()
+
matcher
.
group
(
6
).
length
());
}
}
private
static
void
bold
(
SpannableString
inputText
)
{
Pattern
boldPattern
=
Pattern
.
compile
(
"(^|>|[ >_~`])(\\*{1,2})[^\\*\\r\\n]+(\\*{1,2})([<_~`]|\\B|\\b|$)"
,
Pattern
.
MULTILINE
);
...
...
@@ -96,4 +131,28 @@ public class MarkDown {
Spanned
.
SPAN_EXCLUSIVE_EXCLUSIVE
);
}
}
/*package*/
static
ClickableSpan
createLinkSpan
(
final
String
url
)
{
return
new
ClickableSpan
()
{
@Override
public
void
onClick
(
View
view
)
{
final
Context
context
=
view
.
getContext
();
try
{
Intent
intent
=
new
Intent
(
Intent
.
ACTION_VIEW
,
Uri
.
parse
(
url
));
intent
.
setFlags
(
Intent
.
FLAG_ACTIVITY_NEW_TASK
);
context
.
startActivity
(
intent
);
return
;
}
catch
(
Exception
exception
)
{
}
try
{
ClipboardManager
clipboardManager
=
(
ClipboardManager
)
context
.
getSystemService
(
Context
.
CLIPBOARD_SERVICE
);
clipboardManager
.
setPrimaryClip
(
ClipData
.
newPlainText
(
"linkURL"
,
url
));
Toast
.
makeText
(
context
,
"Copied to clipboard"
,
Toast
.
LENGTH_SHORT
).
show
();
}
catch
(
Exception
exception
)
{
}
}
};
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment