Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support code blocks #4090

Merged
merged 2 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import android.graphics.Typeface.DEFAULT_BOLD
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.text.Editable
import android.text.Html
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.TextPaint
import android.text.style.CharacterStyle
import android.util.TypedValue
Expand All @@ -29,6 +26,7 @@ import com.keylesspalace.tusky.entity.StatusEdit
import com.keylesspalace.tusky.interfaces.LinkListener
import com.keylesspalace.tusky.util.AbsoluteTimeFormatter
import com.keylesspalace.tusky.util.BindingHolder
import com.keylesspalace.tusky.util.TuskyTagHandler
import com.keylesspalace.tusky.util.aspectRatios
import com.keylesspalace.tusky.util.decodeBlurHash
import com.keylesspalace.tusky.util.emojify
Expand Down Expand Up @@ -118,7 +116,7 @@ class ViewEditsAdapter(

val emojifiedText = edit
.content
.parseAsMastodonHtml(TuskyTagHandler(context))
.parseAsMastodonHtml(EditsTagHandler(context))
.emojify(edit.emojis, binding.statusEditContent, animateEmojis)

setClickableText(binding.statusEditContent, emojifiedText, emptyList(), emptyList(), listener)
Expand Down Expand Up @@ -224,7 +222,7 @@ class ViewEditsAdapter(
* Handle XML tags created by [ViewEditsViewModel] and create custom spans to display inserted or
* deleted text.
*/
class TuskyTagHandler(val context: Context) : Html.TagHandler {
class EditsTagHandler(val context: Context) : TuskyTagHandler() {
/** Class to mark the start of a span of deleted text */
class Del

Expand Down Expand Up @@ -255,34 +253,7 @@ class TuskyTagHandler(val context: Context) : Html.TagHandler {
)
}
}
}
}

/** @return the last span in [text] of type [kind], or null if that kind is not in text */
private fun <T> getLast(text: Spanned, kind: Class<T>): Any? {
val spans = text.getSpans(0, text.length, kind)
return spans?.get(spans.size - 1)
}

/**
* Mark the start of a span of [text] with [mark] so it can be discovered later by [end].
*/
private fun start(text: SpannableStringBuilder, mark: Any) {
val len = text.length
text.setSpan(mark, len, len, Spannable.SPAN_MARK_MARK)
}

/**
* Set a [span] over the [text] most from the point recently marked with [mark] to the end
* of the text.
*/
private fun <T> end(text: SpannableStringBuilder, mark: Class<T>, span: Any) {
val len = text.length
val obj = getLast(text, mark)
val where = text.getSpanStart(obj)
text.removeSpan(obj)
if (where != len) {
text.setSpan(span, where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
else -> super.handleTag(opening, tag, output, xmlReader)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ package com.keylesspalace.tusky.components.viewthread.edits
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import at.connyduck.calladapter.networkresult.getOrElse
import com.keylesspalace.tusky.components.viewthread.edits.TuskyTagHandler.Companion.DELETED_TEXT_EL
import com.keylesspalace.tusky.components.viewthread.edits.TuskyTagHandler.Companion.INSERTED_TEXT_EL
import com.keylesspalace.tusky.components.viewthread.edits.EditsTagHandler.Companion.DELETED_TEXT_EL
import com.keylesspalace.tusky.components.viewthread.edits.EditsTagHandler.Companion.INSERTED_TEXT_EL
import com.keylesspalace.tusky.entity.StatusEdit
import com.keylesspalace.tusky.network.MastodonApi
import kotlinx.coroutines.Dispatchers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,78 @@

package com.keylesspalace.tusky.util

import android.text.Editable
import android.text.Html.TagHandler
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.style.TypefaceSpan
import androidx.core.text.parseAsHtml
import org.xml.sax.XMLReader

/**
* parse a String containing html from the Mastodon api to Spanned
*/
@JvmOverloads
fun String.parseAsMastodonHtml(tagHandler: TagHandler? = null): Spanned {
fun String.parseAsMastodonHtml(tagHandler: TagHandler? = tuskyTagHandler): Spanned {
return this.replace("<br> ", "<br>&nbsp;")
.replace("<br /> ", "<br />&nbsp;")
.replace("<br/> ", "<br/>&nbsp;")
.replace("\n", "<br/>")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to inject the nonbreaking space here? Or we only get \n when it's a code block?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When a browser renders HTML, it ignores newlines as a default. The Android HTML parsing code does the same.
In a browser, you can override that behavior with the whitespace css property. Mastodon sets it for the whole status to pre-wrap. That difference did not matter, because Mastodon also replaces all newlines with <br/> so the Android parser would handle them correctly. But inside <code> blocks, newlines are not replaced by Mastodon, so that's what this replacement is for.

Tldr:

we only get \n when it's a code block?

yes

.replace(" ", "&nbsp;&nbsp;")
.parseAsHtml(tagHandler = tagHandler)
/* Html.fromHtml returns trailing whitespace if the html ends in a </p> tag, which
* most status contents do, so it should be trimmed. */
.trimTrailingWhitespace()
}

val tuskyTagHandler = TuskyTagHandler()

open class TuskyTagHandler : TagHandler {

class Code

override fun handleTag(opening: Boolean, tag: String, output: Editable, xmlReader: XMLReader) {
when (tag) {
"code" -> {
if (opening) {
start(output as SpannableStringBuilder, Code())
} else {
end(
output as SpannableStringBuilder,
Code::class.java,
TypefaceSpan("monospace")
)
}
}
}
}

/** @return the last span in [text] of type [kind], or null if that kind is not in text */
protected fun <T> getLast(text: Spanned, kind: Class<T>): Any? {
val spans = text.getSpans(0, text.length, kind)
return spans?.get(spans.size - 1)
}

/**
* Mark the start of a span of [text] with [mark] so it can be discovered later by [end].
*/
protected fun start(text: SpannableStringBuilder, mark: Any) {
val len = text.length
text.setSpan(mark, len, len, Spannable.SPAN_MARK_MARK)
}

/**
* Set a [span] over the [text] from the point recently marked with [mark] to the end
* of the text.
*/
protected fun <T> end(text: SpannableStringBuilder, mark: Class<T>, span: Any) {
val len = text.length
val obj = getLast(text, mark)
val where = text.getSpanStart(obj)
text.removeSpan(obj)
if (where != len) {
text.setSpan(span, where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
}
}
}