Browse Source

Merge pull request #4701 from nextcloud/markdownPreview_SyntaxHighlighting

Markdown preview syntax highlighting
Andy Scherzinger 5 years ago
parent
commit
8a3ea78a55

+ 14 - 8
build.gradle

@@ -48,6 +48,7 @@ configurations {
         exclude group: 'com.google.firebase', module: 'firebase-core'
         exclude group: 'com.google.firebase', module: 'firebase-core'
         exclude group: 'com.google.firebase', module: 'firebase-analytics'
         exclude group: 'com.google.firebase', module: 'firebase-analytics'
         exclude group: 'com.google.firebase', module: 'firebase-measurement-connector'
         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
         // check for updates every build
         resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
         resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
@@ -57,7 +58,8 @@ configurations {
 ext {
 ext {
     jacocoVersion = "0.8.2"
     jacocoVersion = "0.8.2"
     daggerVersion = "2.25.2"
     daggerVersion = "2.25.2"
-    markwonVersion =  '4.1.2'
+    markwonVersion =  "4.1.2"
+    prismVersion = "2.0.0"
     androidLibraryVersion = "master-SNAPSHOT"
     androidLibraryVersion = "master-SNAPSHOT"
 
 
     travisBuild = System.getenv("TRAVIS") == "true"
     travisBuild = System.getenv("TRAVIS") == "true"
@@ -319,13 +321,17 @@ dependencies {
     implementation 'org.conscrypt:conscrypt-android:2.2.1'
     implementation 'org.conscrypt:conscrypt-android:2.2.1'
 
 
     // dependencies for markdown rendering
     // dependencies for markdown rendering
-    implementation ("io.noties.markwon:core:$markwonVersion")
-    implementation ("io.noties.markwon:ext-latex:$markwonVersion")
-    implementation ("io.noties.markwon:ext-strikethrough:$markwonVersion")
-    implementation ("io.noties.markwon:ext-tables:$markwonVersion")
-    implementation ("io.noties.markwon:ext-tasklist:$markwonVersion")
-    implementation ("io.noties.markwon:html:$markwonVersion")
-    implementation ("io.noties.markwon:linkify:$markwonVersion")
+    implementation "io.noties.markwon:core:$markwonVersion"
+    implementation "io.noties.markwon:ext-latex:$markwonVersion"
+    implementation "io.noties.markwon:ext-strikethrough:$markwonVersion"
+    implementation "io.noties.markwon:ext-tables:$markwonVersion"
+    implementation "io.noties.markwon:ext-tasklist:$markwonVersion"
+    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
     // dependencies for local unit tests
     testImplementation 'junit:junit:4.12'
     testImplementation 'junit:junit:4.12'

+ 1 - 1
scripts/analysis/findbugs-results.txt

@@ -1 +1 @@
-421
+421

+ 11 - 0
spotbugs-filter.xml

@@ -34,6 +34,17 @@
         </Or>
         </Or>
     </Match>
     </Match>
 
 
+	<!-- Third-party library code -->
+	<Match>
+	    <Or>
+	        <Package name="~io\.noties\..*" />
+			<Package name="~third_parties\.ezvcard_android\..*" />
+		</Or>
+	</Match>
+	 <Match>
+         <Class name="~com\.owncloud\.android\.ui\.preview\.MarkwonGrammarLocator.*" />
+    </Match>
+
     <Bug pattern="PATH_TRAVERSAL_IN" />
     <Bug pattern="PATH_TRAVERSAL_IN" />
     <Bug pattern="ANDROID_EXTERNAL_FILE_ACCESS" />
     <Bug pattern="ANDROID_EXTERNAL_FILE_ACCESS" />
     <Bug pattern="BAS_BLOATED_ASSIGNMENT_SCOPE" />
     <Bug pattern="BAS_BLOATED_ASSIGNMENT_SCOPE" />

+ 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.tables.TablePlugin;
 import io.noties.markwon.ext.tasklist.TaskListPlugin;
 import io.noties.markwon.ext.tasklist.TaskListPlugin;
 import io.noties.markwon.html.HtmlPlugin;
 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 {
 public class PreviewTextFragment extends FileFragment implements SearchView.OnQueryTextListener, Injectable {
     private static final String EXTRA_FILE = "FILE";
     private static final String EXTRA_FILE = "FILE";
     private static final String EXTRA_ACCOUNT = "ACCOUNT";
     private static final String EXTRA_ACCOUNT = "ACCOUNT";
@@ -123,7 +135,6 @@ public class PreviewTextFragment extends FileFragment implements SearchView.OnQu
         super.onCreateView(inflater, container, savedInstanceState);
         super.onCreateView(inflater, container, savedInstanceState);
         Log_OC.e(TAG, "onCreateView");
         Log_OC.e(TAG, "onCreateView");
 
 
-
         View ret = inflater.inflate(R.layout.text_file_preview, container, false);
         View ret = inflater.inflate(R.layout.text_file_preview, container, false);
         mTextPreview = ret.findViewById(R.id.text_preview);
         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) {
     private Spanned getRenderedMarkdownText(Context context, String markdown) {
+        Prism4j prism4j = new Prism4j(new MarkwonGrammarLocator());
+        Prism4jTheme prism4jTheme = Prism4jThemeDefault.create();
         final Markwon markwon = Markwon.builder(context)
         final Markwon markwon = Markwon.builder(context)
             .usePlugin(TablePlugin.create(context))
             .usePlugin(TablePlugin.create(context))
             .usePlugin(TaskListPlugin.create(context))
             .usePlugin(TaskListPlugin.create(context))
             .usePlugin(StrikethroughPlugin.create())
             .usePlugin(StrikethroughPlugin.create())
             .usePlugin(HtmlPlugin.create())
             .usePlugin(HtmlPlugin.create())
+            .usePlugin(SyntaxHighlightPlugin.create(prism4j, prism4jTheme))
             .build();
             .build();
 
 
         return markwon.toMarkdown(markdown);
         return markwon.toMarkdown(markdown);