From 2262cd4b6d08bc5644a45e557b00fb51caf717f5 Mon Sep 17 00:00:00 2001 From: Nick Hu Date: Wed, 8 Sep 2021 17:01:12 +0100 Subject: [PATCH] Gate LaTeX maths behind labs flag --- .../app/features/html/EventHtmlRenderer.kt | 37 +++++++++++-------- .../features/settings/VectorPreferences.kt | 5 +++ vector/src/main/res/values/strings.xml | 1 + .../src/main/res/xml/vector_settings_labs.xml | 5 +++ 4 files changed, 33 insertions(+), 15 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/html/EventHtmlRenderer.kt b/vector/src/main/java/im/vector/app/features/html/EventHtmlRenderer.kt index e2fb385ae5..5c950b43e9 100644 --- a/vector/src/main/java/im/vector/app/features/html/EventHtmlRenderer.kt +++ b/vector/src/main/java/im/vector/app/features/html/EventHtmlRenderer.kt @@ -20,6 +20,7 @@ import android.content.Context import android.text.Spannable import androidx.core.text.toSpannable import im.vector.app.core.resources.ColorProvider +import im.vector.app.features.settings.VectorPreferences import io.noties.markwon.AbstractMarkwonPlugin import io.noties.markwon.Markwon import io.noties.markwon.MarkwonPlugin @@ -34,27 +35,33 @@ import javax.inject.Singleton @Singleton class EventHtmlRenderer @Inject constructor(htmlConfigure: MatrixHtmlPluginConfigure, - context: Context) { + context: Context, + private val vectorPreferences: VectorPreferences) { interface PostProcessor { fun afterRender(renderedText: Spannable) } - private val markwon = Markwon.builder(context) + private val builder = Markwon.builder(context) .usePlugin(HtmlPlugin.create(htmlConfigure)) - .usePlugin(object : AbstractMarkwonPlugin() { // Markwon expects maths to be in a specific format: https://noties.io/Markwon/docs/v4/ext-latex - override fun processMarkdown(markdown: String): String { - return markdown - .replace(Regex(""".*?""")) { matchResult -> "$$" + matchResult.groupValues[1] + "$$" } - .replace(Regex(""".*?""")) { matchResult -> "\n$$\n" + matchResult.groupValues[1] + "\n$$\n" } - } - }) - .usePlugin(MarkwonInlineParserPlugin.create()) - .usePlugin(JLatexMathPlugin.create(44F) { builder -> - builder.inlinesEnabled(true) - builder.theme().inlinePadding(JLatexMathTheme.Padding.symmetric(24, 8)) - }) - .build() + + private val markwon = if (vectorPreferences.latexMathsIsEnabled()) { + builder + .usePlugin(object : AbstractMarkwonPlugin() { // Markwon expects maths to be in a specific format: https://noties.io/Markwon/docs/v4/ext-latex + override fun processMarkdown(markdown: String): String { + return markdown + .replace(Regex(""".*?""")) { matchResult -> "$$" + matchResult.groupValues[1] + "$$" } + .replace(Regex(""".*?""")) { matchResult -> "\n$$\n" + matchResult.groupValues[1] + "\n$$\n" } + } + }) + .usePlugin(MarkwonInlineParserPlugin.create()) + .usePlugin(JLatexMathPlugin.create(44F) { builder -> + builder.inlinesEnabled(true) + builder.theme().inlinePadding(JLatexMathTheme.Padding.symmetric(24, 8)) + }) + } else { + builder + }.build() val plugins: List = markwon.plugins diff --git a/vector/src/main/java/im/vector/app/features/settings/VectorPreferences.kt b/vector/src/main/java/im/vector/app/features/settings/VectorPreferences.kt index 0b7b495f48..1e673e7b4f 100755 --- a/vector/src/main/java/im/vector/app/features/settings/VectorPreferences.kt +++ b/vector/src/main/java/im/vector/app/features/settings/VectorPreferences.kt @@ -100,6 +100,7 @@ class VectorPreferences @Inject constructor(private val context: Context) { private const val SETTINGS_SEND_MESSAGE_WITH_ENTER = "SETTINGS_SEND_MESSAGE_WITH_ENTER" private const val SETTINGS_ENABLE_CHAT_EFFECTS = "SETTINGS_ENABLE_CHAT_EFFECTS" private const val SETTINGS_SHOW_EMOJI_KEYBOARD = "SETTINGS_SHOW_EMOJI_KEYBOARD" + private const val SETTINGS_LABS_ENABLE_LATEX_MATHS = "SETTINGS_LABS_ENABLE_LATEX_MATHS" // Room directory private const val SETTINGS_ROOM_DIRECTORY_SHOW_ALL_PUBLIC_ROOMS = "SETTINGS_ROOM_DIRECTORY_SHOW_ALL_PUBLIC_ROOMS" @@ -337,6 +338,10 @@ class VectorPreferences @Inject constructor(private val context: Context) { return defaultPrefs.getBoolean(SETTINGS_LABS_UNREAD_NOTIFICATIONS_AS_TAB, false) } + fun latexMathsIsEnabled(): Boolean { + return defaultPrefs.getBoolean(SETTINGS_LABS_ENABLE_LATEX_MATHS, false) + } + fun failFast(): Boolean { return BuildConfig.DEBUG || (developerMode() && defaultPrefs.getBoolean(SETTINGS_DEVELOPER_MODE_FAIL_FAST_PREFERENCE_KEY, false)) } diff --git a/vector/src/main/res/values/strings.xml b/vector/src/main/res/values/strings.xml index 453b5ba432..f96320175a 100644 --- a/vector/src/main/res/values/strings.xml +++ b/vector/src/main/res/values/strings.xml @@ -3595,4 +3595,5 @@ Link this email with your account %s in Settings to receive invites directly in Element. + Enable LaTeX mathematics diff --git a/vector/src/main/res/xml/vector_settings_labs.xml b/vector/src/main/res/xml/vector_settings_labs.xml index 6260f65fd9..e7f06bc3b3 100644 --- a/vector/src/main/res/xml/vector_settings_labs.xml +++ b/vector/src/main/res/xml/vector_settings_labs.xml @@ -50,6 +50,11 @@ android:key="SETTINGS_LABS_USE_RESTRICTED_JOIN_RULE" android:title="@string/labs_use_restricted_join_rule" android:summary="@string/labs_use_restricted_join_rule_desc"/> + +