Search in sources :

Example 1 with LinkRenderer

use of org.pegdown.LinkRenderer in project webprotege by protegeproject.

the class CommentRenderer method renderComment.

public String renderComment(String commentBody) {
    List<ParsedMention> parsedMentions = mentionParser.parseMentions(commentBody);
    StringBuilder rendering = new StringBuilder();
    int currentPos = 0;
    for (ParsedMention pm : parsedMentions) {
        int startIndex = pm.getStartIndex();
        int endIndex = pm.getEndIndex();
        rendering.append(commentBody.substring(currentPos, startIndex));
        Mention mention = pm.getParsedMention();
        if (mention.getMentionedUserId().isPresent()) {
            rendering.append("<span class=\"wp-comment__user-mention\">");
            rendering.append(mention.getMentionedUserId().get().getUserName());
            rendering.append("</span>");
        } else {
            rendering.append(commentBody.substring(startIndex, endIndex));
        }
        currentPos = endIndex;
    }
    if (currentPos < commentBody.length()) {
        rendering.append(commentBody.substring(currentPos));
    }
    PegDownProcessor processor = new PegDownProcessor(Extensions.ABBREVIATIONS | Extensions.QUOTES | Extensions.STRIKETHROUGH | Extensions.AUTOLINKS | Extensions.FENCED_CODE_BLOCKS, new PegDownPlugins.Builder().build());
    String html = processor.markdownToHtml(rendering.toString(), new LinkRenderer() {
    });
    return html;
}
Also used : LinkRenderer(org.pegdown.LinkRenderer) ParsedMention(edu.stanford.bmir.protege.web.shared.issues.mention.ParsedMention) Mention(edu.stanford.bmir.protege.web.shared.issues.Mention) ParsedMention(edu.stanford.bmir.protege.web.shared.issues.mention.ParsedMention) PegDownProcessor(org.pegdown.PegDownProcessor)

Example 2 with LinkRenderer

use of org.pegdown.LinkRenderer in project gitblit by gitblit.

the class MarkupProcessor method parse.

/**
 * Parses the document as Markdown using Pegdown.
 *
 * @param doc
 * @param repositoryName
 * @param commitId
 */
private void parse(final MarkupDocument doc, final String repositoryName, final String commitId) {
    LinkRenderer renderer = new LinkRenderer() {

        @Override
        public Rendering render(ExpImageNode node, String text) {
            if (!isFullUrl(node.url)) {
                // repository-relative image link
                String path = doc.getRelativePath(node.url);
                String contextUrl = RequestCycle.get().getRequest().getRelativePathPrefixToContextRoot();
                String url = RawServlet.asLink(contextUrl, repositoryName, commitId, path);
                return new Rendering(url, text);
            }
            // absolute image link
            return new Rendering(node.url, text);
        }

        @Override
        public Rendering render(RefImageNode node, String url, String title, String alt) {
            Rendering rendering;
            if (!isFullUrl(url)) {
                // repository-relative image link
                String path = doc.getRelativePath(url);
                String contextUrl = RequestCycle.get().getRequest().getRelativePathPrefixToContextRoot();
                String wurl = RawServlet.asLink(contextUrl, repositoryName, commitId, path);
                rendering = new Rendering(wurl, alt);
            } else {
                // absolute image link
                rendering = new Rendering(url, alt);
            }
            return StringUtils.isEmpty(title) ? rendering : rendering.withAttribute("title", encode(title));
        }

        @Override
        public Rendering render(WikiLinkNode node) {
            String path = doc.getRelativePath(node.getText());
            String name = getDocumentName(path);
            String url = getWicketUrl(DocPage.class, repositoryName, commitId, path);
            return new Rendering(url, name);
        }

        @Override
        public Rendering render(ExpLinkNode node, String text) {
            // like "/xx/tt"  or "../somefolder".
            if (isFullUrl(node.url)) {
                // This is URL, fallback to superclass.
                return super.render(node, text);
            }
            // repository-relative link
            String path = doc.getRelativePath(node.url);
            String url = getWicketUrl(DocPage.class, repositoryName, commitId, path);
            return new Rendering(url, text);
        }

        @Override
        public Rendering render(RefLinkNode node, String url, String title, String text) {
            if (isFullUrl(url)) {
                // This is URL, fallback to superclass.
                return super.render(node, url, title, text);
            }
            // repository-relative link
            String path = doc.getRelativePath(url);
            String local_url = getWicketUrl(DocPage.class, repositoryName, commitId, path);
            return super.render(node, local_url, title, text);
        }
    };
    final String content = MarkdownUtils.transformMarkdown(doc.markup, renderer);
    final String safeContent = xssFilter.relaxed(content);
    doc.html = safeContent;
}
Also used : LinkRenderer(org.pegdown.LinkRenderer) RefImageNode(org.pegdown.ast.RefImageNode) WikiLinkNode(org.pegdown.ast.WikiLinkNode) RefLinkNode(org.pegdown.ast.RefLinkNode) ExpImageNode(org.pegdown.ast.ExpImageNode) ExpLinkNode(org.pegdown.ast.ExpLinkNode)

Example 3 with LinkRenderer

use of org.pegdown.LinkRenderer in project gitblit by gitblit.

the class MarkdownUtils method transformMarkdown.

/**
 * Returns the html version of the markdown source text.
 *
 * @param markdown
 * @return html version of markdown text
 * @throws java.text.ParseException
 */
public static String transformMarkdown(String markdown, LinkRenderer linkRenderer) {
    try {
        PegDownProcessor pd = new PegDownProcessor(ALL & ~SMARTYPANTS & ~ANCHORLINKS);
        RootNode astRoot = pd.parseMarkdown(markdown.toCharArray());
        return new WorkaroundHtmlSerializer(linkRenderer == null ? new LinkRenderer() : linkRenderer).toHtml(astRoot);
    } catch (ParsingTimeoutException e) {
        return null;
    }
}
Also used : RootNode(org.pegdown.ast.RootNode) LinkRenderer(org.pegdown.LinkRenderer) WorkaroundHtmlSerializer(com.gitblit.wicket.MarkupProcessor.WorkaroundHtmlSerializer) ParsingTimeoutException(org.pegdown.ParsingTimeoutException) PegDownProcessor(org.pegdown.PegDownProcessor)

Aggregations

LinkRenderer (org.pegdown.LinkRenderer)3 PegDownProcessor (org.pegdown.PegDownProcessor)2 WorkaroundHtmlSerializer (com.gitblit.wicket.MarkupProcessor.WorkaroundHtmlSerializer)1 Mention (edu.stanford.bmir.protege.web.shared.issues.Mention)1 ParsedMention (edu.stanford.bmir.protege.web.shared.issues.mention.ParsedMention)1 ParsingTimeoutException (org.pegdown.ParsingTimeoutException)1 ExpImageNode (org.pegdown.ast.ExpImageNode)1 ExpLinkNode (org.pegdown.ast.ExpLinkNode)1 RefImageNode (org.pegdown.ast.RefImageNode)1 RefLinkNode (org.pegdown.ast.RefLinkNode)1 RootNode (org.pegdown.ast.RootNode)1 WikiLinkNode (org.pegdown.ast.WikiLinkNode)1