-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rustdoc: Add support for local resources #107640
base: master
Are you sure you want to change the base?
rustdoc: Add support for local resources #107640
Conversation
Forgot to update librustdoc inner tests. Should be solved now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should also go through an RFC, since it's adding a new API to rustdoc (like intra-doc links did).
self.cx.tcx.sess.struct_span_err( | ||
item.attr_span(self.cx.tcx), | ||
&format!("`{ori_path}`: No such file"), | ||
).emit(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably emit a warning, at least for awhile, because someone could've made a home-grown build system that already did the resource copying.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a warning forever should be enough indeed.
if !path.is_file() { | ||
self.cx.tcx.sess.struct_span_err( | ||
item.attr_span(self.cx.tcx), | ||
&format!("`{ori_path}`: No such file (expanded into `{}`)", path.display()), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably emit a warning, at least for awhile, because someone could've made a home-grown build system that already did the resource copying.
pub(crate) error_codes: ErrorCodes, | ||
pub(crate) edition: Edition, | ||
pub(crate) playground: &'a Option<Playground>, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because I'm afraid that this PR might take awhile to approve, can you please separate 62e7207 into its own PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure!
|
||
// @has local_resources/foo/0.svg | ||
// @has foo/struct.Enum.html | ||
// @has - '//img[@src="../local_resources/foo/0.svg"]' '' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we please change this so that it attempts to preserve, at least partially, the original filename?
- Pure numbers are annoying for anyone who wants to download the file (right-click, Save As) or image search engines that use the filename to guess at the subject matter.
- Even if everything is sorted so that the numbering is technically deterministic, it still means minor changes to the docs can cause images to swap filenames. This will be very confusing when these URLs are shared elsewhere.
- It would be better if this filename had a file hash in it, and
local_resources
ought to be namedlocal.files
, for consistency with static files and so that the CDN can be configured the same way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem with keeping the original filename is that we can potentially end up with multiple files with the same name in the static files folder. I suppose it'll be debated in the RFC.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a hash to the file name. If two files have the same name and the same hash, then that’s not actually a problem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently the key is the absolute file path, which is how we know if a file was already linked to.
// @has - '//img[@src="../../local_resources/foo/0.svg"]' '' | ||
/// test! | ||
/// | ||
/// ![yep](../../src/librustdoc/html/static/images/rust-logo.svg) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please also add a test case that combines this feature with cross-crate inlining?
…c-into-struct, r=notriddle Turn MarkdownWithToc into a struct with named fields Extracted the commit from rust-lang#107640. r? `@notriddle`
☔ The latest upstream changes (presumably #107738) made this pull request unmergeable. Please resolve the merge conflicts. |
/// This will be used when generating the HTML, once everything is generated, we copy these | ||
/// files into the static folder. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We shouldn't put per-crate files into static.files/
, which is used for toolchain-specific static files. If we go this route, there should be a separate directory for invocation-specific files.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Absolutely. The RFC mentions that there won't be a crate folder. ;)
Opened RFC: rust-lang/rfcs#3397 |
Hello @GuillaumeGomez! Noticed this PR has some merge conflicts, other than that it looks like it's waiting on the RFC :) |
I added the blocked label. |
Fixes #32104.
This PR adds the support for "local resources". What it means exactly: if a local item is referenced in the markdown, rustdoc will copy it into
{LOCAL_RESOURCES_FOLDER_NAME}/{crate_name}
and then refer to it.To do so, I originally tried to do it directly when we generated the markdown, but I ended with a pretty bad code. You can see this (unfinished) version here.
So how is it implemented? First, we go through a new pass which will gather the local resources from the markdown and store them into two maps: one map to keep the correspondance between the item and its "local location", and the other contains all the stored items to prevent duplications. This second is also used to copy the files into the static folder once everything has been generated.
Then, when we generate the markdown content, we replace the links to these items if we can find them in the correspondance map.
Best review it commit by commit.
RFC: rust-lang/rfcs#3397
r? @notriddle