feat: 新增开放接口,支持通过模板名称和项目名称生成文案
- 新增 HtmlUtils 工具类,支持 HTML 转纯文本 - 新增 selectTtCodeByCodeName 方法,支持按名称查询源码 - 新增 /office/copyTemplate/open/generate 开放接口 无需登录,参数:templateName(模板名称)、codeName(项目名称) 返回:填充占位符后的完整文案内容 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
package com.ruoyi.common.utils;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* HTML工具类
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public class HtmlUtils {
|
||||
|
||||
/**
|
||||
* 将HTML内容转换为纯文本
|
||||
* 移除所有HTML标签,将 等转义字符转换为普通空格,合并多余空白符
|
||||
*/
|
||||
public static String htmlToText(String html) {
|
||||
if (StringUtils.isEmpty(html)) {
|
||||
return "";
|
||||
}
|
||||
String text = html;
|
||||
// 移除script和style标签及其内容
|
||||
text = Pattern.compile("<script[^>]*?>.*?</script>", Pattern.DOTALL | Pattern.CASE_INSENSITIVE).matcher(text).replaceAll("");
|
||||
text = Pattern.compile("<style[^>]*?>.*?</style>", Pattern.DOTALL | Pattern.CASE_INSENSITIVE).matcher(text).replaceAll("");
|
||||
// 移除HTML注释
|
||||
text = Pattern.compile("<!--.*?-->", Pattern.DOTALL).matcher(text).replaceAll("");
|
||||
// 移除所有HTML标签
|
||||
text = Pattern.compile("<[^>]+>").matcher(text).replaceAll("");
|
||||
// 转义字符转换
|
||||
text = text.replaceAll(" ", " ");
|
||||
text = text.replaceAll("&", "&");
|
||||
text = text.replaceAll("<", "<");
|
||||
text = text.replaceAll(">", ">");
|
||||
text = text.replaceAll(""", "\"");
|
||||
text = text.replaceAll("'", "'");
|
||||
text = text.replaceAll("'", "'");
|
||||
text = text.replaceAll("—", "—");
|
||||
text = text.replaceAll("–", "–");
|
||||
text = text.replaceAll("…", "…");
|
||||
text = text.replaceAll("“", """);
|
||||
text = text.replaceAll("”", """);
|
||||
text = text.replaceAll("‘", "'");
|
||||
text = text.replaceAll("’", "'");
|
||||
text = text.replaceAll("\\r\\n", "\n");
|
||||
text = text.replaceAll("\\r", "\n");
|
||||
text = text.replaceAll("\\n\\n+", "\n\n");
|
||||
// 合并每行首尾空白并移除全空行
|
||||
String[] lines = text.split("\n");
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (String line : lines) {
|
||||
String trimmed = line.trim();
|
||||
if (trimmed.length() > 0) {
|
||||
if (sb.length() > 0) {
|
||||
sb.append("\n");
|
||||
}
|
||||
sb.append(trimmed);
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user