From 4458e28ce27c142a28ac87a5dd2fed521bb60131 Mon Sep 17 00:00:00 2001 From: ganfra Date: Thu, 21 Feb 2019 19:21:08 +0100 Subject: [PATCH] Html : introduce markown lib to handle html rendering. --- app/build.gradle | 4 ++ .../vector/riotredesign/core/di/AppModule.kt | 5 ++ .../riotredesign/features/home/HomeModule.kt | 2 +- .../detail/timeline/MessageItemFactory.kt | 14 ++++- .../features/markdown/HtmlRenderer.kt | 63 +++++++++++++++++++ build.gradle | 1 + 6 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/im/vector/riotredesign/features/markdown/HtmlRenderer.kt diff --git a/app/build.gradle b/app/build.gradle index 12b174e0b5..e91f74d450 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -60,6 +60,7 @@ dependencies { def epoxy_version = "3.0.0" def arrow_version = "0.8.2" + def markwon_version = '3.0.0-SNAPSHOT' implementation project(":matrix-sdk-android") implementation project(":matrix-sdk-android-rx") @@ -97,6 +98,9 @@ dependencies { implementation 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1' implementation 'com.google.android.material:material:1.1.0-alpha02' implementation 'me.gujun.android:span:1.7' + implementation "ru.noties.markwon:core:$markwon_version" + implementation "ru.noties.markwon:html:$markwon_version" + // DI implementation "org.koin:koin-android:$koin_version" diff --git a/app/src/main/java/im/vector/riotredesign/core/di/AppModule.kt b/app/src/main/java/im/vector/riotredesign/core/di/AppModule.kt index bbc973152c..b022525188 100644 --- a/app/src/main/java/im/vector/riotredesign/core/di/AppModule.kt +++ b/app/src/main/java/im/vector/riotredesign/core/di/AppModule.kt @@ -22,6 +22,7 @@ import im.vector.riotredesign.core.resources.ColorProvider import im.vector.riotredesign.core.resources.LocaleProvider import im.vector.riotredesign.core.resources.StringProvider import im.vector.riotredesign.features.home.room.list.RoomSelectionRepository +import im.vector.riotredesign.features.markdown.HtmlRenderer import org.koin.dsl.module.module class AppModule(private val context: Context) { @@ -48,5 +49,9 @@ class AppModule(private val context: Context) { RoomSelectionRepository(get()) } + single { + HtmlRenderer(context) + } + } } \ No newline at end of file diff --git a/app/src/main/java/im/vector/riotredesign/features/home/HomeModule.kt b/app/src/main/java/im/vector/riotredesign/features/home/HomeModule.kt index 2f2d85a026..1af5f66467 100644 --- a/app/src/main/java/im/vector/riotredesign/features/home/HomeModule.kt +++ b/app/src/main/java/im/vector/riotredesign/features/home/HomeModule.kt @@ -33,7 +33,7 @@ class HomeModule { } single { - MessageItemFactory(get(), get(), get()) + MessageItemFactory(get(), get(), get(), get()) } single { diff --git a/app/src/main/java/im/vector/riotredesign/features/home/room/detail/timeline/MessageItemFactory.kt b/app/src/main/java/im/vector/riotredesign/features/home/room/detail/timeline/MessageItemFactory.kt index e2f61cf0c8..2aae81d899 100644 --- a/app/src/main/java/im/vector/riotredesign/features/home/room/detail/timeline/MessageItemFactory.kt +++ b/app/src/main/java/im/vector/riotredesign/features/home/room/detail/timeline/MessageItemFactory.kt @@ -29,12 +29,14 @@ import im.vector.riotredesign.core.epoxy.RiotEpoxyModel import im.vector.riotredesign.core.extensions.localDateTime import im.vector.riotredesign.core.resources.ColorProvider import im.vector.riotredesign.features.home.room.detail.timeline.helper.TimelineMediaSizeProvider +import im.vector.riotredesign.features.markdown.HtmlRenderer import im.vector.riotredesign.features.media.MediaContentRenderer import me.gujun.android.span.span class MessageItemFactory(private val colorProvider: ColorProvider, private val timelineMediaSizeProvider: TimelineMediaSizeProvider, - private val timelineDateFormatter: TimelineDateFormatter) { + private val timelineDateFormatter: TimelineDateFormatter, + private val htmlRenderer: HtmlRenderer) { private val messagesDisplayedWithInformation = HashSet() @@ -102,9 +104,15 @@ class MessageItemFactory(private val colorProvider: ColorProvider, informationData: MessageInformationData, callback: TimelineEventController.Callback?): MessageTextItem? { - val message = linkifyBody(messageContent.body, callback) + val bodyToUse = messageContent.formattedBody + ?.let { + htmlRenderer.render(it) + } + ?: messageContent.body + + val linkifiedBody = linkifyBody(bodyToUse, callback) return MessageTextItem_() - .message(message) + .message(linkifiedBody) .informationData(informationData) } diff --git a/app/src/main/java/im/vector/riotredesign/features/markdown/HtmlRenderer.kt b/app/src/main/java/im/vector/riotredesign/features/markdown/HtmlRenderer.kt new file mode 100644 index 0000000000..f3c659333a --- /dev/null +++ b/app/src/main/java/im/vector/riotredesign/features/markdown/HtmlRenderer.kt @@ -0,0 +1,63 @@ +/* + * + * * Copyright 2019 New Vector Ltd + * * + * * Licensed under the Apache License, Version 2.0 (the "License"); + * * you may not use this file except in compliance with the License. + * * You may obtain a copy of the License at + * * + * * http://www.apache.org/licenses/LICENSE-2.0 + * * + * * Unless required by applicable law or agreed to in writing, software + * * distributed under the License is distributed on an "AS IS" BASIS, + * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * * See the License for the specific language governing permissions and + * * limitations under the License. + * + */ + +package im.vector.riotredesign.features.markdown + +import android.content.Context +import ru.noties.markwon.AbstractMarkwonPlugin +import ru.noties.markwon.Markwon +import ru.noties.markwon.MarkwonVisitor +import ru.noties.markwon.html.HtmlPlugin +import ru.noties.markwon.html.HtmlTag +import ru.noties.markwon.html.MarkwonHtmlRenderer +import ru.noties.markwon.html.TagHandler +import timber.log.Timber + +class HtmlRenderer(private val context: Context) { + + private val markwon = Markwon.builder(context) + .usePlugin(HtmlPlugin.create()) + .usePlugin(MatrixPlugin.create()) + .build() + + fun render(text: String): CharSequence { + return markwon.toMarkdown(text) + } + +} + +private class MatrixPlugin private constructor() : AbstractMarkwonPlugin() { + + override fun configureHtmlRenderer(builder: MarkwonHtmlRenderer.Builder) { + builder.addHandler("mx-reply", MxReplyTagHandler()) + } + + companion object { + + fun create(): MatrixPlugin { + return MatrixPlugin() + } + } +} + +private class MxReplyTagHandler : TagHandler() { + override fun handle(visitor: MarkwonVisitor, renderer: MarkwonHtmlRenderer, tag: HtmlTag) { + Timber.v("Handle mx-reply") + } + +} \ No newline at end of file diff --git a/build.gradle b/build.gradle index 7216b2637d..af25b46084 100644 --- a/build.gradle +++ b/build.gradle @@ -22,6 +22,7 @@ allprojects { google() jcenter() maven { url 'https://jitpack.io' } + maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' } } }