from __future__ import annotations import tempfile import unittest from pathlib import Path from tools.prepare_mdbook import ( _relative_chapter_path, build_title_cache, collect_figure_labels, collect_labels, convert_math_to_mathjax, normalize_directives, process_figure_captions, rewrite_markdown, write_summary, ) REPO_ROOT = Path(__file__).resolve().parents[1] class PrepareMdBookTests(unittest.TestCase): def test_write_summary_skips_placeholder_pages(self) -> None: with tempfile.TemporaryDirectory() as tmpdir: root = Path(tmpdir) source = root / "en_chapters" source.mkdir() (source / "index.md").write_text( """Machine Learning Systems ======================== ```toc :maxdepth: 2 chapter_preface/index chapter_introduction/index ``` ```toc :maxdepth: 1 appendix/index ``` """, encoding="utf-8", ) chapter_preface = source / "chapter_preface" chapter_preface.mkdir() (chapter_preface / "index.md").write_text( "[TODO: src = zh_chapters/chapter_preface/index.md]\n", encoding="utf-8", ) chapter_intro = source / "chapter_introduction" chapter_intro.mkdir() (chapter_intro / "index.md").write_text("# Introduction\n", encoding="utf-8") appendix = source / "appendix" appendix.mkdir() (appendix / "index.md").write_text("# Appendix\n", encoding="utf-8") summary_path = write_summary( source, placeholder_prefix="[TODO: src = zh_chapters/", ) summary = summary_path.read_text(encoding="utf-8") self.assertEqual( summary, """# Summary [Machine Learning Systems](index.md) [Introduction](chapter_introduction/index.md) [Appendix](appendix/index.md) """, ) title_cache = build_title_cache( source, placeholder_prefix="[TODO: src = zh_chapters/", ) rewritten = rewrite_markdown( (source / "index.md").read_text(encoding="utf-8"), (source / "index.md").resolve(), title_cache, ) self.assertIn("- [Introduction](chapter_introduction/index.md)", rewritten) self.assertIn("- [Appendix](appendix/index.md)", rewritten) self.assertNotIn("chapter_preface/index.md", rewritten) def test_rewrite_markdown_uses_configured_bibliography_title(self) -> None: with tempfile.TemporaryDirectory() as tmpdir: root = Path(tmpdir) page = root / "chapter.md" page.write_text( """# Introduction Reference :cite:`smith2024`. """, encoding="utf-8", ) rewritten = rewrite_markdown( page.read_text(encoding="utf-8"), page.resolve(), {page.resolve(): "Introduction"}, bib_db={ "smith2024": { "author": "Smith, Alice and Doe, Bob", "title": "Systems Paper", "year": "2024", "journal": "ML Systems Journal", } }, bibliography_title="References", ) self.assertIn("## References", rewritten) self.assertNotIn("## 参考文献", rewritten) def test_rewrite_markdown_inlines_frontpage_with_language_switch(self) -> None: with tempfile.TemporaryDirectory() as tmpdir: root = Path(tmpdir) source = root / "en_chapters" static_dir = source / "static" static_dir.mkdir(parents=True) index = source / "index.md" index.write_text( """# Home ```eval_rst .. raw:: html :file: frontpage.html ``` """, encoding="utf-8", ) (static_dir / "frontpage.html").write_text( "
STAR
\n\n图8.1 量化原理
', result) self.assertNotIn(":width:", result) self.assertNotIn(":label:", result) def test_figure_without_number_map(self) -> None: md = "\n:label:`fig1`\n" result = process_figure_captions(md) self.assertIn('', result) self.assertIn("", result) self.assertIn('caption
', result) def test_image_without_label_passthrough(self) -> None: md = "\nSome text\n" result = process_figure_captions(md) self.assertIn("", result) self.assertNotIn('', result) def test_figure_empty_caption(self) -> None: md = "\n:label:`fig1`\n" result = process_figure_captions(md, fig_number_map={"fig1": "1.1"}) self.assertIn('图1.1
', result) class NumrefWithFigureNumberTests(unittest.TestCase): def test_numref_shows_figure_number(self) -> None: result = normalize_directives( "See :numref:`my_fig`.\n", ref_label_map={"my_fig": "ch/page.md"}, current_source_path="ch/page.md", fig_number_map={"my_fig": "8.1"}, ) self.assertIn("[图8.1](#my_fig)", result) def test_numref_cross_file_with_figure_number(self) -> None: result = normalize_directives( "See :numref:`my_fig`.\n", ref_label_map={"my_fig": "other/page.md"}, current_source_path="ch/page.md", fig_number_map={"my_fig": "3.2"}, ) self.assertIn("[图3.2](../other/page.md#my_fig)", result) def test_numref_without_figure_number_shows_name(self) -> None: result = normalize_directives( "See :numref:`tbl`.\n", ref_label_map={"tbl": "ch/page.md"}, current_source_path="ch/page.md", fig_number_map={}, ) self.assertIn("[tbl](#tbl)", result) class LabelNumrefIntegrationTests(unittest.TestCase): def test_rewrite_markdown_with_label_map(self) -> None: with tempfile.TemporaryDirectory() as tmpdir: page = Path(tmpdir) / "chapter" / "page.md" page.parent.mkdir() page.write_text( "# Title\n\n:label:`my_fig`\n\nSee :numref:`my_fig`.\n", encoding="utf-8", ) rewritten = rewrite_markdown( page.read_text(encoding="utf-8"), page.resolve(), {page.resolve(): "Title"}, ref_label_map={"my_fig": "chapter/page.md"}, current_source_path="chapter/page.md", ) self.assertIn('', rewritten) self.assertIn("[my_fig](#my_fig)", rewritten) def test_rewrite_markdown_cross_file_numref(self) -> None: with tempfile.TemporaryDirectory() as tmpdir: page = Path(tmpdir) / "ch1" / "page.md" page.parent.mkdir() page.write_text( "# Title\n\nSee :numref:`other_fig`.\n", encoding="utf-8", ) rewritten = rewrite_markdown( page.read_text(encoding="utf-8"), page.resolve(), {page.resolve(): "Title"}, ref_label_map={"other_fig": "ch2/file.md"}, current_source_path="ch1/page.md", ) self.assertIn("[other_fig](../ch2/file.md#other_fig)", rewritten) def test_rewrite_markdown_figure_with_number_and_caption(self) -> None: with tempfile.TemporaryDirectory() as tmpdir: page = Path(tmpdir) / "ch" / "page.md" page.parent.mkdir() page.write_text( "# Title\n\n\n:width:`800px`\n:label:`qfig`\n\nSee :numref:`qfig`.\n", encoding="utf-8", ) rewritten = rewrite_markdown( page.read_text(encoding="utf-8"), page.resolve(), {page.resolve(): "Title"}, ref_label_map={"qfig": "ch/page.md"}, current_source_path="ch/page.md", fig_number_map={"qfig": "8.1"}, ) self.assertIn('', rewritten) self.assertIn("", rewritten) self.assertIn('图8.1 量化原理
', rewritten) self.assertIn("[图8.1](#qfig)", rewritten) class ConvertMathToMathjaxTests(unittest.TestCase): def test_display_math(self) -> None: result = convert_math_to_mathjax("before $$x^2$$ after") self.assertEqual(result, "before \\\\[x^2\\\\] after") def test_inline_math(self) -> None: result = convert_math_to_mathjax("before $x^2$ after") self.assertEqual(result, "before \\\\(x^2\\\\) after") def test_backslash_doubling_inside_math(self) -> None: result = convert_math_to_mathjax("$$a \\\\ b$$") self.assertEqual(result, "\\\\[a \\\\\\\\ b\\\\]") def test_math_inside_code_block_not_converted(self) -> None: text = "```\n$x^2$\n```" result = convert_math_to_mathjax(text) self.assertEqual(result, text) def test_math_inside_inline_code_not_converted(self) -> None: text = "use `$x$` for math" result = convert_math_to_mathjax(text) self.assertEqual(result, text) def test_cjk_dollar_spans_stripped(self) -> None: result = convert_math_to_mathjax("price $100美元$ done") self.assertEqual(result, "price 100美元 done") def test_no_math_passthrough(self) -> None: text = "No math here at all." self.assertEqual(convert_math_to_mathjax(text), text) def test_mixed_display_and_inline(self) -> None: text = "Inline $a$ and display $$b$$." result = convert_math_to_mathjax(text) self.assertEqual(result, "Inline \\\\(a\\\\) and display \\\\[b\\\\].") def test_asterisk_escaped_inside_math(self) -> None: result = convert_math_to_mathjax("$$n*CHW$$") self.assertEqual(result, "\\\\[n\\*CHW\\\\]") def test_underscore_escaped_inside_math(self) -> None: result = convert_math_to_mathjax("$x_i$") self.assertEqual(result, "\\\\(x\\_i\\\\)") if __name__ == "__main__": unittest.main()