com.vladsch.flexmark.ast.Image Java Examples

The following examples show how to use com.vladsch.flexmark.ast.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: SmartEdit.java    From markdown-writer-fx with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void insertImage() {
	LinkNode linkNode = findNodeAtSelection((s, e, n) -> n instanceof LinkNode);
	if (linkNode != null && !(linkNode instanceof Image)) {
		// link node at caret is not supported --> insert image before or after
		if (textArea.getCaretPosition() != linkNode.getStartOffset())
			selectRange(textArea, linkNode.getEndOffset(), linkNode.getEndOffset());
		linkNode = null;
	}

	Image image = (Image) linkNode;
	if (image != null)
		selectRange(textArea, image.getStartOffset(), image.getEndOffset());

	ImageDialog dialog = new ImageDialog(editor.getNode().getScene().getWindow(), editor.getParentPath());
	if (image != null)
		dialog.init(image.getUrl().toString(), image.getText().toString(), image.getTitle().toString());
	dialog.showAndWait().ifPresent(result -> {
		if (image != null)
			replaceText(textArea, image.getStartOffset(), image.getEndOffset(), result);
		else
			replaceSelection(textArea, result);
	});
}
 
Example #2
Source File: MarkdownParser.java    From sarl with Apache License 2.0 6 votes vote down vote up
/** Create a validation component for an image reference.
 *
 * @param it the image reference.
 * @param currentFile the current file.
 * @param context the validation context.
 * @return the validation components.
 */
protected Iterable<DynamicValidationComponent> createValidatorComponents(Image it, File currentFile,
		DynamicValidationContext context) {
	final Collection<DynamicValidationComponent> components = new ArrayList<>();
	if (isLocalImageReferenceValidation()) {
		final int lineno = computeLineNo(it);
		final URL url = FileSystem.convertStringToURL(it.getUrl().toString(), true);
		if (URISchemeType.FILE.isURL(url)) {
			final DynamicValidationComponent component = createLocalImageValidatorComponent(
					it, url, lineno, currentFile, context);
			if (component != null) {
				components.add(component);
			}
		}
	}
	return components;
}
 
Example #3
Source File: MarkdownView.java    From MarkdownView with Apache License 2.0 5 votes vote down vote up
@Override
public void setAttributes(final Node node, final AttributablePart part, final Attributes attributes) {
    if (node instanceof FencedCodeBlock) {
        if (part.getName().equals("NODE")) {
            String language = ((FencedCodeBlock) node).getInfo().toString();
            if (!TextUtils.isEmpty(language) &&
                    !language.equals("nohighlight")) {
                addJavascript(HIGHLIGHTJS);
                addJavascript(HIGHLIGHT_INIT);

                attributes.addValue("language", language);
                //attributes.addValue("onclick", String.format("javascript:android.onCodeTap('%s', this.textContent);",
                //        language));
            }
        }
    } else if (node instanceof MathJax) {
        addJavascript(MATHJAX);
        addJavascript(MATHJAX_CONFIG);
    } else if (node instanceof Abbreviation) {
        addJavascript(TOOLTIPSTER_JS);
        addStyleSheet(TOOLTIPSTER_CSS);
        addJavascript(TOOLTIPSTER_INIT);
        attributes.addValue("class", "tooltip");
    } else if (node instanceof Heading) {
        //attributes.addValue("onclick", String.format("javascript:android.onHeadingTap(%d, '%s');",
        //        ((Heading) node).getLevel(), ((Heading) node).getText()));
    } else if (node instanceof Image) {
        //attributes.addValue("onclick", String.format("javascript: android.onImageTap(this.src, this.clientWidth, this.clientHeight);"));
    } else if (node instanceof Mark) {
        //attributes.addValue("onclick", String.format("javascript: android.onMarkTap(this.textContent)"));
    } else if (node instanceof Keystroke) {
        //attributes.addValue("onclick", String.format("javascript: android.onKeystrokeTap(this.textContent)"));
    } else if (node instanceof Link ||
            node instanceof AutoLink) {
        //attributes.addValue("onclick", String.format("javascript: android.onLinkTap(this.href, this.textContent)"));
    }
}
 
