简体   繁体   English

生成 rustdoc 时如何使用本地文件作为 crate 徽标?

[英]How to use a local file as crate logo when generating rustdoc?

It is possible to set both the favicon and the logo of the rustdoc for a crate by using:可以使用以下命令为 crate 设置favicon和 rustdoc 的徽标

  • #.[doc(html_favicon_url = "<url_to>/favicon.ico")]
  • #.[doc(html_logo_url = "<url_to>/logo.png")]

as documented here .如此处所述

However I do not want to upload my logo publicly and would therefore like to automatically include these files in /target/doc and reference them from there.但是我不想公开上传我的徽标,因此想自动将这些文件包含在/target/doc中并从那里引用它们。

Currently I have put the respective data urls (base64 encoded) into these fields and it works fine, but it enormously bloats the source file where these attributes are set.目前,我已将各自的数据 url(base64 编码)放入这些字段中,它工作正常,但它极大地膨胀了设置这些属性的源文件。

I know I could just copy the images into target/doc after generating the documentation using a script and then reference them using the relative url, but I would like to avoid this, so that I can still generate the documentation using cargo doc .我知道我可以在使用脚本生成文档后将图像复制到target/doc中,然后使用相关的 url 引用它们,但我想避免这种情况,这样我仍然可以使用cargo doc生成文档。

Edit编辑

The suggestion from the comment to set the --output flag of rustdoc using rustdocflags in .cargo/config.toml also did not work, because it leads to error: Option 'output' given more than once .评论中关于使用rustdocflags .cargo/config.toml中的rustdoc设置 rustdoc 的--output标志的建议也不起作用,因为它会导致error: Option 'output' given more than once Apart from that, it is not suited for me, because (at least as far as I understand) I can only give absolute paths there, whereas I would need a solution using relative paths for the images, because I have those images stored in a subdirectory of the cargo root directory, to allow for easy transfer to another system using git, etc.除此之外,它不适合我,因为(至少据我所知)我只能在那里提供绝对路径,而我需要使用图像的相对路径的解决方案,因为我将这些图像存储在cargo 根目录的子目录,以便使用 git 等轻松转移到另一个系统。

Thanks to the latest comment from eggyal I finally figured out how to do this:感谢eggyal的最新评论,我终于想出了如何做到这一点:

In my build.rs I copy the files to target/doc/ :在我的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.");
}

and then I just had to make sure, to use an absolute path when referencing them, like so:然后我只需要确保在引用它们时使用绝对路径,如下所示:

#![doc(html_favicon_url = "/logo.ico")]
#![doc(html_logo_url = "/logo.png")]

In general it would be better to read the CARGO_TARGET_DIR environment variable instead of hardcoding target/doc , but this is not yet available in build scripts.一般来说,最好阅读CARGO_TARGET_DIR环境变量而不是硬编码target/doc ,但这在构建脚本中尚不可用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM