dsh-zju-lecture-note 2.2.3 → 2.2.4

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.
Files changed (30) hide show
  1. package/CHANGELOG.md +41 -0
  2. package/README.md +8 -7
  3. package/docs/technical.md +26 -4
  4. package/mcp_server.py +1 -1
  5. package/package.json +1 -1
  6. package/skills/scholia-notes/SKILL.md +8 -1
  7. package/skills/scholia-notes/references/format-spec.md +57 -6
  8. package/skills/scholia-notes/references/prelude.typ +42 -0
  9. package/skills/scholia-notes/references/typography-spec.md +4 -4
  10. package/skills/scholia-notes.manifest.json +9 -9
  11. package/skills/zju-lecture-summarizer/SKILL.md +13 -1
  12. package/skills/zju-lecture-summarizer.manifest.json +4 -4
  13. package/zju_lecture/client.py +741 -741
  14. package/zju_lecture/handout.py +353 -57
  15. package/zju_lecture/handout_prelude.typ +35 -2
  16. package/zju_lecture/prelude.typ +42 -0
  17. package/zju_lecture/study_notes.py +296 -1
  18. package/zju_lecture/typst-packages/VENDORED.md +1 -1
  19. package/zju_lecture/typst-packages/preview/noteworthy/0.4.0/LICENSE +20 -20
  20. package/zju_lecture/typst-packages/preview/noteworthy/0.4.0/lib.typ +128 -128
  21. package/zju_lecture/typst-packages/preview/noteworthy/0.4.0/typst.toml +15 -15
  22. package/zju_lecture/typst-packages/preview/scholia/0.1.0/src/lib.typ +2 -2
  23. package/zju_lecture/typst-packages/preview/showybox/2.0.4/LICENSE +21 -21
  24. package/zju_lecture/typst-packages/preview/showybox/2.0.4/lib/func.typ +142 -142
  25. package/zju_lecture/typst-packages/preview/showybox/2.0.4/lib/id.typ +11 -11
  26. package/zju_lecture/typst-packages/preview/showybox/2.0.4/lib/pre-rendering.typ +50 -50
  27. package/zju_lecture/typst-packages/preview/showybox/2.0.4/lib/sections.typ +141 -141
  28. package/zju_lecture/typst-packages/preview/showybox/2.0.4/lib/shadows.typ +118 -118
  29. package/zju_lecture/typst-packages/preview/showybox/2.0.4/showy.typ +267 -267
  30. package/zju_lecture/typst-packages/preview/showybox/2.0.4/typst.toml +11 -11
@@ -40,10 +40,13 @@ from .study_notes import (
40
40
  MARKERS,
41
41
  StudyNotesError,
42
42
  _run_typst,
43
+ _SUMMARY_LINE_RE,
43
44
  find_typst,
44
45
  latex_to_typst,
45
46
  pad_inline_math,
46
47
  split_quote_items,
48
+ split_question_number,
49
+ split_review_labels,
47
50
  strip_heading_number,
48
51
  )
49
52
 
@@ -95,6 +98,11 @@ _TABLE_SEP_RE = re.compile(
95
98
  )
96
99
  _IMAGE_RE = re.compile(r'^!\[([^\]]*)\]\(\s*([^)\s]+)(?:\s+"[^"]*")?\s*\)$')
97
100
  _HR_RE = re.compile(r"^\s*([-*_])(?:\s*\1){2,}\s*$")
101
+ #: 伪标题:整行只有一段粗体(可带 `[mm:ss]` 时间码)。讲义里常拿它当小标题,
102
+ #: 它是**下一个话题**的抬头,不属于前一个装置的正文;跨段收集到这里收尾。
103
+ _PSEUDO_HEADING_RE = re.compile(
104
+ r"^\*\*[^*\n]{1,40}\*\*(?:\s*`?\[\d{1,2}:\d{2}(?::\d{2})?\]`?)?\s*$"
105
+ )
98
106
  _LINK_RE = re.compile(r"\[([^\]]*)\]\(\s*([^)\s]+)(?:\s+\"[^\"]*\")?\s*\)")
