生成 rustdoc 时如何使用本地文件作为 crate 标识?
How to use a local file as crate logo when generating rustdoc?
可以通过以下方式为 crate 设置 favicon 和 logo:
#![doc(html_favicon_url = "<url_to>/favicon.ico")]
#![doc(html_logo_url = "<url_to>/logo.png")]
记录 here.
但是我不想公开上传我的徽标,因此想自动将这些文件包含在 /target/doc
中并从那里引用它们。
目前我已经将相应的数据 urls(base64 编码)放入这些字段并且工作正常,但它极大地膨胀了设置这些属性的源文件。
我知道我可以在使用脚本生成文档后将图像复制到 target/doc
,然后使用相对 url 引用它们,但我想避免这种情况,这样我仍然可以使用 cargo doc
.
生成文档
编辑
评论中关于使用 .cargo/config.toml
中的 rustdocflags
设置 rustdoc
的 --output
标志的建议也没有奏效,因为它会导致 error: Option 'output' given more than once
.除此之外,它不适合我,因为(至少据我所知)我只能在那里给出绝对路径,而我需要一个使用图像相对路径的解决方案,因为我将这些图像存储在cargo 根目录的子目录,允许使用 git 等
轻松转移到另一个系统
感谢 eggyal 的最新评论,我终于想出了如何做到这一点:
在我的 build.rs
中,我将文件复制到 target/doc/
:
fn main() {
// Copy the images to the output when generating documentation
println!("cargo:rerun-if-changed=assets/doc");
std::fs::copy("assets/doc/logo.ico", "target/doc/logo.ico").expect("Failed to copy crate favicon when building documentation.");
std::fs::copy("assets/doc/logo.png", "target/doc/logo.png").expect("Failed to copy crate logo when building documentation.");
}
然后我只需要确保在引用它们时使用绝对路径,就像这样:
#![doc(html_favicon_url = "/logo.ico")]
#![doc(html_logo_url = "/logo.png")]
一般来说,阅读构建脚本中的 CARGO_TARGET_DIR
environment variable instead of hardcoding target/doc
, but this is not yet available 会更好。
可以通过以下方式为 crate 设置 favicon 和 logo:
#![doc(html_favicon_url = "<url_to>/favicon.ico")]
#![doc(html_logo_url = "<url_to>/logo.png")]
记录 here.
但是我不想公开上传我的徽标,因此想自动将这些文件包含在 /target/doc
中并从那里引用它们。
目前我已经将相应的数据 urls(base64 编码)放入这些字段并且工作正常,但它极大地膨胀了设置这些属性的源文件。
我知道我可以在使用脚本生成文档后将图像复制到 target/doc
,然后使用相对 url 引用它们,但我想避免这种情况,这样我仍然可以使用 cargo doc
.
编辑
评论中关于使用 .cargo/config.toml
中的 rustdocflags
设置 rustdoc
的 --output
标志的建议也没有奏效,因为它会导致 error: Option 'output' given more than once
.除此之外,它不适合我,因为(至少据我所知)我只能在那里给出绝对路径,而我需要一个使用图像相对路径的解决方案,因为我将这些图像存储在cargo 根目录的子目录,允许使用 git 等
感谢 eggyal 的最新评论,我终于想出了如何做到这一点:
在我的 build.rs
中,我将文件复制到 target/doc/
:
fn main() {
// Copy the images to the output when generating documentation
println!("cargo:rerun-if-changed=assets/doc");
std::fs::copy("assets/doc/logo.ico", "target/doc/logo.ico").expect("Failed to copy crate favicon when building documentation.");
std::fs::copy("assets/doc/logo.png", "target/doc/logo.png").expect("Failed to copy crate logo when building documentation.");
}
然后我只需要确保在引用它们时使用绝对路径,就像这样:
#![doc(html_favicon_url = "/logo.ico")]
#![doc(html_logo_url = "/logo.png")]
一般来说,阅读构建脚本中的 CARGO_TARGET_DIR
environment variable instead of hardcoding target/doc
, but this is not yet available 会更好。