All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.vladsch.flexmark.ext.emoji.internal.EmojiNodeRenderer Maven / Gradle / Ivy

Go to download

flexmark-java extension for emoji shortcuts using Emoji-Cheat-Sheet.com http://www.emoji-cheat-sheet.com/

There is a newer version: 0.64.8
Show newest version
package com.vladsch.flexmark.ext.emoji.internal;

import com.vladsch.flexmark.ext.emoji.Emoji;
import com.vladsch.flexmark.html.CustomNodeRenderer;
import com.vladsch.flexmark.html.HtmlWriter;
import com.vladsch.flexmark.html.renderer.*;
import com.vladsch.flexmark.util.data.DataHolder;

import java.util.HashSet;
import java.util.Set;

public class EmojiNodeRenderer implements NodeRenderer {
    final EmojiOptions myOptions;

    public EmojiNodeRenderer(DataHolder options) {
        myOptions = new EmojiOptions(options);
    }

    @Override
    public Set> getNodeRenderingHandlers() {
        HashSet> set = new HashSet>();
        set.add(new NodeRenderingHandler(Emoji.class, new CustomNodeRenderer() {
            @Override
            public void render(Emoji node, NodeRendererContext context, HtmlWriter html) {
                EmojiNodeRenderer.this.render(node, context, html);
            }
        }));
        return set;
    }

    private void render(Emoji node, NodeRendererContext context, HtmlWriter html) {
        EmojiResolvedShortcut shortcut = EmojiResolvedShortcut.getEmojiText(node, myOptions.useShortcutType, myOptions.useImageType, myOptions.rootImagePath);

        if (shortcut.emoji == null || shortcut.emojiText == null) {
            // output as text
            html.text(":");
            context.renderChildren(node);
            html.text(":");
        } else {
            if (shortcut.isUnicode) {
                html.text(shortcut.emojiText);
            } else {
                ResolvedLink resolvedLink = context.resolveLink(LinkType.IMAGE, shortcut.emojiText, null);

                html.attr("src", resolvedLink.getUrl());
                html.attr("alt", shortcut.alt);
                if (!myOptions.attrImageSize.isEmpty()) html.attr("height", myOptions.attrImageSize).attr("width", myOptions.attrImageSize);
                if (!myOptions.attrAlign.isEmpty()) html.attr("align", myOptions.attrAlign);
                if (!myOptions.attrImageClass.isEmpty()) html.attr("class", myOptions.attrImageClass);
                html.withAttr(resolvedLink);
                html.tagVoid("img");
            }
        }
    }

    public static class Factory implements NodeRendererFactory {
        @Override
        public NodeRenderer apply(DataHolder options) {
            return new EmojiNodeRenderer(options);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy