From 8f321eaec26a22fa543f31b1428f62e606b11b96 Mon Sep 17 00:00:00 2001 From: duanledexianxianxian Date: Mon, 30 Mar 2020 18:38:13 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=9F=E6=88=90markdown=20api=E6=96=87?= =?UTF-8?q?=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kim/apidoc/core/parser/VisitorParser.java | 52 +++++++++++++--- .../com/kim/apidoc/core/schema/Appendix.java | 61 +++++++++++++++++++ .../java/com/kim/apidoc/core/schema/Node.java | 8 +-- 3 files changed, 109 insertions(+), 12 deletions(-) create mode 100644 apidoc-core/src/main/java/com/kim/apidoc/core/schema/Appendix.java diff --git a/apidoc-core/src/main/java/com/kim/apidoc/core/parser/VisitorParser.java b/apidoc-core/src/main/java/com/kim/apidoc/core/parser/VisitorParser.java index ca7691e..044d98f 100644 --- a/apidoc-core/src/main/java/com/kim/apidoc/core/parser/VisitorParser.java +++ b/apidoc-core/src/main/java/com/kim/apidoc/core/parser/VisitorParser.java @@ -1,14 +1,18 @@ 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 { */ @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 { 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 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); diff --git a/apidoc-core/src/main/java/com/kim/apidoc/core/schema/Appendix.java b/apidoc-core/src/main/java/com/kim/apidoc/core/schema/Appendix.java new file mode 100644 index 0000000..b81afc3 --- /dev/null +++ b/apidoc-core/src/main/java/com/kim/apidoc/core/schema/Appendix.java @@ -0,0 +1,61 @@ +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 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; + } + +} diff --git a/apidoc-core/src/main/java/com/kim/apidoc/core/schema/Node.java b/apidoc-core/src/main/java/com/kim/apidoc/core/schema/Node.java index d887a0b..c3eb0a0 100644 --- a/apidoc-core/src/main/java/com/kim/apidoc/core/schema/Node.java +++ b/apidoc-core/src/main/java/com/kim/apidoc/core/schema/Node.java @@ -1,10 +1,10 @@ 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 { * @return the param tag */ public Optional 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 { */ 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); -- GitLab