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;
}
Aggregations