Example #4
Source File: NodeRendererFactoryImpl.java    From OmniList with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public NodeRenderer create(DataHolder options) {
    return new NodeRenderer() {
        @Override
        public Set<NodeRenderingHandler<?>> getNodeRenderingHandlers() {
            HashSet<NodeRenderingHandler<?>> set = new HashSet<>();
            set.add(new NodeRenderingHandler<>(Image.class, new CustomNodeRenderer<Image>() {
                @Override
                public void render(Image node, NodeRendererContext context, HtmlWriter html) {
                    if (!context.isDoNotRenderLinks()) {
                        String altText = new TextCollectingVisitor().collectAndGetText(node);

                        ResolvedLink resolvedLink = context.resolveLink(LinkType.IMAGE, node.getUrl().unescape(), null);
                        String url = resolvedLink.getUrl();

                        if (!node.getUrlContent().isEmpty()) {
                            // reverse URL encoding of =, &
                            String content = Escaping.percentEncodeUrl(node.getUrlContent()).replace("+", "%2B").replace("%3D", "=").replace("%26", "&amp;");
                            url += content;
                        }

                        final int index = url.indexOf('@');

                        if (index >= 0) {
                            String[] dimensions = url.substring(index + 1, url.length()).split("\\|");
                            url = url.substring(0, index);

                            if (dimensions.length == 2) {
                                String width = dimensions[0] == null || dimensions[0].equals("") ? "auto" : dimensions[0];
                                String height = dimensions[1] == null || dimensions[1].equals("") ? "auto" : dimensions[1];
                                html.attr("style", "width: " + width + "; height: " + height);
                            }
                        }

                        html.attr("src", url);
                        html.attr("alt", altText);

                        if (node.getTitle().isNotNull()) {
                            html.attr("title", node.getTitle().unescape());
                        }

                        html.srcPos(node.getChars()).withAttr(resolvedLink).tagVoid("img");
                    }
                }
            }));
            return set;
        }
    };
}
 
Example #5
Source File: MarkdownView.java    From MarkdownView with Apache License 2.0 4 votes vote down vote up
@Override
public NodeRenderer create(DataHolder options) {
    return new NodeRenderer() {
        @Override
        public Set<NodeRenderingHandler<?>> getNodeRenderingHandlers() {
            HashSet<NodeRenderingHandler<?>> set = new HashSet<>();
            set.add(new NodeRenderingHandler<>(Image.class, new CustomNodeRenderer<Image>() {
                @Override
                public void render(Image node, NodeRendererContext context, HtmlWriter html) {
                    if (!context.isDoNotRenderLinks()) {
                        String altText = new TextCollectingVisitor().collectAndGetText(node);

                        ResolvedLink resolvedLink = context.resolveLink(LinkType.IMAGE, node.getUrl().unescape(), null);
                        String url = resolvedLink.getUrl();

                        if (!node.getUrlContent().isEmpty()) {
                            // reverse URL encoding of =, &
                            String content = Escaping.percentEncodeUrl(node.getUrlContent()).replace("+", "%2B").replace("%3D", "=").replace("%26", "&amp;");
                            url += content;
                        }

                        final int index = url.indexOf('@');

                        if (index >= 0) {
                            String[] dimensions = url.substring(index + 1, url.length()).split("\\|");
                            url = url.substring(0, index);

                            if (dimensions.length == 2) {
                                String width = TextUtils.isEmpty(dimensions[0]) ? "auto" : dimensions[0];
                                String height = TextUtils.isEmpty(dimensions[1]) ? "auto" : dimensions[1];
                                html.attr("style", "width: " + width + "; height: " + height);
                            }
                        }

                        html.attr("src", url);
                        html.attr("alt", altText);

                        if (node.getTitle().isNotNull()) {
                            html.attr("title", node.getTitle().unescape());
                        }

                        html.srcPos(node.getChars()).withAttr(resolvedLink).tagVoid("img");
                    }
                }
            }));
            return set;
        }
    };
}
 
Example #6
Source File: SmartEdit.java    From markdown-writer-fx with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private void updateStateProperties() {
	// avoid too many (and useless) runLater() invocations
	if (updateStatePropertiesRunLaterPending)
		return;
	updateStatePropertiesRunLaterPending = true;

	Platform.runLater(() -> {
		updateStatePropertiesRunLaterPending = false;

		List<Node> nodesAtSelection = findNodesAtSelection((s, e, n) -> true, true, false);

		boolean bold = false;
			boolean italic = false;
			boolean code = false;
			boolean link = false;
			boolean image = false;
			boolean unorderedList = false;
			boolean orderedList = false;
			boolean blockquote = false;
			boolean fencedCode = false;
			boolean header = false;
		for (Node node : nodesAtSelection) {
			if (!bold && node instanceof StrongEmphasis)
				bold = true;
			else if (!italic && node instanceof Emphasis)
				italic = true;
			else if (!code && node instanceof Code)
				code = true;
			else if (!link && (node instanceof Link || node instanceof LinkRef))
				link = true;
			else if (!image && (node instanceof Image || node instanceof ImageRef))
				image = true;
			else if (!unorderedList && node instanceof BulletListItem)
				unorderedList = true;
			else if (!orderedList && node instanceof OrderedListItem)
				orderedList = true;
			else if (!blockquote && node instanceof BlockQuote)
				blockquote = true;
			else if (!fencedCode && node instanceof FencedCodeBlock)
				fencedCode = true;
			else if (!header && node instanceof Heading)
				header = true;
		}
		this.bold.set(bold);
		this.italic.set(italic);
		this.code.set(code);
		this.link.set(link);
		this.image.set(image);
		this.unorderedList.set(unorderedList);
		this.orderedList.set(orderedList);
		this.blockquote.set(blockquote);
		this.fencedCode.set(fencedCode);
		this.header.set(header);
	});
}