dsh-zju-lecture-note 2.2.4 → 2.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -291,6 +291,32 @@ _PSEUDO_HEADING_RE = re.compile(
291
291
  r"^\*\*[^*\n]{1,40}\*\*(?:\s*`?\[\d{1,2}:\d{2}(?::\d{2})?\]`?)?\s*$"
292
292
  )
293
293
 
294
+ #: 行内粗体小标题:`**矛盾从哪里来** \`[00:05]\`–\`[00:06]\`:…`。
295
+ #: 它和整行粗体的伪标题是同一个意思(下一个话题的抬头),只是后面直接接了正文。
296
+ #: 例题四段式的标签(`**题目**:` 等)必须排除,否则 `[例]` 会被拆散。
297
+ _BOLD_LEAD_RE = re.compile(
298
+ r"^\*\*([^*\n]{1,40})\*\*\s*"
299
+ r"(?:`?\[\d{1,2}:\d{2}(?::\d{2})?\]`?\s*"
300
+ r"(?:[–—~-]\s*`?\[\d{1,2}:\d{2}(?::\d{2})?\]`?)?\s*)?"
301
+ r"[::]"
302
+ )
303
+ _REVIEW_HEAD_LABELS = frozenset({"题目", "切入点", "关键步骤", "易错点", "易错"})
304
+
305
+ #: 短引导词 + 时间码 + 冒号:`推导要点 \`[00:43]\`–\`[00:45]\`:…`。
306
+ #: 与粗体小标题同一个意思,只是作者没加粗;框在这一行收尾。证明块例外 ——
307
+ #: 证明的 qed 必须落在整段推导的末尾,不能因为一个「第一步 [00:12]:」就截断。
308
+ _PLAIN_LEAD_RE = re.compile(
309
+ r"^([\u4e00-\u9fff][^\s::,。;、()()]{0,11})\s*"
310
+ r"`?\[\d{1,2}:\d{2}(?::\d{2})?\]`?\s*"
311
+ r"(?:[–—~-]\s*`?\[\d{1,2}:\d{2}(?::\d{2})?\]`?\s*)?"
312
+ r"[::]"
313
+ )
314
+
315
+ #: 未知的中文块标记:`[推导] …`。只认中文标签 —— 行首的 `[1]` 引用、公式里
316
+ #: 的 `[S]` 都不是标记。跨段收集在这里收尾,渲染时按加粗小标题排,
317
+ #: 不再把方括号原样印进 PDF。
318
+ _UNKNOWN_MARKER_RE = re.compile(r"^\[([\u4e00-\u9fff][^\]\n]{0,7})\]\s*(.*)$")
319
+
294
320
  #: 斜杠分式:`\partial D/\partial t`、`\mathrm{d}i/\mathrm{d}t`、
295
321
  #: `\sqrt{\mu_0/\varepsilon_0}`。这些写法在行内会与上下标挤在一起,
296
322
  #: 渲染层统一改成 `\frac{…}{…}`;只处理**明确的微分/根式**,不碰一般 `a/b`。
@@ -1393,21 +1419,60 @@ def _is_marker_line(text: str) -> bool:
1393
1419
  )
1394
1420
 
1395
1421
 
1422
+ def block_boundary(text: str, proof: bool = False) -> bool:
1423
+ """跨段装置是否应该在这一行**收尾**(两条 PDF 链路共用)。
1424
+
1425
+ 框与证明本来就该跨段(v2.2.4:公式与段落不能掉出框外),但「跨段」不能
1426
+ 等于「吞到下一个标题」:一份讲义里 `[定理]` 后面紧跟的普通段落往往是
1427
+ 下一段讲解,`[定义]` 后面常跟着推导与结论。旧规则只认标题、已知标记与
1428
+ 整行粗体,实地讲义里出现过一个定义框吞掉推导、提示块甚至列表的情况。
1429
+
1430
+ 这里补四条**结构性**边界,都是「作者显然在开一个新块」的信号:
1431
+
1432
+ * 引用 / 提示块(``>``)—— 独立块,不属于上一个装置;
1433
+ * 以 ``⚠`` 开头的提醒段 —— 同样是独立提示,不应该被 qed 或方框包住;
1434
+ * 粗体小标题 + 冒号(可带时间码)—— 例题四段式标签例外;
1435
+ * 短引导词 + 时间码 + 冒号 —— 与上一条同义,只是没加粗;
1436
+ ``proof=True`` 时跳过,避免把证明从中间截断(qed 会停在半路);
1437
+ * 未知的中文标记(``[推导]``)—— 作者想开新块,只是名字不在表里。
1438
+
1439
+ 普通段落、块级公式、列表、表格仍然留在装置里,证明与例题的多段正文不受影响。
1440
+ """
1441
+ stripped = text.strip()
1442
+ if not stripped:
1443
+ return False
1444
+ if re.match(r"^#{1,6}\s+", stripped):
1445
+ return True
1446
+ if _is_marker_line(stripped) or _PSEUDO_HEADING_RE.match(stripped):
1447
+ return True
1448
+ if stripped.startswith(">") or stripped.startswith("\u26a0"):
1449
+ return True
1450
+ bold = _BOLD_LEAD_RE.match(stripped)
1451
+ if bold and bold.group(1).strip() not in _REVIEW_HEAD_LABELS:
1452
+ return True
1453
+ if not proof:
1454
+ lead = _PLAIN_LEAD_RE.match(stripped)
1455
+ if lead and lead.group(1).strip() not in _REVIEW_HEAD_LABELS:
1456
+ return True
1457
+ unknown = _UNKNOWN_MARKER_RE.match(stripped)
1458
+ if unknown and unknown.group(1) not in MARKERS:
1459
+ return True
1460
+ return False
1461
+
1462
+
1396
1463
  def _collect_block_lines(
1397
1464
  lines: Sequence[str],
1398
1465
  cursor: int,
1466
+ name: str = "",
1399
1467
  ) -> Tuple[List[str], int]:
1400
- """收集标记行之后、到下一个标题/标记为止的底稿行(空行保留)。"""
1468
+ """收集标记行之后、到下一个标题/标记/新话题为止的底稿行(空行保留)。"""
1401
1469
  body: List[str] = []
1402
1470
  total = len(lines)
1471
+ proof = name == "证明"
1403
1472
  while cursor < total:
1404
1473
  raw = lines[cursor]
1405
1474
  stripped = raw.strip()
1406
- if stripped and (
1407
- re.match(r"^#{1,6}\s+", stripped)
1408
- or _is_marker_line(stripped)
1409
- or _PSEUDO_HEADING_RE.match(stripped)
1410
- ):
1475
+ if stripped and block_boundary(stripped, proof=proof):
1411
1476
  break
1412
1477
  body.append(raw)
1413
1478
  cursor += 1
@@ -1869,7 +1934,7 @@ def render_typst(
1869
1934
  continue
1870
1935
  if _marker_absorbs_blocks(name):
1871
1936
  # 框与证明跨段:同一装置的公式、段落、列表都留在块内。
1872
- extra, cursor = _collect_block_lines(input_lines, cursor)
1937
+ extra, cursor = _collect_block_lines(input_lines, cursor, name)
1873
1938
  fragment, label, body_paragraphs = _render_block_marker(
1874
1939
  name, body, extra, known_labels, warnings
1875
1940
  )
@@ -1885,20 +1950,71 @@ def render_typst(
1885
1950
  knots.append({"call": call, "section": section})
1886
1951
  continue
1887
1952
 
1953
+ # 未知的中文标记(`[推导] …`):作者想开一个新块,但名字不在 MARKERS 表里。
1954
+ # 推导按旧规范的 Proof 排(斜体 Proof. + 块末 qed,跨段、不套框);
1955
+ # 其余未知标记按「加粗小标题 + 正文」兜底。note_lint 仍会提示改成已支持的标记。
1956
+ unknown = _UNKNOWN_MARKER_RE.match(stripped)
1957
+ if unknown:
1958
+ if unknown.group(1) == "推导":
1959
+ extra, cursor = _collect_block_lines(input_lines, cursor, "证明")
1960
+ fragment, label, body_paragraphs = _render_block_marker(
1961
+ "证明", unknown.group(2), extra, known_labels, warnings
1962
+ )
1963
+ lines_out.append(fragment)
1964
+ paragraphs += body_paragraphs
1965
+ if label:
1966
+ labels.append(label)
1967
+ _count("proof")
1968
+ continue
1969
+ lead = "**" + unknown.group(1) + "**"
1970
+ if unknown.group(2):
1971
+ lead += " " + unknown.group(2)
1972
+ lines_out.append(BODY_INDENT + escape_typst(lead, known_labels))
1973
+ paragraph_raw[len(lines_out) - 1] = lead
1974
+ paragraphs += 1
1975
+ _count("paragraph")
1976
+ continue
1977
+
1888
1978
  # Markdown 引用块。Typst **没有** `>` 引用语法(0.15 实测:`>` 会被
1889
1979
  # 当成字面量印出来,整段抬头变成「> 来源:… > 上课时间:…」),
1890
1980
  # 所以走 prelude 里的 `#quoted` 内容块;字段之间用行间断行。
1981
+ # `> [!IMPORTANT]` 这类 GitHub alert 先摘掉标记行:重点走 `#keypoint`
1982
+ # (规范蓝色的文字段落,不是方框),其余 alert 退化成带标签的灰色引用。
1891
1983
  if stripped.startswith(">"):
1892
1984
  quote_lines = [raw_line]
1893
1985
  while cursor < len(input_lines) and input_lines[cursor].strip().startswith(">"):
1894
1986
  quote_lines.append(input_lines[cursor])
1895
1987
  cursor += 1
1896
- items = split_quote_items(quote_lines)
1988
+ kind = ""
1989
+ cleaned: List[str] = []
1990
+ for line in quote_lines:
1991
+ text_line = line.strip()
1992
+ if text_line.startswith(">"):
1993
+ text_line = text_line[1:].lstrip()
1994
+ alert = re.match(
1995
+ r"^\[!(IMPORTANT|WARNING|CAUTION|NOTE|TIP)\]$", text_line, re.I
1996
+ )
1997
+ if alert and not cleaned:
1998
+ kind = alert.group(1).upper()
1999
+ continue
2000
+ cleaned.append(line)
2001
+ items = split_quote_items(cleaned)
1897
2002
  body = " \\\n".join(
1898
2003
  escape_typst(join_cjk_lines(item), known_labels) for item in items
1899
2004
  )
1900
- lines_out.append("#quoted[\n" + body + "\n]")
1901
- _count("quote")
2005
+ if kind == "IMPORTANT":
2006
+ lines_out.append("#keypoint[*重点* " + body + "]")
2007
+ _count("keypoint")
2008
+ elif kind:
2009
+ label = {
2010
+ "WARNING": "注意", "CAUTION": "注意",
2011
+ "NOTE": "说明", "TIP": "提示",
2012
+ }.get(kind, "提示")
2013
+ lines_out.append("#quoted[*" + label + "* " + body + "]")
2014
+ _count("quote")
2015
+ else:
2016
+ lines_out.append("#quoted[\n" + body + "\n]")
2017
+ _count("quote")
1902
2018
  continue
1903
2019
 
1904
2020
  # Markdown 水平分隔线 → Typst 的细分隔线。不处理的话 `---` 会被