api生成器优化

This commit is contained in:
张家训
2026-07-28 15:56:01 +08:00
parent 50dc026c01
commit 476141ebdc
7 changed files with 844 additions and 901 deletions
@@ -65,10 +65,6 @@ public class ApiDocWindow {
private CheckBox apiPackageOnlyCb;
private CheckBox facadeNameCb;
// 导出格式
private ToggleGroup fmtGroup;
private RadioButton rbHtml, rbMd;
// 操作区
private Button exportBtn;
private ProgressBar progressBar;
@@ -219,28 +215,6 @@ public class ApiDocWindow {
fqnPanel.visibleProperty().bind(rbFqn.selectedProperty());
batchPanel.visibleProperty().bind(rbBatch.selectedProperty());
// --- 分割线 ---
Separator sep1 = new Separator();
// --- 导出格式 ---
HBox fmtRow = new HBox(Theme.S4);
fmtRow.setAlignment(Pos.CENTER_LEFT);
Label fmtLabel = new Label("导出格式:");
fmtLabel.setFont(Theme.base(fsBody()));
fmtLabel.setTextFill(Theme.TEXT_PRIMARY);
fmtGroup = new ToggleGroup();
rbHtml = new RadioButton("HTML");
rbMd = new RadioButton("Markdown");
rbHtml.setToggleGroup(fmtGroup);
rbMd.setToggleGroup(fmtGroup);
rbHtml.setSelected(true);
rbHtml.setFont(Theme.base(fsBody() - 1));
rbMd.setFont(Theme.base(fsBody() - 1));
fmtRow.getChildren().addAll(fmtLabel, rbHtml, rbMd);
// --- 分割线 ---
Separator sep2 = new Separator();
// --- 操作按钮 ---
HBox actionRow = new HBox(Theme.S3);
actionRow.setAlignment(Pos.CENTER_LEFT);
@@ -258,7 +232,7 @@ public class ApiDocWindow {
actionRow.getChildren().addAll(exportBtn, progressBar, progressLabel);
configCard.getChildren().addAll(modeLabel, modeRow, inputStack, sep1, fmtRow, sep2, actionRow);
configCard.getChildren().addAll(modeLabel, modeRow, inputStack, actionRow);
// ===================== 历史记录 =====================
Label histTitle = new Label("导出历史");
@@ -371,10 +345,6 @@ public class ApiDocWindow {
}, "ApiDoc-Worker").start();
}
private String getFmt() {
return rbHtml.isSelected() ? "1" : "2";
}
// ==================== 单文件导出 ====================
private void exportSingle(String inputPath, Path filePath, JavaSourceParser parser) throws Exception {
@@ -593,24 +563,13 @@ public class ApiDocWindow {
sel.scope = ApiDocModel.ExportScope.CUSTOM;
sel.projectName = name;
String fmt = getFmt();
if ("1".equals(fmt)) {
Path p = gen.generateHtml(sel, docs);
String filePath = p.toAbsolutePath().toString();
Platform.runLater(() -> {
log("HTML 已导出: " + filePath);
addHistory("HTML", filePath);
enableBtn();
});
} else {
Path p = gen.generateMarkdown(sel, docs);
String filePath = p.toAbsolutePath().toString();
Platform.runLater(() -> {
log("Markdown 已导出: " + filePath);
addHistory("Markdown", filePath);
enableBtn();
});
}
Path p = gen.generateHtml(sel, docs);
String filePath = p.toAbsolutePath().toString();
Platform.runLater(() -> {
log("HTML 已导出: " + filePath);
addHistory("HTML", filePath);
enableBtn();
});
}
private void addHistory(String mode, String filePath) {
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,60 @@
package com.example.workutils.biz.apidoc.generator;
/**
* API 文档渲染上下文,封装传入模板的所有数据。
*
* AI 定位指引:
* - 新增模板变量 → 在本类添加字段,然后在 buildHtml() 中使用
* - 修改文档标题/元信息 → 修改本类对应字段的赋值
*/
public class DocRenderContext {
/** 文档标题 */
private String title;
/** CSS 样式内容(从 api-doc.css 加载) */
private String css;
/** JS 脚本内容(从 api-doc.js 加载) */
private String js;
/** 文档主体 HTML */
private String bodyHtml;
/** 生成时间 */
private String generationTime;
/** 接口总数 */
private int totalClasses;
/** 方法总数 */
private int totalMethods;
// ===== 构造器 =====
public DocRenderContext() {
}
// ===== Getter / Setter =====
public String getTitle() { return title; }
public void setTitle(String title) { this.title = title; }
public String getCss() { return css; }
public void setCss(String css) { this.css = css; }
public String getJs() { return js; }
public void setJs(String js) { this.js = js; }
public String getBodyHtml() { return bodyHtml; }
public void setBodyHtml(String bodyHtml) { this.bodyHtml = bodyHtml; }
public String getGenerationTime() { return generationTime; }
public void setGenerationTime(String generationTime) { this.generationTime = generationTime; }
public int getTotalClasses() { return totalClasses; }
public void setTotalClasses(int totalClasses) { this.totalClasses = totalClasses; }
public int getTotalMethods() { return totalMethods; }
public void setTotalMethods(int totalMethods) { this.totalMethods = totalMethods; }
}
@@ -0,0 +1,69 @@
package com.example.workutils.biz.apidoc.generator;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
/**
* API 文档资源加载器。
* 从 classpath 加载分离的 CSS / JS / HTML 模板资源,
* 替换原先在 {@link ApiDocGenerator} 中硬编码字符串拼接的方式。
*
* AI 定位指引:
* - CSS 样式修改 → src/main/resources/templates/apidoc/api-doc.css
* - JS 交互修改 → src/main/resources/templates/apidoc/api-doc.js
* - HTML 结构修改 → src/main/resources/templates/apidoc/api-doc.html
* - 新增资源类型 → 在本类添加对应加载方法
*/
public class HtmlResourceLoader {
private static final String RESOURCE_BASE = "templates/apidoc/";
/**
* 加载 HTML 模板。
* 对应资源文件:resources/templates/apidoc/api-doc.html。
* 模板中使用 {{PLACEHOLDER}} 格式标记动态替换位。
*/
public String loadTemplate() {
return loadResource(RESOURCE_BASE + "api-doc.html");
}
/**
* 加载 CSS 样式。
* 对应资源文件:resources/templates/apidoc/api-doc.css
*/
public String loadCss() {
return loadResource(RESOURCE_BASE + "api-doc.css");
}
/**
* 加载 JS 脚本。
* 对应资源文件:resources/templates/apidoc/api-doc.js
*/
public String loadJs() {
return loadResource(RESOURCE_BASE + "api-doc.js");
}
/**
* 从 classpath 读取资源文件内容。
* 所有资源必须以 UTF-8 编码。
*/
private String loadResource(String path) {
ClassLoader cl = getClass().getClassLoader();
if (cl == null) {
cl = ClassLoader.getSystemClassLoader();
}
try (InputStream is = cl.getResourceAsStream(path)) {
if (is == null) {
throw new IOException("资源文件未找到: " + path);
}
try (Scanner scanner = new Scanner(is, StandardCharsets.UTF_8.name())) {
scanner.useDelimiter("\\A");
return scanner.hasNext() ? scanner.next() : "";
}
} catch (IOException e) {
throw new RuntimeException("加载资源失败: " + path, e);
}
}
}