Add syntax highlight to sample app

This commit is contained in:
Dimitry Ivanov 2018-07-16 17:51:08 +03:00
parent efad173606
commit 6e2a064304
7 changed files with 51 additions and 3 deletions

View File

@ -30,6 +30,7 @@ dependencies {
implementation project(':library') implementation project(':library')
implementation project(':library-image-loader') implementation project(':library-image-loader')
implementation project(':library-syntax')
implementation 'ru.noties:debug:3.0.0@jar' implementation 'ru.noties:debug:3.0.0@jar'
implementation 'me.saket:better-link-movement-method:2.2.0' implementation 'me.saket:better-link-movement-method:2.2.0'
@ -38,4 +39,7 @@ dependencies {
implementation 'com.google.dagger:dagger:2.10' implementation 'com.google.dagger:dagger:2.10'
annotationProcessor 'com.google.dagger:dagger-compiler:2.10' annotationProcessor 'com.google.dagger:dagger-compiler:2.10'
implementation PRISM_4J
annotationProcessor PRISM_4J_BUNDLER
} }

View File

@ -16,8 +16,14 @@ import okhttp3.Cache;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
import ru.noties.markwon.il.AsyncDrawableLoader; import ru.noties.markwon.il.AsyncDrawableLoader;
import ru.noties.markwon.spans.AsyncDrawable; 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 @Module
@PrismBundle(include = {"c", "clojure", "cpp", "csharp", "css", "dart", "git", "go", "java",
"javascript", "json", "kotlin", "latex", "makefile", "markup", "python", "sql", "yaml"})
class AppModule { class AppModule {
private final App app; private final App app;
@ -75,4 +81,16 @@ class AppModule {
.resources(resources) .resources(resources)
.build(); .build();
} }
@Provides
@Singleton
Prism4j prism4j() {
return new Prism4j(new GrammarLocatorDef());
}
@Singleton
@Provides
Prism4jTheme prism4jTheme() {
return Prism4jThemeDefault.create();
}
} }

View File

@ -14,6 +14,10 @@ import javax.inject.Inject;
import ru.noties.debug.Debug; import ru.noties.debug.Debug;
import ru.noties.markwon.spans.AsyncDrawable; 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 @ActivityScope
public class MarkdownRenderer { public class MarkdownRenderer {
@ -31,6 +35,12 @@ public class MarkdownRenderer {
@Inject @Inject
Handler handler; Handler handler;
@Inject
Prism4j prism4j;
@Inject
Prism4jTheme prism4jTheme;
private Future<?> task; private Future<?> task;
@Inject @Inject
@ -42,7 +52,11 @@ public class MarkdownRenderer {
@Nullable final Uri uri, @Nullable final Uri uri,
@NonNull final String markdown, @NonNull final String markdown,
@NonNull final MarkdownReadyListener listener) { @NonNull final MarkdownReadyListener listener) {
// todo: create prism4j theme factory (accepting light/dark argument)
cancel(); cancel();
task = service.submit(new Runnable() { task = service.submit(new Runnable() {
@Override @Override
public void run() { public void run() {
@ -57,6 +71,10 @@ public class MarkdownRenderer {
final SpannableConfiguration configuration = SpannableConfiguration.builder(context) final SpannableConfiguration configuration = SpannableConfiguration.builder(context)
.asyncDrawableLoader(loader) .asyncDrawableLoader(loader)
.urlProcessor(urlProcessor) .urlProcessor(urlProcessor)
.syntaxHighlight(Prism4jSyntaxHighlight.create(prism4j, prism4jTheme))
.theme(SpannableTheme.builderWithDefaults(context)
.codeBackgroundColor(prism4jTheme.background())
.build())
.build(); .build();
final long start = SystemClock.uptimeMillis(); final long start = SystemClock.uptimeMillis();

View File

@ -14,7 +14,7 @@ public class Prism4jSyntaxHighlight implements SyntaxHighlight {
public static Prism4jSyntaxHighlight create( public static Prism4jSyntaxHighlight create(
@NonNull Prism4j prism4j, @NonNull Prism4j prism4j,
@NonNull Prism4jTheme theme) { @NonNull Prism4jTheme theme) {
return null; return new Prism4jSyntaxHighlight(prism4j, theme, null);
} }
@NonNull @NonNull
@ -22,7 +22,7 @@ public class Prism4jSyntaxHighlight implements SyntaxHighlight {
@NonNull Prism4j prism4j, @NonNull Prism4j prism4j,
@NonNull Prism4jTheme theme, @NonNull Prism4jTheme theme,
@Nullable String fallback) { @Nullable String fallback) {
return null; return new Prism4jSyntaxHighlight(prism4j, theme, fallback);
} }
private final Prism4j prism4j; private final Prism4j prism4j;

View File

@ -1,5 +1,6 @@
package ru.noties.markwon.syntax; package ru.noties.markwon.syntax;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import ru.noties.markwon.SpannableBuilder; import ru.noties.markwon.SpannableBuilder;
@ -7,6 +8,9 @@ import ru.noties.prism4j.Prism4j;
public interface Prism4jTheme { public interface Prism4jTheme {
@ColorInt
int background();
void apply( void apply(
@NonNull String language, @NonNull String language,
@NonNull Prism4j.Syntax syntax, @NonNull Prism4j.Syntax syntax,

View File

@ -64,7 +64,6 @@ public abstract class Prism4jThemeBase implements Prism4jTheme {
final int color = color(language, type, alias); final int color = color(language, type, alias);
if (color != 0) { if (color != 0) {
builder.setSpan(new ForegroundColorSpan(color), start, end);
applyColor(language, type, alias, color, builder, start, end); applyColor(language, type, alias, color, builder, start, end);
} }
} }

View File

@ -61,4 +61,9 @@ public class Prism4jThemeDefault extends Prism4jThemeBase {
builder.setSpan(new EmphasisSpan(), start, end); builder.setSpan(new EmphasisSpan(), start, end);
} }
} }
@Override
public int background() {
return 0xFFf5f2f0;
}
} }