Search in sources :

Example 1 with RefLinkNode

use of org.pegdown.ast.RefLinkNode 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)

Aggregations

LinkRenderer (org.pegdown.LinkRenderer)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 WikiLinkNode (org.pegdown.ast.WikiLinkNode)1