com.vladsch.flexmark.html.HtmlWriter Java Examples

The following examples show how to use com.vladsch.flexmark.html.HtmlWriter. 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: VideoLinkNodeRenderer.java    From MarkdownView with Apache License 2.0 6 votes vote down vote up
private void render(final VideoLink node, final NodeRendererContext context, final HtmlWriter html) {
    final String name = node.getText().toString();

    if (context.isDoNotRenderLinks()) {
        context.renderChildren(node);
    } else if (!TextUtils.isEmpty(name)) {
        ResolvedLink resolvedLink = context.resolveLink(LinkType.LINK, node.getUrl().unescape(), null);
        if (name.equals("youtube") ||
                name.equals("yt")) {
            html.attr("class", "player yt-player");
            html.withAttr().tag("div");
            html.attr("type", "text/html");
            html.attr("frameborder", "0");

            html.attr("allowfullscreen", "");
            html.attr("src", String.format("https://www.youtube.com/embed/%s", resolvedLink.getUrl()));
            html.srcPos(node.getChars()).withAttr(resolvedLink).tag("iframe");
            html.tag("/iframe");
            html.tag("/div");
        } else {
            context.renderChildren(node);
        }
    }
}
 
Example #2
Source File: EmojiNodeRenderer.java    From MarkdownView with Apache License 2.0 6 votes vote down vote up
private void render(Emoji node, NodeRendererContext context, HtmlWriter html) {
    Emoji emoji = node;
    EmojiCheatSheet.EmojiShortcut shortcut = EmojiCheatSheet.getImageShortcut(emoji.getText().toString());

    if (shortcut == null) {
        // output as text
        html.text(":");
        context.renderChildren(node);
        html.text(":");
    } else {
        ResolvedLink resolvedLink = context.resolveLink(LinkType.LINK, rootImagePath + shortcut.image + "." + extImage, null);

        html.attr("src", resolvedLink.getUrl());
        html.attr("alt", "emoji " + shortcut.category + ":" + shortcut.name);
        if (!attrImageSize.isEmpty())
            html.attr("height", attrImageSize).attr("width", attrImageSize);
        if (!attrAlign.isEmpty()) html.attr("align", attrAlign);
        html.withAttr(resolvedLink);
        html.tagVoid("img");
    }
}
 
Example #3
Source File: MarkNodeRenderer.java    From OmniList with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Set<NodeRenderingHandler<?>> getNodeRenderingHandlers() {
    HashSet<NodeRenderingHandler<?>> set = new HashSet<>();
    set.add(new NodeRenderingHandler<>(Mark.class, new CustomNodeRenderer<Mark>() {
        @Override
        public void render(Mark node, NodeRendererContext context, HtmlWriter html) {
            MarkNodeRenderer.this.render(node, context, html);
        }
    }));

    return set;
}
 
Example #4
Source File: UMLNodeRenderer.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
private void render(final UMLBlockQuote node, final NodeRendererContext context,
                    HtmlWriter html) {
  LOGGER.debug("Rendering HTML");

  String firstLine = node.getOpeningTrailing().toString();
  String[] splitWithSpace = firstLine.split(" ");

  LOGGER.debug("Start of the node {} ", firstLine);
  LOGGER.debug("Content within block {} ", node.getFirstChild().getChars());

  Map<String, String> paramMap = new HashMap<>();
  for (int i = 1; i < splitWithSpace.length; i++) {
    String[] splitWithEqual = splitWithSpace[i].split("=");
    paramMap.put(splitWithEqual[0], splitWithEqual[1]);
  }

  String url = "";

  if (splitWithSpace[0].equals(YUML) && !Objects.isNull(node.getFirstChild())) {
    url = createYumlUrl(paramMap, node.getFirstChild().getChars().toString());
    LOGGER.debug("Encoded YUML URL {} ", url);
  } else if (splitWithSpace[0].equals(SEQUENCE) && !Objects.isNull(node.getFirstChild())) {
    url = createWebsequenceUrl(paramMap.get("style"), node.getFirstChild().getChars().toString());
    LOGGER.debug("Encoded web sequence diagram URL {} ", url);
  } else {
    html.withAttr().tagLineIndent("blockquote", new Runnable() {
      @Override
      public void run() {
        context.renderChildren(node);
      }
    });
    return;
  }

  html.attr("src", url);
  html.attr("alt", "");
  html.srcPos(node.getChars()).withAttr().tagVoid("img");
}
 
