Html : introduce markown lib to handle html rendering.

This commit is contained in:
ganfra
2019-02-21 19:21:08 +01:00
parent 14ac3a8ae6
commit 4458e28ce2
6 changed files with 85 additions and 4 deletions

View File

@ -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)
}
}
}

View File

@ -33,7 +33,7 @@ class HomeModule {
}
single {
MessageItemFactory(get(), get(), get())
MessageItemFactory(get(), get(), get(), get())
}
single {

View File

@ -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<String?>()
@ -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)
}

View File

@ -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")
}
}