I|方案 ======= 4.1 架构 -------- .. code:: text mystx(单继承) └── inherit = pydata_sphinx_theme ├── 复用:事件管线 / 三套目录算法 / 双主题 / 搜索 / a11y / Bootstrap 资产 └── 自有(book layer) ├── navigation 侧栏全书树(只改 startdepth 与深度参数) ├── buttons 页眉工具箱(数据 → 宏) └── sidenotes 脚注转边注 + margin 指令 只允许一层继承。\ **禁止**\ 把 SBT 的 ``layout.html`` 复制进来,也禁止"继承 PST 再 monkey-patch SBT"。 4.2 首版能力(3 + 1) --------------------- +--------------------------------+------------------+------------------+----------------+ | 能力 | 来源 | 实现层 | 首版 | +================================+==================+==================+================+ | 全书侧栏树 | SBT 的 | 组件 + 参数 | 是 | | | ``startdepth=0`` | | | | | 用法 | | | +--------------------------------+------------------+------------------+----------------+ | 页眉工具箱 | SBT | Python 数据 + 宏 | 是(5 | | | 的按钮数据协议 | | 个基础按钮) | +--------------------------------+------------------+------------------+----------------+ | 边注 / margin | SBT 的 transform | Python + CSS | 是 | | | + node + 指令 | | | +--------------------------------+------------------+------------------+----------------+ | 单列阅读宽度 + 右留白 | SBT 的 SCSS 思路 | CSS | 是 | +--------------------------------+------------------+------------------+----------------+ | 启动按钮(Binder/Colab/Thebe) | SBT | Python | 否,二期 | +--------------------------------+------------------+------------------+----------------+ 4.3 选项设计 ------------ **复用 PST 原生选项**\ (不重命名):\ ``navbar_*``\ 、\ ``secondary_sidebar_items``\ 、\ ``article_header_start/end``\ 、\ ``footer_start/end``\ 、\ ``announcement``\ 、\ ``icon_links``\ 、\ ``switcher``\ 、\ ``pygments_*``\ 。 **新增 6 个 ``book_*`` 选项**\ : +-------------------------+-----------------+-------------------------------------------------------------------+--------------------------------------------------+ | 选项 | 类型 | 默认 | 作用 | +=========================+=================+===================================================================+==================================================+ | ``book_nav_scope`` | str:\ ``book`` | ``"book"`` | 侧栏渲染全书树还是当前章节子树 | | | / ``section`` | | | +-------------------------+-----------------+-------------------------------------------------------------------+--------------------------------------------------+ | ``book_nav_depth`` | int | ``1`` | 全书树初始展开层数;只展开顶层,当前章节自动展开 | +-------------------------+-----------------+-------------------------------------------------------------------+--------------------------------------------------+ | ``book_header_buttons`` | list[str] | ``["download", "fullscreen", "source", "edit", "issue", "repo"]`` | 页眉按钮及顺序 | +-------------------------+-----------------+-------------------------------------------------------------------+--------------------------------------------------+ | ``book_use_sidenotes`` | bool | ``False`` | 脚注转边注总开关 | +-------------------------+-----------------+-------------------------------------------------------------------+--------------------------------------------------+ | ``book_content_width`` | str | ``"46rem"`` | 正文最大宽度(CSS 变量) | +-------------------------+-----------------+-------------------------------------------------------------------+--------------------------------------------------+ | ``book_footer_items`` | list[str] | ``["author", "copyright", "last-updated"]`` | 内容页脚组件 | +-------------------------+-----------------+-------------------------------------------------------------------+--------------------------------------------------+ 设计原则:\ **一个能力一个开关**\ ,布尔与列表都显式转换;不提供 ``use_*_button`` 六个布尔,改为一个有序列表(顺序即渲染顺序,删除即关闭)。 4.4 模板与组件 -------------- +------------------------------------+-----------------------------------------+----------+ | 文件 | 职责 | 目标行数 | +====================================+=========================================+==========+ | ``theme/mystx/theme.toml`` | ``inherit``\ 、样式表、6 个选项默认值 | ~40 | +------------------------------------+-----------------------------------------+----------+ | ``theme/mystx/layout.html`` | 只覆盖必要 block(导航区、正文容器) | < 60 | +------------------------------------+-----------------------------------------+----------+ | ``components/book-nav.html`` | 调 | ~25 | | | ``generate_toctree_html(startdepth=0)`` | | +------------------------------------+-----------------------------------------+----------+ | ``components/header-buttons.html`` | 遍历 ``header_buttons``\ ,按 ``type`` | ~30 | | | 选宏 | | +------------------------------------+-----------------------------------------+----------+ | ``components/page-toc.html`` | 页内目录标题与层级 | ~15 | +------------------------------------+-----------------------------------------+----------+ | ``components/footer-items.html`` | 内容页脚列表 | ~15 | +------------------------------------+-----------------------------------------+----------+ | ``macros/buttons.html`` | ``link`` / ``javascript`` / ``group`` | ~40 | | | 三个宏 | | +------------------------------------+-----------------------------------------+----------+ 4.5 Python 模块 --------------- +------------------------+----------------------+--------------------------------------------+ | 模块 | 职责 | 关键点 | +========================+======================+============================================+ | ``book/navigation.py`` | 计算侧栏参数 | 不重写 toctree 算法,只传 ``startdepth`` | | | | 与深度 | +------------------------+----------------------+--------------------------------------------+ | ``book/buttons.py`` | 生成按钮字典列表 | URL | | | | 规则纯函数、可单测;无仓库信息时返回空列表 | +------------------------+----------------------+--------------------------------------------+ | ``book/sidenotes.py`` | transform + node + | 优先级 1,仅 ``html`` 格式;关闭时不注册 | | | 指令 | | +------------------------+----------------------+--------------------------------------------+ **落地修正(P3)**\ :节点、指令与变换一律在 ``setup()`` 注册,开关在 ``run()`` 内短路。 理由有两条:\ ``{margin}`` 是块级能力,不该因为脚注开关被禁用;资产注册若依赖 ``builder-inited`` 时的选项,会让"关→开"的增量构建残留缺样式页面。 \| ``__init__.py`` \| ``setup()`` 注册主题与能力 \| 返回 ``parallel_read_safe/write_safe`` \| 4.6 目录结构 ------------ .. code:: text mystx/ ├── pyproject.toml ├── src/mystx/ │ ├── __init__.py │ ├── book/navigation.py │ ├── book/buttons.py │ ├── book/sidenotes.py │ └── theme/mystx/ │ ├── theme.toml │ ├── layout.html │ ├── components/*.html │ ├── macros/buttons.html │ └── static/css/{book,sidenotes,mystx}.css ├── doc/conf.py + index.md ├── tests/{test_buttons.py,test_build.py,test_sidenotes.py} └── doc/dev/design.rst(本文件)