99
107
  _DETAILS_OPEN_RE = re.compile(r"<details[^>]*>", re.IGNORECASE)
100
108
  _DETAILS_CLOSE_RE = re.compile(r"</details\s*>", re.IGNORECASE)
@@ -733,21 +741,270 @@ def _image_block(
733
741
  return "#figure(" + ", ".join(figure_args) + ")"
734
742
 
735
743
 
744
+ def _read_display_math(
745
+ lines: Sequence[str],
746
+ index: int,
747
+ warnings: List[str],
748
+ ) -> Tuple[str, int]:
749
+ """从 ``lines[index]`` 的 ``$$`` 开始读一个块级公式。
750
+
751
+ 返回 ``(Typst 片段, 下一个待处理下标)``;块级公式以 ``$ … $``(两侧
752
+ 有空白)输出,Typst 才会把它当居中块级公式。
753
+ """
754
+ body = [lines[index].strip()[2:]]
755
+ index += 1
756
+ total = len(lines)
757
+ if "$$" in body[0]:
758
+ inner = body[0].split("$$")[0]
759
+ else:
760
+ while index < total:
761
+ line = lines[index].strip()
762
+ index += 1
763
+ if "$$" in line:
764
+ body.append(line.split("$$")[0])
765
+ break
766
+ body.append(line)
767
+ inner = "\n".join(body)
768
+ converted, issues = latex_to_typst(inner.strip())
769
+ warnings.extend(issues)
770
+ return "$ " + converted.strip() + " $", index
771
+
772
+
773
+ def _absorbs_blocks(name: str) -> bool:
774
+ """标记块是否**跨段**:框类装置与证明一直写到下一个标记/标题。
775
+
776
+ ``[直觉]`` / ``[练习]`` / ``[边注]`` / ``[强调]`` 这类装置是一句话,
777
+ 只吃标记行那一句;定理、定义、例、证明的正文常常带块级公式与多个
778
+ 段落,旧实现遇到空行或 ``$$`` 就收尾,公式会被甩到方框外,证明的
779
+ qed 方块也会停在第一段末尾(实地讲义里就是这样误导读者的)。
780
+ """
781
+ call, _shape = MARKERS[name]
782
+ return call == "proof" or call in _KNOT_CALLS
783
+
784
+
785
+ def _collect_marker_body(
786
+ lines: Sequence[str],
787
+ index: int,
788
+ first: str,
789
+ absorb: bool,
790
+ ) -> Tuple[List[str], int]:
791
+ """收集标记行之后的正文行,返回 ``(行列表, 下一个待处理下标)``。
792
+
793
+ ``absorb=False`` 沿用旧规则:遇到空行、标题、公式、列表、表格、代码块
794
+ 或下一个标记就停。``absorb=True`` 一直收到下一个标题或下一个标记,
795
+ 空行保留为段落分隔。
796
+ """
797
+ body: List[str] = [first] if first else []
798
+ total = len(lines)
799
+ if absorb:
800
+ while index < total:
801
+ raw = lines[index]
802
+ stripped = raw.strip()
803
+ if (
804
+ _HEADING_RE.match(stripped)
805
+ or _is_marker_start(stripped)
806
+ or _PSEUDO_HEADING_RE.match(stripped)
807
+ ):
808
+ break
809
+ body.append(raw)
810
+ index += 1
811
+ else:
812
+ while index < total:
813
+ candidate = lines[index].strip()
814
+ if (
815
+ not candidate
816
+ or _HEADING_RE.match(candidate)
817
+ or _is_marker_start(candidate)
818
+ or candidate.startswith("$$")
819
+ or _LIST_RE.match(lines[index])
820
+ or candidate.startswith("|")
821
+ or _FENCE_RE.match(candidate)
822
+ ):
823
+ break
824
+ body.append(candidate)
825
+ index += 1
826
+ while body and not body[0].strip():
827
+ body.pop(0)
828
+ while body and not body[-1].strip():
829
+ body.pop()
830
+ return body, index
831
+
832
+
833
+ def _extract_block_label(body: List[str]) -> Tuple[List[str], str]:
834
+ """摘掉块里的 ``<def:x>``,返回 ``(正文行, label)``。
835
+
836
+ 认三个位置:标记行末尾(最常见)、独占一行的 ``<label>``、最后一段末尾。
837
+ 跨段标记把 ``<label>`` 写在公式后面单独一行也很自然,三种都认,
838
+ 才能和 :func:`_collect_refs` 的编号对得上。
839
+ """
840
+ if body:
841
+ text, label = _extract_label(body[0])
842
+ if label:
843
+ return [text] + list(body[1:]), label
844
+ for position, raw in enumerate(body):
845
+ stripped = raw.strip()
846
+ if not stripped:
847
+ continue
848
+ text, label = _extract_label(stripped)
849
+ if not label or text.strip():
850
+ continue
851
+ rest = list(body)
852
+ del rest[position]
853
+ return rest, label
854
+ for position in range(len(body) - 1, -1, -1):
855
+ if not body[position].strip():
856
+ continue
857
+ text, label = _extract_label(body[position])
858
+ if not label:
859
+ break
860
+ rest = list(body)
861
+ rest[position] = text
862
+ if not text.strip():
863
+ del rest[position]
864
+ return rest, label
865
+ return list(body), ""
866
+
867
+
868
+ def _split_knot_title_lines(body: List[str]) -> Tuple[str, List[str]]:
869
+ """只在**第一段**里找 ``标题 —— 正文``;后面的段落原样保留。"""
870
+ if not body:
871
+ return "", []
872
+ title, rest = _split_knot_title(_join_paragraph([body[0]]))
873
+ if not title:
874
+ return "", list(body)
875
+ remainder = list(body[1:])
876
+ if rest.strip():
877
+ return title, [rest] + remainder
878
+ return title, remainder
879
+
880
+
881
+ def _render_block_sequence(
882
+ lines: Sequence[str],
883
+ warnings: List[str],
884
+ image_resolver,
885
+ refs: Optional[Dict[str, str]] = None,
886
+ ) -> Tuple[str, int]:
887
+ """把标记块里的若干行渲染成 Typst 块序列(块内段落不缩进)。
888
+
889
+ 与 :func:`_render_body` 的区别:不处理标题、不统计章节、不写段首缩进
890
+ (``#h(2em)``)—— 这些内容会作为 ``#theorem{…}`` / ``#proof{…}`` 的
891
+ 内容块嵌进装置里,缩进交给装置自己的内边距。
892
+
893
+ 返回 ``(Typst 内容, 段落数)``:段落数记进「方框:正文」统计,否则内容
894
+ 一搬进装置,分母就凭空变小,比例检查会误报。
895
+ """
896
+ out: List[str] = []
897
+ paragraph: List[str] = []
898
+ paragraphs = 0
899
+ total = len(lines)
900
+ index = 0
901
+
902
+ def flush() -> None:
903
+ nonlocal paragraphs
904
+ if not paragraph:
905
+ return
906
+ text = _join_paragraph(paragraph)
907
+ paragraph.clear()
908
+ if not text.strip():
909
+ return
910
+ # 「题目 / 切入点 / 关键步骤 / 易错点」写在同一段里时,一段一行排开:
911
+ # 四段式例题排成一大坨在框里很难读(实地讲义里就是这样)。
912
+ for part in split_review_labels(text):
913
+ out.append(_render_rich_text(part, warnings, image_resolver, refs))
914
+ paragraphs += 1
915
+
916
+ while index < total:
917
+ raw = lines[index]
918
+ stripped = raw.strip()
919
+ if not stripped:
920
+ flush()
921
+ index += 1
922
+ continue
923
+ if stripped.startswith("$$"):
924
+ flush()
925
+ math, index = _read_display_math(lines, index, warnings)
926
+ out.append(math)
927
+ continue
928
+ if _LIST_RE.match(raw):
929
+ flush()
930
+ list_lines: List[str] = []
931
+ while index < total and _LIST_RE.match(lines[index]):
932
+ list_lines.append(lines[index])
933
+ index += 1
934
+ out.append(_list_block(list_lines, warnings, image_resolver, refs))
935
+ continue
936
+ if stripped.startswith("|") and stripped.endswith("|"):
937
+ rows: List[str] = []
938
+ while index < total:
939
+ candidate = lines[index].strip()
940
+ if candidate.startswith("|") and candidate.endswith("|"):
941
+ rows.append(candidate)
942
+ index += 1
943
+ else:
944
+ break
945
+ if len(rows) >= 2 and _TABLE_SEP_RE.match(rows[1]):
946
+ flush()
947
+ out.append(_table_block(rows, warnings, image_resolver, refs))
948
+ else:
949
+ paragraph.extend(rows)
950
+ continue
951
+ if stripped.startswith(">"):
952
+ flush()
953
+ quote_lines: List[str] = []
954
+ while index < total and lines[index].strip().startswith(">"):
955
+ quote_lines.append(lines[index])
956
+ index += 1
957
+ out.append(_quote_block(quote_lines, warnings, image_resolver, refs))
958
+ continue
959
+ fence = _FENCE_RE.match(stripped)
960
+ if fence:
961
+ flush()
962
+ marker_text = fence.group(1)
963
+ info = fence.group(2) or ""
964
+ code: List[str] = []
965
+ index += 1
966
+ while index < total and not lines[index].strip().startswith(marker_text[:3]):
967
+ code.append(lines[index])
968
+ index += 1
969
+ if index < total:
970
+ index += 1
971
+ out.append(_code_block(marker_text, info, code))
972
+ continue
973
+ image = _IMAGE_RE.match(stripped)
974
+ if image:
975
+ flush()
976
+ out.append(
977
+ _image_block(
978
+ image.group(1), image.group(2), warnings, image_resolver, refs
979
+ )
980
+ )
981
+ index += 1
982
+ continue
983
+ paragraph.append(raw)
984
+ index += 1
985
+
986
+ flush()
987
+ return "\n\n".join(part for part in out if part.strip()), paragraphs
988
+
989
+
736
990
  def _render_marker_block(
737
991
  name: str,
738
- body: str,
992
+ body_lines: Sequence[str],
739
993
  number: str,
740
994
  warnings: List[str],
741
995
  image_resolver,
742
996
  refs: Optional[Dict[str, str]] = None,
743
- ) -> str:
744
- """把一行底稿标记渲染成对应的 PDF 装置。"""
997
+ ) -> Tuple[str, int]:
998
+ """把一个标记块(标记行 + 后续正文)渲染成对应的 PDF 装置。
999
+
1000
+ 返回 ``(Typst 片段, 块内段落数)``;段落数并入全篇的「方框:正文」统计。
1001
+ """
745
1002
  call, shape = MARKERS[name]