Example #5
Source File: UMLNodeRenderer.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
@Override
public Set<NodeRenderingHandler<?>> getNodeRenderingHandlers() {
  Set<NodeRenderingHandler<?>> set = new HashSet<>();
  set.add(new NodeRenderingHandler<>(UMLBlockQuote.class,
      new CustomNodeRenderer<UMLBlockQuote>() {
        @Override
        public void render(UMLBlockQuote node, NodeRendererContext context,
                           HtmlWriter html) {
          UMLNodeRenderer.this.render(node, context, html);
        }
      }));
  return set;
}
 
Example #6
Source File: VideoLinkNodeRenderer.java    From MarkdownView with Apache License 2.0 5 votes vote down vote up
@Override
public Set<NodeRenderingHandler<?>> getNodeRenderingHandlers() {
    HashSet<NodeRenderingHandler<?>> set = new HashSet<>();
    set.add(new NodeRenderingHandler<>(VideoLink.class, new CustomNodeRenderer<VideoLink>() {
        @Override
        public void render(VideoLink node, NodeRendererContext context, HtmlWriter html) {
            VideoLinkNodeRenderer.this.render(node, context, html);
        }
    }));
    return set;
}
 
Example #7
Source File: BeanNodeRenderer.java    From MarkdownView with Apache License 2.0 5 votes vote down vote up
private void render(Bean node, NodeRendererContext context, HtmlWriter html) {
    if (mMarkdownView != null && mMarkdownView.getBean() != null) {
        String name = node.getText().toString().trim();
        Object value = getBeanAttributeValue(name, mMarkdownView.getBean());
        if (value != null) {
            html.attr("class", "bean");
            html.withAttr().tag("span");
            html.append(value.toString());
            html.tag("/span");
        }
    } else {
        context.renderChildren(node);
    }
}
 
Example #8
Source File: BeanNodeRenderer.java    From MarkdownView with Apache License 2.0 5 votes vote down vote up
@Override
public Set<NodeRenderingHandler<?>> getNodeRenderingHandlers() {
    HashSet<NodeRenderingHandler<?>> set = new HashSet<>();
    set.add(new NodeRenderingHandler<>(Bean.class, new CustomNodeRenderer<Bean>() {
        @Override
        public void render(Bean node, NodeRendererContext context, HtmlWriter html) {
            BeanNodeRenderer.this.render(node, context, html);
        }
    }));

    return set;
}
 
Example #9
Source File: EmojiNodeRenderer.java    From MarkdownView with Apache License 2.0 5 votes vote down vote up
@Override
public Set<NodeRenderingHandler<?>> getNodeRenderingHandlers() {
    HashSet<NodeRenderingHandler<?>> set = new HashSet<>();
    set.add(new NodeRenderingHandler<>(Emoji.class, new CustomNodeRenderer<Emoji>() {
        @Override
        public void render(Emoji node, NodeRendererContext context, HtmlWriter html) {
            EmojiNodeRenderer.this.render(node, context, html);
        }
    }));
    return set;
}
 
Example #10
Source File: LabelNodeRenderer.java    From MarkdownView with Apache License 2.0 5 votes vote down vote up
private void render(Label node, NodeRendererContext context, HtmlWriter html) {
    if (node.getType() == 3) html.attr("class", "lbl-success");
    else if (node.getType() == 4) html.attr("class", "lbl-warning");
    else if (node.getType() == 5) html.attr("class", "lbl-danger");
    html.withAttr().tag("lbl");
    context.renderChildren(node);
    html.tag("/lbl");
}
 
Example #11
Source File: LabelNodeRenderer.java    From MarkdownView with Apache License 2.0 5 votes vote down vote up
@Override
public Set<NodeRenderingHandler<?>> getNodeRenderingHandlers() {
    HashSet<NodeRenderingHandler<?>> set = new HashSet<>();
    set.add(new NodeRenderingHandler<>(Label.class, new CustomNodeRenderer<Label>() {
        @Override
        public void render(Label node, NodeRendererContext context, HtmlWriter html) {
            LabelNodeRenderer.this.render(node, context, html);
        }
    }));

    return set;
}
 
Example #12
Source File: MathJaxNodeRenderer.java    From MarkdownView with Apache License 2.0 5 votes vote down vote up
private void render(MathJax node, NodeRendererContext context, HtmlWriter html) {
    html.withAttr().attr("class", "math").tag("span");
    if (node.isInline()) {
        html.append("\\(");
    } else {
        html.append("$$");
    }
    context.renderChildren(node);
    if (node.isInline()) {
        html.append("\\)");
    } else {
        html.append("$$");
    }
    html.tag("/span");
}
 
Example #13
Source File: MathJaxNodeRenderer.java    From MarkdownView with Apache License 2.0 5 votes vote down vote up
@Override
public Set<NodeRenderingHandler<?>> getNodeRenderingHandlers() {
    HashSet<NodeRenderingHandler<?>> set = new HashSet<>();
    set.add(new NodeRenderingHandler<>(MathJax.class, new CustomNodeRenderer<MathJax>() {
        @Override
        public void render(MathJax node, NodeRendererContext context, HtmlWriter html) {
            MathJaxNodeRenderer.this.render(node, context, html);
        }
    }));

    return set;
}
 
Example #14
Source File: MarkNodeRenderer.java    From MarkdownView with Apache License 2.0 5 votes vote down vote up
@Override
public Set<NodeRenderingHandler<?>> getNodeRenderingHandlers() {
    HashSet<NodeRenderingHandler<?>> set = new HashSet<>();
    set.add(new NodeRenderingHandler<>(Mark.class, new CustomNodeRenderer<Mark>() {
        @Override
        public void render(Mark node, NodeRendererContext context, HtmlWriter html) {
            MarkNodeRenderer.this.render(node, context, html);
        }
    }));

    return set;
}
 
Example #15
Source File: KeystrokeNodeRenderer.java    From MarkdownView with Apache License 2.0 5 votes vote down vote up
@Override
public Set<NodeRenderingHandler<?>> getNodeRenderingHandlers() {
    HashSet<NodeRenderingHandler<?>> set = new HashSet<>();
    set.add(new NodeRenderingHandler<>(Keystroke.class, new CustomNodeRenderer<Keystroke>() {
        @Override
        public void render(Keystroke node, NodeRendererContext context, HtmlWriter html) {
            KeystrokeNodeRenderer.this.render(node, context, html);
        }
    }));

    return set;
}
 
Example #16
Source File: TwitterNodeRenderer.java    From MarkdownView with Apache License 2.0 5 votes vote down vote up
@Override
public Set<NodeRenderingHandler<?>> getNodeRenderingHandlers() {
    HashSet<NodeRenderingHandler<?>> set = new HashSet<>();
    set.add(new NodeRenderingHandler<>(Twitter.class, new CustomNodeRenderer<Twitter>() {
        @Override
        public void render(Twitter node, NodeRendererContext context, HtmlWriter html) {
            TwitterNodeRenderer.this.render(node, context, html);
        }
    }));
    return set;
}
 
Example #17
Source File: EmojiNodeRenderer.java    From onedev with MIT License 5 votes vote down vote up
private void render(EmojiNode node, NodeRendererContext context, HtmlWriter html) {
  	String emojiName = node.getText().toString();
  	String emojiUrl = null;
if (RequestCycle.get() != null) {
	String emojiCode = EmojiOnes.getInstance().all().get(emojiName);
	if (emojiCode != null) {
		emojiUrl = RequestCycle.get().urlFor(new PackageResourceReference(
				EmojiOnes.class, "icon/" + emojiCode + ".png"), new PageParameters()).toString();
	} 
}

if (emojiUrl != null) {
          ResolvedLink resolvedLink = context.resolveLink(LinkType.IMAGE, emojiUrl, null);
          html.attr("src", emojiUrl);
          html.attr("alt", "emoji " + emojiName);
          if (!attrImageSize.isEmpty()) 
          	html.attr("height", attrImageSize).attr("width", attrImageSize);
          if (!attrAlign.isEmpty()) 
          	html.attr("align", attrAlign);
          html.withAttr(resolvedLink);
          html.tagVoid("img");
} else {
          // output as text
          html.text(":");
          context.renderChildren(node);
          html.text(":");
}

  }
 
