Add syntax highlight to sample app
This commit is contained in:
		
							parent
							
								
									efad173606
								
							
						
					
					
						commit
						6e2a064304
					
				| @ -30,6 +30,7 @@ dependencies { | ||||
| 
 | ||||
|     implementation project(':library') | ||||
|     implementation project(':library-image-loader') | ||||
|     implementation project(':library-syntax') | ||||
| 
 | ||||
|     implementation 'ru.noties:debug:3.0.0@jar' | ||||
|     implementation 'me.saket:better-link-movement-method:2.2.0' | ||||
| @ -38,4 +39,7 @@ dependencies { | ||||
| 
 | ||||
|     implementation 'com.google.dagger:dagger:2.10' | ||||
|     annotationProcessor 'com.google.dagger:dagger-compiler:2.10' | ||||
| 
 | ||||
|     implementation PRISM_4J | ||||
|     annotationProcessor PRISM_4J_BUNDLER | ||||
| } | ||||
|  | ||||
| @ -16,8 +16,14 @@ import okhttp3.Cache; | ||||
| import okhttp3.OkHttpClient; | ||||
| import ru.noties.markwon.il.AsyncDrawableLoader; | ||||
| import ru.noties.markwon.spans.AsyncDrawable; | ||||
| import ru.noties.markwon.syntax.Prism4jTheme; | ||||
| import ru.noties.markwon.syntax.Prism4jThemeDefault; | ||||
| import ru.noties.prism4j.Prism4j; | ||||
| import ru.noties.prism4j.annotations.PrismBundle; | ||||
| 
 | ||||
| @Module | ||||
| @PrismBundle(include = {"c", "clojure", "cpp", "csharp", "css", "dart", "git", "go", "java", | ||||
|         "javascript", "json", "kotlin", "latex", "makefile", "markup", "python", "sql", "yaml"}) | ||||
| class AppModule { | ||||
| 
 | ||||
|     private final App app; | ||||
| @ -75,4 +81,16 @@ class AppModule { | ||||
|                 .resources(resources) | ||||
|                 .build(); | ||||
|     } | ||||
| 
 | ||||
|     @Provides | ||||
|     @Singleton | ||||
|     Prism4j prism4j() { | ||||
|         return new Prism4j(new GrammarLocatorDef()); | ||||
|     } | ||||
| 
 | ||||
|     @Singleton | ||||
|     @Provides | ||||
|     Prism4jTheme prism4jTheme() { | ||||
|         return Prism4jThemeDefault.create(); | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -14,6 +14,10 @@ import javax.inject.Inject; | ||||
| 
 | ||||
| import ru.noties.debug.Debug; | ||||
| import ru.noties.markwon.spans.AsyncDrawable; | ||||
| import ru.noties.markwon.spans.SpannableTheme; | ||||
| import ru.noties.markwon.syntax.Prism4jSyntaxHighlight; | ||||
| import ru.noties.markwon.syntax.Prism4jTheme; | ||||
| import ru.noties.prism4j.Prism4j; | ||||
| 
 | ||||
| @ActivityScope | ||||
| public class MarkdownRenderer { | ||||
| @ -31,6 +35,12 @@ public class MarkdownRenderer { | ||||
|     @Inject | ||||
|     Handler handler; | ||||
| 
 | ||||
|     @Inject | ||||
|     Prism4j prism4j; | ||||
| 
 | ||||
|     @Inject | ||||
|     Prism4jTheme prism4jTheme; | ||||
| 
 | ||||
|     private Future<?> task; | ||||
| 
 | ||||
|     @Inject | ||||
| @ -42,7 +52,11 @@ public class MarkdownRenderer { | ||||
|             @Nullable final Uri uri, | ||||
|             @NonNull final String markdown, | ||||
|             @NonNull final MarkdownReadyListener listener) { | ||||
| 
 | ||||
|         // todo: create prism4j theme factory (accepting light/dark argument) | ||||
| 
 | ||||
|         cancel(); | ||||
| 
 | ||||
|         task = service.submit(new Runnable() { | ||||
|             @Override | ||||
|             public void run() { | ||||
| @ -57,6 +71,10 @@ public class MarkdownRenderer { | ||||
|                 final SpannableConfiguration configuration = SpannableConfiguration.builder(context) | ||||
|                         .asyncDrawableLoader(loader) | ||||
|                         .urlProcessor(urlProcessor) | ||||
|                         .syntaxHighlight(Prism4jSyntaxHighlight.create(prism4j, prism4jTheme)) | ||||
|                         .theme(SpannableTheme.builderWithDefaults(context) | ||||
|                                 .codeBackgroundColor(prism4jTheme.background()) | ||||
|                                 .build()) | ||||
|                         .build(); | ||||
| 
 | ||||
|                 final long start = SystemClock.uptimeMillis(); | ||||
|  | ||||
| @ -14,7 +14,7 @@ public class Prism4jSyntaxHighlight implements SyntaxHighlight { | ||||
|     public static Prism4jSyntaxHighlight create( | ||||
|             @NonNull Prism4j prism4j, | ||||
|             @NonNull Prism4jTheme theme) { | ||||
|         return null; | ||||
|         return new Prism4jSyntaxHighlight(prism4j, theme, null); | ||||
|     } | ||||
| 
 | ||||
|     @NonNull | ||||
| @ -22,7 +22,7 @@ public class Prism4jSyntaxHighlight implements SyntaxHighlight { | ||||
|             @NonNull Prism4j prism4j, | ||||
|             @NonNull Prism4jTheme theme, | ||||
|             @Nullable String fallback) { | ||||
|         return null; | ||||
|         return new Prism4jSyntaxHighlight(prism4j, theme, fallback); | ||||
|     } | ||||
| 
 | ||||
|     private final Prism4j prism4j; | ||||
|  | ||||
| @ -1,5 +1,6 @@ | ||||
| package ru.noties.markwon.syntax; | ||||
| 
 | ||||
| import android.support.annotation.ColorInt; | ||||
| import android.support.annotation.NonNull; | ||||
| 
 | ||||
| import ru.noties.markwon.SpannableBuilder; | ||||
| @ -7,6 +8,9 @@ import ru.noties.prism4j.Prism4j; | ||||
| 
 | ||||
| public interface Prism4jTheme { | ||||
| 
 | ||||
|     @ColorInt | ||||
|     int background(); | ||||
| 
 | ||||
|     void apply( | ||||
|             @NonNull String language, | ||||
|             @NonNull Prism4j.Syntax syntax, | ||||
|  | ||||
| @ -64,7 +64,6 @@ public abstract class Prism4jThemeBase implements Prism4jTheme { | ||||
| 
 | ||||
|         final int color = color(language, type, alias); | ||||
|         if (color != 0) { | ||||
|             builder.setSpan(new ForegroundColorSpan(color), start, end); | ||||
|             applyColor(language, type, alias, color, builder, start, end); | ||||
|         } | ||||
|     } | ||||
|  | ||||
| @ -61,4 +61,9 @@ public class Prism4jThemeDefault extends Prism4jThemeBase { | ||||
|             builder.setSpan(new EmphasisSpan(), start, end); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public int background() { | ||||
|         return 0xFFf5f2f0; | ||||
|     } | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Dimitry Ivanov
						Dimitry Ivanov