746
1003
  if call is None:
747
- return "#divider"
748
- body, label = _extract_label(body)
1004
+ return "#divider", 0
1005
+ body, label = _extract_block_label(list(body_lines))
749
1006
  if shape == "knot":
750
- title, content = _split_knot_title(body)
1007
+ title, content = _split_knot_title_lines(body)
751
1008
  args = [_json_string(number)]
752
1009
  if title:
753
1010
  args.append(
@@ -755,24 +1012,25 @@ def _render_marker_block(
755
1012
  + _render_inline(title, warnings, image_resolver, refs=refs)
756
1013
  + "]"
757
1014
  )
758
- fragment = (
759
- "#" + call + "(" + ", ".join(args) + ")["
760
- + _render_rich_text(content, warnings, image_resolver, refs)
761
- + "]"
1015
+ content_typst, paragraphs = _render_block_sequence(
1016
+ content, warnings, image_resolver, refs
762
1017
  )
1018
+ fragment = "#" + call + "(" + ", ".join(args) + ")[" + content_typst + "]"
763
1019
  else:
764
1020
  if call == "yourturn":
765
1021
  # 练习块里已经有书写横线,题面里的短填空线一并丢掉(见规范 §2.1)。
766
- body = re.sub(r"_{4,}", " ", body)
767
- content = _render_rich_text(body, warnings, image_resolver, refs)
1022
+ body = [re.sub(r"_{4,}", " ", line) for line in body]
1023
+ content_typst, paragraphs = _render_block_sequence(
1024
+ body, warnings, image_resolver, refs
1025
+ )
768
1026
  if call == "sidenote":
769
1027
  # 边注的符号必须用圆括号参数;方括号写法会被当成第二个内容块。
770
- fragment = "#sidenote([" + content + "], symbol: [!])"
1028
+ fragment = "#sidenote([" + content_typst + "], symbol: [!])"
771
1029
  else:
772
- fragment = "#" + call + "[" + content + "]"
1030
+ fragment = "#" + call + "[" + content_typst + "]"
773
1031
  if label:
774
1032
  fragment += " <" + label + ">"
775
- return fragment
1033
+ return fragment, paragraphs
776
1034
 
777
1035
 
778
1036
  def _is_block_start(line: str) -> bool:
@@ -789,6 +1047,8 @@ def _is_block_start(line: str) -> bool:
789
1047
  or (stripped.startswith("|") and stripped.endswith("|"))
790
1048
  or _IMAGE_RE.match(stripped)
791
1049
  or _is_marker_start(stripped)
1050
+ or stripped.lower().startswith("<details")
1051
+ or stripped.lower().startswith("</details")
792
1052
  )
793
1053
 
794
1054
 
@@ -803,8 +1063,12 @@ def _collect_refs(lines: Sequence[str]) -> Dict[str, str]:
803
1063
  sub = 0
804
1064
  counts: Dict[str, int] = {}
805
1065
 
806
- for raw in lines:
1066
+ index = 0
1067
+ total = len(lines)
1068
+ while index < total:
1069
+ raw = lines[index]
807
1070
  stripped = raw.strip()
1071
+ index += 1
808
1072
  if not stripped:
809
1073
  continue
810
1074
  heading = _HEADING_RE.match(stripped)
@@ -834,9 +1098,14 @@ def _collect_refs(lines: Sequence[str]) -> Dict[str, str]:
834
1098
  call, _shape = MARKERS[name]
835
1099
  if call not in _KNOT_CALLS:
836
1100
  continue
1101
+ # 与 _render_body 用同一个收集器:框可以跨段,<label> 也可能写在
1102
+ # 最后一段末尾;两处必须同进同退,编号才不会漂。
1103
+ body_lines, index = _collect_marker_body(
1104
+ lines, index, marker.group(2), _absorbs_blocks(name)
1105
+ )
837
1106
  counts["knot"] = counts.get("knot", 0) + 1
838
1107
  number = (str(section) + "." if section else "") + str(counts["knot"])
839
- _, label = _extract_label(marker.group(2))
1108
+ _body, label = _extract_block_label(body_lines)
840
1109
  if label:
841
1110
  refs[label] = number
842
1111
  return refs
@@ -859,6 +1128,7 @@ def _render_body(
859
1128
  counts: Dict[str, int] = {}
860
1129
  section_name = ""
861
1130
  boxes_by_section: Dict[str, int] = {}
1131
+ question_count = 0
862
1132
 
863
1133
  def count(name: str, amount: int = 1) -> None:
864
1134
  blocks[name] = blocks.get(name, 0) + amount
@@ -902,35 +1172,55 @@ def _render_body(
902
1172
  marker = _MARKER_BLOCK_RE.match(stripped)
903
1173
  if marker and marker.group(1) in MARKERS:
904
1174
  name = marker.group(1)
905
- body_lines = [marker.group(2)]
906
- index += 1
907
- while index < total:
908
- candidate = lines[index].strip()
909
- if (
910
- not candidate
911
- or _HEADING_RE.match(candidate)
912
- or _is_marker_start(candidate)
913
- or candidate.startswith("$$")
914
- or _LIST_RE.match(lines[index])
915
- or candidate.startswith("|")
916
- or _FENCE_RE.match(candidate)
917
- ):
918
- break
919
- body_lines.append(candidate)
920
- index += 1
921
- body = _join_paragraph(body_lines).strip()
922
- call, shape = MARKERS[name]
1175
+ body_lines, index = _collect_marker_body(
1176
+ lines, index + 1, marker.group(2), _absorbs_blocks(name)
1177
+ )
1178
+ call, _shape = MARKERS[name]
923
1179
  number = ""
924
1180
  if call in _KNOT_CALLS:
925
1181
  counts["knot"] = counts.get("knot", 0) + 1
926
1182
  number = (str(section) + "." if section else "") + str(counts["knot"])
927
1183
  boxes_by_section[section_name] = boxes_by_section.get(section_name, 0) + 1
928
- out.append(
929
- _render_marker_block(name, body, number, warnings, image_resolver, refs)
1184
+ fragment, inner_paragraphs = _render_marker_block(
1185
+ name, body_lines, number, warnings, image_resolver, refs
930
1186
  )
1187
+ out.append(fragment)
1188
+ if inner_paragraphs:
1189
+ # 块内段落也算进「方框:正文」的分母:内容搬进装置之后,
1190
+ # 分母不能凭空变小,否则比例检查会把正常的讲义判成框框滥用。
1191
+ count("paragraph", inner_paragraphs)
931
1192
  count(call or name)
932
1193
  continue
933
1194
 
1195
+ # 自测题的答案块:`<details><summary>答案</summary>…</details>`。
1196
+ # 题干已在上面的段落分支里套成 `#selftest` 琥珀框;答案排在框外,
1197
+ # 用 `#answer`(左侧淡灰细线 + 标签)与题目分开。
1198
+ if stripped.lower().startswith("<details"):
1199
+ index += 1
1200
+ label = "答案"
1201
+ content: List[str] = []
1202
+ while index < total:
1203
+ candidate = lines[index]
1204
+ text_line = candidate.strip()
1205
+ if text_line.lower().startswith("</details"):
1206
+ index += 1
1207
+ break
1208
+ summary = _SUMMARY_LINE_RE.search(text_line)
1209
+ if summary:
1210
+ label = summary.group(1).strip() or label
1211
+ index += 1
1212
+ continue
1213
+ content.append(candidate)
1214
+ index += 1
1215
+ content_typst, inner = _render_block_sequence(
1216
+ content, warnings, image_resolver, refs
1217
+ )
1218
+ out.append("#answer(" + _json_string(label) + ")[" + content_typst + "]")
1219
+ count("answer")
1220
+ if inner:
1221
+ count("paragraph", inner)
1222
+ continue
1223
+
934
1224
  fence = _FENCE_RE.match(stripped)
935
1225
  if fence:
936
1226
  marker_text = fence.group(1)
@@ -947,22 +1237,8 @@ def _render_body(
947
1237
  continue
948
1238
 
949
1239
  if stripped.startswith("$$"):
950
- body = [stripped[2:]]
951
- index += 1
952
- if "$$" in body[0]:
953
- inner = body[0].split("$$")[0]
954
- else:
955
- while index < total:
956
- line = lines[index].strip()
957
- index += 1
958
- if "$$" in line:
959
- body.append(line.split("$$")[0])
960
- break
961
- body.append(line)
962
- inner = "\n".join(body)
963
- converted, issues = latex_to_typst(inner.strip())
964
- warnings.extend(issues)
965
- out.append("$ " + converted.strip() + " $")
1240
+ math, index = _read_display_math(lines, index, warnings)
1241
+ out.append(math)
966
1242
  count("equation")
967
1243
  continue
968
1244
 
@@ -1036,7 +1312,28 @@ def _render_body(
1036
1312
  while index < total and not _is_block_start(lines[index]):
1037
1313
  paragraph.append(lines[index])
1038
1314
  index += 1
1039
- out.append(_paragraph(_join_paragraph(paragraph), warnings, image_resolver, refs))
1315
+ text = _join_paragraph(paragraph)
1316
+ # 紧跟 `<details>` 的段落是自测题题干:套进与「例」同款的琥珀方框,
1317
+ # 答案就会排在框外,题目与答案之间有一道清楚的边界。
1318
+ probe = index
1319
+ while probe < total and not lines[probe].strip():
1320
+ probe += 1
1321
+ if probe < total and lines[probe].strip().lower().startswith("<details"):
1322
+ number, stem = split_question_number(text)
1323
+ if number:
1324
+ question_count = max(question_count, int(number))
1325
+ else:
1326
+ question_count += 1
1327
+ number = str(question_count)
1328
+ out.append(
1329
+ "#selftest(" + number + ")["
1330
+ + _render_rich_text(stem, warnings, image_resolver, refs)
1331
+ + "]"
1332
+ )
1333
+ count("selftest")
1334
+ count("paragraph")
1335
+ continue
1336
+ out.append(_paragraph(text, warnings, image_resolver, refs))
1040
1337
  count("paragraph")
1041
1338
 
1042
1339
  for name, boxes in boxes_by_section.items():
@@ -1150,9 +1447,8 @@ def render_handout(
1150
1447
 
1151
1448
  warnings: List[str] = []
1152
1449
  cleaned = _HTML_COMMENT_RE.sub("", markdown.replace("\r\n", "\n"))
1153
- cleaned = _SUMMARY_RE.sub(lambda m: "**" + m.group(1).strip() + "**", cleaned)
1154
- cleaned = _DETAILS_OPEN_RE.sub("", cleaned)
1155
- cleaned = _DETAILS_CLOSE_RE.sub("", cleaned)
1450
+ # `<details>` / `<summary>` 不再预先摊平:自测题的题干与答案要分别排成
1451
+ # `#selftest`(琥珀框)与 `#answer`(框外、灰色左线),见 _render_body。
1156
1452
 
1157
1453
  body_lines, fields = _extract_cover(
1158
1454
  cleaned, title=title, subtitle=subtitle, author=author, date=date, kicker=kicker
@@ -117,14 +117,14 @@
117
117
  // 标题后的间距比 2.2.1 略大:小节标题与正文之间留出呼吸,
118
118
  // 读者一眼能看出「新的一节从这里开始」(规范见 typography-spec.md §2)。
119
119
  #show heading: set text(font: fonts.heading)
120
- #show heading.where(level: 1): it => block(width: 100%, above: 20pt, below: 12pt)[
120
+ #show heading.where(level: 1): it => block(width: 100%, above: 24pt, below: 12pt)[
121
121
  #text(size: 15pt, weight: "bold", fill: palette.muted)[#counter(heading).display()]
122
122
  #h(0.7em)
123
123
  #text(size: 15pt, weight: "bold", fill: palette.thm)[#it.body]
124
124
  #v(1pt)
125
125
  #line(length: 100%, stroke: 1.1pt + palette.rule)
126
126
  ]
127
- #show heading.where(level: 2): it => block(above: 12pt, below: 0.85em)[
127
+ #show heading.where(level: 2): it => block(above: 22pt, below: 0.85em)[
128
128
  #text(size: 12.5pt, weight: "bold", fill: palette.def)[#counter(heading).display() #it.body]
129
129
  ]
130
130
  #show heading.where(level: 3): it => block(above: 9pt, below: 0.65em)[
@@ -215,6 +215,39 @@
215
215
  },
216
216
  )
217
217
 
218
+ // 自测题:题干复用「例」的琥珀方框;答案排在框外(淡灰左线 + 「答案」标签),
219
+ // 题目与答案之间因此有一道清楚的边界。渲染器把 `<details>` 块映射成这两个装置。
220
+ #let selftest(number, body) = block(
221
+ width: 100%,
222
+ breakable: true,
223
+ fill: tint(palette.eg),
224
+ stroke: (left: 2pt + palette.eg),
225
+ inset: (left: 11pt, right: 10pt, top: 7pt, bottom: 7pt),
226
+ above: 14pt,
227
+ below: 2pt,
228
+ {
229
+ set align(left)
230
+ text(font: fonts.sans, weight: "bold", size: 0.92em, fill: palette.eg)[自测题 #number]
231
+ h(0.4em)
232
+ body
233
+ },
234
+ )
235
+
236
+ #let answer(label, body) = block(
237
+ width: 100%,
238
+ breakable: true,
239
+ inset: (left: 12pt, right: 10pt),
240
+ stroke: (left: 1.2pt + palette.hairline),
241
+ above: 4pt,
242
+ below: 16pt,
243
+ {
244
+ set align(left)
245
+ text(font: fonts.sans, weight: "bold", size: 0.9em, fill: palette.muted)[#label]
246
+ v(0.25em)
247
+ body
248
+ },
249
+ )
250
+
218
251
  // 证明:不套框,只给斜体 Proof. 与行末的 qed 方块。
219
252
  // qed 用 box(rect(...)) 而不是裸 rect:rect 是块级元素,会另起一行;
220
253
  // 填空笔记链路的 mark() 也是 box,两条链路的方块才会都落在行末。
@@ -91,6 +91,48 @@
91
91
  stroke: (bottom: 0.6pt + active.get().fillin),
92
92
  )
93
93
 
94
+ // ── 5b. 自测题:题干琥珀框,答案排在框外 ──
95
+ // Markdown 里的 `<details><summary>答案</summary>…</details>` 由渲染器映射成
96
+ // 这两个装置:题干进 #selftest(与「例」同款的琥珀方框),答案进 #answer
97
+ // (左侧一条淡灰细线 + 「答案」小标签)。题目与答案因此有一道清楚的边界,
98
+ // 不会像以前那样连成一篇。
99
+ #let selftest(number, body) = context {
100
+ let p = active.get()
101
+ block(
102
+ width: 100%,
103
+ breakable: true,
104
+ fill: tint(p, p.eg),
105
+ stroke: (left: 2pt + p.eg),
106
+ inset: (left: 11pt, right: 10pt, top: 7pt, bottom: 7pt),
107
+ above: 14pt,
108
+ below: 2pt,
109
+ {
110
+ set align(left)
111
+ text(font: default-fonts.sans, weight: "bold", size: 0.92em, fill: p.eg)[自测题 #number]
112
+ h(0.4em)
113
+ body
114
+ },
115
+ )
116
+ }
117
+
118
+ #let answer(label, body) = context {
119
+ let p = active.get()
120
+ block(
121
+ width: 100%,
122
+ breakable: true,
123
+ inset: (left: 12pt, right: 10pt),
124
+ stroke: (left: 1.2pt + p.hairline),
125
+ above: 4pt,
126
+ below: 16pt,
127
+ {
128
+ set align(left)
129
+ text(font: default-fonts.sans, weight: "bold", size: 0.9em, fill: p.muted)[#label]
130
+ v(0.25em)
131
+ body
132
+ },
133
+ )
134
+ }
135
+
94
136
  // ── 6. 类型速查(写正文时照着选色相) ──
95
137
  // 结论/定理/证明/回指 → thm 蓝
96
138
  // 定义/公理/术语/标签 → def 青绿