org.commonmark.node.Image Java Examples

The following examples show how to use org.commonmark.node.Image. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MarkdownUtil.java    From ml-blog with MIT License 6 votes vote down vote up
@Override
public void render(Node node) {

    HtmlWriter html = context.getWriter();

    Image image = (Image) node;
    Map<String,String> attrs = new HashMap<>();
    Map<String,String> attrs2 = new HashMap<>();
    attrs.put("href",image.getDestination());
    attrs2.put("src",image.getDestination());
    html.line();
    html.tag("a",attrs);
    html.tag("image",attrs2);
    html.tag("/a");
    html.line();
}
 
Example #2
Source File: CommonmarkPreviewRenderer.java    From markdown-writer-fx with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private void printAttributes(StringBuilder buf, Node node) {
	if (node instanceof Text)
		printAttribute(buf, "literal", ((Text)node).getLiteral());
	else if (node instanceof Code)
		printAttribute(buf, "literal", ((Code)node).getLiteral());
	else if (node instanceof IndentedCodeBlock)
		printAttribute(buf, "literal", ((IndentedCodeBlock)node).getLiteral());
	else if (node instanceof FencedCodeBlock)
		printAttribute(buf, "literal", ((FencedCodeBlock)node).getLiteral());
	else if (node instanceof HtmlBlock)
		printAttribute(buf, "literal", ((HtmlBlock)node).getLiteral());
	else if (node instanceof HtmlInline)
		printAttribute(buf, "literal", ((HtmlInline)node).getLiteral());
	else if (node instanceof Link) {
		printAttribute(buf, "destination", ((Link)node).getDestination());
		printAttribute(buf, "title", ((Link)node).getTitle());
	} else if (node instanceof Image) {
		printAttribute(buf, "destination", ((Image)node).getDestination());
		printAttribute(buf, "title", ((Image)node).getTitle());
	} else if (node instanceof Heading)
		printAttribute(buf, "level", ((Heading)node).getLevel());
}
 
Example #3
Source File: GifAwarePlugin.java    From Markwon with Apache License 2.0 6 votes vote down vote up
@Override
public void configureSpansFactory(@NonNull MarkwonSpansFactory.Builder builder) {

    final GifPlaceholder gifPlaceholder = new GifPlaceholder(
            context.getResources().getDrawable(R.drawable.ic_play_circle_filled_18dp_white),
            0x20000000
    );

    builder.setFactory(Image.class, new SpanFactory() {
        @Override
        public Object getSpans(@NonNull MarkwonConfiguration configuration, @NonNull RenderProps props) {
            return new AsyncDrawableSpan(
                    configuration.theme(),
                    new GifAwareAsyncDrawable(
                            gifPlaceholder,
                            ImageProps.DESTINATION.require(props),
                            configuration.asyncDrawableLoader(),
                            configuration.imageSizeResolver(),
                            ImageProps.IMAGE_SIZE.get(props)
                    ),
                    AsyncDrawableSpan.ALIGN_BOTTOM,
                    ImageProps.REPLACEMENT_TEXT_IS_LINK.get(props, false)
            );
        }
    });
}
 
Example #4
Source File: MarkdownUtil.java    From pybbs with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void setAttributes(Node node, String s, Map<String, String> map) {
    // 给图片添加上类样式,类样式可以在css里自定义
    if (node instanceof Image) {
        map.put("class", "md-image");
    }
    // 给表格添加上类样式,类样式可以在css里自定义
    if (node instanceof TableBlock) {
        map.put("class", "table table-bordered");
    }
}
 
Example #5
Source File: MarkwonSpansFactoryImplTest.java    From Markwon with Apache License 2.0 5 votes vote down vote up
@Test
public void builder() {
    // all passed to builder will be in factory

    final SpanFactory text = mock(SpanFactory.class);
    final SpanFactory link = mock(SpanFactory.class);

    final MarkwonSpansFactory factory = new MarkwonSpansFactoryImpl.BuilderImpl()
            .setFactory(Text.class, text)
            .setFactory(Link.class, link)
            .build();

    assertNotNull(factory.get(Text.class));

    assertNotNull(factory.get(Link.class));

    // a bunch of non-present factories
    //noinspection unchecked
    final Class<? extends Node>[] types = new Class[]{
            Image.class,
            Block.class,
            Emphasis.class,
            Paragraph.class
    };

    for (Class<? extends Node> type : types) {
        assertNull(factory.get(type));
    }
}
 
Example #6
Source File: ImageHandler.java    From Markwon with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public Object getSpans(
        @NonNull MarkwonConfiguration configuration,
        @NonNull RenderProps renderProps,
        @NonNull HtmlTag tag) {

    final Map<String, String> attributes = tag.attributes();
    final String src = attributes.get("src");
    if (TextUtils.isEmpty(src)) {
        return null;
    }

    final SpanFactory spanFactory = configuration.spansFactory().get(Image.class);
    if (spanFactory == null) {
        return null;
    }

    final String destination = configuration.imageDestinationProcessor().process(src);
    final ImageSize imageSize = imageSizeParser.parse(tag.attributes());

    // todo: replacement text is link... as we are not at block level
    // and cannot inspect the parent of this node... (img and a are both inlines)
    //
    // but we can look and see if we are inside a LinkSpan (will have to extend TagHandler
    // to obtain an instance SpannableBuilder for inspection)

    ImageProps.DESTINATION.set(renderProps, destination);
    ImageProps.IMAGE_SIZE.set(renderProps, imageSize);
    ImageProps.REPLACEMENT_TEXT_IS_LINK.set(renderProps, false);

    return spanFactory.getSpans(configuration, renderProps);
}
 
Example #7
Source File: MarkdownUtil.java    From Roothub with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void setAttributes(Node node, String s, Map<String, String> map) {
    // 给图片添加上类样式,类样式可以在css里自定义
    if (node instanceof Image) {
        map.put("class", "md-image");
    }
    // 给表格添加上类样式,类样式可以在css里自定义
    if (node instanceof TableBlock) {
        map.put("class", "table table-bordered");
    }
}
 
Example #8
Source File: IFrameHtmlPlugin.java    From Markwon with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public Object getSpans(@NonNull MarkwonConfiguration configuration, @NonNull RenderProps renderProps, @NonNull HtmlTag tag) {
    final ImageSize imageSize = new ImageSize(new ImageSize.Dimension(640, "px"), new ImageSize.Dimension(480, "px"));
    ImageProps.IMAGE_SIZE.set(renderProps, imageSize);
    ImageProps.DESTINATION.set(renderProps, "https://hey.com/1.png");
    return configuration.spansFactory().require(Image.class)
            .getSpans(configuration, renderProps);
}
 
Example #9
Source File: PicassoImagesPlugin.java    From Markwon with Apache License 2.0 4 votes vote down vote up
@Override
public void configureSpansFactory(@NonNull MarkwonSpansFactory.Builder builder) {
    builder.setFactory(Image.class, new ImageSpanFactory());
}
 
Example #10
Source File: ImageAttributesDelimiterProcessor.java    From commonmark-java with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public void process(Text opener, Text closer, int delimiterCount) {
    // Check if the attributes can be applied - if the previous node is an Image, and if all the attributes are in
    // the set of SUPPORTED_ATTRIBUTES
    if (opener.getPrevious() instanceof Image) {
        boolean canApply = true;
        List<Node> toUnlink = new ArrayList<>();

        Map<String, String> attributesMap = new LinkedHashMap<>();
        Node tmp = opener.getNext();
        while (tmp != null && tmp != closer) {
            Node next = tmp.getNext();
            // Only Text nodes can be used for attributes
            if (tmp instanceof Text) {
                String attributes = ((Text) tmp).getLiteral();
                for (String s : attributes.split("\\s+")) {
                    String[] attribute = s.split("=");
                    if (attribute.length > 1 && SUPPORTED_ATTRIBUTES.contains(attribute[0].toLowerCase())) {
                        attributesMap.put(attribute[0], attribute[1]);
                        // The tmp node can be unlinked, as we have retrieved its value.
                        toUnlink.add(tmp);
                    } else {
                        // This attribute is not supported, so break here (no need to check any further ones).
                        canApply = false;
                        break;
                    }
                }
            } else {
                // This node type is not supported, so break here (no need to check any further ones).
                canApply = false;
                break;
            }
            tmp = next;
        }

        // Only if all of the above checks pass can the attributes be applied.
        if (canApply) {
            // Unlink the tmp nodes
            for (Node node : toUnlink) {
                node.unlink();
            }

            if (attributesMap.size() > 0) {
                ImageAttributes imageAttributes = new ImageAttributes(attributesMap);

                // The new node is added as a child of the image node to which the attributes apply.
                Node nodeToStyle = opener.getPrevious();
                nodeToStyle.appendChild(imageAttributes);
            }
            return;
        }
    }

    // If we got here then the attributes cannot be applied, so fallback to leaving the text unchanged.
    // Need to add back the opening and closing characters (which are removed elsewhere).
    if (opener.getPrevious() == null) {
        opener.getParent().prependChild(new Text("" + getOpeningCharacter()));
    } else {
        opener.getPrevious().insertAfter(new Text("" + getOpeningCharacter()));
    }
    closer.getParent().appendChild(new Text("" + getClosingCharacter()));
}
 
Example #11
Source File: CoilImagesPlugin.java    From Markwon with Apache License 2.0 4 votes vote down vote up
@Override
public void configureSpansFactory(@NonNull MarkwonSpansFactory.Builder builder) {
    builder.setFactory(Image.class, new ImageSpanFactory());
}
 
Example #12
Source File: ImagesPlugin.java    From Markwon with Apache License 2.0 4 votes vote down vote up
@Override
public void configureSpansFactory(@NonNull MarkwonSpansFactory.Builder builder) {
    builder.setFactory(Image.class, new ImageSpanFactory());
}
 
Example #13
Source File: GlideImagesPlugin.java    From Markwon with Apache License 2.0 4 votes vote down vote up
@Override
public void configureSpansFactory(@NonNull MarkwonSpansFactory.Builder builder) {
    builder.setFactory(Image.class, new ImageSpanFactory());
}
 
Example #14
Source File: MarkwonSpansFactoryTest.java    From Markwon with Apache License 2.0 4 votes vote down vote up
@Test
public void builder_get_absent() {
    // nothing is present
    assertNull(builder.getFactory(Image.class));
}
 
Example #15
Source File: MarkwonVisitorImpl.java    From Markwon with Apache License 2.0 4 votes vote down vote up
@Override
public void visit(Image image) {
    visit((Node) image);
}
 
Example #16
Source File: MarkdownUtil.java    From ml-blog with MIT License 4 votes vote down vote up
@Override
public Set<Class<? extends Node>> getNodeTypes() {
    return Collections.singleton(Image.class);
}
 
Example #17
Source File: ImagesPluginTest.java    From Markwon with Apache License 2.0 3 votes vote down vote up
@Test
public void image_span_factory_registered() {

    final MarkwonSpansFactory.Builder builder = mock(MarkwonSpansFactory.Builder.class);

    plugin.configureSpansFactory(builder);

    final ArgumentCaptor<SpanFactory> captor = ArgumentCaptor.forClass(SpanFactory.class);

    verify(builder, times(1))
            .setFactory(eq(Image.class), captor.capture());

    assertNotNull(captor.getValue());
}