Преглед изворни кода

add syntax highlighting to markdown rendering
* bump gradle memory due to GC mem limit during code generation (gradle.properties)

Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>

Andy Scherzinger пре 5 година
родитељ
комит
bf43a798df
2 измењених фајлова са 23 додато и 3 уклоњено
  1. 7 1
      build.gradle
  2. 16 2
      src/main/java/com/owncloud/android/ui/preview/PreviewTextFragment.java

+ 7 - 1
build.gradle

@@ -48,6 +48,7 @@ configurations {
         exclude group: 'com.google.firebase', module: 'firebase-core'
         exclude group: 'com.google.firebase', module: 'firebase-analytics'
         exclude group: 'com.google.firebase', module: 'firebase-measurement-connector'
+        exclude group: 'org.jetbrains', module: 'annotations-java5' // via prism4j, already using annotations explicitely
 
         // check for updates every build
         resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
@@ -57,7 +58,8 @@ configurations {
 ext {
     jacocoVersion = "0.8.2"
     daggerVersion = "2.25.2"
-    markwonVersion =  '4.1.2'
+    markwonVersion =  "4.1.2"
+    prismVersion = "2.0.0"
     androidLibraryVersion = "master-SNAPSHOT"
 
     travisBuild = System.getenv("TRAVIS") == "true"
@@ -327,6 +329,10 @@ dependencies {
     implementation ("io.noties.markwon:html:$markwonVersion")
     implementation ("io.noties.markwon:linkify:$markwonVersion")
 
+    implementation ("io.noties.markwon:syntax-highlight:$markwonVersion")
+    implementation ("io.noties:prism4j:${prismVersion}")
+    kapt "io.noties:prism4j-bundler:${prismVersion}"
+
     // dependencies for local unit tests
     testImplementation 'junit:junit:4.12'
     testImplementation 'org.mockito:mockito-core:3.1.0'

+ 16 - 2
src/main/java/com/owncloud/android/ui/preview/PreviewTextFragment.java

@@ -73,7 +73,19 @@ import io.noties.markwon.ext.strikethrough.StrikethroughPlugin;
 import io.noties.markwon.ext.tables.TablePlugin;
 import io.noties.markwon.ext.tasklist.TaskListPlugin;
 import io.noties.markwon.html.HtmlPlugin;
-
+import io.noties.markwon.syntax.Prism4jTheme;
+import io.noties.markwon.syntax.Prism4jThemeDefault;
+import io.noties.markwon.syntax.SyntaxHighlightPlugin;
+import io.noties.prism4j.Prism4j;
+import io.noties.prism4j.annotations.PrismBundle;
+
+@PrismBundle(
+    include = {
+        "c", "clike", "clojure", "cpp", "csharp", "css", "dart", "git", "go", "groovy", "java", "javascript", "json",
+        "kotlin", "latex", "makefile", "markdown", "markup", "python", "scala", "sql", "swift", "yaml"
+    },
+    grammarLocatorClassName = ".MarkwonGrammarLocator"
+)
 public class PreviewTextFragment extends FileFragment implements SearchView.OnQueryTextListener, Injectable {
     private static final String EXTRA_FILE = "FILE";
     private static final String EXTRA_ACCOUNT = "ACCOUNT";
@@ -123,7 +135,6 @@ public class PreviewTextFragment extends FileFragment implements SearchView.OnQu
         super.onCreateView(inflater, container, savedInstanceState);
         Log_OC.e(TAG, "onCreateView");
 
-
         View ret = inflater.inflate(R.layout.text_file_preview, container, false);
         mTextPreview = ret.findViewById(R.id.text_preview);
 
@@ -259,11 +270,14 @@ public class PreviewTextFragment extends FileFragment implements SearchView.OnQu
     }
 
     private Spanned getRenderedMarkdownText(Context context, String markdown) {
+        Prism4j prism4j = new Prism4j(new MarkwonGrammarLocator());
+        Prism4jTheme prism4jTheme = Prism4jThemeDefault.create();
         final Markwon markwon = Markwon.builder(context)
             .usePlugin(TablePlugin.create(context))
             .usePlugin(TaskListPlugin.create(context))
             .usePlugin(StrikethroughPlugin.create())
             .usePlugin(HtmlPlugin.create())
+            .usePlugin(SyntaxHighlightPlugin.create(prism4j, prism4jTheme))
             .build();
 
         return markwon.toMarkdown(markdown);