Move HTML specifics to html module
This commit is contained in:
		
							parent
							
								
									f3476ca5cc
								
							
						
					
					
						commit
						df0177af95
					
				@ -6,4 +6,6 @@
 | 
			
		||||
  internally caches latest state and skips scheduling if drawables are already processed
 | 
			
		||||
* configure with registry
 | 
			
		||||
* removed priority
 | 
			
		||||
* images-plugin moved to standalone again
 | 
			
		||||
* images-plugin moved to standalone again
 | 
			
		||||
* removed MarkwonPlugin#configureHtmlRenderer -> now part of HtmlPlugin
 | 
			
		||||
* TagHandler now has `supportedTags()` method
 | 
			
		||||
@ -49,11 +49,6 @@ public abstract class AbstractMarkwonPlugin implements MarkwonPlugin {
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void configureHtmlRenderer(@NonNull MarkwonHtmlRenderer.Builder builder) {
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @NonNull
 | 
			
		||||
    @Override
 | 
			
		||||
    public String processMarkdown(@NonNull String markdown) {
 | 
			
		||||
 | 
			
		||||
@ -16,7 +16,6 @@ import java.util.List;
 | 
			
		||||
import java.util.Set;
 | 
			
		||||
 | 
			
		||||
import ru.noties.markwon.core.MarkwonTheme;
 | 
			
		||||
import ru.noties.markwon.html.MarkwonHtmlRenderer;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @since 3.0.0
 | 
			
		||||
@ -84,7 +83,6 @@ class MarkwonBuilderImpl implements Markwon.Builder {
 | 
			
		||||
        final MarkwonConfiguration.Builder configurationBuilder = new MarkwonConfiguration.Builder();
 | 
			
		||||
        final MarkwonVisitor.Builder visitorBuilder = new MarkwonVisitorImpl.BuilderImpl();
 | 
			
		||||
        final MarkwonSpansFactory.Builder spanFactoryBuilder = new MarkwonSpansFactoryImpl.BuilderImpl();
 | 
			
		||||
        final MarkwonHtmlRenderer.Builder htmlRendererBuilder = MarkwonHtmlRenderer.builder();
 | 
			
		||||
 | 
			
		||||
        for (MarkwonPlugin plugin : plugins) {
 | 
			
		||||
            plugin.configureParser(parserBuilder);
 | 
			
		||||
@ -92,12 +90,10 @@ class MarkwonBuilderImpl implements Markwon.Builder {
 | 
			
		||||
            plugin.configureConfiguration(configurationBuilder);
 | 
			
		||||
            plugin.configureVisitor(visitorBuilder);
 | 
			
		||||
            plugin.configureSpansFactory(spanFactoryBuilder);
 | 
			
		||||
            plugin.configureHtmlRenderer(htmlRendererBuilder);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        final MarkwonConfiguration configuration = configurationBuilder.build(
 | 
			
		||||
                themeBuilder.build(),
 | 
			
		||||
                htmlRendererBuilder.build(),
 | 
			
		||||
                spanFactoryBuilder.build());
 | 
			
		||||
 | 
			
		||||
        final RenderProps renderProps = new RenderPropsImpl();
 | 
			
		||||
 | 
			
		||||
@ -122,6 +122,15 @@ public class MarkwonConfiguration {
 | 
			
		||||
            return this;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /**
 | 
			
		||||
         * @since 4.0.0-SNAPSHOT
 | 
			
		||||
         */
 | 
			
		||||
        @NonNull
 | 
			
		||||
        public Builder htmlRenderer(@NonNull MarkwonHtmlRenderer htmlRenderer) {
 | 
			
		||||
            this.htmlRenderer = htmlRenderer;
 | 
			
		||||
            return this;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        @NonNull
 | 
			
		||||
        public Builder syntaxHighlight(@NonNull SyntaxHighlight syntaxHighlight) {
 | 
			
		||||
            this.syntaxHighlight = syntaxHighlight;
 | 
			
		||||
@ -158,11 +167,9 @@ public class MarkwonConfiguration {
 | 
			
		||||
        @NonNull
 | 
			
		||||
        public MarkwonConfiguration build(
 | 
			
		||||
                @NonNull MarkwonTheme theme,
 | 
			
		||||
                @NonNull MarkwonHtmlRenderer htmlRenderer,
 | 
			
		||||
                @NonNull MarkwonSpansFactory spansFactory) {
 | 
			
		||||
 | 
			
		||||
            this.theme = theme;
 | 
			
		||||
            this.htmlRenderer = htmlRenderer;
 | 
			
		||||
            this.spansFactory = spansFactory;
 | 
			
		||||
 | 
			
		||||
            // @since 4.0.0-SNAPSHOT
 | 
			
		||||
@ -170,6 +177,11 @@ public class MarkwonConfiguration {
 | 
			
		||||
                asyncDrawableLoader = AsyncDrawableLoader.noOp();
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            // @since 4.0.0-SNAPSHOT
 | 
			
		||||
            if (htmlRenderer == null) {
 | 
			
		||||
                htmlRenderer = MarkwonHtmlRenderer.noOp();
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (syntaxHighlight == null) {
 | 
			
		||||
                syntaxHighlight = new SyntaxHighlightNoOp();
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
@ -16,7 +16,7 @@ import ru.noties.markwon.html.MarkwonHtmlRenderer;
 | 
			
		||||
 *
 | 
			
		||||
 * @see AbstractMarkwonPlugin
 | 
			
		||||
 * @see ru.noties.markwon.core.CorePlugin
 | 
			
		||||
 * @see ru.noties.markwon.image.ImagesPlugin
 | 
			
		||||
 * @see ru.noties.markwon.movement.MovementMethodPlugin
 | 
			
		||||
 * @since 3.0.0
 | 
			
		||||
 */
 | 
			
		||||
public interface MarkwonPlugin {
 | 
			
		||||
@ -88,13 +88,13 @@ public interface MarkwonPlugin {
 | 
			
		||||
     */
 | 
			
		||||
    void configureSpansFactory(@NonNull MarkwonSpansFactory.Builder builder);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Configure {@link MarkwonHtmlRenderer} to add or remove HTML {@link ru.noties.markwon.html.TagHandler}s
 | 
			
		||||
     *
 | 
			
		||||
     * @see MarkwonHtmlRenderer
 | 
			
		||||
     * @see MarkwonHtmlRenderer.Builder
 | 
			
		||||
     */
 | 
			
		||||
    void configureHtmlRenderer(@NonNull MarkwonHtmlRenderer.Builder builder);
 | 
			
		||||
//    /**
 | 
			
		||||
//     * Configure {@link MarkwonHtmlRenderer} to add or remove HTML {@link ru.noties.markwon.html.TagHandler}s
 | 
			
		||||
//     *
 | 
			
		||||
//     * @see MarkwonHtmlRenderer
 | 
			
		||||
//     * @see MarkwonHtmlRenderer.Builder
 | 
			
		||||
//     */
 | 
			
		||||
//    void configureHtmlRenderer(@NonNull MarkwonHtmlRenderer.Builder builder);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Process input markdown and return new string to be used in parsing stage further.
 | 
			
		||||
@ -128,7 +128,7 @@ public interface MarkwonPlugin {
 | 
			
		||||
    /**
 | 
			
		||||
     * This method will be called <strong>before</strong> calling <code>TextView#setText</code>.
 | 
			
		||||
     * <p>
 | 
			
		||||
     * It can be useful to prepare a TextView for markdown. For example {@link ru.noties.markwon.image.ImagesPlugin}
 | 
			
		||||
     * It can be useful to prepare a TextView for markdown. For example {@code ru.noties.markwon.image.ImagesPlugin}
 | 
			
		||||
     * uses this method to unregister previously registered {@link ru.noties.markwon.image.AsyncDrawableSpan}
 | 
			
		||||
     * (if there are such spans in this TextView at this point). Or {@link ru.noties.markwon.core.CorePlugin}
 | 
			
		||||
     * which measures ordered list numbers
 | 
			
		||||
@ -141,7 +141,7 @@ public interface MarkwonPlugin {
 | 
			
		||||
    /**
 | 
			
		||||
     * This method will be called <strong>after</strong> markdown was applied.
 | 
			
		||||
     * <p>
 | 
			
		||||
     * It can be useful to trigger certain action on spans/textView. For example {@link ru.noties.markwon.image.ImagesPlugin}
 | 
			
		||||
     * It can be useful to trigger certain action on spans/textView. For example {@code ru.noties.markwon.image.ImagesPlugin}
 | 
			
		||||
     * uses this method to register {@link ru.noties.markwon.image.AsyncDrawableSpan} and start
 | 
			
		||||
     * asynchronously loading images.
 | 
			
		||||
     * <p>
 | 
			
		||||
 | 
			
		||||
@ -3,8 +3,6 @@ package ru.noties.markwon.html;
 | 
			
		||||
import android.support.annotation.NonNull;
 | 
			
		||||
import android.support.annotation.Nullable;
 | 
			
		||||
 | 
			
		||||
import java.util.Collection;
 | 
			
		||||
 | 
			
		||||
import ru.noties.markwon.MarkwonVisitor;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
@ -12,9 +10,12 @@ import ru.noties.markwon.MarkwonVisitor;
 | 
			
		||||
 */
 | 
			
		||||
public abstract class MarkwonHtmlRenderer {
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @since 4.0.0-SNAPSHOT
 | 
			
		||||
     */
 | 
			
		||||
    @NonNull
 | 
			
		||||
    public static Builder builder() {
 | 
			
		||||
        return new MarkwonHtmlRendererImpl.BuilderImpl();
 | 
			
		||||
    public static MarkwonHtmlRenderer noOp() {
 | 
			
		||||
        return new MarkwonHtmlRendererNoOp();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public abstract void render(
 | 
			
		||||
@ -24,33 +25,4 @@ public abstract class MarkwonHtmlRenderer {
 | 
			
		||||
 | 
			
		||||
    @Nullable
 | 
			
		||||
    public abstract TagHandler tagHandler(@NonNull String tagName);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @since 3.0.0
 | 
			
		||||
     */
 | 
			
		||||
    public interface Builder {
 | 
			
		||||
 | 
			
		||||
        /**
 | 
			
		||||
         * @param allowNonClosedTags parameter to indicate that all non-closed HTML tags should be
 | 
			
		||||
         *                           closed at the end of a document. if {@code true} all non-closed
 | 
			
		||||
         *                           tags will be force-closed at the end. Otherwise these tags will be
 | 
			
		||||
         *                           ignored and thus not rendered.
 | 
			
		||||
         * @return self
 | 
			
		||||
         */
 | 
			
		||||
        @NonNull
 | 
			
		||||
        Builder allowNonClosedTags(boolean allowNonClosedTags);
 | 
			
		||||
 | 
			
		||||
        @NonNull
 | 
			
		||||
        Builder setHandler(@NonNull String tagName, @Nullable TagHandler tagHandler);
 | 
			
		||||
 | 
			
		||||
        @NonNull
 | 
			
		||||
        Builder setHandler(@NonNull Collection<String> tagNames, @Nullable TagHandler tagHandler);
 | 
			
		||||
 | 
			
		||||
        @Nullable
 | 
			
		||||
        TagHandler getHandler(@NonNull String tagName);
 | 
			
		||||
 | 
			
		||||
        @NonNull
 | 
			
		||||
        MarkwonHtmlRenderer build();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -2,6 +2,8 @@ package ru.noties.markwon.html;
 | 
			
		||||
 | 
			
		||||
import android.support.annotation.NonNull;
 | 
			
		||||
 | 
			
		||||
import java.util.Collection;
 | 
			
		||||
 | 
			
		||||
import ru.noties.markwon.MarkwonVisitor;
 | 
			
		||||
 | 
			
		||||
public abstract class TagHandler {
 | 
			
		||||
@ -12,6 +14,13 @@ public abstract class TagHandler {
 | 
			
		||||
            @NonNull HtmlTag tag
 | 
			
		||||
    );
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @since 4.0.0-SNAPSHOT
 | 
			
		||||
     */
 | 
			
		||||
    @NonNull
 | 
			
		||||
    public abstract Collection<String> supportedTags();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    protected static void visitChildren(
 | 
			
		||||
            @NonNull MarkwonVisitor visitor,
 | 
			
		||||
            @NonNull MarkwonHtmlRenderer renderer,
 | 
			
		||||
 | 
			
		||||
@ -10,19 +10,6 @@ import org.commonmark.node.Node;
 | 
			
		||||
import ru.noties.markwon.AbstractMarkwonPlugin;
 | 
			
		||||
import ru.noties.markwon.MarkwonConfiguration;
 | 
			
		||||
import ru.noties.markwon.MarkwonVisitor;
 | 
			
		||||
import ru.noties.markwon.html.tag.BlockquoteHandler;
 | 
			
		||||
import ru.noties.markwon.html.tag.EmphasisHandler;
 | 
			
		||||
import ru.noties.markwon.html.tag.HeadingHandler;
 | 
			
		||||
import ru.noties.markwon.html.tag.ImageHandler;
 | 
			
		||||
import ru.noties.markwon.html.tag.LinkHandler;
 | 
			
		||||
import ru.noties.markwon.html.tag.ListHandler;
 | 
			
		||||
import ru.noties.markwon.html.tag.StrikeHandler;
 | 
			
		||||
import ru.noties.markwon.html.tag.StrongEmphasisHandler;
 | 
			
		||||
import ru.noties.markwon.html.tag.SubScriptHandler;
 | 
			
		||||
import ru.noties.markwon.html.tag.SuperScriptHandler;
 | 
			
		||||
import ru.noties.markwon.html.tag.UnderlineHandler;
 | 
			
		||||
 | 
			
		||||
import static java.util.Arrays.asList;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @since 3.0.0
 | 
			
		||||
@ -36,48 +23,60 @@ public class HtmlPlugin extends AbstractMarkwonPlugin {
 | 
			
		||||
 | 
			
		||||
    public static final float SCRIPT_DEF_TEXT_SIZE_RATIO = .75F;
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void configureConfiguration(@NonNull MarkwonConfiguration.Builder builder) {
 | 
			
		||||
        builder.htmlParser(MarkwonHtmlParserImpl.create());
 | 
			
		||||
    private final MarkwonHtmlRendererImpl.Builder builder;
 | 
			
		||||
 | 
			
		||||
    @SuppressWarnings("WeakerAccess")
 | 
			
		||||
    HtmlPlugin() {
 | 
			
		||||
        this.builder = new MarkwonHtmlRendererImpl.Builder();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @param allowNonClosedTags whether or not non-closed tags should be closed
 | 
			
		||||
     *                           at the document end. By default `false`
 | 
			
		||||
     * @since 4.0.0-SNAPSHOT
 | 
			
		||||
     */
 | 
			
		||||
    @NonNull
 | 
			
		||||
    public HtmlPlugin allowNonClosedTags(boolean allowNonClosedTags) {
 | 
			
		||||
        builder.allowNonClosedTags(allowNonClosedTags);
 | 
			
		||||
        return this;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @since 4.0.0-SNAPSHOT
 | 
			
		||||
     */
 | 
			
		||||
    @NonNull
 | 
			
		||||
    public HtmlPlugin addHandler(@NonNull TagHandler tagHandler) {
 | 
			
		||||
        builder.addHandler(tagHandler);
 | 
			
		||||
        return this;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @since 4.0.0-SNAPSHOT
 | 
			
		||||
     */
 | 
			
		||||
    @Nullable
 | 
			
		||||
    public TagHandler getHandler(@NonNull String tagName) {
 | 
			
		||||
        return builder.getHandler(tagName);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Indicate if HtmlPlugin should register default HTML tag handlers. Pass `true` to <strong>not</strong>
 | 
			
		||||
     * include default handlers. By default default handlers are included. You can use
 | 
			
		||||
     * {@link TagHandlerNoOp} to no-op certain default tags.
 | 
			
		||||
     *
 | 
			
		||||
     * @see TagHandlerNoOp
 | 
			
		||||
     * @since 4.0.0-SNAPSHOT
 | 
			
		||||
     */
 | 
			
		||||
    @NonNull
 | 
			
		||||
    public HtmlPlugin excludeDefaults(boolean excludeDefaults) {
 | 
			
		||||
        builder.excludeDefaults(excludeDefaults);
 | 
			
		||||
        return this;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void configureHtmlRenderer(@NonNull MarkwonHtmlRenderer.Builder builder) {
 | 
			
		||||
 | 
			
		||||
    public void configureConfiguration(@NonNull MarkwonConfiguration.Builder builder) {
 | 
			
		||||
        builder
 | 
			
		||||
                .setHandler(
 | 
			
		||||
                        "img",
 | 
			
		||||
                        ImageHandler.create())
 | 
			
		||||
                .setHandler(
 | 
			
		||||
                        "a",
 | 
			
		||||
                        new LinkHandler())
 | 
			
		||||
                .setHandler(
 | 
			
		||||
                        "blockquote",
 | 
			
		||||
                        new BlockquoteHandler())
 | 
			
		||||
                .setHandler(
 | 
			
		||||
                        "sub",
 | 
			
		||||
                        new SubScriptHandler())
 | 
			
		||||
                .setHandler(
 | 
			
		||||
                        "sup",
 | 
			
		||||
                        new SuperScriptHandler())
 | 
			
		||||
                .setHandler(
 | 
			
		||||
                        asList("b", "strong"),
 | 
			
		||||
                        new StrongEmphasisHandler())
 | 
			
		||||
                .setHandler(
 | 
			
		||||
                        asList("s", "del"),
 | 
			
		||||
                        new StrikeHandler())
 | 
			
		||||
                .setHandler(
 | 
			
		||||
                        asList("u", "ins"),
 | 
			
		||||
                        new UnderlineHandler())
 | 
			
		||||
                .setHandler(
 | 
			
		||||
                        asList("ul", "ol"),
 | 
			
		||||
                        new ListHandler())
 | 
			
		||||
                .setHandler(
 | 
			
		||||
                        asList("i", "em", "cite", "dfn"),
 | 
			
		||||
                        new EmphasisHandler())
 | 
			
		||||
                .setHandler(
 | 
			
		||||
                        asList("h1", "h2", "h3", "h4", "h5", "h6"),
 | 
			
		||||
                        new HeadingHandler());
 | 
			
		||||
                .htmlRenderer(this.builder.build())
 | 
			
		||||
                .htmlParser(MarkwonHtmlParserImpl.create());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
 | 
			
		||||
@ -195,7 +195,6 @@ public class MarkwonHtmlParserImpl extends MarkwonHtmlParser {
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            //noinspection unchecked
 | 
			
		||||
            action.apply(Collections.unmodifiableList((List<? extends Inline>) inlineTags));
 | 
			
		||||
            inlineTags.clear();
 | 
			
		||||
        } else {
 | 
			
		||||
 | 
			
		||||
@ -3,19 +3,30 @@ package ru.noties.markwon.html;
 | 
			
		||||
import android.support.annotation.NonNull;
 | 
			
		||||
import android.support.annotation.Nullable;
 | 
			
		||||
 | 
			
		||||
import java.util.Collection;
 | 
			
		||||
import java.util.Collections;
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
 | 
			
		||||
import ru.noties.markwon.MarkwonVisitor;
 | 
			
		||||
import ru.noties.markwon.html.tag.BlockquoteHandler;
 | 
			
		||||
import ru.noties.markwon.html.tag.EmphasisHandler;
 | 
			
		||||
import ru.noties.markwon.html.tag.HeadingHandler;
 | 
			
		||||
import ru.noties.markwon.html.tag.ImageHandler;
 | 
			
		||||
import ru.noties.markwon.html.tag.LinkHandler;
 | 
			
		||||
import ru.noties.markwon.html.tag.ListHandler;
 | 
			
		||||
import ru.noties.markwon.html.tag.StrikeHandler;
 | 
			
		||||
import ru.noties.markwon.html.tag.StrongEmphasisHandler;
 | 
			
		||||
import ru.noties.markwon.html.tag.SubScriptHandler;
 | 
			
		||||
import ru.noties.markwon.html.tag.SuperScriptHandler;
 | 
			
		||||
import ru.noties.markwon.html.tag.UnderlineHandler;
 | 
			
		||||
 | 
			
		||||
class MarkwonHtmlRendererImpl extends MarkwonHtmlRenderer {
 | 
			
		||||
 | 
			
		||||
    private final boolean allowNonClosedTags;
 | 
			
		||||
    private final Map<String, TagHandler> tagHandlers;
 | 
			
		||||
 | 
			
		||||
    @SuppressWarnings("WeakerAccess")
 | 
			
		||||
    MarkwonHtmlRendererImpl(boolean allowNonClosedTags, @NonNull Map<String, TagHandler> tagHandlers) {
 | 
			
		||||
        this.allowNonClosedTags = allowNonClosedTags;
 | 
			
		||||
        this.tagHandlers = tagHandlers;
 | 
			
		||||
@ -86,58 +97,82 @@ class MarkwonHtmlRendererImpl extends MarkwonHtmlRenderer {
 | 
			
		||||
        return tagHandlers.get(tagName);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    static class BuilderImpl implements Builder {
 | 
			
		||||
    static class Builder {
 | 
			
		||||
 | 
			
		||||
        private final Map<String, TagHandler> tagHandlers = new HashMap<>(2);
 | 
			
		||||
        private boolean allowNonClosedTags;
 | 
			
		||||
        private boolean excludeDefaults;
 | 
			
		||||
 | 
			
		||||
        @NonNull
 | 
			
		||||
        @Override
 | 
			
		||||
        public Builder allowNonClosedTags(boolean allowNonClosedTags) {
 | 
			
		||||
        private boolean isBuilt;
 | 
			
		||||
 | 
			
		||||
        void allowNonClosedTags(boolean allowNonClosedTags) {
 | 
			
		||||
            checkState();
 | 
			
		||||
            this.allowNonClosedTags = allowNonClosedTags;
 | 
			
		||||
            return this;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        @NonNull
 | 
			
		||||
        @Override
 | 
			
		||||
        public Builder setHandler(@NonNull String tagName, @Nullable TagHandler tagHandler) {
 | 
			
		||||
            if (tagHandler == null) {
 | 
			
		||||
                tagHandlers.remove(tagName);
 | 
			
		||||
            } else {
 | 
			
		||||
                tagHandlers.put(tagName, tagHandler);
 | 
			
		||||
        void addHandler(@NonNull TagHandler tagHandler) {
 | 
			
		||||
            checkState();
 | 
			
		||||
            for (String tag : tagHandler.supportedTags()) {
 | 
			
		||||
                tagHandlers.put(tag, tagHandler);
 | 
			
		||||
            }
 | 
			
		||||
            return this;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        @NonNull
 | 
			
		||||
        @Override
 | 
			
		||||
        public Builder setHandler(@NonNull Collection<String> tagNames, @Nullable TagHandler tagHandler) {
 | 
			
		||||
            if (tagHandler == null) {
 | 
			
		||||
                for (String tagName : tagNames) {
 | 
			
		||||
                    tagHandlers.remove(tagName);
 | 
			
		||||
                }
 | 
			
		||||
            } else {
 | 
			
		||||
                for (String tagName : tagNames) {
 | 
			
		||||
                    tagHandlers.put(tagName, tagHandler);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            return this;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        @Nullable
 | 
			
		||||
        @Override
 | 
			
		||||
        public TagHandler getHandler(@NonNull String tagName) {
 | 
			
		||||
        TagHandler getHandler(@NonNull String tagName) {
 | 
			
		||||
            checkState();
 | 
			
		||||
            return tagHandlers.get(tagName);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public void excludeDefaults(boolean excludeDefaults) {
 | 
			
		||||
            checkState();
 | 
			
		||||
            this.excludeDefaults = excludeDefaults;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        @NonNull
 | 
			
		||||
        @Override
 | 
			
		||||
        public MarkwonHtmlRenderer build() {
 | 
			
		||||
 | 
			
		||||
            checkState();
 | 
			
		||||
 | 
			
		||||
            isBuilt = true;
 | 
			
		||||
 | 
			
		||||
            if (!excludeDefaults) {
 | 
			
		||||
                // register default handlers, check if a handler is present already for specified tag
 | 
			
		||||
                registerDefaultHandlers();
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            // okay, let's validate that we have at least one tagHandler registered
 | 
			
		||||
            // if we have none -> return no-op implementation
 | 
			
		||||
            return tagHandlers.size() > 0
 | 
			
		||||
                    ? new MarkwonHtmlRendererImpl(allowNonClosedTags, Collections.unmodifiableMap(tagHandlers))
 | 
			
		||||
                    : new MarkwonHtmlRendererNoOp();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void checkState() {
 | 
			
		||||
            if (isBuilt) {
 | 
			
		||||
                throw new IllegalStateException("Builder has been already built");
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void registerDefaultHandlers() {
 | 
			
		||||
            add(ImageHandler.create());
 | 
			
		||||
            add(new LinkHandler());
 | 
			
		||||
            add(new BlockquoteHandler());
 | 
			
		||||
            add(new SubScriptHandler());
 | 
			
		||||
            add(new SuperScriptHandler());
 | 
			
		||||
            add(new StrongEmphasisHandler());
 | 
			
		||||
            add(new StrikeHandler());
 | 
			
		||||
            add(new UnderlineHandler());
 | 
			
		||||
            add(new ListHandler());
 | 
			
		||||
            add(new EmphasisHandler());
 | 
			
		||||
            add(new HeadingHandler());
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void add(@NonNull TagHandler tagHandler) {
 | 
			
		||||
            for (String tag : tagHandler.supportedTags()) {
 | 
			
		||||
                if (!tagHandlers.containsKey(tag)) {
 | 
			
		||||
                    tagHandlers.put(tag, tagHandler);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,43 @@
 | 
			
		||||
package ru.noties.markwon.html;
 | 
			
		||||
 | 
			
		||||
import android.support.annotation.NonNull;
 | 
			
		||||
 | 
			
		||||
import java.util.Arrays;
 | 
			
		||||
import java.util.Collection;
 | 
			
		||||
import java.util.Collections;
 | 
			
		||||
 | 
			
		||||
import ru.noties.markwon.MarkwonVisitor;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @since 4.0.0-SNAPSHOT
 | 
			
		||||
 */
 | 
			
		||||
public class TagHandlerNoOp extends TagHandler {
 | 
			
		||||
 | 
			
		||||
    @NonNull
 | 
			
		||||
    public static TagHandlerNoOp create(@NonNull String tag) {
 | 
			
		||||
        return new TagHandlerNoOp(Collections.singleton(tag));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @NonNull
 | 
			
		||||
    public static TagHandlerNoOp create(@NonNull String... tags) {
 | 
			
		||||
        return new TagHandlerNoOp(Arrays.asList(tags));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private final Collection<String> tags;
 | 
			
		||||
 | 
			
		||||
    @SuppressWarnings("WeakerAccess")
 | 
			
		||||
    TagHandlerNoOp(Collection<String> tags) {
 | 
			
		||||
        this.tags = tags;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void handle(@NonNull MarkwonVisitor visitor, @NonNull MarkwonHtmlRenderer renderer, @NonNull HtmlTag tag) {
 | 
			
		||||
        // no op
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @NonNull
 | 
			
		||||
    @Override
 | 
			
		||||
    public Collection<String> supportedTags() {
 | 
			
		||||
        return tags;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -4,6 +4,9 @@ import android.support.annotation.NonNull;
 | 
			
		||||
 | 
			
		||||
import org.commonmark.node.BlockQuote;
 | 
			
		||||
 | 
			
		||||
import java.util.Collection;
 | 
			
		||||
import java.util.Collections;
 | 
			
		||||
 | 
			
		||||
import ru.noties.markwon.MarkwonConfiguration;
 | 
			
		||||
import ru.noties.markwon.MarkwonVisitor;
 | 
			
		||||
import ru.noties.markwon.SpanFactory;
 | 
			
		||||
@ -35,4 +38,10 @@ public class BlockquoteHandler extends TagHandler {
 | 
			
		||||
            );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @NonNull
 | 
			
		||||
    @Override
 | 
			
		||||
    public Collection<String> supportedTags() {
 | 
			
		||||
        return Collections.singleton("blockquote");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -5,6 +5,9 @@ import android.support.annotation.Nullable;
 | 
			
		||||
 | 
			
		||||
import org.commonmark.node.Emphasis;
 | 
			
		||||
 | 
			
		||||
import java.util.Arrays;
 | 
			
		||||
import java.util.Collection;
 | 
			
		||||
 | 
			
		||||
import ru.noties.markwon.MarkwonConfiguration;
 | 
			
		||||
import ru.noties.markwon.RenderProps;
 | 
			
		||||
import ru.noties.markwon.SpanFactory;
 | 
			
		||||
@ -23,4 +26,10 @@ public class EmphasisHandler extends SimpleTagHandler {
 | 
			
		||||
        }
 | 
			
		||||
        return spanFactory.getSpans(configuration, renderProps);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @NonNull
 | 
			
		||||
    @Override
 | 
			
		||||
    public Collection<String> supportedTags() {
 | 
			
		||||
        return Arrays.asList("i", "em", "cite", "dfn");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -5,6 +5,9 @@ import android.support.annotation.Nullable;
 | 
			
		||||
 | 
			
		||||
import org.commonmark.node.Heading;
 | 
			
		||||
 | 
			
		||||
import java.util.Arrays;
 | 
			
		||||
import java.util.Collection;
 | 
			
		||||
 | 
			
		||||
import ru.noties.markwon.MarkwonConfiguration;
 | 
			
		||||
import ru.noties.markwon.RenderProps;
 | 
			
		||||
import ru.noties.markwon.SpanFactory;
 | 
			
		||||
@ -41,4 +44,10 @@ public class HeadingHandler extends SimpleTagHandler {
 | 
			
		||||
 | 
			
		||||
        return factory.getSpans(configuration, renderProps);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @NonNull
 | 
			
		||||
    @Override
 | 
			
		||||
    public Collection<String> supportedTags() {
 | 
			
		||||
        return Arrays.asList("h1", "h2", "h3", "h4", "h5", "h6");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -6,6 +6,8 @@ import android.text.TextUtils;
 | 
			
		||||
 | 
			
		||||
import org.commonmark.node.Image;
 | 
			
		||||
 | 
			
		||||
import java.util.Collection;
 | 
			
		||||
import java.util.Collections;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
 | 
			
		||||
import ru.noties.markwon.MarkwonConfiguration;
 | 
			
		||||
@ -18,6 +20,12 @@ import ru.noties.markwon.image.ImageSize;
 | 
			
		||||
 | 
			
		||||
public class ImageHandler extends SimpleTagHandler {
 | 
			
		||||
 | 
			
		||||
    @NonNull
 | 
			
		||||
    @Override
 | 
			
		||||
    public Collection<String> supportedTags() {
 | 
			
		||||
        return Collections.singleton("img");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    interface ImageSizeParser {
 | 
			
		||||
        @Nullable
 | 
			
		||||
        ImageSize parse(@NonNull Map<String, String> attributes);
 | 
			
		||||
@ -30,6 +38,7 @@ public class ImageHandler extends SimpleTagHandler {
 | 
			
		||||
 | 
			
		||||
    private final ImageSizeParser imageSizeParser;
 | 
			
		||||
 | 
			
		||||
    @SuppressWarnings("WeakerAccess")
 | 
			
		||||
    ImageHandler(@NonNull ImageSizeParser imageSizeParser) {
 | 
			
		||||
        this.imageSizeParser = imageSizeParser;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -6,6 +6,9 @@ import android.text.TextUtils;
 | 
			
		||||
 | 
			
		||||
import org.commonmark.node.Link;
 | 
			
		||||
 | 
			
		||||
import java.util.Collection;
 | 
			
		||||
import java.util.Collections;
 | 
			
		||||
 | 
			
		||||
import ru.noties.markwon.MarkwonConfiguration;
 | 
			
		||||
import ru.noties.markwon.RenderProps;
 | 
			
		||||
import ru.noties.markwon.SpanFactory;
 | 
			
		||||
@ -30,4 +33,10 @@ public class LinkHandler extends SimpleTagHandler {
 | 
			
		||||
        }
 | 
			
		||||
        return null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @NonNull
 | 
			
		||||
    @Override
 | 
			
		||||
    public Collection<String> supportedTags() {
 | 
			
		||||
        return Collections.singleton("a");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -4,6 +4,9 @@ import android.support.annotation.NonNull;
 | 
			
		||||
 | 
			
		||||
import org.commonmark.node.ListItem;
 | 
			
		||||
 | 
			
		||||
import java.util.Arrays;
 | 
			
		||||
import java.util.Collection;
 | 
			
		||||
 | 
			
		||||
import ru.noties.markwon.MarkwonConfiguration;
 | 
			
		||||
import ru.noties.markwon.MarkwonVisitor;
 | 
			
		||||
import ru.noties.markwon.RenderProps;
 | 
			
		||||
@ -65,6 +68,12 @@ public class ListHandler extends TagHandler {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @NonNull
 | 
			
		||||
    @Override
 | 
			
		||||
    public Collection<String> supportedTags() {
 | 
			
		||||
        return Arrays.asList("ol", "ul");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private static int currentBulletListLevel(@NonNull HtmlTag.Block block) {
 | 
			
		||||
        int level = 0;
 | 
			
		||||
        while ((block = block.parent()) != null) {
 | 
			
		||||
 | 
			
		||||
@ -3,6 +3,8 @@ package ru.noties.markwon.html.tag;
 | 
			
		||||
import android.support.annotation.NonNull;
 | 
			
		||||
import android.support.annotation.Nullable;
 | 
			
		||||
 | 
			
		||||
import java.util.Collection;
 | 
			
		||||
 | 
			
		||||
import ru.noties.markwon.MarkwonConfiguration;
 | 
			
		||||
import ru.noties.markwon.MarkwonVisitor;
 | 
			
		||||
import ru.noties.markwon.RenderProps;
 | 
			
		||||
@ -19,6 +21,11 @@ public abstract class SimpleTagHandler extends TagHandler {
 | 
			
		||||
            @NonNull RenderProps renderProps,
 | 
			
		||||
            @NonNull HtmlTag tag);
 | 
			
		||||
 | 
			
		||||
    @NonNull
 | 
			
		||||
    @Override
 | 
			
		||||
    public abstract Collection<String> supportedTags();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void handle(@NonNull MarkwonVisitor visitor, @NonNull MarkwonHtmlRenderer renderer, @NonNull HtmlTag tag) {
 | 
			
		||||
        final Object spans = getSpans(visitor.configuration(), visitor.renderProps(), tag);
 | 
			
		||||
 | 
			
		||||
@ -4,6 +4,9 @@ import android.support.annotation.NonNull;
 | 
			
		||||
import android.support.annotation.Nullable;
 | 
			
		||||
import android.text.style.StrikethroughSpan;
 | 
			
		||||
 | 
			
		||||
import java.util.Arrays;
 | 
			
		||||
import java.util.Collection;
 | 
			
		||||
 | 
			
		||||
import ru.noties.markwon.MarkwonConfiguration;
 | 
			
		||||
import ru.noties.markwon.MarkwonVisitor;
 | 
			
		||||
import ru.noties.markwon.SpanFactory;
 | 
			
		||||
@ -47,6 +50,12 @@ public class StrikeHandler extends TagHandler {
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @NonNull
 | 
			
		||||
    @Override
 | 
			
		||||
    public Collection<String> supportedTags() {
 | 
			
		||||
        return Arrays.asList("s", "del");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Nullable
 | 
			
		||||
    private static Object getMarkdownSpans(@NonNull MarkwonVisitor visitor) {
 | 
			
		||||
        final MarkwonConfiguration configuration = visitor.configuration();
 | 
			
		||||
 | 
			
		||||
@ -5,6 +5,9 @@ import android.support.annotation.Nullable;
 | 
			
		||||
 | 
			
		||||
import org.commonmark.node.StrongEmphasis;
 | 
			
		||||
 | 
			
		||||
import java.util.Arrays;
 | 
			
		||||
import java.util.Collection;
 | 
			
		||||
 | 
			
		||||
import ru.noties.markwon.MarkwonConfiguration;
 | 
			
		||||
import ru.noties.markwon.RenderProps;
 | 
			
		||||
import ru.noties.markwon.SpanFactory;
 | 
			
		||||
@ -23,4 +26,10 @@ public class StrongEmphasisHandler extends SimpleTagHandler {
 | 
			
		||||
        }
 | 
			
		||||
        return spanFactory.getSpans(configuration, renderProps);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @NonNull
 | 
			
		||||
    @Override
 | 
			
		||||
    public Collection<String> supportedTags() {
 | 
			
		||||
        return Arrays.asList("b", "strong");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -3,6 +3,9 @@ package ru.noties.markwon.html.tag;
 | 
			
		||||
import android.support.annotation.NonNull;
 | 
			
		||||
import android.support.annotation.Nullable;
 | 
			
		||||
 | 
			
		||||
import java.util.Collection;
 | 
			
		||||
import java.util.Collections;
 | 
			
		||||
 | 
			
		||||
import ru.noties.markwon.MarkwonConfiguration;
 | 
			
		||||
import ru.noties.markwon.RenderProps;
 | 
			
		||||
import ru.noties.markwon.html.HtmlTag;
 | 
			
		||||
@ -14,4 +17,10 @@ public class SubScriptHandler extends SimpleTagHandler {
 | 
			
		||||
    public Object getSpans(@NonNull MarkwonConfiguration configuration, @NonNull RenderProps renderProps, @NonNull HtmlTag tag) {
 | 
			
		||||
        return new SubScriptSpan();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @NonNull
 | 
			
		||||
    @Override
 | 
			
		||||
    public Collection<String> supportedTags() {
 | 
			
		||||
        return Collections.singleton("sub");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -3,6 +3,9 @@ package ru.noties.markwon.html.tag;
 | 
			
		||||
import android.support.annotation.NonNull;
 | 
			
		||||
import android.support.annotation.Nullable;
 | 
			
		||||
 | 
			
		||||
import java.util.Collection;
 | 
			
		||||
import java.util.Collections;
 | 
			
		||||
 | 
			
		||||
import ru.noties.markwon.MarkwonConfiguration;
 | 
			
		||||
import ru.noties.markwon.RenderProps;
 | 
			
		||||
import ru.noties.markwon.html.HtmlTag;
 | 
			
		||||
@ -14,4 +17,10 @@ public class SuperScriptHandler extends SimpleTagHandler {
 | 
			
		||||
    public Object getSpans(@NonNull MarkwonConfiguration configuration, @NonNull RenderProps renderProps, @NonNull HtmlTag tag) {
 | 
			
		||||
        return new SuperScriptSpan();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @NonNull
 | 
			
		||||
    @Override
 | 
			
		||||
    public Collection<String> supportedTags() {
 | 
			
		||||
        return Collections.singleton("sup");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -3,6 +3,9 @@ package ru.noties.markwon.html.tag;
 | 
			
		||||
import android.support.annotation.NonNull;
 | 
			
		||||
import android.text.style.UnderlineSpan;
 | 
			
		||||
 | 
			
		||||
import java.util.Arrays;
 | 
			
		||||
import java.util.Collection;
 | 
			
		||||
 | 
			
		||||
import ru.noties.markwon.MarkwonVisitor;
 | 
			
		||||
import ru.noties.markwon.SpannableBuilder;
 | 
			
		||||
import ru.noties.markwon.html.HtmlTag;
 | 
			
		||||
@ -31,4 +34,10 @@ public class UnderlineHandler extends TagHandler {
 | 
			
		||||
                tag.end()
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @NonNull
 | 
			
		||||
    @Override
 | 
			
		||||
    public Collection<String> supportedTags() {
 | 
			
		||||
        return Arrays.asList("u", "ins");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user