Example #18
Source File: EmojiNodeRenderer.java    From onedev with MIT License 5 votes vote down vote up
@Override
public Set<NodeRenderingHandler<?>> getNodeRenderingHandlers() {
    HashSet<NodeRenderingHandler<?>> set = new HashSet<NodeRenderingHandler<?>>();
    set.add(new NodeRenderingHandler<EmojiNode>(EmojiNode.class, new CustomNodeRenderer<EmojiNode>() {
        @Override
        public void render(EmojiNode node, NodeRendererContext context, HtmlWriter html) {
            EmojiNodeRenderer.this.render(node, context, html);
        }
    }));
    return set;
}
 
Example #19
Source File: MathJaxNodeRenderer.java    From OmniList with GNU Affero General Public License v3.0 5 votes vote down vote up
private void render(MathJax node, NodeRendererContext context, HtmlWriter html) {
    html.withAttr().attr("class", "math").tag("span");
    if (node.isInline()) {
        html.append("\\(");
    } else {
        html.append("$$");
    }
    context.renderChildren(node);
    if (node.isInline()) {
        html.append("\\)");
    } else {
        html.append("$$");
    }
    html.tag("/span");
}
 
Example #20
Source File: MathJaxNodeRenderer.java    From OmniList with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Set<NodeRenderingHandler<?>> getNodeRenderingHandlers() {
    HashSet<NodeRenderingHandler<?>> set = new HashSet<>();
    set.add(new NodeRenderingHandler<>(MathJax.class, new CustomNodeRenderer<MathJax>() {
        @Override
        public void render(MathJax node, NodeRendererContext context, HtmlWriter html) {
            MathJaxNodeRenderer.this.render(node, context, html);
        }
    }));

    return set;
}
 
Example #21
Source File: MarkNodeRenderer.java    From MarkdownView with Apache License 2.0 4 votes vote down vote up
private void render(Mark node, NodeRendererContext context, HtmlWriter html) {
    html.srcPos(node.getText()).withAttr().tag("mark");
    context.renderChildren(node);
    html.tag("/mark");
}
 
Example #22
Source File: KeystrokeNodeRenderer.java    From MarkdownView with Apache License 2.0 4 votes vote down vote up
private void render(Keystroke node, NodeRendererContext context, HtmlWriter html) {
    html.withAttr().tag("kbd");
    html.append(node.getText().trim());
    html.tag("/kbd");
}
 
Example #23
Source File: TwitterNodeRenderer.java    From MarkdownView with Apache License 2.0 4 votes vote down vote up
private void render(final Twitter node, final NodeRendererContext context, final HtmlWriter html) {
    final String name = node.getText().toString();

    if (context.isDoNotRenderLinks()) {
        context.renderChildren(node);
    } else if (!TextUtils.isEmpty(name)) {
        String value, url;

        try {
            value = URLEncoder.encode(context.resolveLink(LinkType.LINK, node.getUrl().unescape(), null).getUrl(), "utf-8");
        } catch (UnsupportedEncodingException e) {
            context.renderChildren(node);
            return;
        }

        if (name.equals("tweet")) {
            url = String.format("https://publish.twitter.com/oembed?url=https://twitter.com/twitter/status/%s", value);
        } else if (name.equals("tweet-hide-cards")) {
            url = String.format("https://publish.twitter.com/oembed?url=https://twitter.com/twitter/status/%s&hide_media=true", value);
        } else if (name.equals("follow")) {
            mHtml = String.format("<a href=\"https://twitter.com/%s\" data-size=\"large\" class=\"twitter-follow-button\" data-show-count=\"true\">Follow @%s</a><script async src=\"https://platform.twitter.com/widgets.js\" charset=\"utf-8\"></script>",
                    value, value);
            html.srcPos(node.getChars()).append(mHtml);
            return;
        } else {
            context.renderChildren(node);
            return;
        }

        mHtml = null;
        new Thread(new LoadTweetRunnable(url)).start();
        mCondition.close();
        mCondition.block();

        if (mHtml == null) {
            context.renderChildren(node);
        } else {
            mHtml = mHtml.replaceAll("src=\"//", "src=\"https://");
            html.srcPos(node.getChars()).append(mHtml);
        }
    }
}
 
Example #24
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 #25
Source File: MarkNodeRenderer.java    From OmniList with GNU Affero General Public License v3.0 4 votes vote down vote up
private void render(Mark node, NodeRendererContext context, HtmlWriter html) {
    html.srcPos(node.getText()).withAttr().tag("mark");
    context.renderChildren(node);
    html.tag("/mark");
}
 
Example #26
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;
        }
    };
}