This commit is contained in:
@@ -419,69 +419,93 @@ public class RulesCLIManager implements Listener {
|
||||
if (!s.isEmpty()) segs.add(s.trim());
|
||||
}
|
||||
}
|
||||
// 2) 按页累积段 + 章节粘连:封页时若下一页首段是 bullet/编号项,回找当前页最近的章节标题,
|
||||
// 把该标题移到下一页首行,让"标题与其下属条目同页",避免玩家翻到下一页看到孤儿 bullet
|
||||
StringBuilder cur = new StringBuilder();
|
||||
int curLen = 0;
|
||||
// 2) 按页累积段 + 章节粘连(★ 125 续15l-10b 重写):
|
||||
// 逐段塞进当前页;封页前回找「当前页里最后一个章节标题段」,
|
||||
// 若它不是本页首段(说明前面还有上一章内容)且其后还跟着同章条目,
|
||||
// 就把「该标题 + 其后所有段」整体扣到下一页开头,
|
||||
// 让「章节标题与整章条目同页」,玩家翻页绝不看到孤儿 bullet / 半章。
|
||||
// 这样「1. 玩家权利」标题永远和它的 4 条 bullet 在同一页,
|
||||
// 下一章标题(「三、运营方…」)不会被压到本页尾。
|
||||
List<String> nonEmpty = new ArrayList<>();
|
||||
for (String seg : segs) {
|
||||
if (seg.isEmpty()) continue;
|
||||
int addLen = seg.length() + 1;
|
||||
if (curLen + addLen > pageSize && cur.length() > 0) {
|
||||
String curText = cur.toString().trim();
|
||||
// ★ 125 续15l-10:章节粘连放宽——不依赖「下一页首段是否条目」(清洗后 bullet 已无 "- " 前缀),
|
||||
// 改为「当前页末段是章节标题(N. / 第X、 / ###)且剩余空间装不下下一条」时,
|
||||
// 把该标题整行移到下一页,让标题与下属条目同页,避免玩家翻页看到孤儿条目
|
||||
String[] curLines = curText.split("\n");
|
||||
int lastIdx = curLines.length - 1;
|
||||
String lastLine = curLines[lastIdx].trim();
|
||||
boolean lastIsTitle = lastLine.matches("^\\d+\\.\\s.*")
|
||||
|| lastLine.matches("^#{1,6}\\s.*")
|
||||
|| lastLine.matches("^第[一二三四五六七八九十]+、.*")
|
||||
|| lastLine.matches("^[一二三四五六七八九十]+、.*");
|
||||
if (lastIsTitle && curLines.length > 1) {
|
||||
// 当前页末段是标题 → 把标题移到下一页(标题后空一行),正文封当前页
|
||||
StringBuilder pageBody = new StringBuilder();
|
||||
for (int li = 0; li < lastIdx; li++) {
|
||||
if (!curLines[li].trim().isEmpty()) {
|
||||
pageBody.append(curLines[li]).append('\n');
|
||||
}
|
||||
}
|
||||
String bodyText = pageBody.toString().trim();
|
||||
if (!bodyText.isEmpty()) {
|
||||
pages.add(bodyText);
|
||||
}
|
||||
String movedTitle = lastLine;
|
||||
cur.setLength(0);
|
||||
cur.append(movedTitle).append("\n\n");
|
||||
curLen = movedTitle.length() + 2;
|
||||
} else {
|
||||
pages.add(curText);
|
||||
cur.setLength(0);
|
||||
curLen = 0;
|
||||
}
|
||||
}
|
||||
cur.append(seg).append('\n');
|
||||
curLen += addLen;
|
||||
if (!seg.isEmpty()) nonEmpty.add(seg);
|
||||
}
|
||||
if (cur.length() > 0) pages.add(cur.toString().trim());
|
||||
int i = 0;
|
||||
while (i < nonEmpty.size()) {
|
||||
// 预取:从 i 起能塞进一页的连续段
|
||||
List<String> buf = new ArrayList<>();
|
||||
int blen = 0;
|
||||
while (i < nonEmpty.size()) {
|
||||
String seg = nonEmpty.get(i);
|
||||
int addLen = seg.length() + 1;
|
||||
if (!buf.isEmpty() && blen + addLen > pageSize) break;
|
||||
buf.add(seg);
|
||||
blen += addLen;
|
||||
i++;
|
||||
}
|
||||
// 章节粘连:buf 里回找最后一个标题段(从后往前第一个 isSectionTitle)
|
||||
int lastTitle = -1;
|
||||
for (int k = buf.size() - 1; k >= 0; k--) {
|
||||
if (isSectionTitle(buf.get(k))) { lastTitle = k; break; }
|
||||
}
|
||||
if (lastTitle > 0) {
|
||||
// 标题在 buf 中间/尾,且前还有上一章内容 → 把「标题..末」移到下一页
|
||||
List<String> head = new ArrayList<>(buf.subList(0, lastTitle));
|
||||
List<String> tail = new ArrayList<>(buf.subList(lastTitle, buf.size()));
|
||||
if (!head.isEmpty()) {
|
||||
pages.add(String.join("\n", head).trim());
|
||||
}
|
||||
// tail 作为下一页开头,剩余段继续累积到下一页
|
||||
List<String> cur = tail;
|
||||
int curLen = 0;
|
||||
for (String x : cur) curLen += x.length() + 1;
|
||||
while (i < nonEmpty.size()) {
|
||||
String seg2 = nonEmpty.get(i);
|
||||
int add2 = seg2.length() + 1;
|
||||
if (curLen + add2 > pageSize) break;
|
||||
cur.add(seg2);
|
||||
curLen += add2;
|
||||
i++;
|
||||
}
|
||||
pages.add(String.join("\n", cur).trim());
|
||||
continue;
|
||||
}
|
||||
// 无粘连:整 buf 封一页
|
||||
pages.add(String.join("\n", buf).trim());
|
||||
}
|
||||
|
||||
if (pages.isEmpty()) pages.add("(空协议原文)");
|
||||
return pages;
|
||||
}
|
||||
|
||||
/** 在 [0, limit) 内从后往前找最近的句子边界标点(。!?;;、),返回其下一位作为切点;找不到返回 limit。 */
|
||||
/** 在 [0, limit) 内从后往前找最近的句末标点(。!?.,★15l-10b 不再认分号/顿号),返回其下一位作为切点;找不到则在 limit 处硬切。 */
|
||||
private int findSemanticCut(String s, int limit) {
|
||||
int scanEnd = Math.min(s.length(), limit);
|
||||
// 从后往前找最近的语义边界(句号/分号/问号/叹号/逗号 + 空格)
|
||||
// ★ 125 续15l-10b:只认句末标点 。!?.(句号/叹号/问号/英文句点),
|
||||
// 不再认 ;、(分号/顿号)——分号/顿号是条内停顿而非句末,认它们会把一条
|
||||
// bullet 劈成「前半句+后半句」跨页(如「所有玩家一律平等;本服务器没有…」被劈),
|
||||
// 玩家翻页读到半句。退而求其次在 limit/2 处硬切,保句子尽量完整。
|
||||
for (int i = scanEnd - 1; i >= limit / 2; i--) {
|
||||
char ch = s.charAt(i);
|
||||
if (ch == '。' || ch == '!' || ch == '?' || ch == ';' || ch == '、' || ch == '.') {
|
||||
return i + 1; // 在标点后切(标点归前半句)
|
||||
if (ch == '。' || ch == '!' || ch == '?' || ch == '.') {
|
||||
return i + 1; // 在句末标点后切(标点归前半句)
|
||||
}
|
||||
}
|
||||
// 退而求其次:在 limit 处硬切(保留句子完整性靠上面已尽力)
|
||||
return Math.min(limit, s.length());
|
||||
}
|
||||
|
||||
/** ★ 125 续15l-10b:判断一行(清洗后)是否为章节标题,用于章节粘连分页。
|
||||
* 匹配:N. xxx(1. 玩家权利)/ #..# xxx / 第X、xxx / 一二三、xxx。 */
|
||||
private boolean isSectionTitle(String line) {
|
||||
String l = line.trim();
|
||||
if (l.isEmpty()) return false;
|
||||
return l.matches("^\\d+\\.\\s.*")
|
||||
|| l.matches("^#{1,6}\\s.*")
|
||||
|| l.matches("^第[一二三四五六七八九十]+、.*")
|
||||
|| l.matches("^[一二三四五六七八九十]+、.*");
|
||||
}
|
||||
|
||||
// ==================== GUI(按钮式:选协议 + 翻页,领地范式)====================
|
||||
|
||||
/** GUI 标题(ASCII,基岩版可识别) */
|
||||
|
||||
Reference in New Issue
Block a user