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

KaTeX rendering #1104

Merged
merged 1 commit into from
Nov 26, 2020
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
21 changes: 21 additions & 0 deletions app/src/main/assets/katex/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2013-2020 Khan Academy and other contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 1 addition & 0 deletions app/src/main/assets/katex/contrib/auto-render.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions app/src/main/assets/katex/katex.min.css

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions app/src/main/assets/katex/katex.min.js

Large diffs are not rendered by default.

45 changes: 45 additions & 0 deletions app/src/main/java/fr/gaulupeau/apps/Poche/data/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
import android.util.Log;
import android.view.KeyEvent;

import androidx.core.util.Supplier;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import fr.gaulupeau.apps.InThePoche.R;
import fr.gaulupeau.apps.Poche.App;
import fr.gaulupeau.apps.Poche.network.ConnectivityChangeReceiver;
Expand Down Expand Up @@ -217,6 +223,27 @@ public void setFloat(int keyResourceID, float value) {
setFloat(context.getString(keyResourceID), value);
}

public Set<String> getStringSet(String key, Set<String> defValues) {
return pref.getStringSet(key, defValues);
}

public Set<String> getStringSet(int keyResourceID, Set<String> defValues) {
return pref.getStringSet(context.getString(keyResourceID), defValues);
}

public Set<String> getStringSet(int keyResourceID, Supplier<Set<String>> defValueSupplier) {
Set<String> set = getStringSet(keyResourceID, (Set<String>) null);
return set != null ? set : defValueSupplier.get();
}

public void setStringSet(String key, Set<String> values) {
pref.edit().putStringSet(key, values).apply();
}

public void setStringSet(int keyResourceID, Set<String> values) {
setStringSet(context.getString(keyResourceID), values);
}

public String getUrl() {
return getString(R.string.pref_key_connection_url);
}
Expand Down Expand Up @@ -458,6 +485,24 @@ public void setOnyxWorkaroundEnabled(boolean value) {
setBoolean(R.string.pref_key_ui_onyxworkaround_enabled, value);
}

public boolean isMathRenderingEnabled() {
return getBoolean(R.string.pref_key_ui_mathRendering_enabled, false);
}

public void setMathRenderingEnabled(boolean value) {
setBoolean(R.string.pref_key_ui_mathRendering_enabled, value);
}

public Set<String> getMathRenderingDelimiters() {
return getStringSet(R.string.pref_key_ui_mathRendering_delimiters, () ->
new HashSet<>(Arrays.asList(context.getResources().getStringArray(
R.array.pref_option_mathRendering_delimiters_defaultValues))));
}

public void setMathRenderingDelimiters(Set<String> values) {
setStringSet(R.string.pref_key_ui_mathRendering_delimiters, values);
}

public boolean isTapToScrollEnabled() {
return getBoolean(R.string.pref_key_ui_tapToScroll_enabled, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,11 @@ private String getExtraHead() {
"\t\t<script src=\"onyx-style-workaround.js\"></script>";
}

if (settings.isMathRenderingEnabled()) {
String delimiters = TextUtils.join(",", settings.getMathRenderingDelimiters());
extra += String.format(StorageHelper.readRawString(R.raw.katex_part), delimiters);
}

return extra;
}

Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/raw/katex_part.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<link rel="stylesheet" href="katex/katex.min.css">
<script defer src="katex/katex.min.js"></script>
<script defer src="katex/contrib/auto-render.min.js"></script>
<script>
document.addEventListener("DOMContentLoaded", function() {
renderMathInElement(document.body, {delimiters: [%1$s]});
});
</script>
1 change: 1 addition & 0 deletions app/src/main/res/raw/webview_htmlbase.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
Expand Down
17 changes: 17 additions & 0 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,21 @@
<item>XML</item>
<item>CSV</item>
</string-array>
<string-array name="pref_option_mathRendering_delimiters" translatable="false">
<item>\\[ … \\]</item>
<item>$$ … $$</item>
<item>\\( … \\)</item>
<item>$ … $</item>
</string-array>
<string-array name="pref_option_mathRendering_delimiters_values" translatable="false">
<item>{left: \"\\\\[\", right: \"\\\\]\", display: true}</item>
<item>{left: \"$$\", right: \"$$\", display: true}</item>
<item>{left: \"\\\\(\", right: \"\\\\)\", display: false}</item>
<item>{left: \"$\", right: \"$\", display: false}</item>
</string-array>
<string-array name="pref_option_mathRendering_delimiters_defaultValues" translatable="false">
<item>{left: \"\\\\[\", right: \"\\\\]\", display: true}</item>
<item>{left: \"$$\", right: \"$$\", display: true}</item>
<item>{left: \"\\\\(\", right: \"\\\\)\", display: false}</item>
</string-array>
</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings-preference-keys.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
<string name="pref_key_ui_previewImage_enabled" translatable="false">ui.previewImage.enabled</string>
<string name="pref_key_ui_annotations_enabled" translatable="false">ui.annotations.enabled</string>
<string name="pref_key_ui_onyxworkaround_enabled" translatable="false">ui.onyxworkaround.enabled</string>
<string name="pref_key_ui_mathRendering_enabled" translatable="false">ui.mathRendering.enabled</string>
<string name="pref_key_ui_mathRendering_delimiters" translatable="false">ui.mathRendering.delimiters</string>

<string name="pref_key_tts" translatable="false">tts</string>
<string name="pref_key_tts_category" translatable="false">tts.category</string>
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@
<string name="pref_desc_ui_previewImage_enabled">Display the preview image before the article</string>
<string name="pref_name_ui_annotations_enabled">Annotations support</string>
<string name="pref_desc_ui_annotations_enabled">Experimental feature! See, edit and create annotations</string>
<string name="pref_name_ui_mathRendering_enabled">Math rendering</string>
<string name="pref_desc_ui_mathRendering_enabled">Enable LaTeX-math rendering via KaTeX</string>
<string name="pref_name_ui_mathRendering_delimiters">Math delimiters</string>
<string name="pref_desc_ui_mathRendering_delimiters">Select the environment delimiters that KaTeX should process</string>

<string name="pref_categoryName_tts">Text-to-speech</string>
<string name="pref_name_tts_fastForwardTime">Fast-forward skip time</string>
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,19 @@
android:key="@string/pref_key_ui_annotations_enabled"
android:summary="@string/pref_desc_ui_annotations_enabled"
android:title="@string/pref_name_ui_annotations_enabled" />
<CheckBoxPreference
android:key="@string/pref_key_ui_mathRendering_enabled"
android:title="@string/pref_name_ui_mathRendering_enabled"
android:summary="@string/pref_desc_ui_mathRendering_enabled"
android:defaultValue="false"/>
<MultiSelectListPreference
android:key="@string/pref_key_ui_mathRendering_delimiters"
android:title="@string/pref_name_ui_mathRendering_delimiters"
android:summary="@string/pref_desc_ui_mathRendering_delimiters"
android:dialogTitle="@string/pref_name_ui_mathRendering_delimiters"
android:entries="@array/pref_option_mathRendering_delimiters"
android:entryValues="@array/pref_option_mathRendering_delimiters_values"
android:defaultValue="@array/pref_option_mathRendering_delimiters_defaultValues"/>
</PreferenceCategory>
</PreferenceScreen>
<PreferenceScreen
Expand Down