Search in sources :

Example 1 with MarkdownDoclet

use of ch.raffael.mddoclet.MarkdownDoclet in project markdown-doclet by Abnaxos.

the class DocCommentProcessor method processDocComment.

/**
 * Process the given `PsiDocComment` using Markdown and return a new `PsiDocComment`
 * representing the resulting HTML. The resulting `PsiDocComment` can then be passed
 * to IDEA's default QuickDoc implementation.
 *
 * @todo Error reporting ({@link com.sun.javadoc.DocErrorReporter} without {@link com.sun.javadoc.RootDoc}).
 *
 * @param docComment    The `PsiDocComment` to process.
 *
 * @return A `PsiDocComment` representing the resulting HTML.
 */
public PsiDocComment processDocComment(PsiDocComment docComment) {
    if (!isEnabled() || docComment == null) {
        return docComment;
    }
    final Map<String, URL> umlDiagrams = generateUmlDiagrams(docComment);
    Options options = new Options() {

        @Override
        protected ToHtmlSerializer createDocletSerializer() {
            return new DocletSerializer(this, getLinkRenderer()) {

                @Override
                protected void printImageTag(LinkRenderer.Rendering rendering) {
                    URL diagram = umlDiagrams.get(rendering.href);
                    if (diagram != null) {
                        super.printImageTag(mapHref(rendering, diagram.toString()));
                    } else if (!ABSOLUTE_IMG_RE.matcher(rendering.href).matches() || rendering.href.contains("{@}")) {
                        URL baseUrl = VfsUtilCore.convertToURL(file.getVirtualFile().getUrl());
                        if (baseUrl != null) {
                            try {
                                super.printImageTag(mapHref(rendering, new URL(baseUrl, rendering.href).toString()));
                            } catch (MalformedURLException e) {
                                super.printImageTag(rendering);
                            }
                        }
                    } else {
                        super.printImageTag(rendering);
                    }
                }

                private LinkRenderer.Rendering mapHref(LinkRenderer.Rendering rendering, String newHref) {
                    LinkRenderer.Rendering mapped = new LinkRenderer.Rendering(newHref, rendering.text);
                    mapped.attributes.addAll(rendering.attributes);
                    return mapped;
                }
            };
        }
    };
    PsiDocCommentOwner context = PsiTreeUtil.getParentOfType(docComment, PsiDocCommentOwner.class);
    markdownOptions.applyTo(options);
    MarkdownDoclet doclet = new MarkdownDoclet(options, null);
    MarkdownTaglets.instance().setDocErrorReporter(doclet);
    StringBuilder buf = new StringBuilder();
    StringBuilder tagBlock = new StringBuilder();
    boolean start = true;
    // List<String> tagBlockInlineTags = new ArrayList<>();
    for (PsiElement elem : docComment.getChildren()) {
        if (elem instanceof PsiDocToken) {
            IElementType tokenType = ((PsiDocToken) elem).getTokenType();
            if (tokenType == JavaDocTokenType.DOC_COMMENT_START || tokenType == JavaDocTokenType.DOC_COMMENT_END) {
                continue;
            }
        }
        if (start && elem instanceof PsiWhiteSpace) {
            continue;
        } else if (elem instanceof PsiInlineDocTag) {
            start = false;
            if (tagBlock.length() == 0) {
                // inlineTags.add(elem.getText());
                buf.append(elem.getText());
            } else {
                // tagBlockInlineTags.add(elem.getText());
                tagBlock.append(elem.getText());
            }
        } else if (elem instanceof PsiDocTag) {
            PsiDocTag docTag = (PsiDocTag) elem;
            switch(docTag.getName()) {
                case "see":
                    tagBlock.append('\n');
                    renderSeeTag(doclet, tagBlock, docTag);
                    break;
                case "param":
                case "throws":
                case "exception":
                    renderSimpleTag(doclet, tagBlock, docTag, true);
                    break;
                case "return":
                    renderSimpleTag(doclet, tagBlock, docTag, false);
                    break;
                case "todo":
                    renderTodoTag(doclet, tagBlock, docTag);
                    break;
                default:
                    tagBlock.append('\n').append(stripLead(elem.getText()));
                    break;
            }
        } else {
            start = false;
            if (tagBlock.length() == 0) {
                buf.append(elem.getText());
            } else {
                tagBlock.append(elem.getText());
            }
        }
    }
    String markdown = stripLead(buf.toString());
    Plugin.print("Markdown source", markdown);
    String docCommentText = "/**\n" + escapeAsterisks(doclet.toHtml(markdown, false)) + "\n" + escapeAsterisks(tagBlock.toString()) + "\n*/";
    Plugin.print("Processed DocComment", docCommentText);
    PsiDocComment processedDocComment = docCommentFromText(context, docCommentText);
    LinkExpander linkExpander = new LinkExpander(context, processedDocComment, docCommentText);
    processedDocComment = linkExpander.apply();
    return processedDocComment;
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) Options(ch.raffael.mddoclet.Options) MalformedURLException(java.net.MalformedURLException) PsiDocTag(com.intellij.psi.javadoc.PsiDocTag) MarkdownDoclet(ch.raffael.mddoclet.MarkdownDoclet) DocletSerializer(ch.raffael.mddoclet.DocletSerializer) PsiDocCommentOwner(com.intellij.psi.PsiDocCommentOwner) PsiDocToken(com.intellij.psi.javadoc.PsiDocToken) URL(java.net.URL) IElementType(com.intellij.psi.tree.IElementType) LinkRenderer(org.pegdown.LinkRenderer) PsiInlineDocTag(com.intellij.psi.javadoc.PsiInlineDocTag) TagRendering(ch.raffael.mddoclet.tags.TagRendering) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Aggregations

DocletSerializer (ch.raffael.mddoclet.DocletSerializer)1 MarkdownDoclet (ch.raffael.mddoclet.MarkdownDoclet)1 Options (ch.raffael.mddoclet.Options)1 TagRendering (ch.raffael.mddoclet.tags.TagRendering)1 PsiDocCommentOwner (com.intellij.psi.PsiDocCommentOwner)1 PsiElement (com.intellij.psi.PsiElement)1 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)1 PsiDocComment (com.intellij.psi.javadoc.PsiDocComment)1 PsiDocTag (com.intellij.psi.javadoc.PsiDocTag)1 PsiDocToken (com.intellij.psi.javadoc.PsiDocToken)1 PsiInlineDocTag (com.intellij.psi.javadoc.PsiInlineDocTag)1 IElementType (com.intellij.psi.tree.IElementType)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 LinkRenderer (org.pegdown.LinkRenderer)1