Add darkula theme to syntax highlight
This commit is contained in:
parent
f7d58bccc8
commit
65289867c4
@ -16,7 +16,7 @@ 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.Prism4jThemeDarkula;
|
||||||
import ru.noties.markwon.syntax.Prism4jThemeDefault;
|
import ru.noties.markwon.syntax.Prism4jThemeDefault;
|
||||||
import ru.noties.prism4j.Prism4j;
|
import ru.noties.prism4j.Prism4j;
|
||||||
import ru.noties.prism4j.annotations.PrismBundle;
|
import ru.noties.prism4j.annotations.PrismBundle;
|
||||||
@ -90,7 +90,13 @@ class AppModule {
|
|||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
@Provides
|
@Provides
|
||||||
Prism4jTheme prism4jTheme() {
|
Prism4jThemeDefault prism4jThemeDefault() {
|
||||||
return Prism4jThemeDefault.create();
|
return Prism4jThemeDefault.create();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Singleton
|
||||||
|
@Provides
|
||||||
|
Prism4jThemeDarkula prism4jThemeDarkula() {
|
||||||
|
return Prism4jThemeDarkula.create();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -64,7 +64,7 @@ public class MainActivity extends Activity {
|
|||||||
markdownLoader.load(uri(), new MarkdownLoader.OnMarkdownTextLoaded() {
|
markdownLoader.load(uri(), new MarkdownLoader.OnMarkdownTextLoaded() {
|
||||||
@Override
|
@Override
|
||||||
public void apply(final String text) {
|
public void apply(final String text) {
|
||||||
markdownRenderer.render(MainActivity.this, uri(), text, new MarkdownRenderer.MarkdownReadyListener() {
|
markdownRenderer.render(MainActivity.this, themes.isLight(), uri(), text, new MarkdownRenderer.MarkdownReadyListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onMarkdownReady(CharSequence markdown) {
|
public void onMarkdownReady(CharSequence markdown) {
|
||||||
Markwon.setText(textView, markdown, BetterLinkMovementMethod.getInstance());
|
Markwon.setText(textView, markdown, BetterLinkMovementMethod.getInstance());
|
||||||
|
@ -15,8 +15,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.spans.SpannableTheme;
|
||||||
|
import ru.noties.markwon.syntax.Prism4jThemeDarkula;
|
||||||
import ru.noties.markwon.syntax.Prism4jSyntaxHighlight;
|
import ru.noties.markwon.syntax.Prism4jSyntaxHighlight;
|
||||||
import ru.noties.markwon.syntax.Prism4jTheme;
|
import ru.noties.markwon.syntax.Prism4jTheme;
|
||||||
|
import ru.noties.markwon.syntax.Prism4jThemeDefault;
|
||||||
import ru.noties.prism4j.Prism4j;
|
import ru.noties.prism4j.Prism4j;
|
||||||
|
|
||||||
@ActivityScope
|
@ActivityScope
|
||||||
@ -39,7 +41,10 @@ public class MarkdownRenderer {
|
|||||||
Prism4j prism4j;
|
Prism4j prism4j;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
Prism4jTheme prism4jTheme;
|
Prism4jThemeDefault prism4jThemeDefault;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
Prism4jThemeDarkula prism4JThemeDarkula;
|
||||||
|
|
||||||
private Future<?> task;
|
private Future<?> task;
|
||||||
|
|
||||||
@ -49,6 +54,7 @@ public class MarkdownRenderer {
|
|||||||
|
|
||||||
public void render(
|
public void render(
|
||||||
@NonNull final Context context,
|
@NonNull final Context context,
|
||||||
|
final boolean isLightTheme,
|
||||||
@Nullable final Uri uri,
|
@Nullable final Uri uri,
|
||||||
@NonNull final String markdown,
|
@NonNull final String markdown,
|
||||||
@NonNull final MarkdownReadyListener listener) {
|
@NonNull final MarkdownReadyListener listener) {
|
||||||
@ -68,12 +74,20 @@ public class MarkdownRenderer {
|
|||||||
urlProcessor = new UrlProcessorRelativeToAbsolute(uri.toString());
|
urlProcessor = new UrlProcessorRelativeToAbsolute(uri.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final Prism4jTheme prism4jTheme = isLightTheme
|
||||||
|
? prism4jThemeDefault
|
||||||
|
: prism4JThemeDarkula;
|
||||||
|
|
||||||
|
final int background = isLightTheme
|
||||||
|
? prism4jTheme.background()
|
||||||
|
: 0x0Fffffff;
|
||||||
|
|
||||||
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))
|
.syntaxHighlight(Prism4jSyntaxHighlight.create(prism4j, prism4jTheme))
|
||||||
.theme(SpannableTheme.builderWithDefaults(context)
|
.theme(SpannableTheme.builderWithDefaults(context)
|
||||||
.codeBackgroundColor(prism4jTheme.background())
|
.codeBackgroundColor(background)
|
||||||
.codeTextColor(prism4jTheme.textColor())
|
.codeTextColor(prism4jTheme.textColor())
|
||||||
.build())
|
.build())
|
||||||
.build();
|
.build();
|
||||||
|
@ -43,4 +43,8 @@ public class Themes {
|
|||||||
.putBoolean(KEY_THEME_DARK, newValue)
|
.putBoolean(KEY_THEME_DARK, newValue)
|
||||||
.apply();
|
.apply();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isLight() {
|
||||||
|
return !preferences.getBoolean(KEY_THEME_DARK, false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,61 @@
|
|||||||
|
package ru.noties.markwon.syntax;
|
||||||
|
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
|
import android.support.annotation.Nullable;
|
||||||
|
import android.text.SpannableStringBuilder;
|
||||||
|
import android.text.Spanned;
|
||||||
|
|
||||||
|
import ru.noties.markwon.spans.EmphasisSpan;
|
||||||
|
import ru.noties.markwon.spans.StrongEmphasisSpan;
|
||||||
|
|
||||||
|
public class Prism4jThemeDarkula extends Prism4jThemeBase {
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
public static Prism4jThemeDarkula create() {
|
||||||
|
return new Prism4jThemeDarkula();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int background() {
|
||||||
|
return 0xFF2d2d2d;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int textColor() {
|
||||||
|
return 0xFFa9b7c6;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
@Override
|
||||||
|
protected ColorHashMap init() {
|
||||||
|
return new ColorHashMap()
|
||||||
|
.add(0xFF808080, "comment", "prolog", "cdata")
|
||||||
|
.add(0xFFcc7832, "delimiter", "boolean", "keyword", "selector", "important", "atrule")
|
||||||
|
.add(0xFFa9b7c6, "operator", "punctuation", "attr-name")
|
||||||
|
.add(0xFFe8bf6a, "tag", "doctype", "builtin")
|
||||||
|
.add(0xFF6897bb, "entity", "number", "symbol")
|
||||||
|
.add(0xFF9876aa, "property", "constant", "variable")
|
||||||
|
.add(0xFF6a8759, "string", "char")
|
||||||
|
.add(0xFFbbb438, "annotation")
|
||||||
|
.add(0xFFa5c261, "attr-value")
|
||||||
|
.add(0xFF287bde, "url")
|
||||||
|
.add(0xFFffc66d, "function")
|
||||||
|
.add(0xFF364135, "regex")
|
||||||
|
.add(0xFF294436, "inserted")
|
||||||
|
.add(0xFF484a4a, "deleted");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void applyColor(@NonNull String language, @NonNull String type, @Nullable String alias, int color, @NonNull SpannableStringBuilder builder, int start, int end) {
|
||||||
|
super.applyColor(language, type, alias, color, builder, start, end);
|
||||||
|
|
||||||
|
if (isOfType("important", type, alias)
|
||||||
|
|| isOfType("bold", type, alias)) {
|
||||||
|
builder.setSpan(new StrongEmphasisSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isOfType("italic", type, alias)) {
|
||||||
|
builder.setSpan(new EmphasisSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -17,6 +17,16 @@ public class Prism4jThemeDefault extends Prism4jThemeBase {
|
|||||||
return new Prism4jThemeDefault();
|
return new Prism4jThemeDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int background() {
|
||||||
|
return 0xFFf5f2f0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int textColor() {
|
||||||
|
return 0xdd000000;
|
||||||
|
}
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
@Override
|
@Override
|
||||||
protected ColorHashMap init() {
|
protected ColorHashMap init() {
|
||||||
@ -62,14 +72,4 @@ public class Prism4jThemeDefault extends Prism4jThemeBase {
|
|||||||
builder.setSpan(new EmphasisSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
builder.setSpan(new EmphasisSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public int background() {
|
|
||||||
return 0xFFf5f2f0;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int textColor() {
|
|
||||||
return 0xFF000000;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user