API 参考
注意
API 的主要入口点是通过插件接收的事件。这些事件的描述链接回此页面。
mkdocs.structure.files.Files
一个 File 对象的集合。
src_paths: dict[str, File]
property
已软弃用,建议使用 src_uris
。
__iter__() -> Iterator[File]
遍历其中的文件。
__len__() -> int
文件数量。
__contains__(path: str) -> bool
已软弃用,建议使用 get_file_from_path(path) is not None
。
get_file_from_path(path: str) -> File | None
返回一个 File 实例,其 File.src_uri 等于 path。
append(file: File) -> None
将文件添加到 Files 集合中。
remove(file: File) -> None
从 Files 集合中删除文件。
copy_static_files(dirty: bool = False, *, inclusion: Callable[[InclusionLevel], bool] = InclusionLevel.is_included) -> None
将静态文件从源复制到目标。
documentation_pages(*, inclusion: Callable[[InclusionLevel], bool] = InclusionLevel.is_included) -> Sequence[File]
返回所有 Markdown 页面文件对象的迭代器。
static_pages() -> Sequence[File]
返回所有静态页面文件对象的迭代器。
media_files() -> Sequence[File]
返回所有不是文档或静态页面的文件对象的迭代器。
javascript_files() -> Sequence[File]
返回所有 javascript 文件对象的迭代器。
css_files() -> Sequence[File]
返回所有 CSS 文件对象的迭代器。
add_files_from_theme(env: jinja2.Environment, config: MkDocsConfig) -> None
从 Jinja 环境中检索静态文件并添加到集合中。
mkdocs.structure.files.File
一个 MkDocs File 对象。
它表示一个文件的内容如何在目标站点中填充。
文件总是具有 abs_dest_path
(通过连接 dest_dir
和 dest_path
获得),其中 dest_dir
被理解为站点目录。
content_bytes
/content_string
(MkDocs 1.6 中新增)始终可以用于获取文件的内容。但它可能由以下两个源之一支持
-
位于
abs_src_path
的物理源文件(默认情况下通过连接src_dir
和src_uri
获得)。src_dir
被理解为docs目录。然后
content_bytes
/content_string
将读取abs_src_path
处的文件。src_dir
应该为真实文件填充,而应为生成的文件填充None
。 -
从 MkDocs 1.6 开始,文件也可以存储在内存中 -
content_string
/content_bytes
。然后
src_dir
和abs_src_path
将保持None
。content_bytes
/content_string
需要被写入,或者通过构造函数中的content
参数填充。但
src_uri
仍然为这些文件填充!虚拟文件假装它起源于docs
目录中的该路径,并且其他值是派生的。
对于静态文件,文件只是复制到目标,并且 dest_uri
等于 src_uri
。
对于 Markdown 文件(由 src_uri
中的文件扩展名确定),目标内容将是渲染后的内容,并且 dest_uri
将具有 .html
扩展名以及对路径的一些额外转换,基于 use_directory_urls
。
src_uri: str
instance-attribute
源文件相对于源目录的纯路径(始终为“/”分隔)。
generated_by: str | None = None
class-attribute
instance-attribute
如果为非 None,则表示插件动态生成了此文件。
该值是插件的入口点名称,可用于在 PluginCollection 中按键查找插件。
dest_path: str
property
writable
与 dest_uri
相同(并与它同步),但在 Windows 上将使用反斜杠。不建议使用。
src_path: str = path
instance-attribute
property
writable
与 src_uri
相同(并与它同步),但在 Windows 上将使用反斜杠。不建议使用。
src_dir: str | None = src_dir
instance-attribute
源文件所在的顶级目录的 OS 路径。
假定为 docs_dir;不会为生成的文件填充。
dest_dir: str = dest_dir
instance-attribute
文件应复制到的目标目录(顶级 site_dir)的 OS 路径。
use_directory_urls: bool = use_directory_urls
instance-attribute
是否应使用目录 URL('foo/')或不使用('foo.html')。
如果为 False
,则 Markdown 文件映射到同名 HTML 文件(文件扩展名更改为 .html
)。如果为 True,则 Markdown 文件映射到嵌套在使用 path
中文件的“名称”的目录中的 HTML 索引文件(index.html
)。非 Markdown 文件保留其原始路径。
inclusion: InclusionLevel = inclusion
class-attribute
instance-attribute
文件是否将从构建的站点中排除。
name = cached_property(_get_stem)
class-attribute
instance-attribute
返回文件名,不含扩展名。
dest_uri = cached_property(_get_dest_path)
class-attribute
instance-attribute
目标文件相对于目标目录的纯路径(始终为“/”分隔)。
url = cached_property(_get_url)
class-attribute
instance-attribute
目标文件相对于目标目录的 URI,以字符串形式表示。
abs_src_path: str | None
cached
property
源文件的绝对具体路径。在 Windows 上将使用反斜杠。
注意:不要使用此路径读取文件,建议使用 content_bytes
/content_string
。
abs_dest_path: str
cached
property
目标文件的绝对具体路径。在 Windows 上将使用反斜杠。
content_bytes: bytes
属性
可写
获取此文件的内容作为字节串。
如果由实际文件 (abs_src_path
) 支持,则在无法读取时可能会引发异常。
如果用作设置器,它会定义文件的内容,并且 abs_src_path
会变为未设置。
content_string: str
属性
可写
获取此文件的内容作为字符串。假设 UTF-8 编码,可能会引发异常。
如果由实际文件 (abs_src_path
) 支持,则在无法读取时也可能会引发异常。
如果用作设置器,它会定义文件的内容,并且 abs_src_path
会变为未设置。
generated(config: MkDocsConfig, src_uri: str, *, content: str | bytes | None = None, abs_src_path: str | None = None, inclusion: InclusionLevel = InclusionLevel.UNDEFINED) -> File
类方法
创建一个虚拟文件,由内存中的 content
或 abs_src_path
中的文件支持。
它将假装是 src_uri
中文档目录下的文件。
edit_uri() -> str | None
一个相对于源代码库的路径,用于“编辑”按钮。
默认为 src_uri
,可以覆盖。对于生成的 文件,应将其设置为 None
。
url_relative_to(other: File | str) -> str
返回相对于另一个文件的 url。
copy_file(dirty: bool = False) -> None
将源文件复制到目标位置,确保父目录存在。
is_documentation_page() -> bool
如果文件是 Markdown 页面,则返回 True。
is_static_page() -> bool
如果文件是静态页面 (HTML、XML、JSON),则返回 True。
is_media_file() -> bool
如果文件不是文档或静态页面,则返回 True。
is_javascript() -> bool
如果文件是 JavaScript 文件,则返回 True。
is_css() -> bool
如果文件是 CSS 文件,则返回 True。
mkdocs.config.base.Config
基类:UserDict
MkDocs 配置、插件配置(和子配置)对象的基类。
它应该被子类化,并定义 ConfigOption
作为属性。有关示例,请参阅 mkdocs/contrib/search/init.py 和 mkdocs/config/defaults.py。
MkDocs 1.4 之前的行为现在由 LegacyConfig 处理。