Add HtmlConfigure for HtmlPlugin

This commit is contained in:
Dimitry Ivanov 2019-06-04 23:44:52 +03:00
parent 4b918bf094
commit 79b99abb24
3 changed files with 91 additions and 21 deletions

View File

@ -27,11 +27,29 @@ import ru.noties.markwon.html.tag.UnderlineHandler;
*/ */
public class HtmlPlugin extends AbstractMarkwonPlugin { public class HtmlPlugin extends AbstractMarkwonPlugin {
/**
* @see #create(HtmlConfigure)
* @since 4.0.0-SNAPSHOT
*/
public interface HtmlConfigure {
void configureHtml(@NonNull HtmlPlugin plugin);
}
@NonNull @NonNull
public static HtmlPlugin create() { public static HtmlPlugin create() {
return new HtmlPlugin(); return new HtmlPlugin();
} }
/**
* @since 4.0.0-SNAPSHOT
*/
@NonNull
public static HtmlPlugin create(@NonNull HtmlConfigure configure) {
final HtmlPlugin plugin = create();
configure.configureHtml(plugin);
return plugin;
}
public static final float SCRIPT_DEF_TEXT_SIZE_RATIO = .75F; public static final float SCRIPT_DEF_TEXT_SIZE_RATIO = .75F;
private final MarkwonHtmlRendererImpl.Builder builder; private final MarkwonHtmlRendererImpl.Builder builder;

View File

@ -6,20 +6,33 @@ import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.text.Layout;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.style.AlignmentSpan;
import android.text.style.ForegroundColorSpan; import android.text.style.ForegroundColorSpan;
import android.widget.TextView; import android.widget.TextView;
import org.commonmark.node.Heading; import org.commonmark.node.Heading;
import org.commonmark.node.Paragraph; import org.commonmark.node.Paragraph;
import java.util.Collection;
import java.util.Collections;
import ru.noties.markwon.AbstractMarkwonPlugin; import ru.noties.markwon.AbstractMarkwonPlugin;
import ru.noties.markwon.Markwon; import ru.noties.markwon.Markwon;
import ru.noties.markwon.MarkwonConfiguration; import ru.noties.markwon.MarkwonConfiguration;
import ru.noties.markwon.MarkwonPlugin; import ru.noties.markwon.MarkwonPlugin;
import ru.noties.markwon.MarkwonSpansFactory; import ru.noties.markwon.MarkwonSpansFactory;
import ru.noties.markwon.MarkwonVisitor; import ru.noties.markwon.MarkwonVisitor;
import ru.noties.markwon.RenderProps;
import ru.noties.markwon.core.MarkwonTheme; import ru.noties.markwon.core.MarkwonTheme;
import ru.noties.markwon.html.HtmlPlugin;
import ru.noties.markwon.html.HtmlTag;
import ru.noties.markwon.html.tag.SimpleTagHandler;
import ru.noties.markwon.image.ImageItem;
import ru.noties.markwon.image.ImagesPlugin;
import ru.noties.markwon.image.SchemeHandler;
import ru.noties.markwon.image.network.NetworkSchemeHandler;
import ru.noties.markwon.movement.MovementMethodPlugin; import ru.noties.markwon.movement.MovementMethodPlugin;
public class BasicPluginsActivity extends Activity { public class BasicPluginsActivity extends Activity {
@ -42,6 +55,8 @@ public class BasicPluginsActivity extends Activity {
step_4(); step_4();
step_5(); step_5();
step_6();
} }
/** /**
@ -167,30 +182,67 @@ public class BasicPluginsActivity extends Activity {
final String markdown = "![image](myownscheme://en.wikipedia.org/static/images/project-logos/enwiki-2x.png)"; final String markdown = "![image](myownscheme://en.wikipedia.org/static/images/project-logos/enwiki-2x.png)";
final Markwon markwon = Markwon.builder(this) final Markwon markwon = Markwon.builder(this)
// .usePlugin(ImagesPlugin.create(this)) .usePlugin(ImagesPlugin.create())
// .usePlugin(new AbstractMarkwonPlugin() { .usePlugin(new AbstractMarkwonPlugin() {
// @Override @Override
// public void configureImages(@NonNull AsyncDrawableLoader.Builder builder) { public void configure(@NonNull Registry registry) {
// // we can have a custom SchemeHandler
// // here we will just use networkSchemeHandler to redirect call // use registry.require to obtain a plugin, does also
// builder.addSchemeHandler("myownscheme", new SchemeHandler() { // a runtime validation if this plugin is registered
// registry.require(ImagesPlugin.class, plugin -> plugin.addSchemeHandler(new SchemeHandler() {
// final NetworkSchemeHandler networkSchemeHandler = NetworkSchemeHandler.create();
// // it's a sample only, most likely you won't need to
// @Nullable // use existing scheme-handler, this for demonstration purposes only
// @Override final NetworkSchemeHandler handler = NetworkSchemeHandler.create();
// public ImageItem handle(@NonNull String raw, @NonNull Uri uri) {
// raw = raw.replace("myownscheme", "https"); @NonNull
// return networkSchemeHandler.handle(raw, Uri.parse(raw)); @Override
// } public ImageItem handle(@NonNull String raw, @NonNull Uri uri) {
// }); final String url = raw.replace("myownscheme", "https");
// } return handler.handle(url, Uri.parse(url));
// }) }
@NonNull
@Override
public Collection<String> supportedSchemes() {
return Collections.singleton("myownscheme");
}
}));
}
})
// or we can init plugin with this factory method
// .usePlugin(ImagesPlugin.create(plugin -> {
// plugin.addSchemeHandler(/**/)
// }))
.build(); .build();
markwon.setMarkdown(textView, markdown); markwon.setMarkdown(textView, markdown);
} }
public void step_6() {
final Markwon markwon = Markwon.builder(this)
.usePlugin(HtmlPlugin.create())
.usePlugin(new AbstractMarkwonPlugin() {
@Override
public void configure(@NonNull Registry registry) {
registry.require(HtmlPlugin.class, plugin -> plugin.addHandler(new SimpleTagHandler() {
@Override
public Object getSpans(@NonNull MarkwonConfiguration configuration, @NonNull RenderProps renderProps, @NonNull HtmlTag tag) {
return new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER);
}
@NonNull
@Override
public Collection<String> supportedTags() {
return Collections.singleton("center");
}
}));
}
})
.build();
}
// text lifecycle (after/before) // text lifecycle (after/before)
// rendering lifecycle (before/after) // rendering lifecycle (before/after)
// renderProps // renderProps

View File

@ -86,8 +86,8 @@ public class RecyclerActivity extends Activity {
// .addSchemeHandler(OkHttpNetworkSchemeHandler.create()) // .addSchemeHandler(OkHttpNetworkSchemeHandler.create())
// .addMediaDecoder(SvgMediaDecoder.create()); // .addMediaDecoder(SvgMediaDecoder.create());
// })) // }))
// .usePlugin(PicassoImagesPlugin.create(context)) .usePlugin(PicassoImagesPlugin.create(context))
.usePlugin(GlideImagesPlugin.create(context)) // .usePlugin(GlideImagesPlugin.create(context))
// important to use TableEntryPlugin instead of TablePlugin // important to use TableEntryPlugin instead of TablePlugin
.usePlugin(TableEntryPlugin.create(context)) .usePlugin(TableEntryPlugin.create(context))
.usePlugin(HtmlPlugin.create()) .usePlugin(HtmlPlugin.create())