Commit 8f321eae authored by duanledexianxianxian's avatar duanledexianxianxian 😁

生成markdown api文档

parent 01c179c7
package com.kim.apidoc.core.parser;
import com.github.javaparser.ast.comments.Comment;
import com.github.javaparser.ast.comments.JavadocComment;
import com.kim.apidoc.core.schema.Chapter;
import com.kim.apidoc.core.schema.Node;
import com.kim.apidoc.core.schema.Project;
import com.kim.apidoc.core.schema.Section;
import com.github.javaparser.javadoc.Javadoc;
import com.github.javaparser.javadoc.JavadocBlockTag;
import com.kim.apidoc.core.common.helper.CommentHelper;
import com.kim.apidoc.core.schema.*;
import com.kim.apidoc.core.common.helper.OptionalHelper;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
import jdk.nashorn.internal.runtime.options.Option;
import java.util.Optional;
/**
* The type Visitor parser.
......@@ -37,8 +41,14 @@ public class VisitorParser extends VoidVisitorAdapter<Node> {
*/
@Override
public void visit(final ClassOrInterfaceDeclaration classOrInterfaceDeclaration, final Node arg) {
if (arg instanceof Project && isExistTag(classOrInterfaceDeclaration, "code")) {
}
if (arg instanceof Project && isExistTag(classOrInterfaceDeclaration, "resultData")) {
}
if (arg instanceof Project) {
if (arg instanceof Project && parserStrategy.accept(classOrInterfaceDeclaration)) {
Project project = (Project) arg;
// 章节
Chapter chapter = new Chapter();
......@@ -49,14 +59,40 @@ public class VisitorParser extends VoidVisitorAdapter<Node> {
OptionalHelper.any(chapter.getTag("book"), chapter.getTag("group"))
.ifPresent(tag -> chapter.setBookName(tag.getContent()));
if (parserStrategy.accept(classOrInterfaceDeclaration)) {
parserStrategy.visit(classOrInterfaceDeclaration, chapter);
}
parserStrategy.visit(classOrInterfaceDeclaration, chapter);
project.addChapter(chapter);
super.visit(classOrInterfaceDeclaration, chapter);
}
}
/**
* 处理特殊的tag
*
* @param classOrInterfaceDeclaration the class or interface declaration
* @param tag the tag
* @return boolean boolean
*/
private Boolean isExistTag(ClassOrInterfaceDeclaration classOrInterfaceDeclaration, String tag) {
if (classOrInterfaceDeclaration.getComment().isPresent()) {
Comment comment = classOrInterfaceDeclaration.getComment().get();
if (!comment.isJavadocComment()) {
return false;
}
Javadoc javadoc = comment.asJavadocComment().parse();
Optional<JavadocBlockTag> blockTagOptional =
javadoc.getBlockTags().stream().filter(blockTag -> {
return tag.equalsIgnoreCase(blockTag.getTagName());
}).findFirst();
if (blockTagOptional.isPresent()) {
return true;
}
}
return false;
}
@Override
public void visit(JavadocComment n, Node arg) {
super.visit(n, arg);
......
package com.kim.apidoc.core.schema;
import com.github.fengyuchenglun.core.common.Cell;
import com.github.fengyuchenglun.core.resolver.ast.Enums;
import com.github.fengyuchenglun.core.resolver.ast.Fields;
import com.github.javaparser.ast.body.BodyDeclaration;
import com.github.javaparser.ast.comments.JavadocComment;
import com.github.javaparser.ast.nodeTypes.NodeWithSimpleName;
import lombok.Getter;
import lombok.Setter;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* 附录
*/
@Setter
@Getter
public class Appendix extends Node {
Map<String, Row> rows = new LinkedHashMap<>();
public boolean isEmpty() {
return cells.isEmpty();
}
@Nullable
public static Appendix parse(JavadocComment n) {
// 无注释
if (!n.getCommentedNode().isPresent()) {
return null;
}
// 注释所在的类或者枚举
final com.github.javaparser.ast.Node node = n.getCommentedNode().get();
if(!(node instanceof BodyDeclaration)){
return null;
}
final BodyDeclaration bodyDeclaration = (BodyDeclaration) node;
// 不是枚举与接口/类
if(!bodyDeclaration.isEnumDeclaration() && !bodyDeclaration.isClassOrInterfaceDeclaration()){
return null;
}
Appendix appendix = new Appendix();
// 枚举
if (bodyDeclaration.isEnumDeclaration()) {
appendix.getRows().addAll(Enums.toDetails(bodyDeclaration.asEnumDeclaration()));
}else if (bodyDeclaration.isClassOrInterfaceDeclaration()) {
appendix.getRows().addAll(Fields.getConstants(bodyDeclaration.asClassOrInterfaceDeclaration()));
}
if (node instanceof NodeWithSimpleName) {
appendix.setName(((NodeWithSimpleName) node).getNameAsString());
}
appendix.accept(node.getComment());
return appendix;
}
}
package com.kim.apidoc.core.schema;
import com.github.javaparser.ast.comments.Comment;
import com.github.javaparser.javadoc.Javadoc;
import com.kim.apidoc.core.Context;
import com.kim.apidoc.core.common.helper.CommentHelper;
import com.kim.apidoc.core.common.helper.StringHelper;
import com.github.javaparser.ast.comments.Comment;
import com.github.javaparser.javadoc.Javadoc;
import lombok.Getter;
import lombok.Setter;
......@@ -120,7 +120,7 @@ public class Node implements Comparable<Node> {
* @return the param tag
*/
public Optional<Tag> getParamTag(String id) {
return Optional.ofNullable(tags.get("param:"+id));
return Optional.ofNullable(tags.get("param:" + id));
}
/**
......@@ -130,7 +130,7 @@ public class Node implements Comparable<Node> {
*/
public void putTag(Tag tag) {
String id = tag.id;
if(StringHelper.nonBlank(tag.getKey())){
if (StringHelper.nonBlank(tag.getKey())) {
id += ":" + tag.getKey();
}
tags.put(id, tag);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment